aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--meson.build16
-rwxr-xr-xscripts/check_formatting.py58
-rw-r--r--scripts/meson.build4
-rw-r--r--test/meson.build11
4 files changed, 89 insertions, 0 deletions
diff --git a/meson.build b/meson.build
index f1b75cd6..c6da8288 100644
--- a/meson.build
+++ b/meson.build
@@ -194,6 +194,22 @@ endif
# Support #
###########
+ttl_metadata_files = files(
+ 'serd.ttl',
+ 'test/extra/abbreviate/manifest.ttl',
+ 'test/extra/bad/manifest.ttl',
+ 'test/extra/big/manifest.ttl',
+ 'test/extra/good/manifest.ttl',
+ 'test/extra/lax/manifest.ttl',
+ 'test/extra/perfect/manifest.ttl',
+ 'test/extra/prefix/manifest.ttl',
+ 'test/extra/pretty/manifest.ttl',
+ 'test/extra/qualify/manifest.ttl',
+ 'test/extra/root/manifest.ttl',
+)
+
+subdir('scripts')
+
if not get_option('tests').disabled()
subdir('test')
endif
diff --git a/scripts/check_formatting.py b/scripts/check_formatting.py
new file mode 100755
index 00000000..e3342603
--- /dev/null
+++ b/scripts/check_formatting.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+# Copyright 2023 David Robillard <d@drobilla.net>
+# SPDX-License-Identifier: ISC
+
+"""Run a file through a formatter and error if the output differs."""
+
+import argparse
+import difflib
+import os
+import shlex
+import subprocess
+import sys
+
+DEVNULL = subprocess.DEVNULL
+PIPE = subprocess.PIPE
+
+
+def run_formatter(command, good_path):
+ """Run the formatter and compare the output."""
+
+ with subprocess.Popen(
+ command, stderr=DEVNULL, stdout=PIPE, encoding="utf-8"
+ ) as proc:
+ out = list(proc.stdout)
+
+ with open(good_path, "r", encoding="utf-8") as good:
+ good_lines = list(good)
+
+ same = True
+ for line in difflib.unified_diff(good_lines, out, fromfile=good_path):
+ sys.stderr.write(line)
+ same = False
+
+ return same
+
+
+def main():
+ """Run the command line tool."""
+
+ parser = argparse.ArgumentParser(
+ usage="%(prog)s [OPTION]... INPUT TOOL [ARG]...", description=__doc__
+ )
+
+ parser.add_argument("--wrapper", default="", help="executable wrapper")
+ parser.add_argument("input", help="file to check")
+ parser.add_argument("tool", help="formatter executable")
+ parser.add_argument("arg", nargs=argparse.REMAINDER, help="tool argument")
+
+ args = parser.parse_args(sys.argv[1:])
+ path = os.path.normpath(args.input)
+ command = shlex.split(args.wrapper) + [args.tool] + args.arg + [path]
+
+ return 0 if run_formatter(command, args.input) else 1
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/scripts/meson.build b/scripts/meson.build
new file mode 100644
index 00000000..7ec4087d
--- /dev/null
+++ b/scripts/meson.build
@@ -0,0 +1,4 @@
+# Copyright 2023 David Robillard <d@drobilla.net>
+# SPDX-License-Identifier: 0BSD OR ISC
+
+check_formatting_py = files('check_formatting.py')
diff --git a/test/meson.build b/test/meson.build
index dcfc627a..c55f2c5d 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -13,6 +13,7 @@ plot_scripts = files(
)
simple_scripts = files(
+ '../scripts/check_formatting.py',
'run_suite.py',
'serd_test_util/__init__.py',
'test_quiet.py',
@@ -76,6 +77,16 @@ if get_option('lint')
pylint_args = ['--disable', 'bad-option-value']
test('pylint', pylint, args: pylint_args + pylint_scripts, suite: 'scripts')
endif
+
+ # Check Turtle formatting with serdi
+ foreach ttl_file : ttl_metadata_files
+ test(
+ '@0@'.format(ttl_file).underscorify(),
+ check_formatting_py,
+ args: [ttl_file, serdi, '-o', 'turtle'],
+ suite: 'data',
+ )
+ endforeach
endif
###################