Skip to content

Commit 75e17fd

Browse files
committed
Update verify tests to use python
1 parent 04657ce commit 75e17fd

File tree

3 files changed

+87
-55
lines changed

3 files changed

+87
-55
lines changed

Makefile

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -125,52 +125,6 @@ $(FASTOBO): | build
125125
fi ; \
126126
fi
127127

128-
# ----------------------------------------
129-
# FILE UTILITIES
130-
# ----------------------------------------
131-
132-
# cleans csv files from a directory, optionally matching pattern(s)
133-
# --> to prevent existing file inclusion in concat_csv
134-
# args = input-directory, pattern(s)-to-match-files (should end with .csv)
135-
define clean_existing_csv
136-
@PATTERN=($(2)) ; \
137-
if [ "$$PATTERN" ]; then \
138-
TMP_FILES=$$(find $(1) -name "$(firstword $(2))" $(patsubst %,-o -name "%",$(wordlist 2,$(words $(2)),$(2)))) ; \
139-
else \
140-
TMP_FILES=$$(find $(1) -name "*.csv") ; \
141-
fi ; \
142-
if [ "$$TMP_FILES" ]; then \
143-
rm -f $$TMP_FILES ; \
144-
fi
145-
endef
146-
147-
# concatenate multiple CSV files into one
148-
# args = file category ('TEST' to error, if output), output-file, input-directory, pattern(s)-to-match-files (should end with .csv)
149-
define concat_csv
150-
@PATTERN=($(4)) ; \
151-
if [ "$$PATTERN" ]; then \
152-
TMP_FILES=$$(find $(3) -name "$(firstword $(4))" $(patsubst %,-o -name "%",$(wordlist 2,$(words $(4)),$(4)))) ; \
153-
else \
154-
TMP_FILES=$$(find $(3) -name "*.csv") ; \
155-
fi ; \
156-
if [ "$$TMP_FILES" ]; then \
157-
awk 'BEGIN { OFS = FS = "," } ; { \
158-
if (FNR == 1) { \
159-
gsub(/^.*\/|\.csv/, "", FILENAME) ; \
160-
if (NR != 1) { print "" } ; \
161-
print "$(1): " FILENAME ; print $$0 \
162-
} \
163-
else { print $$0 } \
164-
}' $$TMP_FILES > $(2) \
165-
&& rm -f $$TMP_FILES ; \
166-
if [ "$(1)" = "TEST" ] ; then \
167-
exit 1 ; \
168-
fi ; \
169-
elif [ "$(1)" = "TEST" ]; then \
170-
echo "" > $(2) ; \
171-
fi
172-
endef
173-
174128

175129
##########################################
176130
## CI TESTS & DIFF
@@ -218,34 +172,42 @@ build/doid-edit-reasoned.owl: $(EDIT) | check_robot build
218172
--output $@
219173
@echo -e "\n## Reasoning completed successfully!"
220174

221-
# Verify doid-edit.owl
175+
# Verify *-edit.owl
222176
EDIT_V_QUERIES := $(wildcard src/sparql/verify/edit-verify-*.rq src/sparql/verify/verify-*.rq)
177+
EDIT_V_RES := $(patsubst src/sparql/verify/%.rq,build/reports/temp/%.csv,$(EDIT_V_QUERIES))
223178

224179
.PRECIOUS: build/reports/edit-verify.csv
225180
verify-edit: build/reports/edit-verify.csv
226181
build/reports/edit-verify.csv: $(EDIT) | check_robot build/reports/temp
227-
$(call clean_existing_csv,$(word 2,$|),edit-verify-*.csv verify-*.csv)
182+
@rm -f $(EDIT_V_RES)
228183
@$(ROBOT) verify \
229184
--input $< \
230185
--queries $(EDIT_V_QUERIES) \
231186
--fail-on-violation false \
232187
--output-dir $(word 2,$|)
233-
$(call concat_csv,TEST,$@,$(word 2,$|),edit-verify-*.csv verify-*.csv)
188+
@python3 src/util/concat_csv.py \
189+
--input $(EDIT_V_RES) \
190+
--category TEST \
191+
--output $@
234192

235-
# Verify of doid-edit.owl that should be run quarterly (not part of release)
193+
# Verify of *-edit.owl that should be run quarterly (not part of release)
236194
QUARTER_V_QUERIES := $(wildcard src/sparql/verify/quarter-verify-*.rq)
195+
QUARTER_V_RES := $(patsubst src/sparql/verify/%.rq,build/reports/temp/%.csv,$(QUARTER_V_QUERIES))
237196

