2024-03-26 16:12:51 +00:00
|
|
|
#!/usr/bin/env sh
|
2024-03-26 18:56:20 +00:00
|
|
|
# Copyright (C) 2023 - 2024:
|
|
|
|
# The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# This file is part of the Trixy crate for Trinitrix.
|
|
|
|
#
|
|
|
|
# Trixy is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the Lesser GNU General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of
|
|
|
|
# the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# and the Lesser GNU General Public License along with this program.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/>.
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
# Parts of a shell library {{{
|
|
|
|
die() {
|
2024-03-26 16:28:21 +00:00
|
|
|
error "$1"
|
2024-03-26 16:12:51 +00:00
|
|
|
if [ -n "$2" ]; then
|
2024-03-26 16:28:21 +00:00
|
|
|
exit "$2"
|
2024-03-26 16:12:51 +00:00
|
|
|
else
|
2024-03-26 16:28:21 +00:00
|
|
|
exit 1
|
2024-03-26 16:12:51 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print() {
|
|
|
|
# The direct usage is intended as `print` is rather lowlevel
|
|
|
|
# shellcheck disable=2059
|
2024-03-26 16:28:21 +00:00
|
|
|
printf "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
println() {
|
|
|
|
# The direct usage is intended as `println` is rather lowlevel
|
|
|
|
# shellcheck disable=2059
|
2024-03-26 16:28:21 +00:00
|
|
|
printf "$*\n"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
eprint() {
|
2024-03-26 16:28:21 +00:00
|
|
|
>&2 print "$@"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
eprintln() {
|
2024-03-26 16:28:21 +00:00
|
|
|
>&2 println "$@"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
|
2024-03-26 16:28:21 +00:00
|
|
|
if [ -n "$NO_COLOR" ]; then
|
2024-03-26 16:12:51 +00:00
|
|
|
error() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "==> ERROR:" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
warning() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "==> WARNING:" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
debug() {
|
2024-03-26 16:28:21 +00:00
|
|
|
[ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "==> [Debug:]" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
debug2() {
|
2024-03-26 16:28:21 +00:00
|
|
|
[ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln " -> [Debug:]" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
msg() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "==>" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
msg2() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln " ->" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
prompt() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprint "..>" "$*"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
else
|
|
|
|
# See https://stackoverflow.com/a/33206814 for ansi codes
|
|
|
|
error() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "\033[1;91m==> ERROR:\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
warning() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "\033[1;91m==> WARNING:\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
debug() {
|
2024-03-26 16:28:21 +00:00
|
|
|
[ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "\033[1;94m==> [Debug:]\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
debug2() {
|
2024-03-26 16:28:21 +00:00
|
|
|
[ -n "$SHELL_LIBRARY_DEBUG" ] && eprintln "\033[1;94m -> [Debug:]\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
msg() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "\033[1;96m==>\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
msg2() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprintln "\033[1;96m ->\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
prompt() {
|
2024-03-26 16:28:21 +00:00
|
|
|
eprint "\033[1;96m..>\033[0m" "\033[1;93m$*\033[0m"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
mktmp() {
|
2024-03-26 16:28:21 +00:00
|
|
|
ensure_tmp_dir
|
|
|
|
mktemp -p "$SHELL_LIBRARY_TEMP_PREFIX_DIR"
|
2024-03-26 16:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ensure_tmp_dir() {
|
2024-03-26 16:28:21 +00:00
|
|
|
if ! [ -d "$SHELL_LIBRARY_TEMP_PREFIX_DIR" ]; then
|
|
|
|
SHELL_LIBRARY_TEMP_PREFIX_DIR="$(mktemp -d)"
|
2024-03-26 16:12:51 +00:00
|
|
|
fi
|
2024-03-26 16:28:21 +00:00
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
# A new version of tmp, which allows you to specify the commandline args as normal
|
|
|
|
# arguments
|
|
|
|
tmp() {
|
|
|
|
if echo "$1" | grep -E ' ' -q; then
|
|
|
|
warn "Detected an old version of tmp, as there are spaces in the input string!"
|
|
|
|
fi
|
|
|
|
|
2024-03-26 16:28:21 +00:00
|
|
|
TEMP_DIR_WITH_DEL="$(mktmp)"
|
|
|
|
"$@" 1>"$TEMP_DIR_WITH_DEL"
|
|
|
|
echo "$TEMP_DIR_WITH_DEL"
|
|
|
|
}
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
# Takes a path to search as argument.
|
|
|
|
# Also takes further arguments as names to search for.
|
|
|
|
# Prints the path to a file, if a file matching the given name is found.
|
|
|
|
search_dir_for_file() {
|
2024-03-26 16:28:21 +00:00
|
|
|
[ -d "$1" ] || die "Arg $1 is not a directory"
|
|
|
|
directory="$1" && shift 1
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
while read -r dir_entry; do
|
2024-03-26 16:28:21 +00:00
|
|
|
dir_entry_count=$((dir_entry_count + 1))
|
|
|
|
dir_entry_basename="$(basename "$dir_entry")"
|
2024-03-26 16:12:51 +00:00
|
|
|
|
2024-03-26 16:28:21 +00:00
|
|
|
for name in "$@"; do
|
2024-03-26 16:12:51 +00:00
|
|
|
if [ "$name" = "$dir_entry_basename" ]; then
|
2024-03-26 16:28:21 +00:00
|
|
|
println "$dir_entry"
|
2024-03-26 16:12:51 +00:00
|
|
|
fi
|
|
|
|
done
|
2024-03-26 16:28:21 +00:00
|
|
|
done <"$(tmp fd . "$directory" --type file --max-depth 1)"
|
|
|
|
print ""
|
2024-03-26 16:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Returns the path to the directory which contains all the specified files:
|
|
|
|
# `search_upward_files file1 file2 file3` returns a path containing file1 ...
|
|
|
|
search_upward_files() {
|
2024-03-26 16:28:21 +00:00
|
|
|
directory="$(pwd)"
|
|
|
|
final_directory=""
|
|
|
|
output="$(mktmp)"
|
|
|
|
while [ "$(wc -l <"$output")" = 0 ]; do
|
|
|
|
search_dir_for_file "$directory" "$@" >"$output"
|
|
|
|
directory="$(dirname "$directory")"
|
2024-03-26 16:12:51 +00:00
|
|
|
if [ "$directory" = "/" ]; then
|
2024-03-26 16:28:21 +00:00
|
|
|
warning "We bailed out, as there seems to be no directory containing $*"
|
|
|
|
final_directory=""
|
|
|
|
return
|
2024-03-26 16:12:51 +00:00
|
|
|
fi
|
|
|
|
done
|
2024-03-26 16:28:21 +00:00
|
|
|
final_directory="$(dirname "$(head -n1 "$output")")"
|
|
|
|
print "$final_directory"
|
2024-03-26 16:12:51 +00:00
|
|
|
}
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
help() {
|
2024-03-26 16:28:21 +00:00
|
|
|
cat <<EOF
|
2024-03-26 16:12:51 +00:00
|
|
|
A test manager (for trixy integration tests)
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
./scripts/tests [OPTIONS] COMMAND
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--help | -h
|
|
|
|
Display this help and exit.
|
|
|
|
|
|
|
|
COMMANDS:
|
|
|
|
update [TESTNAME]
|
|
|
|
Updates the 'expected.md' file of a test with TESTNAME, if
|
|
|
|
TESTNAME is blank, all tests are updated.
|
|
|
|
|
|
|
|
new TESTNAME
|
|
|
|
Generates a new test from the '.template' directory and drops
|
|
|
|
you into an editor for the 'input.tri' file.
|
|
|
|
It will also generate the 'expected.md' file, after you close
|
|
|
|
the editor.
|
|
|
|
|
|
|
|
ARGUMENTS:
|
|
|
|
TESTNAME | *([a-zA-Z0-9]) := [[ false ]]
|
|
|
|
The name of a test.
|
|
|
|
(The notation above is only really useful, when
|
|
|
|
the shell library is employed to generate
|
|
|
|
a completion script.)
|
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2024-03-26 16:28:21 +00:00
|
|
|
ROOT_DIR="$(search_upward_files "Cargo.toml")"
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
new() {
|
2024-03-26 16:28:21 +00:00
|
|
|
test_name="$1"
|
|
|
|
test_dir="$ROOT_DIR/tests/$test_name"
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
[ -d "$test_dir" ] && die "$test_dir already exists, refusing to override"
|
|
|
|
|
|
|
|
cp --recursive "$ROOT_DIR/tests/.template" "$test_dir"
|
|
|
|
|
|
|
|
sed --in-place "s|template|$test_name|g" "$test_dir/main.rs"
|
|
|
|
|
|
|
|
editor="${EDITOR:-$VI}"
|
|
|
|
"$editor" "$test_dir/input.tri"
|
|
|
|
update "$test_name"
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
test_name="$1"
|
2024-03-26 16:28:21 +00:00
|
|
|
test_dir="$ROOT_DIR/tests/$test_name"
|
2024-03-26 16:12:51 +00:00
|
|
|
|
|
|
|
if [ -n "$test_name" ]; then
|
|
|
|
cargo run --features build-binary -- \
|
|
|
|
"$test_dir/input.tri" generate all --no-vendored \
|
2024-03-26 16:28:21 +00:00
|
|
|
>"$test_dir/expected.md"
|
2024-03-26 16:12:51 +00:00
|
|
|
else
|
|
|
|
fd . "$ROOT_DIR/tests" --type directory --max-depth=1 --exec basename | while read -r dir; do
|
|
|
|
msg2 "Updating $dir.."
|
|
|
|
update "$dir"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in "$@"; do
|
|
|
|
case "$arg" in
|
2024-03-26 16:28:21 +00:00
|
|
|
"--help" | "-h")
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
2024-03-26 16:12:51 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "$1" in
|
2024-03-26 16:28:21 +00:00
|
|
|
"update")
|
|
|
|
shift 1
|
|
|
|
[ -n "$1" ] && test_name="$1"
|
|
|
|
update "$test_name"
|
|
|
|
;;
|
|
|
|
"new")
|
|
|
|
shift 1
|
|
|
|
test_name="$1"
|
|
|
|
[ -z "$test_name" ] && die "You must specify a TESTNAME"
|
|
|
|
new "$test_name"
|
|
|
|
;;
|
2024-03-26 16:12:51 +00:00
|
|
|
esac
|
|
|
|
# vim: ft=sh
|