1
1
#!/usr/bin/env python3
2
2
3
- import os
4
- import glob
5
3
import shutil
6
4
import subprocess
7
5
import sys
8
6
import tempfile
9
7
import json
8
+ from pathlib import Path
10
9
11
10
# Allow high-performance tests to be skipped
12
11
ALLOW_SKIP = ['alphametics' , 'largest-series-product' ]
13
12
14
13
15
- def check_assignment (name , test_file ) :
14
+ def check_assignment (name : str , test_file : Path ) -> int :
16
15
# Returns the exit code of the tests
17
- workdir = tempfile .mkdtemp (name )
16
+ workdir = Path ( tempfile .mkdtemp (name ) )
18
17
example_name = name .replace ("-" , "_" )
19
18
try :
20
- test_file_out = os . path . join ( workdir , os . path . basename ( test_file ))
19
+ test_file_out = workdir / test_file . name
21
20
if name in ALLOW_SKIP :
22
21
shutil .copyfile (test_file , test_file_out )
23
22
else :
24
- with open (test_file , 'r' ) as src_file :
23
+ with test_file . open ('r' ) as src_file :
25
24
lines = [line for line in src_file .readlines ()
26
25
if not line .strip ().startswith ('@unittest.skip' )]
27
- with open (test_file_out , 'w' ) as dst_file :
26
+ with test_file_out . open ('w' ) as dst_file :
28
27
dst_file .writelines (lines )
29
- shutil .copyfile (os .path .join (os .path .dirname (test_file ), 'example.py' ),
30
- os .path .join (workdir , '{}.py' .format (example_name )))
28
+ exemplar_file = test_file .with_name ('exemplar.py' )
29
+ if not exemplar_file .is_file ():
30
+ exemplar_file = exemplar_file .with_name ('example.py' )
31
+ print (exemplar_file )
32
+ shutil .copyfile (exemplar_file , workdir / f'{ example_name } .py' )
31
33
return subprocess .call ([sys .executable , test_file_out ])
32
34
finally :
33
35
shutil .rmtree (workdir )
34
36
35
37
36
38
def load_config ():
39
+ config_file = Path ('config.json' )
37
40
try :
38
- with open ('./config.json' ) as json_file :
41
+ with config_file . open () as json_file :
39
42
data = json .load (json_file )
40
43
except IOError :
41
- print ('FAIL: config.json file not found' )
44
+ print (f 'FAIL: { config_file } file not found' )
42
45
raise SystemExit (1 )
43
46
44
47
try :
45
48
problems = [entry ['slug' ] for entry in data ['exercises' ]
46
49
if "deprecated" not in entry ]
47
50
except KeyError :
48
- print ('FAIL: config.json has an incorrect format' )
51
+ print (f 'FAIL: { config_file } has an incorrect format' )
49
52
raise SystemExit (1 )
50
53
51
54
return problems
@@ -60,14 +63,15 @@ def main():
60
63
exercises = load_config ()
61
64
62
65
failures = []
66
+ exercises_dir = Path ('exercises' )
63
67
for exercise in exercises :
64
- test_file = glob .glob ('./exercises/{}/ *_test.py' . format ( exercise ) )
68
+ test_file = next (( exercises_dir / exercise ) .glob ('*_test.py' ), None )
65
69
print ('# ' , exercise )
66
70
if not test_file :
67
71
print ('FAIL: File with test cases not found' )
68
72
failures .append ('{} (FileNotFound)' .format (exercise ))
69
73
else :
70
- if check_assignment (exercise , test_file [ 0 ] ):
74
+ if check_assignment (exercise , test_file ):
71
75
failures .append ('{} (TestFailed)' .format (exercise ))
72
76
print ('' )
73
77
0 commit comments