238197
.PRECIOUS: build/reports/quarterly_test.csv
239198
quarterly_test: build/reports/quarterly_test.csv
240199
build/reports/quarterly_test.csv: $(EDIT) | check_robot build/reports/temp
241200
@echo "Verifying $<..."
242-
$(call clean_existing_csv,$(word 2,$|),quarter-verify-*.csv)
201+
@rm -f $(QUARTER_V_RES)
243202
@$(ROBOT) verify \
244203
--input $< \
245204
--queries $(QUARTER_V_QUERIES) \
246205
--fail-on-violation false \
247206
--output-dir $(word 2,$|)
248-
$(call concat_csv,TEST,$@,$(word 2,$|),quarter-verify-*.csv)
207+
@python3 src/util/concat_csv.py \
208+
--input $(QUARTER_V_RES) \
209+
--category TEST \
210+
--output $@
249211

250212
# ----------------------------------------
251213
# DIFF

src/util/concat_csv.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
"""
3+
concat_csv.py
4+
-------------
5+
Concatenates multiple CSV files into a single output CSV file.
6+
7+
- Accepts a list of file paths as input; files that do not exist are silently skipped.
8+
- Prepends each input file's content with a header including the category and file name.
9+
- Removes the input files after concatenation.
10+
- Always creates an output file, even if no input files are found.
11+
- If the category is 'TEST', exits with error code 1 IF any input files are processed.
12+
13+
Usage:
14+
python3 concat_csv.py --input [file1 file2 ...] --category <category> --output <output_file>
15+
16+
Example:
17+
python3 concat_csv.py --input file1.csv file2.csv --category TEST --output output.csv
18+
"""
19+
import sys
20+
import os
21+
import csv
22+
import argparse
23+
24+
25+
def concat_csv(category, output_file, input_files):
26+
files = sorted(f for f in input_files if os.path.isfile(f))
27+
if not files:
28+
# No files exist - create empty output and exit successfully
29+
open(output_file, "w").close()
30+
return
31+
# Files found - concatenate into output
32+
with open(output_file, "w", newline="") as out_f:
33+
writer = csv.writer(out_f)
34+
for idx, file in enumerate(files):
35+
with open(file, newline="") as in_f:
36+
reader = csv.reader(in_f)
37+
out_f.write(
38+
f"{category}: {os.path.splitext(os.path.basename(file))[0]}\n"
39+
)
40+
for row in reader:
41+
writer.writerow(row)
42+
# write single blank line between sections, but not after the last file
43+
if idx != len(files) - 1:
44+
out_f.write("\n")
45+
# Remove input files after concatenation
46+
for file in files:
47+
try:
48+
os.remove(file)
49+
except Exception as e:
50+
print(f"Warning: could not remove {file}: {e}", file=sys.stderr)
51+
if category == "TEST":
52+
sys.exit(1)
53+
54+
55+
if __name__ == "__main__":
56+
parser = argparse.ArgumentParser(
57+
description="Concatenate CSV files into a single output file."
58+
)
59+
parser.add_argument(
60+
"--category", required=True, help="Category label for each section header"
61+
)
62+
parser.add_argument("--output", required=True, help="Output CSV file path")
63+
parser.add_argument(
64+
"--input",
65+
nargs="+",
66+
metavar="file",
67+
help="Input CSV file paths (non-existent files are silently skipped)",
68+
)
69+
args = parser.parse_args()
70+
concat_csv(args.category, args.output, args.input)

src/util/diff-re.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ def compare_re(old_data, new_data, delim1="\n", delim2="|"):
139139

140140
result = []
141141
if same:
142-
result.append(f"[same]: {'|'.join(map(str, same))}")
142+
result.append(f"[same]: {'|'.join(map(str, sorted(same)))}")
143143
if old:
144-
result.append(f"[old]: {'|'.join(map(str, old))}")
144+
result.append(f"[old]: {'|'.join(map(str, sorted(old)))}")
145145
if new:
146-
result.append(f"[new]: {'|'.join(map(str, new))}")
146+
result.append(f"[new]: {'|'.join(map(str, sorted(new)))}")
147147
entry[col] = delim1.join(result)
148148

149149
# Add ONLY if entry has changes

0 commit comments

Comments
 (0)