]> Lady’s Gitweb - HOMEDIR/blob - sh/testio.sh
Add recordjar canonicalization script
[HOMEDIR] / sh / testio.sh
1 #!/usr/bin/env sh
2 # @(#)🏠📂 HOMEDIR sh/testio.sh 2026-04-05T19:41:30Z
3 # SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
4 # SPDX-License-Identifier: MPL-2.0
5
6 ## ⁌ An input~output transformation test 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 testio.sh [testname...]
18 ##
19 ## If no test names are provided, all of the tests are run.
20
21 set -o 'errexit'
22
23 ## § Configuration
24 ##
25 ## All of the commands used by this script are overridable with your
26 ## own implementations by setting the corresponding `cmd_COMMAND´
27 ## variable.
28
29 : "${cmd_CMP:=cmp}"
30 : "${cmd_BASENAME:=basename}"
31 : "${cmd_PRINTF:=printf}"
32 : "${cmd_PWD:=pwd}"
33 : "${cmd_REALPATH:=realpath}"
34 : "${cmd_TEST:=test}"
35 : "${cmd_TR:=tr}"
36 : "${cmd_WC:=wc}"
37
38 ## By default, tests are assumed to be located in the `TESTING´
39 ## subdirectory of the current directory.
40 ## This can be overridden by setting the `path_TESTDIR´ environment
41 ## variable.
42
43 realpwd="$(
44 "${cmd_PWD}" -P
45 )"
46 defaulttestdir="${realpwd}"'/TESTING'
47 : "${path_TESTDIR:=${defaulttestdir}}"
48
49 ## The default transformation command is assumed to be located in
50 ## `bin/${programname}´, where `${programname}´ is determined by
51 ## Ascii‐lowercasing the current directory name.
52 ## This can be overridden by setting the `cmd_TRANSFORM´ environment
53 ## variable.
54
55 programname="$(
56 "${cmd_BASENAME}" -- "${realpwd}" |
57 "${cmd_TR}" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
58 )"
59 defaulttransform="${realpwd}"'/bin/'"${programname}"
60 : "${cmd_TRANSFORM:=${defaulttransform}}"
61
62 ## Input files are read from `📥´ and expected output is read from
63 ## `📤´.
64 ## These locations (relative to `${path_TESTDIR}´) can be customized by
65 ## setting the `name_INPUT´ and `name_OUTPUT´ variables,
66 ## respectively.
67 ## If an input file exists but there is no corresponding output, the
68 ## transform is expected to error.
69
70 : "${name_INPUT:=📥}"
71 : "${name_OUTPUT:=📤}"
72
73 ## If `${do_OVERWRITE}´ is not empty, then the test runner will
74 ## overwrite the outputs of the selected tests instead of testing
75 ## them.
76
77 : "${do_OVERWRITE:=}"
78
79 ## § Implementation
80
81 ## The `status´ variable records the number of failed tests, which is
82 ## also the exit status.
83
84 status=0
85
86 ## The `ellipsis´ function simply renders spaced periods until
87 ## `${charstowrite}´ hits 0.
88 ## Ideally these would be one‐dot leaders, but fonts disagree as to the
89 ## appropriate width of that character.
90
91 ellipsis () {
92 if "${cmd_TEST}" $((${charstowrite} % 2)) -ne 0
93 then :
94 "${cmd_PRINTF}" '%s' ' ' >&2
95 fi
96 while "${cmd_TEST}" ${charstowrite} -gt 1
97 do :
98 "${cmd_PRINTF}" '%s' '. ' >&2
99 charstowrite=$((${charstowrite} - 2))
100 done
101 }
102
103 ## The `testrun´ function runs the test of name `${testname}´.
104
105 testrun () {
106 "${cmd_PRINTF}" '%s' "${testname}"' ' >&2
107 charstowrite=$((42 - ${#testname})) ellipsis
108 infile="${path_TESTDIR}"'/'"${name_INPUT}"'/'"${testname}"
109 outfile="${path_TESTDIR}"'/'"${name_OUTPUT}"'/'"${testname}"
110 if ! "${cmd_TEST}" -f "${infile}"
111 then :
112 status=$((${status} + 1))
113 "${cmd_PRINTF}" '%s\n' 'MISSING' >&2
114 elif "${cmd_TEST}" -n "${do_OVERWRITE}"
115 then :
116 if "${cmd_TRANSFORM}" <"${infile}" >|"${outfile}" 2>/dev/null
117 then :
118 "${cmd_PRINTF}" '%s\n' 'WRITTEN' >&2
119 else :
120 "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
121 fi
122 elif ! "${cmd_TEST}" -f "${outfile}"
123 then :
124 if "${cmd_TRANSFORM}" <"${infile}" >/dev/null 2>/dev/null
125 then :
126 status=$((${status} + 1))
127 "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
128 else :
129 "${cmd_PRINTF}" '%s\n' 'SUCCESS' >&2
130 fi
131 else :
132 "${cmd_TRANSFORM}" <"${infile}" 2>/dev/null |
133 if "${cmd_CMP}" -s - "${outfile}" 2>/dev/null
134 then :
135 "${cmd_PRINTF}" '%s\n' 'SUCCESS' >&2
136 else :
137 status=$((${status} + 1))
138 "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
139 fi
140 fi
141 }
142
143 ## If there is no argument, all tests are run; otherwise, the specified
144 ## tests are run.
145
146 if "${cmd_TEST}" $# -eq 0
147 then :
148 for test in "${path_TESTDIR}"'/'"${name_INPUT}"'/'*
149 do :
150 testname="$(
151 "${cmd_BASENAME}" -- "${test}"
152 )" testrun
153 done
154 else :
155 for name
156 do :
157 testname="${name}" testrun
158 done
159 fi
160
161 ## Finally, the number of errors is returned as the exit status.
162
163 exit ${status}
This page took 0.482424 seconds and 5 git commands to generate.