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/serd_test_util/__init__.py | 71 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'test/serd_test_util/__init__.py') diff --git a/test/serd_test_util/__init__.py b/test/serd_test_util/__init__.py index f0b1c19a..45cc6e64 100644 --- a/test/serd_test_util/__init__.py +++ b/test/serd_test_util/__init__.py @@ -3,8 +3,48 @@ """Utilities for data-driven tests.""" import datetime +import difflib +import os import re import subprocess +import sys + + +def error(message): + """Log an error message to stderr""" + + sys.stderr.write("error: ") + sys.stderr.write(message) + + +def test_input_syntax(test_class): + """Return the output syntax use for a given test class.""" + + if "NTriples" in test_class: + return "NTriples" + + if "Turtle" in test_class: + return "Turtle" + + if "NQuads" in test_class: + return "NQuads" + + if "Trig" in test_class: + return "Trig" + + raise Exception("Unknown test class <{}>".format(test_class)) + + +def test_output_syntax(test_class): + """Return the output syntax use for a given test class.""" + + if "NTriples" in test_class or "Turtle" in test_class: + return "NTriples" + + if "NQuads" in test_class or "Trig" in test_class: + return "NQuads" + + raise Exception("Unknown test class <{}>".format(test_class)) def earl_assertion(test, passed, asserter): @@ -33,7 +73,7 @@ def earl_assertion(test, passed, asserter): def load_rdf(command_prefix, filename): - """Load an RDF file as dictionaries via serdi (only supports URIs).""" + """Load an RDF file as dictionaries via serd-pipe (only supports URIs).""" rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" model = {} @@ -61,3 +101,32 @@ def load_rdf(command_prefix, filename): instances[o].update([s]) return model, instances + + +def file_equals(patha, pathb): + """Return true if the file at patha is the same as the file at pathb.""" + + for path in (patha, pathb): + if not os.access(path, os.F_OK): + error("missing file {}\n".format(path)) + return False + + with open(patha, "r", encoding="utf-8") as fa: + with open(pathb, "r", encoding="utf-8") as fb: + return show_diff(fa.readlines(), fb.readlines(), patha, pathb) + + +def show_diff(from_lines, to_lines, from_filename, to_filename): + """Print a diff between files to stderr.""" + + same = True + for line in difflib.unified_diff( + from_lines, + to_lines, + fromfile=os.path.abspath(from_filename), + tofile=os.path.abspath(to_filename), + ): + sys.stderr.write(line) + same = False + + return same -- cgit v1.2.1