|
1 import sys |
|
2 import os.path |
|
3 import re |
|
4 |
|
5 assert len(sys.argv) == 2 |
|
6 mochiPath = sys.argv[1] |
|
7 |
|
8 extDotPos = mochiPath.find('.html') |
|
9 assert extDotPos != -1, 'mochitest target must be an html doc.' |
|
10 |
|
11 testPath = mochiPath[:extDotPos] + '.solo.html' |
|
12 |
|
13 def ReadLocalFile(include): |
|
14 incPath = os.path.dirname(mochiPath) |
|
15 filePath = os.path.join(incPath, include) |
|
16 |
|
17 data = None |
|
18 try: |
|
19 f = open(filePath, 'r') |
|
20 data = f.read() |
|
21 except: |
|
22 pass |
|
23 |
|
24 try: |
|
25 f.close() |
|
26 except: |
|
27 pass |
|
28 |
|
29 return data |
|
30 |
|
31 kSimpleTestReplacement = '''\n |
|
32 <script> |
|
33 // SimpleTest.js replacement |
|
34 function ok(val, text) { |
|
35 var elem = document.getElementById('mochi-to-testcase-output'); |
|
36 var status = val ? 'Test <font color=\\'green\\'>passed</font>: ' |
|
37 : 'Test <font color=\\'red\\' >FAILED</font>: '; |
|
38 elem.innerHTML += '\\n<br/>\\n' + status + text; |
|
39 } |
|
40 |
|
41 function todo(val, text) { |
|
42 ok(!val, 'Todo: ' + text); |
|
43 } |
|
44 </script> |
|
45 <div id='mochi-to-testcase-output'></div> |
|
46 \n''' |
|
47 |
|
48 fin = open(mochiPath, 'r') |
|
49 fout = open(testPath, 'w') |
|
50 includePattern = re.compile('<script\\s*src=[\'"](.*)\\.js[\'"]>\\s*</script>') |
|
51 cssPattern = re.compile('<link\\s*rel=[\'"]stylesheet[\'"]\\s*href=[\'"]([^=>]*)[\'"]>') |
|
52 for line in fin: |
|
53 skipLine = False |
|
54 for css in cssPattern.findall(line): |
|
55 skipLine = True |
|
56 print('Ignoring stylesheet: ' + css) |
|
57 |
|
58 for inc in includePattern.findall(line): |
|
59 skipLine = True |
|
60 if inc == '/MochiKit/MochiKit': |
|
61 continue |
|
62 |
|
63 if inc == '/tests/SimpleTest/SimpleTest': |
|
64 print('Injecting SimpleTest replacement') |
|
65 fout.write(kSimpleTestReplacement); |
|
66 continue |
|
67 |
|
68 incData = ReadLocalFile(inc + '.js') |
|
69 if not incData: |
|
70 print('Warning: Unknown JS file ignored: ' + inc + '.js') |
|
71 continue |
|
72 |
|
73 print('Injecting include: ' + inc + '.js') |
|
74 fout.write('\n<script>\n// Imported from: ' + inc + '.js\n'); |
|
75 fout.write(incData); |
|
76 fout.write('\n</script>\n'); |
|
77 continue |
|
78 |
|
79 if skipLine: |
|
80 continue |
|
81 |
|
82 fout.write(line) |
|
83 continue |
|
84 |
|
85 fin.close() |
|
86 fout.close() |