2021-01-31 16:03:49 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
import tempfile
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from data import Config, ExerciseInfo
|
|
|
|
|
|
|
|
|
|
# Allow high-performance tests to be skipped
|
|
|
|
|
ALLOW_SKIP = ['alphametics', 'largest-series-product']
|
|
|
|
|
|
|
|
|
|
|
2021-01-31 16:31:55 -05:00
|
|
|
def check_assignment(exercise: ExerciseInfo, quiet=False) -> int:
|
2021-01-31 16:03:49 -05:00
|
|
|
# Returns the exit code of the tests
|
|
|
|
|
workdir = Path(tempfile.mkdtemp(exercise.slug))
|
|
|
|
|
solution_file = exercise.solution_stub.name
|
|
|
|
|
try:
|
|
|
|
|
test_file_out = workdir / exercise.test_file.name
|
|
|
|
|
if exercise.slug in ALLOW_SKIP:
|
|
|
|
|
shutil.copyfile(exercise.test_file, test_file_out)
|
|
|
|
|
else:
|
|
|
|
|
with exercise.test_file.open('r') as src_file:
|
|
|
|
|
lines = [line for line in src_file.readlines()
|
|
|
|
|
if not line.strip().startswith('@unittest.skip')]
|
|
|
|
|
with test_file_out.open('w') as dst_file:
|
|
|
|
|
dst_file.writelines(lines)
|
|
|
|
|
shutil.copyfile(exercise.exemplar_file, workdir / solution_file)
|
2021-01-31 16:31:55 -05:00
|
|
|
kwargs = {}
|
|
|
|
|
if quiet:
|
|
|
|
|
kwargs['stdout'] = subprocess.DEVNULL
|
|
|
|
|
kwargs['stderr'] = subprocess.DEVNULL
|
2021-02-05 22:31:06 -05:00
|
|
|
return subprocess.run([sys.executable, '-m', 'pytest', test_file_out], **kwargs).returncode
|
2021-01-31 16:03:49 -05:00
|
|
|
finally:
|
|
|
|
|
shutil.rmtree(workdir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
config = Config.load()
|
|
|
|
|
exercises = config.exercises.all()
|
|
|
|
|
if len(sys.argv) >= 2:
|
|
|
|
|
# test specific exercises
|
|
|
|
|
exercises = [
|
|
|
|
|
e for e in exercises if e.slug in sys.argv[1:]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
failures = []
|
|
|
|
|
for exercise in exercises:
|
|
|
|
|
print('# ', exercise.slug)
|
|
|
|
|
if not exercise.test_file:
|
|
|
|
|
print('FAIL: File with test cases not found')
|
|
|
|
|
failures.append('{} (FileNotFound)'.format(exercise.slug))
|
|
|
|
|
else:
|
|
|
|
|
if check_assignment(exercise):
|
2021-02-05 22:41:19 -05:00
|
|
|
failures.append('{} (TestFailed)'.format(exercise.slug))
|
2021-01-31 16:03:49 -05:00
|
|
|
print('')
|
|
|
|
|
|
|
|
|
|
print('TestEnvironment:', sys.executable.capitalize(), '\n\n')
|
|
|
|
|
|
|
|
|
|
if failures:
|
|
|
|
|
print('FAILURES: ', ', '.join(failures))
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
else:
|
|
|
|
|
print('SUCCESS!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|