Commit cfaf3c73 authored by Denis Bilenko's avatar Denis Bilenko

testrunner.py: replace all prints with logging to stderr; do not assume that newline is \n

parent b4e647b1
...@@ -86,7 +86,7 @@ def store_record(database_path, table, dictionary, _added_colums_per_db={}): ...@@ -86,7 +86,7 @@ def store_record(database_path, table, dictionary, _added_colums_per_db={}):
try: try:
cursor.execute(sql, dictionary) cursor.execute(sql, dictionary)
except sqlite3.Error: except sqlite3.Error:
print ('sql=%r\ndictionary=%r' % (sql, dictionary)) log('sql=%r\ndictionary=%r', sql, dictionary)
raise raise
conn.commit() conn.commit()
return cursor.lastrowid return cursor.lastrowid
...@@ -97,13 +97,13 @@ def delete_record(database_path, table, dictionary, _added_colums_per_db={}): ...@@ -97,13 +97,13 @@ def delete_record(database_path, table, dictionary, _added_colums_per_db={}):
return return
keys = dictionary.keys() keys = dictionary.keys()
conn = sqlite3.connect(database_path) conn = sqlite3.connect(database_path)
print ('deleting %s from database' % (dictionary, )) #print ('deleting %s from database' % (dictionary, ))
sql = 'delete from %s where %s' % (table, ' AND '.join('%s=:%s' % (key, key) for key in keys)) sql = 'delete from %s where %s' % (table, ' AND '.join('%s=:%s' % (key, key) for key in keys))
cursor = conn.cursor() cursor = conn.cursor()
try: try:
cursor.execute(sql, dictionary) cursor.execute(sql, dictionary)
except sqlite3.Error: except sqlite3.Error:
print ('sql=%r\ndictionary=%r' % (sql, dictionary)) log('sql=%r\ndictionary=%r', sql, dictionary)
raise raise
conn.commit() conn.commit()
return cursor.lastrowid return cursor.lastrowid
...@@ -322,15 +322,15 @@ def spawn_subprocess(args, options, base_params): ...@@ -322,15 +322,15 @@ def spawn_subprocess(args, options, base_params):
else: else:
if not output_printed and options.verbosity >= -1: if not output_printed and options.verbosity >= -1:
sys.stdout.write(output) sys.stdout.write(output)
print ('%s failed with code %s' % (' '.join(args), retcode)) log('%s failed with code %s', ' '.join(args), retcode)
elif retcode == 0: elif retcode == 0:
if not output_printed and options.verbosity >= 1: if not output_printed and options.verbosity >= 1:
sys.stdout.write(output) sys.stdout.write(output)
if options.verbosity >= 0: if options.verbosity >= 0:
print ('%s passed' % ' '.join(args)) log('%s passed', ' '.join(args))
success = True success = True
else: else:
print ('%s timed out' % ' '.join(args)) log('%s timed out', ' '.join(args))
sys.stdout.flush() sys.stdout.flush()
if options.db: if options.db:
params['output'] = output params['output'] = output
...@@ -363,12 +363,12 @@ def spawn_subprocesses(options, args): ...@@ -363,12 +363,12 @@ def spawn_subprocesses(options, args):
traceback.print_exc() traceback.print_exc()
if options.db: if options.db:
try: try:
print ('-' * 80) log('-' * 80)
if print_stats(options): if print_stats(options):
success = False success = False
except sqlite3.OperationalError: except sqlite3.OperationalError:
traceback.print_exc() traceback.print_exc()
print ('To view stats again for this run, use %s --stats --runid %s --db %s' % (sys.argv[0], options.runid, options.db)) log('To view stats again for this run, use %s --stats --runid %s --db %s', sys.argv[0], options.runid, options.db)
if not success: if not success:
sys.exit(1) sys.exit(1)
...@@ -409,6 +409,8 @@ def get_warnings(output): ...@@ -409,6 +409,8 @@ def get_warnings(output):
return _warning_re.findall(output[:OUTPUT_LIMIT]) + ['AbridgedOutputWarning'] return _warning_re.findall(output[:OUTPUT_LIMIT]) + ['AbridgedOutputWarning']
newline_re = re.compile('[\r\n]+')
def get_exceptions(output): def get_exceptions(output):
""" """
>>> get_exceptions('''test$ python -c "1/0" >>> get_exceptions('''test$ python -c "1/0"
...@@ -419,7 +421,7 @@ def get_exceptions(output): ...@@ -419,7 +421,7 @@ def get_exceptions(output):
""" """
errors = [] errors = []
readtb = False readtb = False
for line in output.split('\n'): for line in newline_re.split(output):
if 'Traceback (most recent call last):' in line: if 'Traceback (most recent call last):' in line:
readtb = True readtb = True
else: else:
...@@ -500,14 +502,14 @@ def print_stats(options): ...@@ -500,14 +502,14 @@ def print_stats(options):
cursor = db.cursor() cursor = db.cursor()
if options.runid is None: if options.runid is None:
options.runid = cursor.execute('select runid from test order by started_at desc limit 1').fetchall()[0][0] options.runid = cursor.execute('select runid from test order by started_at desc limit 1').fetchall()[0][0]
print ('Using the latest runid: %s' % options.runid) log('Using the latest runid: %s', options.runid)
total = len(get_testcases(cursor, options.runid)) total = len(get_testcases(cursor, options.runid))
failed, errors = get_failed_testcases(cursor, options.runid) failed, errors = get_failed_testcases(cursor, options.runid)
timedout = get_testcases(cursor, options.runid, 'TIMEOUT') timedout = get_testcases(cursor, options.runid, 'TIMEOUT')
for test, output, retcode in cursor.execute('select test, output, retcode from test where runid=?', (options.runid, )): for test, output, retcode in cursor.execute('select test, output, retcode from test where runid=?', (options.runid, )):
info, skipped = get_info(output or '', test) info, skipped = get_info(output or '', test)
if info: if info:
print ('%s: %s' % (test, info)) log('%s: %s', test, info)
if retcode == 'TIMEOUT': if retcode == 'TIMEOUT':
for testcase in timedout: for testcase in timedout:
if testcase.startswith(test + '.'): if testcase.startswith(test + '.'):
...@@ -525,18 +527,18 @@ def print_stats(options): ...@@ -525,18 +527,18 @@ def print_stats(options):
total += 1 total += 1
if failed: if failed:
failed.sort() failed.sort()
print ('FAILURES: ') log('FAILURES: ')
for testcase in failed: for testcase in failed:
error = errors.get(testcase) error = errors.get(testcase)
if error: if error:
error = repr(error)[1:-1][:100] error = repr(error)[1:-1][:100]
print (' - %s: %s' % (testcase, error)) log(' - %s: %s', testcase, error)
else: else:
print (' - %s' % (testcase, )) log(' - %s', testcase)
if timedout: if timedout:
print ('TIMEOUTS: ') log('TIMEOUTS: ')
print (' - ' + '\n - '.join(timedout)) log(' - ' + '\n - '.join(timedout))
print ('%s testcases passed; %s failed; %s timed out' % (total, len(failed), len(timedout))) log('%s testcases passed; %s failed; %s timed out', total, len(failed), len(timedout))
if failed or timedout: if failed or timedout:
return True return True
return False return False
...@@ -562,9 +564,9 @@ def main(): ...@@ -562,9 +564,9 @@ def main():
if options.db: if options.db:
if sqlite3: if sqlite3:
options.db = os.path.abspath(options.db) options.db = os.path.abspath(options.db)
print ('Using the database: %s' % options.db) log('Using the database: %s', options.db)
else: else:
sys.stderr.write('Cannot access the database %r: no sqlite3 module found.\n' % (options.db, )) log('Cannot access the database %r: no sqlite3 module found.', options.db)
options.db = False options.db = False
if options.db: if options.db:
...@@ -583,12 +585,29 @@ def main(): ...@@ -583,12 +585,29 @@ def main():
except ImportError: except ImportError:
import random import random
options.runid = str(random.random())[2:] options.runid = str(random.random())[2:]
print ('Generated runid: %s' % (options.runid, )) log('Generated runid: %s', options.runid)
if options.record: if options.record:
run_tests(options, args) run_tests(options, args)
else: else:
spawn_subprocesses(options, args) spawn_subprocesses(options, args)
def log(message, *args):
try:
string = message % args
except Exception:
traceback.print_exc()
try:
message = '%r %% %r\n\n' % (message, args)
except Exception:
pass
try:
sys.stderr.write(message)
except Exception:
traceback.print_exc()
else:
sys.stderr.write(string + '\n')
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment