]> Lady’s Gitweb - HOMEDIR/blob - sh/diffio.sh
Add recordjar canonicalization script
[HOMEDIR] / sh / diffio.sh
1 #!/usr/bin/env sh
2 # @(#)🏠📂 HOMEDIR sh/diffio.sh 2026-04-05T19:29:49Z
3 # SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
4 # SPDX-License-Identifier: MPL-2.0
5
6 ## ⁌ An input~output transformation diff script
7 ##
8 ## ∎ Copyright © 2026 Lady [@ Ladys Computer].
9 ##
10 ## ⋮ This Source Code Form is subject to the terms of the Mozilla
11 ## Public License, version 2.0.
12 ## If a copy of the M·P·L was not distributed with this file, You can
13 ## obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
14 ##
15 ## Usage :⁠—
16 ##
17 ## »|sh$sh diffio.sh testname
18 ##
19 ## This script diffs the result of transforming the provided test with
20 ## its expected output.
21 ##
22 ## It is an error if no test name is provided.
23
24 set -o 'errexit'
25
26 ## § Configuration
27 ##
28 ## All of the commands used by this script are overridable with your
29 ## own implementations by setting the corresponding `cmd_COMMAND´
30 ## variable.
31
32 : "${cmd_CMP:=cmp}"
33 : "${cmd_BASENAME:=basename}"
34 : "${cmd_DIFF:=diff}"
35 : "${cmd_PRINTF:=printf}"
36 : "${cmd_PWD:=pwd}"
37 : "${cmd_REALPATH:=realpath}"
38 : "${cmd_SED:=sed}"
39 : "${cmd_TEST:=test}"
40 : "${cmd_TR:=tr}"
41
42 ## By default, tests are assumed to be located in the `TESTING´
43 ## subdirectory of the current directory.
44 ## This can be overridden by setting the `path_TESTDIR´ environment
45 ## variable.
46
47 realpwd="$(
48 "${cmd_PWD}" -P
49 )"
50 defaulttestdir="${realpwd}"'/TESTING'
51 : "${path_TESTDIR:=${defaulttestdir}}"
52
53 ## The default transformation command is assumed to be located in
54 ## `bin/${programname}´, where `${programname}´ is determined by
55 ## Ascii‐lowercasing the current directory name.
56 ## This can be overridden by setting the `cmd_TRANSFORM´ environment
57 ## variable.
58
59 programname="$(
60 "${cmd_BASENAME}" -- "${realpwd}" |
61 "${cmd_TR}" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
62 )"
63 defaulttransform="${realpwd}"'/bin/'"${programname}"
64 : "${cmd_TRANSFORM:=${defaulttransform}}"
65
66 ## Input files are read from `📥´ and expected output is read from
67 ## `📤´.
68 ## These locations (relative to `${path_TESTDIR}´) can be customized by
69 ## setting the `name_INPUT´ and `name_OUTPUT´ variables,
70 ## respectively.
71 ## If an input file exists but there is no corresponding output, the
72 ## transform is expected to error.
73
74 : "${name_INPUT:=📥}"
75 : "${name_OUTPUT:=📤}"
76
77 ## § Implementation
78
79 inputfile="${path_TESTDIR}"'/'"${name_INPUT}"'/'"${1}"
80 outputfile="${path_TESTDIR}"'/'"${name_OUTPUT}"'/'"${1}"
81
82 ## If no test name was provided, or the input file does not exist, it
83 ## is an error.
84
85 if "${cmd_TEST}" -z "${1}"
86 then :
87 "${cmd_PRINTF}" '%s\n' "diffio: No name specified."
88 exit 8
89 elif ! "${cmd_TEST}" -f "${inputfile}"
90 then :
91 "${cmd_PRINTF}" '%s\n' "diffio: Input file not found."
92 exit 9
93 fi
94
95 ## If there is no output file, it is treated as empty.
96
97 if ! "${cmd_TEST}" -f "${outputfile}"
98 then :
99 outputfile=/dev/null
100 fi
101
102 ## The transformed output is diffed with the result.
103
104 "${cmd_TRANSFORM}" <"${inputfile}" |
105 "${cmd_DIFF}" -U 1 "${outputfile}" - |
106 "${cmd_SED}" '1,2d'
This page took 0.484926 seconds and 5 git commands to generate.