- Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathgen_test_html.py
executable file
·137 lines (102 loc) · 3.77 KB
/
gen_test_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generates *_test.html files from *_test.js files.
This modifies files in place and will overwrite existing *_test.html files.
Usage:
$ python ./buildtools/gen_test_html.py
"""
fromcollectionsimportnamedtuple
importos
importre
fromstringimportTemplate
importcommon
# Stores the paths of files related to a test file
# (e.g. *_test.html, *_test_dom.html)
RelatedPaths=namedtuple("RelatedPaths", ["html", "dom"])
# The root-level directories containing JS tests.
DIRECTORIES_WITH_TESTS= ["javascript", "soy"]
defmain():
common.cd_to_firebaseui_root()
template_data=_read_file("./buildtools/test_template.html")
template=Template(template_data)
fordirectoryinDIRECTORIES_WITH_TESTS:
forjs_pathincommon.get_files_with_suffix(directory, "_test.js"):
_gen_html(js_path, template)
def_gen_html(js_path, template):
"""Generates a Closure test HTML wrapper file and saves it to the filesystem.
Args:
js_path: The path to the JS test (*_test.js) file.
template: The template for the HTML wrapper.
"""
try:
related_paths=_get_related_paths_from_js_path(js_path)
js_data=_read_file(js_path)
dom= (_read_file(related_paths.dom)
ifos.path.isfile(related_paths.dom) else"")
package=_extract_closure_package(js_data)
generated_html=template.substitute(package=package, dom=dom)
_write_file(related_paths.html, generated_html)
exceptExceptionase: # pylint: disable=bare-except
print("HTML generation failed for: %s"%js_path)
print(e)
def_get_related_paths_from_js_path(js_path):
"""Converts the JS test file path to paths of related files.
For example, ./path/to/foo_test.js becomes
./generated/tests/path/to/foo_test.html
./path/to/foo_test_dom.html
Args:
js_path: The path to the JS test (*_test.js) file.
Returns:
The paths to the related files, as a RelatedPaths.
"""
base_name=os.path.splitext(js_path)[0]
returnRelatedPaths(common.TESTS_BASE_PATH+base_name+".html",
base_name+"_dom.html")
def_extract_closure_package(js_data):
"""Extracts the package name that is goog.provide() or goog.module()
in the JS file.
Args:
js_data: The contents of a JS test (*_test.js) file.
Returns:
The closure package goog.provide() or good.module() by the file.
Raises:
ValueError: The JS does not contain a goog.provide() or goog.module().
"""
matches=re.search(r"goog\.(provide|module)\([\n\s]*'(.+)'\);", js_data)
ifmatchesisNone:
raiseValueError("goog.provide() or goog.module() not found in file")
returnmatches.group(2)
def_read_file(path):
"""Reads a file into a string.
Args:
path: The path to a file.
Returns:
The contents of the file.
"""
withopen(path) asf:
returnf.read()
def_write_file(path, contents):
"""Writes a string to file, overwriting existing content.
Intermediate directories are created if not present.
Args:
path: The path to a file.
contents: The string to write to the file.
"""
dir_name=os.path.dirname(path)
ifnotos.path.exists(dir_name):
os.makedirs(dir_name)
withopen(path, "w") asf:
f.write(contents)
if__name__=="__main__":
main()