Files
cnmcdee a2106045a6 json: --json output and libsfile linkable library
- sfile --json: one JSON object per file (JSON Lines) carrying format,
  offsets, decoded fields and geotags when present; default CLI
  output byte-identical (verified against the pre-refactor binary).
- libsfile: src/sfile.h + src/sfile_lib.c expose the decoder
  (sfile_decode_file/mem, sfile_result_to_json, format table
  introspection); make lib builds libsfile.a and libsfile.so.1.1.0,
  make install installs them plus the header.
- tests/test_json.py: JSON parses and matches the text table on the
  same files (make test-json); tests/test_library.c + test_lib.sh:
  static and shared link test (make test-lib).
2026-12-02 09:28:39 +00:00

110 lines
4.8 KiB
Bash
Executable File

#!/bin/sh
# test_lib.sh — build + run the libsfile link test (static and shared).
#
# Compiles tests/test_library.c against libsfile.a and against
# libsfile.so, then checks both against the `sfile` CLI on the same
# fixtures: the field lines must match the CLI's header table exactly,
# the geotag must come through, and the JSON the library emits must
# parse. Usage: ./tests/test_lib.sh (run from the repo root)
set -e
cd "$(dirname "$0")/.."
TMP=$(mktemp -d /tmp/sfile-lib-test-XXXXXX)
trap 'rm -rf "$TMP"' EXIT INT TERM
python3 tests/make_fixtures.py "$TMP" > /dev/null
fail=0
note() { # note <ok|FAIL> <message>
if [ "$1" = ok ]; then echo " PASS $2"; else echo " FAIL $2"; fail=1; fi
}
# CLI field lines in the same "field: name=value" form the C program prints.
cli_fields() {
./sfile "$1" | grep -E '^ [0-9]+ ' \
| sed -E 's/^ +[0-9]+ +[0-9]+ +([^ ]+) +/\1=/' \
| sed 's/[[:space:]]*$//; s/^/field: /'
}
for variant in static shared; do
echo "link variant: $variant"
if [ "$variant" = static ]; then
gcc -O2 -Wall -Wextra -std=c11 -Isrc -o "$TMP/t_$variant" \
tests/test_library.c libsfile.a
else
gcc -O2 -Wall -Wextra -std=c11 -Isrc -o "$TMP/t_$variant" \
tests/test_library.c -L. -lsfile -Wl,-rpath,"$PWD"
fi
for fx in sample.png track.sps; do
if "$TMP/t_$variant" "$TMP/$fx" > "$TMP/out.txt" 2>&1; then
note ok "$variant $fx: exit 0"
else
note FAIL "$variant $fx: exit $?"
fi
# fields printed by the library vs fields printed by the CLI
grep '^field: ' "$TMP/out.txt" | sed 's/[[:space:]]*$//' > "$TMP/lib_fields.txt"
cli_fields "$TMP/$fx" > "$TMP/cli_fields.txt"
if diff -q "$TMP/lib_fields.txt" "$TMP/cli_fields.txt" > /dev/null; then
note ok "$variant $fx: library fields == CLI fields"
else
note FAIL "$variant $fx: library fields != CLI fields"
diff "$TMP/lib_fields.txt" "$TMP/cli_fields.txt" | head -10
fi
# the library's own JSON line must parse
grep '^json: ' "$TMP/out.txt" | sed 's/^json: //' > "$TMP/lib.json"
if python3 -c "import json,sys; json.load(open('$TMP/lib.json'))"; then
note ok "$variant $fx: library JSON parses"
else
note FAIL "$variant $fx: library JSON does not parse"
fi
if [ "$fx" = sample.png ]; then
# PNG has a geotagging mechanism but this fixture has none.
if python3 -c "import json,sys; d=json.load(open('$TMP/lib.json')); sys.exit(0 if d.get('geotags')==[] else 1)"; then
note ok "$variant $fx: library JSON geotags empty (none found)"
else
note FAIL "$variant $fx: library JSON geotags should be []"
fi
fi
if [ "$fx" = track.sps ]; then
# SPS header lat/lon fields are not a geotag: no mechanism.
if python3 -c "import json,sys; d=json.load(open('$TMP/lib.json')); sys.exit(0 if 'geotags' not in d else 1)"; then
note ok "$variant $fx: library JSON has no geotags key (no mechanism)"
else
note FAIL "$variant $fx: library JSON should have no geotags key"
fi
fi
done
# --mem path decodes from a buffer, not from disk
if "$TMP/t_$variant" --mem "$TMP/sample.png" > "$TMP/mem.txt" 2>&1 \
&& grep -q '^field: width=800$' "$TMP/mem.txt"; then
note ok "$variant --mem: decodes PNG from memory (width=800)"
else
note FAIL "$variant --mem: PNG decode from memory"
fi
done
# geotags come through the library (EXIF GPS JPEG: one result).
# Note: test_library decodes from disk here, so sfile_decode_file()
# runs geotag_scan(); --mem mode never scans (no path to scan).
if "$TMP/t_static" tests/geotag/test-gps.jpg | grep -q '^geotag: 37.774900 -122.419400 (EXIF GPS IFD (JPEG APP1 segment))$'; then
note ok "library geotag: 37.774900 -122.419400 (EXIF GPS)"
else
note FAIL "library geotag missing or wrong"
fi
if "$TMP/t_static" tests/geotag/test-gps.jpg > "$TMP/jpg.txt" 2>&1; then
grep '^json: ' "$TMP/jpg.txt" | sed 's/^json: //' > "$TMP/jpg.json"
if python3 -c "import json,sys,math; d=json.load(open('$TMP/jpg.json')); g=d['geotags'][0]; sys.exit(0 if len(d['geotags'])==1 and math.isclose(g['latitude'],37.7749,abs_tol=1e-6) and math.isclose(g['longitude'],-122.4194,abs_tol=1e-6) and g['source']=='EXIF GPS IFD (JPEG APP1 segment)' and g.get('altitude_m')==10.0 and g.get('datum')=='WGS-84' and g.get('datetime_utc')=='2026-12-01 12:34:56' else 1)"; then
note ok "library JSON geotags: EXIF GPS, alt 10.0, WGS-84, 2026-12-01 12:34:56"
else
note FAIL "library JSON geotags wrong"
fi
fi
if [ "$fail" = 0 ]; then
echo "test_lib: all checks passed"
exit 0
else
echo "test_lib: FAILED"
exit 1
fi