Commit 69fd46be 69fd46be96fe84fd08f21e8a8e174c4c2f45b134 by Nicolas Perriault

added cli command tests using python

1 parent 77f924d6
.PHONY: default all
default: all
all: selftest clitest jshint
selftest:
casperjs selftest
clitest:
python tests/clitests/runtests.py
jshint:
jshint --config=.jshintconfig .
#!/usr/bin/env python
import json
import os
import subprocess
import sys
def test_cmd(cmd):
try:
return subprocess.check_output([__file__] + cmd.split(' ')).strip()
except subprocess.CalledProcessError as err:
sys.stderr.write('FAIL: %s\n' % err)
sys.stderr.write(' return code: %d\n' % err.returncode)
sys.stderr.write(' args: %s\n' % ','.join(err.args))
sys.stderr.write(' cmd: %s\n' % ' '.join(err.cmd))
sys.stderr.write(' output: %s\n' % err.output)
sys.exit(1)
def resolve(path):
if os.path.islink(path):
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__)), '
PHANTOMJS_ARGS = []
SYS_ARGS = sys.argv[1:]
if len(SYS_ARGS) and SYS_ARGS[0] == '__selfcommandtest':
print('Starting Python executable tests...')
with open('package.json') as f:
pkg_version = json.load(f).get('version')
scripts_path = os.path.join(CASPER_PATH, 'tests', 'commands')
assert(test_cmd('--help').find(pkg_version) > -1)
assert(test_cmd('--version').strip() == pkg_version)
assert(test_cmd(os.path.join(scripts_path, 'script.js')) == 'it works')
test_output = test_cmd('test --no-colors ' + os.path.join(scripts_path, 'mytest.js'))
assert('PASS ok1' in test_output)
assert('PASS ok2' in test_output)
assert('PASS ok3' in test_output)
module_test = test_cmd(os.path.join(CASPER_PATH, 'tests', 'clitests', 'modules', 'test.js'))
assert(module_test == 'hello, world')
print('Python executable tests passed.')
sys.exit(0)
for arg in SYS_ARGS:
found = False
for native in PHANTOMJS_NATIVE_ARGS:
......
#!/usr/bin/env python
import json
import os
import signal
import subprocess
import unittest
TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
CASPERJS_ROOT = os.path.abspath(os.path.join(TEST_ROOT, '..', '..'))
CASPER_EXEC = os.path.join(CASPERJS_ROOT, 'bin', 'casperjs')
class TimeoutException(Exception):
pass
def timeout(timeout_time):
def timeout_function(f):
def f2(*args):
def timeout_handler(signum, frame):
raise TimeoutException()
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout_time) # triger alarm in timeout_time seconds
try:
retval = f(*args)
except TimeoutException:
raise AssertionError('timeout of %ds. exhausted' % timeout_time)
finally:
signal.signal(signal.SIGALRM, old_handler)
signal.alarm(0)
return retval
return f2
return timeout_function
class CasperExecTest(unittest.TestCase):
def setUp(self):
with open(os.path.join(CASPERJS_ROOT, 'package.json')) as f:
self.pkg_version = json.load(f).get('version')
def runCommand(self, cmd):
cmd_args = [CASPER_EXEC, '--no-colors'] + cmd.split(' ')
try:
return subprocess.check_output(cmd_args).strip()
except subprocess.CalledProcessError as err:
raise IOError('Command %s exited with status %s' % (cmd, err.status))
def assertCommandOutputEquals(self, cmd, result):
self.assertEquals(self.runCommand(cmd), result)
def assertCommandOutputContains(self, cmd, what):
if isinstance(what, (list, tuple)):
for entry in what:
self.assertIn(entry, self.runCommand(cmd))
else:
self.assertIn(what, self.runCommand(cmd))
@timeout(20)
def test_version(self):
self.assertCommandOutputEquals('--version', self.pkg_version)
@timeout(20)
def test_help(self):
self.assertCommandOutputContains('--help', self.pkg_version)
@timeout(20)
def test_require(self):
script_path = os.path.join(TEST_ROOT, 'modules', 'test.js')
self.assertCommandOutputEquals(script_path, 'hello, world')
@timeout(20)
def test_simple_script(self):
script_path = os.path.join(TEST_ROOT, 'scripts', 'script.js')
self.assertCommandOutputEquals(script_path, 'it works')
@timeout(20)
def test_simple_test_script(self):
script_path = os.path.join(TEST_ROOT, 'tester', 'mytest.js')
self.assertCommandOutputContains('test ' + script_path, [
script_path,
'PASS ok1',
'PASS ok2',
'PASS ok3',
'3 tests executed',
'3 passed',
'0 failed',
])
@timeout(20)
def test_new_style_test(self):
# using begin()
script_path = os.path.join(TEST_ROOT, 'tester', 'passing.js')
self.assertCommandOutputContains('test ' + script_path, [
script_path,
'# true',
'PASS Subject is strictly true',
'PASS 1 tests executed',
'1 passed',
'0 failed',
])
if __name__ == '__main__':
unittest.main()
/*jshint strict:false*/
casper.test.begin('true', 1, function(test) {
test.assert(false);
test.done();
});
/*jshint strict:false*/
casper.test.begin('true', 1, function(test) {
test.assert(true);
test.done();
});