]> sigrok.org Git - libsigrokdecode.git/blobdiff - tests/pdtest
pdtest: Use -dd to turn on SR/SRD debugging.
[libsigrokdecode.git] / tests / pdtest
index 59348d6957d4f1176b724da43b4a3b76735bf2c0..ef0c3503ad23d780d3c4b201d1255e1e78f44ce1 100755 (executable)
@@ -7,7 +7,7 @@ from tempfile import mkstemp
 from subprocess import Popen, PIPE
 from difflib import Differ
 
-DEBUG = False
+DEBUG = 0
 VERBOSE = False
 
 
@@ -46,7 +46,7 @@ def usage(msg=None):
     sys.exit()
 
 
-def check_testcase(tc):
+def check_tclist(tc):
     if 'pdlist' not in tc or not tc['pdlist']:
         return("No protocol decoders")
     if 'input' not in tc or not tc['input']:
@@ -167,11 +167,11 @@ def parse_testfile(path, pd, tc, op_type, op_class):
             tclist = []
         else:
             tclist = [target_tc]
-            for t in tclist:
-                error = check_testcase(t)
-                if error:
-                    ERR("Error in %s: %s" % (path, error))
-                    return []
+    for t in tclist:
+        error = check_tclist(t)
+        if error:
+            ERR("Error in %s: %s" % (path, error))
+            return []
 
     return tclist
 
@@ -221,6 +221,8 @@ def run_tests(tests):
     for tclist in tests:
         for tc in tclist:
             args = [cmd]
+            if DEBUG > 1:
+                args.append('-d')
             for pd in tc['pdlist']:
                 args.extend(['-P', pd['name']])
                 for label, probe in pd['probes']:
@@ -244,17 +246,24 @@ def run_tests(tests):
                     fd, outfile = mkstemp()
                     os.close(fd)
                     opargs.extend(['-f', outfile])
-                    DBG("Running %s %s" % (cmd, ' '.join(args + opargs)))
-                    stdout, stderr = Popen(args + opargs, stdout=PIPE, stderr=PIPE).communicate()
+                    DBG("Running %s" % (' '.join(args + opargs)))
+                    p = Popen(args + opargs, stdout=PIPE, stderr=PIPE)
+                    stdout, stderr = p.communicate()
                     if stdout:
                         results[-1]['statistics'] = stdout.decode('utf-8').strip()
                     if stderr:
                         results[-1]['error'] = stderr.decode('utf-8').strip()
                         errors += 1
-                    match = "%s/%s/test/%s" % (decoders_dir, op['pd'], op['match'])
-                    diff = diff_files(match, outfile)
-                    if diff:
-                        results[-1]['diff'] = diff
+                    elif p.returncode != 0:
+                        # 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
                 except Exception as e:
                     results[-1]['error'] = str(e)
                 finally:
@@ -348,10 +357,10 @@ if len(sys.argv) == 1:
 
 opt_all = opt_run = opt_show = opt_list = False
 report_dir = None
-opts, args = getopt(sys.argv[1:], "dvarslR:")
+opts, args = getopt(sys.argv[1:], "dvarslRS:")
 for opt, arg in opts:
     if opt == '-d':
-        DEBUG = True
+        DEBUG += 1
     if opt == '-v':
         VERBOSE = True
     elif opt == '-a':
@@ -364,6 +373,8 @@ for opt, arg in opts:
         opt_list = True
     elif opt == '-R':
         report_dir = arg
+    elif opt == '-S':
+        dumps_dir = arg
 
 if opt_run and opt_show:
     usage("Use either -s or -r, not both.")
@@ -382,6 +393,9 @@ try:
         usage("Specify either -a or tests.")
 
     if opt_run:
+        if not os.path.isdir(dumps_dir):
+            ERR("Could not find sigrok-dumps repository at %s" % dumps_dir)
+            sys.exit(1)
         results, errors = run_tests(testlist)
         ret = errors
     elif opt_show: