#!/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}
