Skip to content

Commit c7a21d7

Browse files
author
Jianchun Xu
committed
xplat: runtests.py review cleanup
Address review comments. Add TestResult class to record results. Changed `print` calls to try to support python 3.
1 parent fdd4ee2 commit c7a21d7

File tree

1 file changed

+87
-50
lines changed

1 file changed

+87
-50
lines changed

test/runtests.py

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@
77
import sys
88
import os
99
import subprocess as SP
10-
import argparse, textwrap
10+
import argparse
1111
import xml.etree.ElementTree as ET
1212

13+
# handle command line args
1314
parser = argparse.ArgumentParser(
1415
description='ChakraCore *nix Test Script',
1516
formatter_class=argparse.RawDescriptionHelpFormatter,
16-
epilog=textwrap.dedent('''\
17-
Samples:
17+
epilog='''\
18+
Samples:
1819
19-
test all folders:
20-
{0}
20+
test all folders:
21+
{0}
2122
22-
test only Array:
23-
{0} Array
23+
test only Array:
24+
{0} Array
25+
26+
test a single file:
27+
{0} Basics/hello.js
28+
'''.format(sys.argv[0]))
2429

25-
test a single file:
26-
{0} Basics/hello.js
27-
'''.format(sys.argv[0]))
28-
)
2930
parser.add_argument('folders', metavar='folder', nargs='*',
3031
help='folder subset to run tests')
31-
parser.add_argument('-b', '--binary', metavar='binary', help='ch full path');
32+
parser.add_argument('-b', '--binary', metavar='bin', help='ch full path')
3233
parser.add_argument('-d', '--debug', action='store_true',
3334
help='use debug build');
34-
parser.add_argument('-t', '--test', action='store_true', help='use test build');
35-
parser.add_argument('--x86', action='store_true', help='use x86 build');
36-
parser.add_argument('--x64', action='store_true', help='use x64 build');
35+
parser.add_argument('-t', '--test', action='store_true', help='use test build')
36+
parser.add_argument('--x86', action='store_true', help='use x86 build')
37+
parser.add_argument('--x64', action='store_true', help='use x64 build')
3738
args = parser.parse_args()
3839

3940

@@ -46,52 +47,85 @@
4647
arch = os.environ.get('_BuildArch', 'x86')
4748

4849
# flavor: debug, test, release
50+
type_flavor = {'chk':'debug', 'test':'test', 'fre':'release'}
4951
flavor = 'debug' if args.debug else ('test' if args.test else None)
5052
if flavor == None:
51-
flavor = {'chk':'debug', 'test':'test', 'fre':'release'}\
52-
[os.environ.get('_BuildType', 'fre')]
53+
flavor = type_flavor[os.environ.get('_BuildType', 'fre')]
5354

5455
# binary: full ch path
5556
binary = args.binary
5657
if binary == None:
5758
if sys.platform == 'win32':
58-
binary = os.path.join(repo_root,
59-
'Build/VcBuild/bin/{}_{}/ch.exe'.format(arch, flavor))
59+
binary = 'Build/VcBuild/bin/{}_{}/ch.exe'.format(arch, flavor)
6060
else:
61-
binary = os.path.join(repo_root, "BuildLinux/ch")
61+
binary = 'BuildLinux/ch'
62+
binary = os.path.join(repo_root, binary)
6263
if not os.path.isfile(binary):
63-
print '{} not found. Did you run ./build.sh already?'.format(binary)
64+
print('{} not found. Did you run ./build.sh already?'.format(binary))
6465
sys.exit(1)
6566

66-
pass_count = 0
67-
fail_count = 0
67+
68+
# records pass_count/fail_count
69+
class PassFailCount(object):
70+
def __init__(self):
71+
self.pass_count = 0
72+
self.fail_count = 0
73+
74+
def __str__(self):
75+
return 'passed {}, failed {}'.format(self.pass_count, self.fail_count)
76+
77+
# records total and individual folder's pass_count/fail_count
78+
class TestResult(PassFailCount):
79+
def __init__(self):
80+
super(self.__class__, self).__init__()
81+
self.folders = {}
82+
83+
def _get_folder_result(self, folder):
84+
r = self.folders.get(folder)
85+
if not r:
86+
r = PassFailCount()
87+
self.folders[folder] = r
88+
return r
89+
90+
def log(self, filename=None, folder=None, fail=False):
91+
if not folder:
92+
folder = os.path.basename(os.path.dirname(filename))
93+
r = self._get_folder_result(folder)
94+
if fail:
95+
r.fail_count += 1
96+
self.fail_count += 1
97+
else:
98+
r.pass_count += 1
99+
self.pass_count += 1
100+
101+
test_result = TestResult()
102+
68103

