Clean up and rename subunit-filter --with
authorMartin Pool <mbp@sourcefrog.net>
Thu, 6 Aug 2009 04:34:11 +0000 (14:34 +1000)
committerMartin Pool <mbp@sourcefrog.net>
Thu, 6 Aug 2009 04:34:11 +0000 (14:34 +1000)
filters/subunit-filter

index 7b14fa5b57d542ef281015ba7a89dc3e1c165152..6db8c04d9efd5ceda1ba1079cec42134b6d2c783 100755 (executable)
@@ -52,37 +52,41 @@ parser.add_option("--no-skip", action="store_true",
     help="exclude skips", dest="skip")
 parser.add_option("--no-success", action="store_true",
     help="exclude successes", default=True, dest="success")
-parser.add_option("-m", "--match", type=str,
-    help="regexp to include (case-sensitive by default)")
-parser.add_option("--anti-match", type=str,
-    help="regexp to exclude (case-sensitive by default)")
+parser.add_option("-m", "--with", type=str,
+    help="regexp to include (case-sensitive by default)",
+    action="append", dest="with_regexps")
+parser.add_option("--without", type=str,
+    help="regexp to exclude (case-sensitive by default)",
+    action="append", dest="without_regexps")
 
 (options, args) = parser.parse_args()
 
-def make_re_matcher(needle_re_string):
-    needle_re = re.compile(needle_re_string, re.MULTILINE)
-    return lambda test, err: (
-        needle_re.search(str(test)) or
-        ((err is not None) and needle_re.search(str(err))))
 
-if options.match:
-    match_predicate = make_re_matcher(options.match)
-else:
-    match_predicate = lambda test, err: True
+def _compile_re_from_list(l):
+    return re.compile("|".join(l), re.MULTILINE)
 
-if options.anti_match:
-    anti_match_predicate = make_re_matcher(options.anti_match)
-else:
-    anti_match_predicate = lambda test, err: False
 
-filter_predicate = lambda test, err: (
-    match_predicate(test, err) and not anti_match_predicate(test, err))
+def check_regexps(test, err):
+    """Check if this test and error match the regexp filters."""
+    test_str = str(test)
+    err_str = err and str(err) # may be None
+    if options.with_regexps:
+        with_re = _compile_re_from_list(options.with_regexps)
+        if not (with_re.search(test_str)
+            or with_re.search(err_str)):
+            return False
+    if options.without_regexps:
+        without_re = _compile_re_from_list(options.without_regexps)
+        if (without_re.search(test_str) or without_re.search(err_str)):
+            return False
+    return True
+
 
 result = TestProtocolClient(sys.stdout)
 result = TestResultFilter(result, filter_error=options.error,
     filter_failure=options.failure, filter_success=options.success,
     filter_skip=options.skip,
-    filter_predicate=filter_predicate)
+    filter_predicate=check_regexps)
 if options.no_passthrough:
     passthrough_stream = DiscardStream()
 else: