-#!/usr/bin/env /usr/bin/python3
+#!/usr/bin/env python3
+##
+## This file is part of the libsigrokdecode project.
+##
+## Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
+##
+## This program is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program. If not, see <http://www.gnu.org/licenses/>.
+##
import os
import sys
from tempfile import mkstemp
from subprocess import Popen, PIPE
from difflib import Differ
+from hashlib import md5
+from shutil import copy
DEBUG = 0
VERBOSE = False
-l List all tests
-s Show test(s)
-r Run test(s)
+ -f Fix failed test(s)
-R <directory> Save test reports to <directory>
<test> Protocol decoder name ("i2c") and optionally test name ("i2c/icc")""")
sys.exit()
return tests
-def diff_files(f1, f2):
+def diff_text(f1, f2):
t1 = open(f1).readlines()
t2 = open(f2).readlines()
diff = []
return diff
-def run_tests(tests):
+def compare_binary(f1, f2):
+ h1 = md5()
+ h1.update(open(f1, 'rb').read())
+ h2 = md5()
+ h2.update(open(f2, 'rb').read())
+ if h1.digest() == h2.digest():
+ result = None
+ else:
+ result = ["Binary output does not match."]
+
+ return result
+
+
+def run_tests(tests, fix=False):
errors = 0
results = []
cmd = os.path.join(tests_dir, 'runtc')
# runtc indicated an error, but didn't output a
# message on stderr about it
results[-1]['error'] = "Unknown error: runtc %d" % p.returncode
- # Only bother with the diff if it all worked.
if 'error' not in results[-1]:
- match = "%s/%s/test/%s" % (decoders_dir, op['pd'], op['match'])
- diff = diff_files(match, outfile)
- if diff:
- results[-1]['diff'] = diff
+ match = os.path.join(decoders_dir, op['pd'], 'test', op['match'])
+ try:
+ diff = diff_error = None
+ if op['type'] in ('annotation', 'python'):
+ diff = diff_text(match, outfile)
+ elif op['type'] == 'binary':
+ diff = compare_binary(match, outfile)
+ else:
+ diff = ["Unsupported output type '%s'." % op['type']]
+ except Exception as e:
+ diff_error = e
+ if fix:
+ if diff or diff_error:
+ copy(outfile, match)
+ DBG("Wrote %s" % match)
+ else:
+ if diff:
+ results[-1]['diff'] = diff
+ elif diff_error is not None:
+ raise diff_error
except Exception as e:
results[-1]['error'] = str(e)
finally:
if len(sys.argv) == 1:
usage()
-opt_all = opt_run = opt_show = opt_list = False
+opt_all = opt_run = opt_show = opt_list = opt_fix = False
report_dir = None
-opts, args = getopt(sys.argv[1:], "dvarslRS:")
+opts, args = getopt(sys.argv[1:], "dvarslfR:S:")
for opt, arg in opts:
if opt == '-d':
DEBUG += 1
opt_show = True
elif opt == '-l':
opt_list = True
+ elif opt == '-f':
+ opt_fix = True
elif opt == '-R':
report_dir = arg
elif opt == '-S':
show_tests(testlist)
elif opt_list:
list_tests(testlist)
+ elif opt_fix:
+ run_tests(testlist, fix=True)
else:
usage()
except Exception as e: