Skip to content

Commit 2740f5e

Browse files
author
Jianchun Xu
committed
[MERGE #951] cross platform: python script to run test files [suggestion]
Merge pull request #951 from obastemur:testing This PR - adds a Python based test script to run all the test files or the ones from a given folder. In addition to executing JS files, it also compares the results to predefined baseline files. - replaces the functionality of `runtests.sh` to call `runtests.py` Try: ``` ./build.sh ./test/runtests.py Basics ``` In order to run a single file ``` ./test/runtests.py Basics/hello.js ```
2 parents 593393e + f45468c commit 2740f5e

File tree

2 files changed

+122
-13
lines changed

2 files changed

+122
-13
lines changed

test/runtests.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
#-------------------------------------------------------------------------------------------------------
3+
# Copyright (C) Microsoft. All rights reserved.
4+
# Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
5+
#-------------------------------------------------------------------------------------------------------
6+
7+
import sys
8+
import os
9+
import subprocess as SP
10+
11+
test_all = True
12+
test_root = os.path.dirname(os.path.realpath(__file__))
13+
14+
# ugly trick
15+
ch_path = os.path.join(os.path.dirname(test_root), "BuildLinux/ch")
16+
17+
if not os.path.isfile(ch_path):
18+
print "BuildLinux/ch not found. Did you run ./build.sh already?"
19+
sys.exit(1)
20+
21+
if len(sys.argv) > 1:
22+
if sys.argv[1] in ['-?', '--help']:
23+
print "ChakraCore *nix Test Script\n"
24+
25+
print "Usage:"
26+
print "test.py <optional test path>\n"
27+
28+
print "-?, --help : Show help\n"
29+
30+
print "Samples:"
31+
print "test only Array:"
32+
print "\t./test.py Array\n"
33+
34+
print "test a single file:"
35+
print "\t./test.py Basics/hello.js\n"
36+
37+
print "test all folders:"
38+
print "\t./test.py"
39+
sys.exit(0)
40+
test_all = None
41+
42+
test_dirs=['']
43+
if test_all:
44+
test_dirs = os.listdir(test_root)
45+
else:
46+
test_dirs[0] = sys.argv[1]
47+
48+
def show_failed(filename, output, exit_code, expected_output):
49+
print "\nFailed ->", filename
50+
if expected_output == None:
51+
print "\nOutput:"
52+
print "----------------------------"
53+
print output
54+
print "----------------------------"
55+
else:
56+
lst_output = output.split('\n')
57+
lst_expected = expected_output.split('\n')
58+
ln = min(len(lst_output), len(lst_expected))
59+
for i in range(0, ln):
60+
if lst_output[i] != lst_expected[i]:
61+
print "Output: (at line " + str(i) + ")"
62+
print "----------------------------"
63+
print lst_output[i]
64+
print "----------------------------"
65+
print "Expected Output:"
66+
print "----------------------------"
67+
print lst_expected[i]
68+
print "----------------------------"
69+
break
70+
71+
print "exit code:", exit_code
72+
print "\nFailed!"
73+
sys.exit(exit_code)
74+
75+
def test_path(folder, is_file):
76+
files=['']
77+
if is_file == False:
78+
print "Testing ->", os.path.basename(folder)
79+
files = os.listdir(folder)
80+
else:
81+
files[0] = folder
82+
83+
for js_file in files:
84+
if is_file or os.path.splitext(js_file)[1] == '.js':
85+
js_file = os.path.join(folder, js_file)
86+
js_output = ""
87+
88+
if not os.path.isfile(js_file):
89+
print "Javascript file doesn't exist (" + js_file + ")"
90+
sys.exit(1)
91+
92+
p = SP.Popen([ch_path, js_file], stdout=SP.PIPE, stderr=SP.STDOUT, close_fds=True)
93+
js_output = p.communicate()[0].replace('\r','')
94+
exit_code = p.wait()
95+
96+
if exit_code != 0:
97+
show_failed(js_file, js_output, exit_code, None)
98+
else: #compare outputs
99+
baseline = os.path.splitext(js_file)[0] + '.baseline'
100+
baseline = os.path.join(folder, baseline)
101+
if os.path.isfile(baseline):
102+
expected_output = None
103+
with open(baseline, 'r') as bs_file:
104+
expected_output = bs_file.read().replace('\r', '')
105+
# todo: compare line by line and use/implement wild cards support
106+
# todo: by default we discard line endings (xplat), make this optional
107+
if expected_output.replace('\n', '') != js_output.replace('\n', ''):
108+
show_failed(js_file, js_output, exit_code, expected_output)
109+
110+
if not is_file:
111+
print "\tPassed ->", os.path.basename(js_file)
112+
113+
is_file = len(test_dirs) == 1 and os.path.splitext(test_dirs[0])[1] == '.js'
114+
115+
for folder in test_dirs:
116+
full_path = os.path.join(test_root, folder)
117+
if os.path.isdir(full_path) or is_file:
118+
test_path(full_path, is_file)
119+
120+
print 'Success!'

test/runtests.sh

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,15 @@
33
# Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
44
#-------------------------------------------------------------------------------------------------------
55
#
6-
# Eventual test running harness on *nix for ChakraCore
7-
# Today, it's simply there to make sure hello.js doesn't regress
8-
#
6+
# todo-CI: REMOVE THIS AFTER ENABLING runtests.py on CI
97

108
test_path=`dirname "$0"`
119
ch_path="$test_path/../BuildLinux/ch"
12-
hello_path="$test_path/Basics/hello.js"
1310

1411
if [ ! -f $ch_path ]; then
1512
echo 'ch not found- exiting'
1613
# TODO change this to exit 1 once clang requirement on build machines is satisfied
1714
exit 0
1815
fi
1916

20-
output=`$ch_path $hello_path 2>&1 | tail -n 1`
21-
22-
if [ ! $output == "PASS" ]; then
23-
echo "Hello world failed"
24-
exit 1
25-
fi
26-
27-
echo "Hello world passed"
28-
exit 0
17+
"$test_path/runtests.py" Basics/hello.js

0 commit comments

Comments
 (0)