From 6638e53935ee1c5f3bfb23c08bd2aae2994b6219 Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Sat, 4 Apr 2020 19:52:57 -0400 Subject: change algorithm, fix edge cases, add tests --- test/a/x | 3 +++ test/b/x | 3 +++ test/c/x | 3 +++ test/start | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100755 test/a/x create mode 100755 test/b/x create mode 100755 test/c/x create mode 100755 test/start (limited to 'test') diff --git a/test/a/x b/test/a/x new file mode 100755 index 0000000..e7d6387 --- /dev/null +++ b/test/a/x @@ -0,0 +1,3 @@ +#!/bin/sh +echo "in a with $0, next" +nextbin "$0" "$@" diff --git a/test/b/x b/test/b/x new file mode 100755 index 0000000..0355509 --- /dev/null +++ b/test/b/x @@ -0,0 +1,3 @@ +#!/bin/sh +echo "in b with $0, next" +nextbin "$0" "$@" diff --git a/test/c/x b/test/c/x new file mode 100755 index 0000000..cc0070b --- /dev/null +++ b/test/c/x @@ -0,0 +1,3 @@ +#!/bin/sh +echo "in c with $0, done" +exit 0 diff --git a/test/start b/test/start new file mode 100755 index 0000000..4cf0d9d --- /dev/null +++ b/test/start @@ -0,0 +1,59 @@ +#!/bin/sh + +set -e + +cd "${0%/*}" +exec 2>&1 + +check() { + for arg in $1; do + read -r line || { echo "FAIL: insufficient output" >&2; return 1; } + echo "GOT: $line" + case "$arg" in + [a-z]) case "$line" in "in $arg "*) ;; *) echo "FAIL: expected $arg" >&2; return 1; esac;; + err) case "$line" in nextbin*failed*) ;; *) echo "FAIL: expected err" >&2; return 1; esac;; + *) echo "INVALID TEST" >&2; exit 1 + esac + done + if read -r line; then + echo "FAIL: too much output" >&2 + return 1 + fi + echo "OK" +} + +try() { + # shellcheck disable=SC2016 + printf 'TEST: PATH="$PATH:%s" %s, expect %s\n' "$@" + PATH="$PATH:$1" "$2" 2>&1 | check "$3" + # shellcheck disable=SC2016 + printf 'TEST: PATH="%s:$PATH" %s, expect %s\n' "$@" + PATH="$1:$PATH" "$2" 2>&1 | check "$3" +} + +do_try() { + try "$1" "$2/x" "$3" + try "$1" "./$2/x" "$3" + try "$1" "$PWD/$2/x" "$3" + try "$1" "$2/../$2/x" "$3" + try "$1" "./$2/../$2/x" "$3" + try "$1" "$PWD/$2/../$2/x" "$3" + echo +} + +do_try2() { + try "$1" "x" "$3" + do_try "$@" +} + +export PATH="$PWD/..:$PATH" + +do_try "" a "a err" +do_try2 "$PWD/a" a "a err" +do_try "$PWD/b" a "a b err" +do_try2 "$PWD/a:$PWD/b" a "a b err" +do_try "$PWD/a:$PWD/b" b "b err" +do_try2 "$PWD/a:$PWD/b:$PWD/c" a "a b c" +do_try "$PWD/a:$PWD/b:$PWD/c" b "b c" + +echo "ALL TESTS COMPLETED SUCCESSFULLY :)" -- cgit v1.2.3-70-g09d2