Skip to content

Commit c7db887

Browse files
committed
Add --server option to test command
1 parent a2e8514 commit c7db887

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

bin/tstlib.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ def pop_argument(args, index=0):
9999
if index >= len(args):
100100
return None
101101

102-
return args.pop(0)
102+
return args.pop(index)
103103

104104

105-
def pop_option(args, option, short=None, default=None, type=str):
106-
selectors = ['--' + option]
105+
def pop_option(args, option, short=None, default=None, vtype=str):
106+
if type(option) == list:
107+
selectors = ['--' + opt for opt in option]
108+
else:
109+
selectors = ['--' + option]
110+
107111
if short is not None:
108112
selectors.append('-' + short)
109113

@@ -114,7 +118,7 @@ def pop_option(args, option, short=None, default=None, type=str):
114118
value = args.pop(index + 1)
115119
selector = args.pop(index)
116120

117-
return type(value)
121+
return vtype(value)
118122

119123

120124
def pop_flag(args, flag, short=None):
@@ -518,7 +522,6 @@ def read_tstjson(file=TSTJSON, exit=False, quit_on_fail=False):
518522

519523
try:
520524
with codecs.open(file, mode='r', encoding='utf-8') as f:
521-
print(file)
522525
tstjson = json.loads(to_unicode(f.read()))
523526

524527
except ValueError:
@@ -607,7 +610,6 @@ def abort(msg):
607610
if files[filename]['category'] == 'answer':
608611
assignment['checksum'] = md5.md5(contents.encode('utf-8')).hexdigest()
609612

610-
assignment['checksum'] = md5.md5(contents).hexdigest()
611613
assignment['files'] = files
612614
assignment['unknown_files'] = unknown_files
613615

commands/tst-checkout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def main(args):
159159
# parse command line
160160
command = tst.pop_argument(args)
161161
overwrite = tst.pop_flag(args, 'overwrite', short='o')
162-
index = tst.pop_option(args, 'index', short='i', type=int)
162+
index = tst.pop_option(args, 'index', short='i', vtype=int)
163163
name = tst.pop_argument(args)
164164
dirname = tst.pop_argument(args)
165165
_assert(args == [], "tst: fatal: too many arguments")

commands/tst-commit

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,20 @@ def commit_assignment(tstjson, args):
300300
# take unique unknown file as answer, add to tstjson and notify user
301301
filename = assignment['unknown_files'].pop(0)
302302
tst.cprint(tst.LCYAN, "Assuming `%s` as answer file" % filename)
303-
304303
_assert(filename, "No answer file was identified")
305304

305+
# read current answer
306306
if not os.path.exists(filename):
307307
tst.cprint(tst.LRED, "Answer file not found: '%s'" % filename)
308308
print("---")
309309
print("Use `tst commit <filename>` to change and commit the answer file")
310310
print("Or bring back the file and run `tst commit` again")
311311
sys.exit()
312-
313-
# read current answer
314312
with codecs.open(filename, mode='r', encoding='utf-8') as f:
315313
contents = f.read()
314+
316315
checksum = md5.md5(contents.encode('utf-8')).hexdigest()
317-
if filename == current_answer_file and checksum == tstjson.get('last_commit'):
316+
if filename == current_answer_file and checksum == tstjson.get('checksum'):
318317
tst.cprint(tst.LCYAN, 'No changes to commit')
319318
sys.exit()
320319

@@ -341,7 +340,7 @@ def commit_assignment(tstjson, args):
341340
'category': 'answer',
342341
'data': contents
343342
}
344-
tstjson['last_commit'] = checksum
343+
tstjson['checksum'] = checksum
345344
tst.save_tstjson(tstjson)
346345

347346
tst.cprint(tst.LCYAN, "Assignment successfully saved")

commands/tst-test

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import re
1111
import sys
1212
import json
1313
import glob
14+
import time
1415
import shlex
1516
import signal
1617
import string
@@ -682,12 +683,60 @@ def debug(msg=''):
682683
print(line.encode('utf-8'), file=sys.stderr)
683684

684685

686+
def tests_report(summary):
687+
num_tests = len(summary)
688+
successes = sum(1 for c in summary if c == '.')
689+
fails = sum(1 for c in summary if c in '*F')
690+
errors = num_tests - successes - fails
691+
692+
return num_tests, successes, fails, errors
693+
694+
685695
def request_server_tests(tstjson):
696+
697+
# basic data
686698
server = tst.Server()
687-
url = '/api/assignment/%s/tests' % tstjson['iid']
688-
response = server.post(url, payload="", exit_on_fail=True)
689-
print("Acceptance tests requested")
690-
print(response.stdout)
699+
url = '/api/assignment/%s/validation' % tstjson['iid']
700+
701+
while True:
702+
703+
# request last validation results
704+
response = server.post(url, exit_on_fail=True)
705+
status = response.json()['status']
706+
707+
if status == 'no validation requested':
708+
# request last answer validation
709+
tst.cprint(tst.LGREEN, "Requesting validation...")
710+
response = server.post(url, payload="", exit_on_fail=True)
711+
712+
elif status == 'waiting worker response':
713+
# wait a few seconds
714+
tst.cprint(tst.LCYAN, "Waiting for the worker...")
715+
time.sleep(3)
716+
717+
elif status == 'tests finished':
718+
break
719+
720+
else:
721+
print(response.stdout)
722+
_assert(False, "fatal: please contact developers")
723+
724+
summary = response.json()['summary']
725+
num_tests, successes, fails, errors = tests_report(summary)
726+
727+
# add num tests to msg
728+
msg = "%d tests run, %d errors, %d fails." % (num_tests, errors, fails)
729+
730+
if all(c == '.' for c in summary):
731+
tst.cprint(tst.LCYAN, summary)
732+
else:
733+
tst.cprint(tst.LRED, summary)
734+
print(msg)
735+
if not fails and not errors:
736+
tst.cprint(tst.LGREEN, "Answer accepted")
737+
else:
738+
tst.cprint(tst.LGREEN, "Answer rejected")
739+
691740

692741

693742
def main():

0 commit comments

Comments
 (0)