From: Lady Date: Mon, 23 Mar 2026 02:16:53 +0000 (-0400) Subject: Add diffio and testio X-Git-Tag: 0.1.1 X-Git-Url: https://git.ladys.computer/HOMEDIR/commitdiff_plain/refs/tags/0.1.1 Add diffio and testio These commands are designed to make testing transformation programs easy. Message-type: text/lesml --- diff --git a/bin/diffio b/bin/diffio new file mode 120000 index 0000000..833b7d3 --- /dev/null +++ b/bin/diffio @@ -0,0 +1 @@ +../sh/diffio.sh \ No newline at end of file diff --git a/bin/testio b/bin/testio new file mode 120000 index 0000000..0de08c1 --- /dev/null +++ b/bin/testio @@ -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 index 0000000..799821c --- /dev/null +++ b/sh/diffio.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env sh +# @(#)šŸ šŸ“‚Ā HOMEDIR sh/diffio.sh 2026-03-23T02:16:54Z +# SPDX-FileCopyrightText: 2026 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 {šŸ”—}. +## +## 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 index 0000000..6c45d8f --- /dev/null +++ b/sh/testio.sh @@ -0,0 +1,163 @@ +#!/usr/bin/env sh +# @(#)šŸ šŸ“‚Ā HOMEDIR sh/testio.sh 2026-04-05T19:41:30Z +# SPDX-FileCopyrightText: 2026 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 {šŸ”—}. +## +## 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}