From b404312686874e539b617d1f27ccbaa5a82936af Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 21 Oct 2021 15:38:10 -0400 Subject: Replace serdi with more fine-grained tools Especially with the new functionality, the complexity of the command-line interface alone was really becoming unmanageable. The serdi implementation also had the highest cyclomatic complexity of the entire codebase by a huge margin. So, take a page from the Unix philosophy and split serdi into several more finely-honed tools that can be freely composed. Though there is still unfortunately quite a bit of option overlap between them due to the common details of reading RDF, I think the resulting tools are a lot easier to understand, both from a user and a developer perspective. --- test/test_sort.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100755 test/test_sort.py (limited to 'test/test_sort.py') diff --git a/test/test_sort.py b/test/test_sort.py new file mode 100755 index 00000000..4080b93c --- /dev/null +++ b/test/test_sort.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +"""Run the collation tests for serd-sort.""" + +import argparse +import os +import random +import shlex +import subprocess +import sys +import tempfile + +import serd_test_util + +collations = [ + "GOPS", + "GOSP", + "GPSO", + "GSOP", + "GSPO", + "OPS", + "OSP", + "POS", + "PSO", + "SOP", + "SPO", + "pretty", +] + + +def check(test_dir, command_prefix, out_dir, input_path, name): + """Sort a single input in the named order and check the output. + + The expected output is assumed to exist at test_dir/NAME.nq. + """ + + output_path = os.path.join(out_dir, name + ".nq") + result_path = os.path.join(test_dir, name + ".nq") + options = [] if name == "pretty" else ["-c", name] + + # Randomly add irrelevant options just to cover them + if random.choice([True, False]): + options += ["-R", "http://example.org/"] + if random.choice([True, False]): + options += ["-I", "TriG"] + + command = command_prefix + options + ["-o", output_path, input_path] + + proc = subprocess.run(command, capture_output=True, check=False) + if proc.returncode != 0: + cmd_string = " ".join(shlex.quote(c) for c in command) + serd_test_util.error("Unexpected failure: {}".format(cmd_string)) + sys.stderr.write(proc.stderr.decode("utf-8")) + return False + + if not serd_test_util.file_equals(result_path, output_path): + serd_test_util.error( + "Output {} differs from {}\n".format(output_path, result_path) + ) + return False + + return True + + +def run_tests(test_dir, command_prefix, out_dir): + """Run all the tests in the suite.""" + + input_trig = os.path.join(test_dir, "input.trig") + + n_failures = 0 + for name in collations: + if not check(test_dir, command_prefix, out_dir, input_trig, name): + n_failures += 1 + + return n_failures + + +def main(): + """Run the command line tool.""" + + parser = argparse.ArgumentParser( + usage="%(prog)s [OPTION]... INPUT", + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + + parser.add_argument( + "--tool", default="tools/serd-sort", help="serd-sort executable" + ) + + parser.add_argument("--wrapper", default="", help="executable wrapper") + parser.add_argument( + "input", help="path to input.trig in the test directory" + ) + + args = parser.parse_args(sys.argv[1:]) + wrapper_prefix = shlex.split(args.wrapper) + command_prefix = wrapper_prefix + [args.tool] + + with tempfile.TemporaryDirectory() as out_dir: + return run_tests(os.path.dirname(args.input), command_prefix, out_dir) + + +if __name__ == "__main__": + try: + sys.exit(main()) + except subprocess.CalledProcessError as error: + if error.stderr is not None: + sys.stderr.write(error.stderr.decode("utf-8")) + + sys.stderr.write("error: %s\n" % error) + sys.exit(error.returncode) -- cgit v1.2.1