11#!/usr/bin/env python3
22
3- import os
4- import glob
53import shutil
64import subprocess
75import sys
86import tempfile
97import json
8+ from pathlib import Path
109
1110# Allow high-performance tests to be skipped
1211ALLOW_SKIP = ['alphametics' , 'largest-series-product' ]
1312
1413
15- def check_assignment (name , test_file ) :
14+ def check_assignment (name : str , test_file : Path ) -> int :
1615 # Returns the exit code of the tests
17- workdir = tempfile .mkdtemp (name )
16+ workdir = Path ( tempfile .mkdtemp (name ) )
1817 example_name = name .replace ("-" , "_" )
1918 try :
20- test_file_out = os . path . join ( workdir , os . path . basename ( test_file ))
19+ test_file_out = workdir / test_file . name
2120 if name in ALLOW_SKIP :
2221 shutil .copyfile (test_file , test_file_out )
2322 else :
24- with open (test_file , 'r' ) as src_file :
23+ with test_file . open ('r' ) as src_file :
2524 lines = [line for line in src_file .readlines ()
2625 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 :
2827 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' )
3133 return subprocess .call ([sys .executable , test_file_out ])
3234 finally :
3335 shutil .rmtree (workdir )
3436
3537
3638def load_config ():
39+ config_file = Path ('config.json' )
3740 try :
38- with open ('./config.json' ) as json_file :
41+ with config_file . open () as json_file :
3942 data = json .load (json_file )
4043 except IOError :
41- print ('FAIL: config.json file not found' )
44+ print (f 'FAIL: { config_file } file not found' )
4245 raise SystemExit (1 )
4346
4447 try :
4548 problems = [entry ['slug' ] for entry in data ['exercises' ]
4649 if "deprecated" not in entry ]
4750 except KeyError :
48- print ('FAIL: config.json has an incorrect format' )
51+ print (f 'FAIL: { config_file } has an incorrect format' )
4952 raise SystemExit (1 )
5053
5154 return problems
@@ -60,14 +63,15 @@ def main():
6063 exercises = load_config ()
6164
6265 failures = []
66+ exercises_dir = Path ('exercises' )
6367 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 )
6569 print ('# ' , exercise )
6670 if not test_file :
6771 print ('FAIL: File with test cases not found' )
6872 failures .append ('{} (FileNotFound)' .format (exercise ))
6973 else :
70- if check_assignment (exercise , test_file [ 0 ] ):
74+ if check_assignment (exercise , test_file ):
7175 failures .append ('{} (TestFailed)' .format (exercise ))
7276 print ('' )
7377
0 commit comments