]> Lady’s Gitweb - HOMEDIR/commitdiff
Add diffio and testio 0.1.1
authorLady <redacted>
Mon, 23 Mar 2026 02:16:53 +0000 (22:16 -0400)
committerLady <redacted>
Sun, 5 Apr 2026 20:02:58 +0000 (16:02 -0400)
These commands are designed to make testing transformation programs
  easy.

Message-type: text/lesml

bin/diffio [new symlink]
bin/testio [new symlink]
sh/diffio.sh [new file with mode: 0755]
sh/testio.sh [new file with mode: 0755]

diff --git a/bin/diffio b/bin/diffio
new file mode 120000 (symlink)
index 0000000..833b7d3
--- /dev/null
@@ -0,0 +1 @@
+../sh/diffio.sh
\ No newline at end of file
diff --git a/bin/testio b/bin/testio
new file mode 120000 (symlink)
index 0000000..0de08c1
--- /dev/null
@@ -0,0 +1 @@
+../sh/testio.sh
\ No newline at end of file
diff --git a/sh/diffio.sh b/sh/diffio.sh
new file mode 100755 (executable)
index 0000000..799821c
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env sh
+# @(#)🏠📂 HOMEDIR       sh/diffio.sh    2026-03-23T02:16:54Z
+# SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
+# SPDX-License-Identifier: MPL-2.0
+
+## ⁌ An input~output transformation diff script
+##
+## ∎ Copyright © 2026 Lady [@ Ladys Computer].
+##
+## ⋮ This Source Code Form is subject to the terms of the Mozilla
+##   Public License, version 2.0.
+## If a copy of the M·P·L was not distributed with this file, You can
+##   obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
+##
+## Usage :⁠—
+##
+## »|sh$sh diffio.sh testname
+##
+## This script diffs the result of transforming the provided test with
+##   its expected output.
+##
+## It is an error if no test name is provided.
+
+set -o 'errexit'
+
+## § Configuration
+##
+## All of the commands used by this script are overridable with your
+##   own implementations by setting the corresponding `cmd_COMMAND´
+##   variable.
+
+: "${cmd_CMP:=cmp}"
+: "${cmd_BASENAME:=basename}"
+: "${cmd_DIFF:=diff}"
+: "${cmd_PRINTF:=printf}"
+: "${cmd_PWD:=pwd}"
+: "${cmd_REALPATH:=realpath}"
+: "${cmd_SED:=sed}"
+: "${cmd_TEST:=test}"
+: "${cmd_TR:=tr}"
+
+## By default, tests are assumed to be located in the `TESTING´
+##   subdirectory of the current directory.
+## This can be overridden by setting the `path_TESTDIR´ environment
+##   variable.
+
+realpwd="$(
+  "${cmd_PWD}" -P
+)"
+defaulttestdir="${realpwd}"'/TESTING'
+: "${path_TESTDIR:=${defaulttestdir}}"
+
+## The default transformation command is assumed to be located in
+##   `bin/${programname}´, where `${programname}´ is determined by
+##   Ascii‐lowercasing the current directory name.
+## This can be overridden by setting the `cmd_TRANSFORM´ environment
+##   variable.
+
+programname="$(
+  "${cmd_BASENAME}" -- "${realpwd}" |
+  "${cmd_TR}" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
+)"
+defaulttransform="${realpwd}"'/bin/'"${programname}"
+: "${cmd_TRANSFORM:=${defaulttransform}}"
+
+## Input files are read from `📥´ and expected output is read from
+##   `📤´.
+## These locations (relative to `${path_TESTDIR}´) can be customized by
+##   setting the `name_INPUT´ and `name_OUTPUT´ variables,
+##   respectively.
+## If an input file exists but there is no corresponding output, the
+##   transform is expected to error.
+
+: "${name_INPUT:=📥}"
+: "${name_OUTPUT:=📤}"
+
+## § Implementation
+
+inputfile="${path_TESTDIR}"'/'"${name_INPUT}"'/'"${1}"
+outputfile="${path_TESTDIR}"'/'"${name_OUTPUT}"'/'"${1}"
+
+## If no test name was provided, or the input file does not exist, it
+##   is an error.
+
+if "${cmd_TEST}" -z "${1}"
+then :
+  "${cmd_PRINTF}" '%s\n' "diffio: No name specified."
+  exit 8
+elif ! "${cmd_TEST}" -f "${inputfile}"
+then :
+  "${cmd_PRINTF}" '%s\n' "diffio: Input file not found."
+  exit 9
+fi
+
+## If there is no output file, then the transform is expected to fail.
+## Otherwise, the transformed output is diffed with the result.
+
+if ! "${cmd_TEST}" -f "${inputfile}"
+then :
+  if "${cmd_TRANSFORM}" <"${infile}" >/dev/null 2>/dev/null
+  then :
+    "${cmd_PRINTF}" '%s\n' \
+      'diffio: Expected transform failure, but it succeeded.' \
+      >&2
+    exit 10
+  else :
+    "${cmd_PRINTF}" '%s\n' \
+      'diffio: Transform did not succeed, as expected.' \
+      >&2
+  fi
+else :
+  "${cmd_TRANSFORM}" <"${inputfile}" 2>/dev/null |
+  "${cmd_DIFF}" -U 1 - "${outputfile}" |
+  "${cmd_SED}" '1,2d'
+fi
diff --git a/sh/testio.sh b/sh/testio.sh
new file mode 100755 (executable)
index 0000000..6c45d8f
--- /dev/null
@@ -0,0 +1,163 @@
+#!/usr/bin/env sh
+# @(#)🏠📂 HOMEDIR       sh/testio.sh    2026-04-05T19:41:30Z
+# SPDX-FileCopyrightText: 2026 Lady <https://www.ladys.computer/about/#lady>
+# SPDX-License-Identifier: MPL-2.0
+
+## ⁌ An input~output transformation test script
+##
+## ∎ Copyright © 2026 Lady [@ Ladys Computer].
+##
+## ⋮ This Source Code Form is subject to the terms of the Mozilla
+##   Public License, version 2.0.
+## If a copy of the M·P·L was not distributed with this file, You can
+##   obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
+##
+## Usage :⁠—
+##
+## »|sh$sh testio.sh [testname...]
+##
+## If no test names are provided, all of the tests are run.
+
+set -o 'errexit'
+
+## § Configuration
+##
+## All of the commands used by this script are overridable with your
+##   own implementations by setting the corresponding `cmd_COMMAND´
+##   variable.
+
+: "${cmd_CMP:=cmp}"
+: "${cmd_BASENAME:=basename}"
+: "${cmd_PRINTF:=printf}"
+: "${cmd_PWD:=pwd}"
+: "${cmd_REALPATH:=realpath}"
+: "${cmd_TEST:=test}"
+: "${cmd_TR:=tr}"
+: "${cmd_WC:=wc}"
+
+## By default, tests are assumed to be located in the `TESTING´
+##   subdirectory of the current directory.
+## This can be overridden by setting the `path_TESTDIR´ environment
+##   variable.
+
+realpwd="$(
+  "${cmd_PWD}" -P
+)"
+defaulttestdir="${realpwd}"'/TESTING'
+: "${path_TESTDIR:=${defaulttestdir}}"
+
+## The default transformation command is assumed to be located in
+##   `bin/${programname}´, where `${programname}´ is determined by
+##   Ascii‐lowercasing the current directory name.
+## This can be overridden by setting the `cmd_TRANSFORM´ environment
+##   variable.
+
+programname="$(
+  "${cmd_BASENAME}" -- "${realpwd}" |
+  "${cmd_TR}" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
+)"
+defaulttransform="${realpwd}"'/bin/'"${programname}"
+: "${cmd_TRANSFORM:=${defaulttransform}}"
+
+## Input files are read from `📥´ and expected output is read from
+##   `📤´.
+## These locations (relative to `${path_TESTDIR}´) can be customized by
+##   setting the `name_INPUT´ and `name_OUTPUT´ variables,
+##   respectively.
+## If an input file exists but there is no corresponding output, the
+##   transform is expected to error.
+
+: "${name_INPUT:=📥}"
+: "${name_OUTPUT:=📤}"
+
+## If `${do_OVERWRITE}´ is not empty, then the test runner will
+##   overwrite the outputs of the selected tests instead of testing
+##   them.
+
+: "${do_OVERWRITE:=}"
+
+## § Implementation
+
+## The `status´ variable records the number of failed tests, which is
+##   also the exit status.
+
+status=0
+
+## The `ellipsis´ function simply renders spaced periods until
+##   `${charstowrite}´ hits 0.
+## Ideally these would be one‐dot leaders, but fonts disagree as to the
+##   appropriate width of that character.
+
+ellipsis () {
+  if "${cmd_TEST}" $((${charstowrite} % 2)) -ne 0
+  then :
+    "${cmd_PRINTF}" '%s' ' ' >&2
+  fi
+  while "${cmd_TEST}" ${charstowrite} -gt 1
+  do :
+    "${cmd_PRINTF}" '%s' '. ' >&2
+    charstowrite=$((${charstowrite} - 2))
+  done
+}
+
+## The `testrun´ function runs the test of name `${testname}´.
+
+testrun () {
+  "${cmd_PRINTF}" '%s' "${testname}"' ' >&2
+  charstowrite=$((42 - ${#testname})) ellipsis
+  infile="${path_TESTDIR}"'/'"${name_INPUT}"'/'"${testname}"
+  outfile="${path_TESTDIR}"'/'"${name_OUTPUT}"'/'"${testname}"
+  if ! "${cmd_TEST}" -f "${infile}"
+  then :
+    status=$((${status} + 1))
+    "${cmd_PRINTF}" '%s\n' 'MISSING' >&2
+  elif "${cmd_TEST}" -n "${do_OVERWRITE}"
+  then :
+    if "${cmd_TRANSFORM}" <"${infile}" >|"${outfile}" 2>/dev/null
+    then :
+      "${cmd_PRINTF}" '%s\n' 'WRITTEN' >&2
+    else :
+      "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
+    fi
+  elif ! "${cmd_TEST}" -f "${outfile}"
+  then :
+    if "${cmd_TRANSFORM}" <"${infile}" >/dev/null 2>/dev/null
+    then :
+      status=$((${status} + 1))
+      "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
+    else :
+      "${cmd_PRINTF}" '%s\n' 'SUCCESS' >&2
+    fi
+  else :
+    "${cmd_TRANSFORM}" <"${infile}" 2>/dev/null |
+    if "${cmd_CMP}" -s - "${outfile}" 2>/dev/null
+    then :
+      "${cmd_PRINTF}" '%s\n' 'SUCCESS' >&2
+    else :
+      status=$((${status} + 1))
+      "${cmd_PRINTF}" '%s\n' 'FAILURE' >&2
+    fi
+  fi
+}
+
+## If there is no argument, all tests are run; otherwise, the specified
+##   tests are run.
+
+if "${cmd_TEST}" $# -eq 0
+then :
+  for test in "${path_TESTDIR}"'/'"${name_INPUT}"'/'*
+  do :
+    testname="$(
+      "${cmd_BASENAME}" -- "${test}"
+    )" testrun
+  done
+else :
+  for name
+  do :
+    testname="${name}" testrun
+  done
+fi
+
+## Finally, the number of errors is returned as the exit status.
+
+exit ${status}
This page took 0.328287 seconds and 4 git commands to generate.