1
+ #!/usr/bin/env python
2
+
3
+ # ---
4
+ # Licensed to the Apache Software Foundation (ASF) under one
5
+ # or more contributor license agreements. See the NOTICE file
6
+ # distributed with this work for additional information
7
+ # regarding copyright ownership. The ASF licenses this file
8
+ # to you under the Apache License, Version 2.0 (the
9
+ # "License"); you may not use this file except in compliance
10
+ # with the License. You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing,
15
+ # software distributed under the License is distributed on an
16
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ # KIND, either express or implied. See the License for the
18
+ # specific language governing permissions and limitations
19
+ # under the License.
20
+ # ---
21
+
22
+ import os
23
+ import re
24
+ import sys
25
+ import shutil
26
+ import datetime
27
+ import subprocess
28
+
29
+ PROGRAM = sys .argv [0 ]
30
+
31
+ #-------------------------------------------------------------------------------
32
+ #
33
+ #-------------------------------------------------------------------------------
34
+ def main ():
35
+
36
+ # all work done in directory of this program
37
+ baseDir = os .path .dirname (PROGRAM )
38
+ os .chdir (baseDir )
39
+
40
+ testsDir = os .path .abspath ("../../thirdparty/commonjs-tests" )
41
+ outDir = os .path .abspath ("out" )
42
+
43
+ # validate testsDir
44
+ if not os .path .exists (testsDir ):
45
+ error ("tests dir does not exist: %s" % testsDir )
46
+
47
+ if not os .path .isdir (testsDir ):
48
+ error ("tests dir is not a directory: %s" % testsDir )
49
+
50
+ # validate and reset outDir
51
+ if os .path .exists (outDir ):
52
+ if not os .path .isdir (outDir ):
53
+ error ("out dir is not a directory: %s" % outDir )
54
+
55
+ shutil .rmtree (outDir )
56
+
57
+ os .makedirs (outDir )
58
+
59
+ tests = getTests (testsDir )
60
+
61
+ # now all work done in outDir
62
+ os .chdir ("out" )
63
+
64
+ # build the individual tests
65
+ iframes = []
66
+ for test in tests :
67
+ testName = test .replace ('/' , '-' )
68
+ htmlFile = buildTest (os .path .join (testsDir , test ), testName )
69
+ iframes .append ("<iframe width='100%%' height='30' src='%s'></iframe>" % htmlFile )
70
+
71
+ iframesLines = "\n " .join (iframes )
72
+
73
+ # build the browser launcher
74
+ html = fileContents ("../launcher-main.template.html" )
75
+
76
+ html = html .replace ("@iframes@" , iframesLines )
77
+ html = html .replace ("@date@" , getDate ())
78
+
79
+ oFileName = "launcher-all.html"
80
+ oFile = file (oFileName , "w" )
81
+ oFile .write (html )
82
+ oFile .close ()
83
+
84
+ print
85
+ print "Generated browser test: %s" % os .path .abspath (oFileName )
86
+ print
87
+ print "You can run the test as a local file under Safari but not Chrome."
88
+ print "To test under Chrome, access the files via http://"
89
+
90
+ #-------------------------------------------------------------------------------
91
+ #
92
+ #-------------------------------------------------------------------------------
93
+ def buildTest (testDir , testName ):
94
+
95
+ log ("generating tests for %s" % (testName ))
96
+
97
+ html = fileContents ("../launcher-in-iframe.template.html" )
98
+
99
+ # get the list of modules
100
+ modules = getModules (testDir )
101
+
102
+ modulesSource = []
103
+
104
+ modulesSource .append ("try {" )
105
+
106
+ for module in modules :
107
+ source = fileContents ("%s.js" % os .path .join (testDir , module ))
108
+
109
+ modulesSource .append ("//----------------------------------------------" )
110
+ modulesSource .append ("define('%s', function(require,exports,module) {" % module )
111
+ modulesSource .append (source .strip ())
112
+ modulesSource .append ("});" )
113
+ modulesSource .append ("" )
114
+
115
+ modulesSource .append ("}" )
116
+ modulesSource .append ("catch(e) {" )
117
+ modulesSource .append (" console.log('exception thrown loading modules: ' + e)" )
118
+ modulesSource .append ("}" )
119
+
120
+ modulesLines = "\n " .join (modulesSource )
121
+
122
+ html = html .replace ("@modules@" , modulesLines )
123
+ html = html .replace ("@title@" , testName )
124
+ html = html .replace ("@date@" , getDate ())
125
+
126
+ # build HTML launcher for iframe
127
+ oFileName = "%s.html" % testName
128
+ oFile = file (oFileName , "w" )
129
+ oFile .write (html )
130
+ oFile .close ()
131
+
132
+ return oFileName
133
+
134
+ #-------------------------------------------------------------------------------
135
+ #
136
+ #-------------------------------------------------------------------------------
137
+ def getModules (testDir ):
138
+ modules = []
139
+ for root , dirs , files in os .walk (testDir ):
140
+ for file in files :
141
+ if not file .endswith (".js" ): continue
142
+
143
+ moduleSource = os .path .relpath (os .path .join (root , file ), testDir )
144
+ moduleName = os .path .splitext (moduleSource )[0 ]
145
+ modules .append (moduleName )
146
+
147
+ return modules
148
+
149
+ #-------------------------------------------------------------------------------
150
+ #
151
+ #-------------------------------------------------------------------------------
152
+ def getTests (testDir ):
153
+ tests = []
154
+ for root , dirs , files in os .walk (testDir ):
155
+ if "program.js" in files :
156
+ tests .append (os .path .relpath (root , testDir ))
157
+
158
+ return tests
159
+
160
+ #-------------------------------------------------------------------------------
161
+ #
162
+ #-------------------------------------------------------------------------------
163
+ def run (cmdArgs ):
164
+ result = subprocess .Popen (cmdArgs , stdout = subprocess .PIPE ).communicate ()[0 ]
165
+ if not re .match (r"\s*" , result ):
166
+ print result
167
+
168
+ #-------------------------------------------------------------------------------
169
+ #
170
+ #-------------------------------------------------------------------------------
171
+ def fileContents (iFileName ):
172
+ iFile = file (iFileName )
173
+ contents = iFile .read ()
174
+ iFile .close ()
175
+
176
+ return contents
177
+
178
+ def getDate ():
179
+ return datetime .datetime .today ().isoformat (" " )
180
+ #-------------------------------------------------------------------------------
181
+ #
182
+ #-------------------------------------------------------------------------------
183
+ def log (message ):
184
+ print "%s: %s" % (PROGRAM , message )
185
+
186
+ #-------------------------------------------------------------------------------
187
+ #
188
+ #-------------------------------------------------------------------------------
189
+ def error (message ):
190
+ log (message )
191
+ exit (1 )
192
+
193
+ #-------------------------------------------------------------------------------
194
+ #
195
+ #-------------------------------------------------------------------------------
196
+ main ()
0 commit comments