Commit 69fd46be 69fd46be96fe84fd08f21e8a8e174c4c2f45b134 by Nicolas Perriault

added cli command tests using python

1 parent 77f924d6
1 .PHONY: default all
2
3 default: all
4
5 all: selftest clitest jshint
6
7 selftest:
8 casperjs selftest
9
10 clitest:
11 python tests/clitests/runtests.py
12
13 jshint:
14 jshint --config=.jshintconfig .
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import json
4 import os 3 import os
5 import subprocess
6 import sys 4 import sys
7 5
8 def test_cmd(cmd):
9 try:
10 return subprocess.check_output([__file__] + cmd.split(' ')).strip()
11 except subprocess.CalledProcessError as err:
12 sys.stderr.write('FAIL: %s\n' % err)
13 sys.stderr.write(' return code: %d\n' % err.returncode)
14 sys.stderr.write(' args: %s\n' % ','.join(err.args))
15 sys.stderr.write(' cmd: %s\n' % ' '.join(err.cmd))
16 sys.stderr.write(' output: %s\n' % err.output)
17 sys.exit(1)
18
19 def resolve(path): 6 def resolve(path):
20 if os.path.islink(path): 7 if os.path.islink(path):
21 path = os.path.join(os.path.dirname(path), os.readlink(path)) 8 path = os.path.join(os.path.dirname(path), os.readlink(path))
...@@ -48,23 +35,6 @@ CASPER_PATH = os.path.abspath(os.path.join(os.path.dirname(resolve(__file__)), ' ...@@ -48,23 +35,6 @@ CASPER_PATH = os.path.abspath(os.path.join(os.path.dirname(resolve(__file__)), '
48 PHANTOMJS_ARGS = [] 35 PHANTOMJS_ARGS = []
49 SYS_ARGS = sys.argv[1:] 36 SYS_ARGS = sys.argv[1:]
50 37
51 if len(SYS_ARGS) and SYS_ARGS[0] == '__selfcommandtest':
52 print('Starting Python executable tests...')
53 with open('package.json') as f:
54 pkg_version = json.load(f).get('version')
55 scripts_path = os.path.join(CASPER_PATH, 'tests', 'commands')
56 assert(test_cmd('--help').find(pkg_version) > -1)
57 assert(test_cmd('--version').strip() == pkg_version)
58 assert(test_cmd(os.path.join(scripts_path, 'script.js')) == 'it works')
59 test_output = test_cmd('test --no-colors ' + os.path.join(scripts_path, 'mytest.js'))
60 assert('PASS ok1' in test_output)
61 assert('PASS ok2' in test_output)
62 assert('PASS ok3' in test_output)
63 module_test = test_cmd(os.path.join(CASPER_PATH, 'tests', 'clitests', 'modules', 'test.js'))
64 assert(module_test == 'hello, world')
65 print('Python executable tests passed.')
66 sys.exit(0)
67
68 for arg in SYS_ARGS: 38 for arg in SYS_ARGS:
69 found = False 39 found = False
70 for native in PHANTOMJS_NATIVE_ARGS: 40 for native in PHANTOMJS_NATIVE_ARGS:
......
1 #!/usr/bin/env python
2
3 import json
4 import os
5 import signal
6 import subprocess
7 import unittest
8
9 TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
10 CASPERJS_ROOT = os.path.abspath(os.path.join(TEST_ROOT, '..', '..'))
11 CASPER_EXEC = os.path.join(CASPERJS_ROOT, 'bin', 'casperjs')
12
13 class TimeoutException(Exception):
14 pass
15
16 def timeout(timeout_time):
17 def timeout_function(f):
18 def f2(*args):
19 def timeout_handler(signum, frame):
20 raise TimeoutException()
21 old_handler = signal.signal(signal.SIGALRM, timeout_handler)
22 signal.alarm(timeout_time) # triger alarm in timeout_time seconds
23 try:
24 retval = f(*args)
25 except TimeoutException:
26 raise AssertionError('timeout of %ds. exhausted' % timeout_time)
27 finally:
28 signal.signal(signal.SIGALRM, old_handler)
29 signal.alarm(0)
30 return retval
31 return f2
32 return timeout_function
33
34 class CasperExecTest(unittest.TestCase):
35 def setUp(self):
36 with open(os.path.join(CASPERJS_ROOT, 'package.json')) as f:
37 self.pkg_version = json.load(f).get('version')
38
39 def runCommand(self, cmd):
40 cmd_args = [CASPER_EXEC, '--no-colors'] + cmd.split(' ')
41 try:
42 return subprocess.check_output(cmd_args).strip()
43 except subprocess.CalledProcessError as err:
44 raise IOError('Command %s exited with status %s' % (cmd, err.status))
45
46 def assertCommandOutputEquals(self, cmd, result):
47 self.assertEquals(self.runCommand(cmd), result)
48
49 def assertCommandOutputContains(self, cmd, what):
50 if isinstance(what, (list, tuple)):
51 for entry in what:
52 self.assertIn(entry, self.runCommand(cmd))
53 else:
54 self.assertIn(what, self.runCommand(cmd))
55
56 @timeout(20)
57 def test_version(self):
58 self.assertCommandOutputEquals('--version', self.pkg_version)
59
60 @timeout(20)
61 def test_help(self):
62 self.assertCommandOutputContains('--help', self.pkg_version)
63
64 @timeout(20)
65 def test_require(self):
66 script_path = os.path.join(TEST_ROOT, 'modules', 'test.js')
67 self.assertCommandOutputEquals(script_path, 'hello, world')
68
69 @timeout(20)
70 def test_simple_script(self):
71 script_path = os.path.join(TEST_ROOT, 'scripts', 'script.js')
72 self.assertCommandOutputEquals(script_path, 'it works')
73
74 @timeout(20)
75 def test_simple_test_script(self):
76 script_path = os.path.join(TEST_ROOT, 'tester', 'mytest.js')
77 self.assertCommandOutputContains('test ' + script_path, [
78 script_path,
79 'PASS ok1',
80 'PASS ok2',
81 'PASS ok3',
82 '3 tests executed',
83 '3 passed',
84 '0 failed',
85 ])
86
87 @timeout(20)
88 def test_new_style_test(self):
89 # using begin()
90 script_path = os.path.join(TEST_ROOT, 'tester', 'passing.js')
91 self.assertCommandOutputContains('test ' + script_path, [
92 script_path,
93 '# true',
94 'PASS Subject is strictly true',
95 'PASS 1 tests executed',
96 '1 passed',
97 '0 failed',
98 ])
99
100 if __name__ == '__main__':
101 unittest.main()
1 /*jshint strict:false*/
2 casper.test.begin('true', 1, function(test) {
3 test.assert(false);
4 test.done();
5 });
1 /*jshint strict:false*/
2 casper.test.begin('true', 1, function(test) {
3 test.assert(true);
4 test.done();
5 });