69104
def show_failed(filename, output, exit_code, expected_output):
70-
print "\nFailed ->", filename
105+
print("\nFailed -> {}".format(filename))
71106
if expected_output == None:
72-
print "\nOutput:"
73-
print "----------------------------"
74-
print output
75-
print "----------------------------"
107+
print("\nOutput:")
108+
print("----------------------------")
109+
print(output)
110+
print("----------------------------")
76111
else:
77112
lst_output = output.split('\n')
78113
lst_expected = expected_output.split('\n')
79114
ln = min(len(lst_output), len(lst_expected))
80115
for i in range(0, ln):
81116
if lst_output[i] != lst_expected[i]:
82-
print "Output: (at line " + str(i) + ")"
83-
print "----------------------------"
84-
print lst_output[i]
85-
print "----------------------------"
86-
print "Expected Output:"
87-
print "----------------------------"
88-
print lst_expected[i]
89-
print "----------------------------"
117+
print("Output: (at line " + str(i) + ")")
118+
print("----------------------------")
119+
print(lst_output[i])
120+
print("----------------------------")
121+
print("Expected Output:")
122+
print("----------------------------")
123+
print(lst_expected[i])
124+
print("----------------------------")
90125
break
91126

92-
print "exit code:", exit_code
93-
global fail_count
94-
fail_count += 1
127+
print("exit code: {}".format(exit_code))
128+
test_result.log(filename, fail=True)
95129

96130
def test_path(path):
97131
if os.path.isfile(path):
@@ -103,19 +137,18 @@ def test_path(path):
103137
if len(tests) == 0:
104138
return
105139

106-
print "Testing ->", os.path.basename(folder)
140+
print("Testing -> " + os.path.basename(folder))
107141
for test in tests:
108142
test_one(folder, test)
109143

110144
def test_one(folder, test):
111145
js_file = os.path.join(folder, test['files'])
112146
js_output = ""
113147

114-
cmd = [x for x in [binary,
115-
'-WERExceptionSupport',
116-
'-ExtendedErrorStackForTestHost',
117-
test.get('compile-flags'),
118-
js_file] if x != None]
148+
flags = test.get('compile-flags')
149+
cmd = [binary, '-WERExceptionSupport', '-ExtendedErrorStackForTestHost'] \
150+
+ (flags.split() if flags else []) \
151+
+ [js_file]
119152
p = SP.Popen(cmd, stdout=SP.PIPE, stderr=SP.STDOUT)
120153
js_output = p.communicate()[0].replace('\r','')
121154
exit_code = p.wait()
@@ -133,9 +166,8 @@ def test_one(folder, test):
133166
if expected_output.replace('\n', '') != js_output.replace('\n', ''):
134167
return show_failed(js_file, js_output, exit_code, expected_output)
135168

136-
print "\tPassed ->", os.path.basename(js_file)
137-
global pass_count
138-
pass_count += 1
169+
print("\tPassed -> " + os.path.basename(js_file))
170+
test_result.log(folder=folder)
139171

140172
def load_tests(folder, file):
141173
try:
@@ -169,8 +201,13 @@ def main():
169201
for folder in args.folders:
170202
test_path(folder)
171203

172-
print 'Passed:', pass_count, 'Failed:', fail_count
173-
print 'Success!' if fail_count == 0 else 'Failed!'
204+
print('\n')
205+
print('============================')
206+
for folder, result in sorted(test_result.folders.items()):
207+
print('{}: {}'.format(folder, result))
208+
print('============================')
209+
print('Total: {}'.format(test_result))
210+
print('Success!' if test_result.fail_count == 0 else 'Failed!')
174211
return 0
175212

176213
if __name__ == '__main__':

0 commit comments

Comments
 (0)