Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: run_tests.py

Issue 4895499043733504: Issue 510 - [Typed objects] Don`t hardcode script load order in unit tests (Closed)
Patch Set: Using generators to produce responses Created Feb. 5, 2015, 2:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/common.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: run_tests.py
===================================================================
--- a/run_tests.py
+++ b/run_tests.py
@@ -11,51 +11,82 @@
# Adblock Plus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+import io
+import json
+import os
+import posixpath
+import re
+import urlparse
+
import flask
-import os
-import re
-from urlparse import urlparse
app = flask.Flask(__name__)
+def get_scripts(dir, reldir=""):
+ for filename in os.listdir(dir):
+ path = os.path.join(dir, filename)
+ # "Relative path" will be the parameter passed in to require(), meaning
+ # that it always has to use forward slashes.
+ relpath = posixpath.join(reldir, filename)
+ if os.path.isdir(path):
+ for path, relpath in get_scripts(path, relpath):
+ yield path, relpath
+ elif os.path.splitext(path)[1] == ".js":
+ yield path, relpath
+
def js_encode(str):
- return re.sub(r"(['\\])", r"\\\1", str)
+ return json.dumps(str)
+
+def script_as_string(path, sourceURL, backcompat):
+ if backcompat:
+ from jshydra.abp_rewrite import doRewrite
+ data = doRewrite([os.path.abspath(path)], []).decode("utf-8")
+ else:
+ with io.open(path, "r", encoding="utf-8") as handle:
+ data = handle.read()
+ data += "\n//# sourceURL=%s" % sourceURL
+ return js_encode(data)
@app.route("/<path:path>", methods = ["GET"])
@app.route("/", methods = ["GET"])
def multiplex(path=""):
- request_url = urlparse(flask.request.url)
+ base_url = flask.request.url
+ request_url = urlparse.urlparse(base_url)
request_path = request_url.path
islib = request_path.startswith("/lib/")
backcompat = request_url.query == "backcompat"
rootdir = os.path.dirname(__file__)
- if not islib:
- rootdir = os.path.join("test")
-
- if islib or backcompat:
- path = flask.safe_join(rootdir, request_path.lstrip("/"))
- if not os.path.isfile(path):
- return flask.abort(404)
-
- module = os.path.splitext(request_path[len("/lib/"):])[0]
- if backcompat:
- from jshydra.abp_rewrite import doRewrite
- data = doRewrite([os.path.abspath(path)], ["module=true"] if islib else [])
- data = re.sub(r"require\.scopes\[.*?\]", "require.scopes['%s']" % module, data)
+ if request_path == "/lib.js":
+ def generate_libs():
+ for path, relpath in get_scripts(os.path.join(rootdir, "lib")):
+ url = urlparse.urljoin(base_url, "/lib/" + relpath)
+ yield "require.sources[%s] = %s;\n" % (
+ js_encode(posixpath.splitext(relpath)[0]),
+ script_as_string(path, url, backcompat)
+ )
+ return flask.Response(generate_libs(), mimetype="application/javascript")
+ elif request_path == "/tests.js":
+ def generate_tests():
+ yield "var tests = ["
+ for path, relpath in get_scripts(os.path.join(rootdir, "test", "tests")):
+ url = urlparse.urljoin(base_url, "/tests/" + relpath)
+ yield " %s,\n" % script_as_string(path, url, backcompat)
+ yield "];"
+ return flask.Response(generate_tests(), mimetype="application/javascript")
+ else:
+ if request_path.startswith("/lib/"):
+ rootdir = os.path.join(rootdir, "lib")
else:
- with open(path, "rb") as file:
- data = "require.scopes['%s'] = function(){var exports={};%s\nreturn exports;}();" % (module, file.read())
- return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"})
- else:
+ rootdir = os.path.join(rootdir, "test")
if request_path.endswith("/"):
request_path += "index.html"
return flask.send_from_directory(rootdir, request_path.lstrip("/"))
if __name__ == "__main__":
app.run(debug=True)
« no previous file with comments | « no previous file | test/common.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld