Re-use more common code.
[third_party/subunit] / filters / subunit-filter
index ae080a4af6966af7c65c11dc0374e2e9750d9bf7..4f63d2ea497a4dcab3a893e6367824d9c5ed7bd8 100755 (executable)
@@ -34,8 +34,14 @@ from subunit import (
     DiscardStream,
     ProtocolTestCase,
     TestProtocolClient,
+    read_test_list,
+    )
+from subunit.filters import run_tests_from_stream
+from subunit.test_results import (
+    and_predicates,
+    _make_tag_filter,
+    TestResultFilter,
     )
-from subunit.test_results import TestResultFilter
 
 parser = OptionParser(description=__doc__)
 parser.add_option("--error", action="store_false",
@@ -46,14 +52,26 @@ parser.add_option("--failure", action="store_false",
     help="include failures", default=False, dest="failure")
 parser.add_option("-f", "--no-failure", action="store_true",
     help="exclude failures", dest="failure")
+parser.add_option("--passthrough", action="store_false",
+    help="Show all non subunit input.", default=False, dest="no_passthrough")
 parser.add_option("--no-passthrough", action="store_true",
     help="Hide all non subunit input.", default=False, dest="no_passthrough")
 parser.add_option("-s", "--success", action="store_false",
     help="include successes", dest="success")
-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("--no-skip", action="store_true",
+    help="exclude skips", dest="skip")
+parser.add_option("--xfail", action="store_false",
+    help="include expected falures", default=True, dest="xfail")
+parser.add_option("--no-xfail", action="store_true",
+    help="exclude expected falures", default=True, dest="xfail")
+parser.add_option(
+    "--with-tag", type=str,
+    help="include tests with these tags", action="append", dest="with_tags")
+parser.add_option(
+    "--without-tag", type=str,
+    help="exclude tests with these tags", action="append", dest="without_tags")
 parser.add_option("-m", "--with", type=str,
     help="regexp to include (case-sensitive by default)",
     action="append", dest="with_regexps")
@@ -65,8 +83,17 @@ 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 only_genuine_failures_callback(option, opt, value, parser):
+    parser.rargs.insert(0, '--no-passthrough')
+    parser.rargs.insert(0, '--no-xfail')
+    parser.rargs.insert(0, '--no-skip')
+    parser.rargs.insert(0, '--no-success')
 
+parser.add_option("-F", "--only-genuine-failures", action="callback",
+    callback=only_genuine_failures_callback,
+    help="Only pass through failures and exceptions.")
+
+(options, args) = parser.parse_args()
 
 def _compile_re_from_list(l):
     return re.compile("|".join(l), re.MULTILINE)
@@ -81,7 +108,7 @@ def _make_regexp_filter(with_regexps, without_regexps):
     with_re = with_regexps and _compile_re_from_list(with_regexps)
     without_re = without_regexps and _compile_re_from_list(without_regexps)
 
-    def check_regexps(test, outcome, err, details):
+    def check_regexps(test, outcome, err, details, tags):
         """Check if this test and error match the regexp filters."""
         test_str = str(test) + outcome + str(err) + str(details)
         if with_re and not with_re.search(test_str):
@@ -92,28 +119,34 @@ def _make_regexp_filter(with_regexps, without_regexps):
     return check_regexps
 
 
-def read_test_list(path):
-    f = open(path, 'rb')
-    try:
-        return [l.rstrip("\n") for l in f.readlines()]
-    finally:
-        f.close()
+
+def make_result(output, options, predicate):
+    """Make the result that we'll send the test outcomes to."""
+    fixup_expected_failures = set()
+    for path in options.fixup_expected_failures or ():
+        fixup_expected_failures.update(read_test_list(path))
+    return TestResultFilter(
+        TestProtocolClient(output),
+        filter_error=options.error,
+        filter_failure=options.failure,
+        filter_success=options.success,
+        filter_skip=options.skip,
+        filter_xfail=options.xfail,
+        filter_predicate=predicate,
+        fixup_expected_failures=fixup_expected_failures)
+
 
 regexp_filter = _make_regexp_filter(options.with_regexps,
         options.without_regexps)
-fixup_expected_failures = set()
-for path in options.fixup_expected_failures:
-    fixup_expected_failures.update(read_test_list(path))
-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=regexp_filter,
-    fixup_expected_failures=fixup_expected_failures)
+tag_filter = _make_tag_filter(options.with_tags, options.without_tags)
+
+filter_predicate = and_predicates([regexp_filter, tag_filter])
+
 if options.no_passthrough:
     passthrough_stream = DiscardStream()
 else:
     passthrough_stream = None
-test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream)
-test.run(result)
+
+result = make_result(sys.stdout, options, tag_filter)
+run_tests_from_stream(sys.stdin, result, passthrough_stream)
 sys.exit(0)