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 .
+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("/", 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)
Index: test/common.js
===================================================================
--- a/test/common.js
+++ b/test/common.js
@@ -14,22 +14,26 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see .
*/
function addScript(url)
{
var script = document.createElement("script");
script.src = url;
- if (js17supported)
- script.type = "text/javascript;version=1.7";
- else
+ if (!js17supported)
script.src += "?backcompat";
script.async = false;
document.head.appendChild(script);
return script;
}
function require(module)
{
+ if (require.sources.hasOwnProperty(module))
+ {
+ require.scopes[module] = evalModule(require.sources[module]);
+ delete require.sources[module];
+ }
return require.scopes[module];
}
require.scopes = {};
+require.sources = {};
Index: test/index.html
===================================================================
--- a/test/index.html
+++ b/test/index.html
@@ -1,44 +1,54 @@
-
-
-
- var tests = [
- "typedObjects.js"
- ];
- for (var i = 0; i < tests.length; i++)
- addScript("/tests/" + tests[i]);
+
+
+