casperjs
2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
import json
import os
import subprocess
import sys
def test_cmd(cmd):
try:
return subprocess.check_output([__file__] + cmd.split(' '))
except subprocess.CalledProcessError as err:
sys.stderr.write('FAIL: %s\n' % ' '.join(err.cmd))
sys.stderr.write(' %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))
return resolve(path)
return path
PHANTOMJS_NATIVE_ARGS = [
'cookies-file',
'config',
'debug',
'disk-cache',
'ignore-ssl-errors',
'load-images',
'load-plugins',
'local-storage-path',
'local-storage-quota',
'local-to-remote-url-access',
'max-disk-cache-size',
'output-encoding',
'proxy',
'proxy-auth',
'proxy-type',
'remote-debugger-port',
'remote-debugger-autorun',
'script-encoding',
'web-security',
]
CASPER_ARGS = []
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\n')
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)
print('Python executable tests passed.')
sys.exit(0)
for arg in SYS_ARGS:
found = False
for native in PHANTOMJS_NATIVE_ARGS:
if arg.startswith('--%s' % native):
PHANTOMJS_ARGS.append(arg)
found = True
if not found:
CASPER_ARGS.append(arg)
CASPER_COMMAND = os.environ.get('PHANTOMJS_EXECUTABLE', 'phantomjs').split(' ')
CASPER_COMMAND.extend(PHANTOMJS_ARGS)
CASPER_COMMAND.extend([
os.path.join(CASPER_PATH, 'bin', 'bootstrap.js'),
'--casper-path=%s' % CASPER_PATH,
'--cli'
])
CASPER_COMMAND.extend(CASPER_ARGS)
try:
os.execvp(CASPER_COMMAND[0], CASPER_COMMAND)
except OSError as err:
print(('Fatal: %s; did you install phantomjs?' % err))
sys.exit(1)