]> sigrok.org Git - libsigrokdecode.git/blobdiff - tests/pdtest
pdtest: Add -f option to automatically fix failing tests.
[libsigrokdecode.git] / tests / pdtest
index ef0c3503ad23d780d3c4b201d1255e1e78f44ce1..608cd8a464012f3e6bfded10c6c1601f60ad0ca8 100755 (executable)
@@ -41,6 +41,7 @@ def usage(msg=None):
   -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()
@@ -214,7 +215,7 @@ def diff_files(f1, f2):
     return diff
 
 
-def run_tests(tests):
+def run_tests(tests, fix=False):
     errors = 0
     results = []
     cmd = os.path.join(tests_dir, 'runtc')
@@ -258,12 +259,25 @@ def run_tests(tests):
                         # 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'] == 'annotation':
+                                diff = diff_textfiles(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:
@@ -355,9 +369,9 @@ decoders_dir = os.path.abspath(os.path.join(base_dir, 'decoders'))
 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:], "dvarslfRS:")
 for opt, arg in opts:
     if opt == '-d':
         DEBUG += 1
@@ -371,6 +385,8 @@ for opt, arg in opts:
         opt_show = True
     elif opt == '-l':
         opt_list = True
+    elif opt == '-f':
+        opt_fix = True
     elif opt == '-R':
         report_dir = arg
     elif opt == '-S':
@@ -402,6 +418,8 @@ try:
         show_tests(testlist)
     elif opt_list:
         list_tests(testlist)
+    elif opt_fix:
+        run_tests(testlist, fix=True)
     else:
         usage()
 except Exception as e: