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

Delta Between Two Patch Sets: run_tests.py

Issue 4895499043733504: Issue 510 - [Typed objects] Don`t hardcode script load order in unit tests (Closed)
Left Patch Set: Created Jan. 10, 2015, 12:13 a.m.
Right Patch Set: Using generators to produce responses Created Feb. 5, 2015, 2:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/common.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # This file is part of Adblock Plus <http://adblockplus.org/>, 4 # This file is part of Adblock Plus <https://adblockplus.org/>,
5 # Copyright (C) 2006-2014 Eyeo GmbH 5 # Copyright (C) 2006-2015 Eyeo GmbH
6 # 6 #
7 # Adblock Plus is free software: you can redistribute it and/or modify 7 # Adblock Plus is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3 as 8 # it under the terms of the GNU General Public License version 3 as
9 # published by the Free Software Foundation. 9 # published by the Free Software Foundation.
10 # 10 #
11 # Adblock Plus is distributed in the hope that it will be useful, 11 # Adblock Plus is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details. 14 # GNU General Public License for more details.
15 # 15 #
16 # You should have received a copy of the GNU General Public License 16 # You should have received a copy of the GNU General Public License
17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import io
20 import json
21 import os
22 import posixpath
23 import re
24 import urlparse
25
19 import flask 26 import flask
20 import os
21 import re
22 import io
Felix Dahlke 2015/02/03 06:21:16 Nit: The import statements seem to be ordered alph
Sebastian Noack 2015/02/03 09:21:10 Note that according to PEP-8, third-party module i
Wladimir Palant 2015/02/03 16:26:04 Done.
23 import urlparse
24 27
25 app = flask.Flask(__name__) 28 app = flask.Flask(__name__)
26 29
27 def get_scripts(dir, reldir = ""): 30 def get_scripts(dir, reldir=""):
Felix Dahlke 2015/02/03 06:21:16 This would be a bit shorter (and IMHO easier to re
Sebastian Noack 2015/02/03 09:21:10 Even more compact: import glob ... def get_scri
Wladimir Palant 2015/02/03 16:26:04 This won't find anything in subdirectories. Perso
28 for name in os.listdir(dir): 31 for filename in os.listdir(dir):
29 path = os.path.join(dir, name) 32 path = os.path.join(dir, filename)
30 relpath = reldir + "/" + name 33 # "Relative path" will be the parameter passed in to require(), meaning
34 # that it always has to use forward slashes.
35 relpath = posixpath.join(reldir, filename)
31 if os.path.isdir(path): 36 if os.path.isdir(path):
32 for path, relpath in get_scripts(path, relpath): 37 for path, relpath in get_scripts(path, relpath):
33 yield (path, relpath) 38 yield path, relpath
34 elif os.path.splitext(path)[1] == ".js": 39 elif os.path.splitext(path)[1] == ".js":
35 yield (path, relpath) 40 yield path, relpath
36 41
37 def js_encode(str): 42 def js_encode(str):
38 return (str.replace("\\", "\\\\").replace("'", "\\'") 43 return json.dumps(str)
39 .replace("\r", "\\r").replace("\n", "\\n"))
Felix Dahlke 2015/02/03 06:21:16 Makes me wonder if we'll run into trouble when usi
Wladimir Palant 2015/02/03 16:26:04 No, these should be fine within a string. See http
40 44
41 def script_as_string(path, sourceURL, backcompat): 45 def script_as_string(path, sourceURL, backcompat):
42 if backcompat: 46 if backcompat:
43 from jshydra.abp_rewrite import doRewrite 47 from jshydra.abp_rewrite import doRewrite
44 data = doRewrite([os.path.abspath(path)], []).decode("utf-8") 48 data = doRewrite([os.path.abspath(path)], []).decode("utf-8")
45 else: 49 else:
46 with io.open(path, "r", encoding="utf-8") as handle: 50 with io.open(path, "r", encoding="utf-8") as handle:
47 data = handle.read() 51 data = handle.read()
48 data += "\n//# sourceURL=%s" % sourceURL 52 data += "\n//# sourceURL=%s" % sourceURL
49 return js_encode(data) 53 return js_encode(data)
50 54
51 @app.route("/<path:path>", methods = ["GET"]) 55 @app.route("/<path:path>", methods = ["GET"])
52 @app.route("/", methods = ["GET"]) 56 @app.route("/", methods = ["GET"])
53 def multiplex(path=""): 57 def multiplex(path=""):
54 request_url = urlparse.urlparse(flask.request.url) 58 base_url = flask.request.url
59 request_url = urlparse.urlparse(base_url)
55 request_path = request_url.path 60 request_path = request_url.path
56 islib = request_path.startswith("/lib/") 61 islib = request_path.startswith("/lib/")
57 backcompat = request_url.query == "backcompat" 62 backcompat = request_url.query == "backcompat"
58 63
59 rootdir = os.path.dirname(__file__) 64 rootdir = os.path.dirname(__file__)
60 if request_path == "/lib.js": 65 if request_path == "/lib.js":
61 data = "" 66 def generate_libs():
62 for path, relpath in get_scripts(os.path.join(rootdir, "lib")): 67 for path, relpath in get_scripts(os.path.join(rootdir, "lib")):
63 url = urlparse.urljoin(flask.request.url, "/lib" + relpath) 68 url = urlparse.urljoin(base_url, "/lib/" + relpath)
64 data += "require.modules['%s'] = '%s';\n" % ( 69 yield "require.sources[%s] = %s;\n" % (
65 os.path.splitext(relpath)[0][1:], 70 js_encode(posixpath.splitext(relpath)[0]),
66 script_as_string(path, url, backcompat) 71 script_as_string(path, url, backcompat)
67 ) 72 )
68 return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"} ) 73 return flask.Response(generate_libs(), mimetype="application/javascript")
69 elif request_path == "/tests.js": 74 elif request_path == "/tests.js":
70 data = "var tests = [" 75 def generate_tests():
71 for path, relpath in get_scripts(os.path.join(rootdir, "test", "tests")): 76 yield "var tests = ["
72 url = urlparse.urljoin(flask.request.url, "/tests" + relpath) 77 for path, relpath in get_scripts(os.path.join(rootdir, "test", "tests")):
73 data += " '%s',\n" % script_as_string(path, url, backcompat) 78 url = urlparse.urljoin(base_url, "/tests/" + relpath)
74 data += "];" 79 yield " %s,\n" % script_as_string(path, url, backcompat)
75 return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"} ) 80 yield "];"
81 return flask.Response(generate_tests(), mimetype="application/javascript")
76 else: 82 else:
77 if request_path.startswith("/lib/"): 83 if request_path.startswith("/lib/"):
78 rootdir = os.path.join(rootdir, "lib") 84 rootdir = os.path.join(rootdir, "lib")
79 else: 85 else:
80 rootdir = os.path.join(rootdir, "test") 86 rootdir = os.path.join(rootdir, "test")
81 if request_path.endswith("/"): 87 if request_path.endswith("/"):
82 request_path += "index.html" 88 request_path += "index.html"
83 return flask.send_from_directory(rootdir, request_path.lstrip("/")) 89 return flask.send_from_directory(rootdir, request_path.lstrip("/"))
84 90
85 if __name__ == "__main__": 91 if __name__ == "__main__":
86 app.run(debug=True) 92 app.run(debug=True)
LEFTRIGHT
« no previous file | test/common.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld