Skip to content

Commit ec8c439

Browse files
committed
Configure Travis build. Fixes exercism#1
1 parent 3a288c4 commit ec8c439

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: python
2+
script: ./test/check-exercises.py

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# xPython
22

3+
[![Build Status](https://travis-ci.org/exercism/xpython.png?branch=master)](https://travis-ci.org/exercism/xpython)
4+
35
Exercism exercises in Python
46

57
## License

test/check-exercises.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
import os
3+
import ast
4+
import imp
5+
import glob
6+
import shutil
7+
import subprocess
8+
import tempfile
9+
10+
11+
def check_assignment(name, test_file, example_name):
12+
# Returns the exit code of the tests
13+
workdir = tempfile.mkdtemp(name)
14+
try:
15+
test_file_out = os.path.join(workdir, os.path.basename(test_file))
16+
shutil.copyfile(test_file, test_file_out)
17+
shutil.copyfile(os.path.join(os.path.dirname(test_file), 'example.py'),
18+
os.path.join(workdir, '{}.py'.format(example_name)))
19+
return subprocess.call(['python', test_file_out])
20+
finally:
21+
shutil.rmtree(workdir)
22+
23+
24+
def modname_heuristic(test_file):
25+
with open(test_file) as f:
26+
tree = ast.parse(f.read(), filename=test_file)
27+
# return the first nonexistent module that the tests import
28+
for node in ast.walk(tree):
29+
for modname in possible_module_names(node):
30+
if is_module_missing(modname):
31+
return modname
32+
33+
34+
def possible_module_names(node):
35+
if isinstance(node, ast.Import):
36+
for alias in node.names:
37+
yield alias.name
38+
elif isinstance(node, ast.ImportFrom):
39+
yield node.module
40+
41+
42+
def is_module_missing(modname):
43+
try:
44+
imp.find_module(modname)
45+
except ImportError:
46+
return True
47+
else:
48+
return False
49+
50+
51+
def assignment_name(test_file):
52+
return os.path.basename(test_file).rpartition('_')[0]
53+
54+
55+
def main():
56+
failures = []
57+
for test_file in glob.glob('./*/*_test.py'):
58+
name = assignment_name(test_file)
59+
if check_assignment(name, test_file, modname_heuristic(test_file)):
60+
failures.append(name)
61+
if failures:
62+
print 'FAILURES: ' + ' '.join(failures)
63+
raise SystemExit(1)
64+
else:
65+
print 'SUCCESS!'
66+
67+
if __name__ == '__main__':
68+
main()

0 commit comments

Comments
 (0)