make test scripts standalone

This commit is contained in:
felixlohmeier 2022-12-03 00:30:01 +00:00
parent aea762f22f
commit 8b2e35ced2
7 changed files with 740 additions and 677 deletions

316
orcli
View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
# This script was generated by bashly 0.8.10 (https://bashly.dannyb.co)
# This script was generated by bashly 0.9.0 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
@ -377,7 +377,7 @@ orcli_test_usage() {
fi
printf "Usage:\n"
printf " orcli test [FILE...]\n"
printf " orcli test\n"
printf " orcli test --help | -h\n"
echo
@ -390,15 +390,6 @@ orcli_test_usage() {
printf " Show this help\n"
echo
# :command.usage_args
printf "Arguments:\n"
# :argument.usage
echo " FILE..."
printf " Path to one or more files\n"
printf " Default: tests/*.sh\n"
echo
fi
}
@ -635,7 +626,7 @@ normalize_input() {
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for (( i=0 ; i < ${#flags} ; i++ )); do
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
else
@ -648,14 +639,14 @@ normalize_input() {
# :command.inspect_args
inspect_args() {
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
if (( ${#args[@]} )); then
if ((${#args[@]})); then
echo args:
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
else
echo args: none
fi
if (( ${#other_args[@]} )); then
if ((${#other_args[@]})); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
@ -1052,16 +1043,12 @@ orcli_test_command() {
# src/test_command.sh
# shellcheck shell=bash disable=SC2154
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
# check existence of files
for i in "${!files[@]}"; do
if ! [[ -f "${files[$i]}" ]]; then
error "cannot open ${files[$i]} (no such file)!"
if ! [[ -f "tests/help.sh" ]]; then
error "Cannot open test files!"
fi
done
cd "tests"
files=(*.sh)
# locate orcli and OpenRefine
scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
@ -1100,15 +1087,10 @@ orcli_test_command() {
for i in "${!files[@]}"; do
set +e
bash -e <(
# support ./orcli
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
# separate subdirectory for each test
echo "mkdir -p ${OPENREFINE_TMPDIR}/${files[$i]}"
echo "cd ${OPENREFINE_TMPDIR}/${files[$i]} || exit 1"
# echo test file
awk 1 "${files[$i]}"
) &>"$OPENREFINE_TMPDIR/test.log"
results+=(${?})
@ -1379,30 +1361,37 @@ orcli_run_command() {
# :command.parse_requirements
parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--version | -v )
--version | -v)
version_command
exit
;;
--help | -h )
--help | -h)
long_usage=yes
orcli_usage
exit
;;
*)
break
;;
esac
done
# :command.environment_variables_filter
# :command.environment_variables_default
export OPENREFINE_URL="${OPENREFINE_URL:-http://localhost:3333}"
# :command.dependencies_filter
if ! [[ -x "$(command -v curl)" ]]; then
if ! command -v curl >/dev/null 2>&1; then
printf "missing dependency: curl\n" >&2
exit 1
fi
if ! [[ -x "$(command -v jq)" ]]; then
if ! command -v jq >/dev/null 2>&1; then
printf "missing dependency: jq\n" >&2
exit 1
fi
@ -1411,59 +1400,59 @@ parse_requirements() {
action=${1:-}
case $action in
-* )
-*)
;;
completions )
completions)
action="completions"
shift
orcli_completions_parse_requirements "$@"
shift $#
;;
import )
import)
action="import"
shift
orcli_import_parse_requirements "$@"
shift $#
;;
list )
list)
action="list"
shift
orcli_list_parse_requirements "$@"
shift $#
;;
info )
info)
action="info"
shift
orcli_info_parse_requirements "$@"
shift $#
;;
test )
test)
action="test"
shift
orcli_test_parse_requirements "$@"
shift $#
;;
transform )
transform)
action="transform"
shift
orcli_transform_parse_requirements "$@"
shift $#
;;
export )
export)
action="export"
shift
orcli_export_parse_requirements "$@"
shift $#
;;
run )
run)
action="run"
shift
orcli_run_parse_requirements "$@"
@ -1471,12 +1460,12 @@ parse_requirements() {
;;
# :command.command_fallback
"" )
"")
orcli_usage >&2
exit 1
;;
* )
*)
printf "invalid command: %s\n" "$action" >&2
exit 1
;;
@ -1488,12 +1477,12 @@ parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
@ -1509,14 +1498,20 @@ parse_requirements() {
# :command.parse_requirements
orcli_completions_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_completions_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="completions"
@ -1526,12 +1521,12 @@ orcli_completions_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
@ -1547,30 +1542,36 @@ orcli_completions_parse_requirements() {
# :command.parse_requirements
orcli_import_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_import_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action=${1:-}
case $action in
-* )
-*)
;;
csv )
csv)
action="csv"
shift
orcli_import_csv_parse_requirements "$@"
shift $#
;;
tsv )
tsv)
action="tsv"
shift
orcli_import_tsv_parse_requirements "$@"
@ -1578,12 +1579,12 @@ orcli_import_parse_requirements() {
;;
# :command.command_fallback
"" )
"")
orcli_import_usage >&2
exit 1
;;
* )
*)
printf "invalid command: %s\n" "$action" >&2
exit 1
;;
@ -1595,12 +1596,12 @@ orcli_import_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
@ -1616,14 +1617,20 @@ orcli_import_parse_requirements() {
# :command.parse_requirements
orcli_import_csv_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_import_csv_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="import csv"
@ -1633,7 +1640,7 @@ orcli_import_csv_parse_requirements() {
key="$1"
case "$key" in
# :flag.case
--separator )
--separator)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -1648,7 +1655,7 @@ orcli_import_csv_parse_requirements() {
;;
# :flag.case
--encoding )
--encoding)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -1663,7 +1670,7 @@ orcli_import_csv_parse_requirements() {
;;
# :flag.case
--trimStrings )
--trimStrings)
# :flag.case_no_arg
args[--trimStrings]=1
@ -1671,7 +1678,7 @@ orcli_import_csv_parse_requirements() {
;;
# :flag.case
--projectName )
--projectName)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -1686,19 +1693,19 @@ orcli_import_csv_parse_requirements() {
;;
# :flag.case
--quiet | -q )
--quiet | -q)
# :flag.case_no_arg
args[--quiet]=1
shift
;;
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
if [[ -z ${args[file]+x} ]]; then
@ -1724,14 +1731,20 @@ orcli_import_csv_parse_requirements() {
# :command.parse_requirements
orcli_import_tsv_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_import_tsv_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="import tsv"
@ -1741,7 +1754,7 @@ orcli_import_tsv_parse_requirements() {
key="$1"
case "$key" in
# :flag.case
--encoding )
--encoding)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -1756,7 +1769,7 @@ orcli_import_tsv_parse_requirements() {
;;
# :flag.case
--trimStrings )
--trimStrings)
# :flag.case_no_arg
args[--trimStrings]=1
@ -1764,7 +1777,7 @@ orcli_import_tsv_parse_requirements() {
;;
# :flag.case
--projectName )
--projectName)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -1779,19 +1792,19 @@ orcli_import_tsv_parse_requirements() {
;;
# :flag.case
--quiet | -q )
--quiet | -q)
# :flag.case_no_arg
args[--quiet]=1
shift
;;
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
if [[ -z ${args[file]+x} ]]; then
@ -1816,14 +1829,20 @@ orcli_import_tsv_parse_requirements() {
# :command.parse_requirements
orcli_list_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_list_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="list"
@ -1833,12 +1852,12 @@ orcli_list_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
@ -1854,14 +1873,20 @@ orcli_list_parse_requirements() {
# :command.parse_requirements
orcli_info_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_info_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="info"
@ -1871,12 +1896,12 @@ orcli_info_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
if [[ -z ${args[project]+x} ]]; then
@ -1904,14 +1929,20 @@ orcli_info_parse_requirements() {
# :command.parse_requirements
orcli_test_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_test_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="test"
@ -1921,44 +1952,41 @@ orcli_test_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
if [[ -z ${args[file]+x} ]]; then
args[file]="\"$1\""
shift
else
args[file]="${args[file]} \"$1\""
shift
fi
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
exit 1
;;
esac
done
# :command.default_assignments
[[ -n ${args[file]:-} ]] || args[file]="tests/*.sh"
}
# :command.parse_requirements
orcli_transform_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_transform_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="transform"
@ -1968,19 +1996,19 @@ orcli_transform_parse_requirements() {
key="$1"
case "$key" in
# :flag.case
--quiet | -q )
--quiet | -q)
# :flag.case_no_arg
args[--quiet]=1
shift
;;
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
if [[ -z ${args[project]+x} ]]; then
@ -2015,23 +2043,29 @@ orcli_transform_parse_requirements() {
# :command.parse_requirements
orcli_export_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_export_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action=${1:-}
case $action in
-* )
-*)
;;
tsv )
tsv)
action="tsv"
shift
orcli_export_tsv_parse_requirements "$@"
@ -2039,12 +2073,12 @@ orcli_export_parse_requirements() {
;;
# :command.command_fallback
"" )
"")
orcli_export_usage >&2
exit 1
;;
* )
*)
printf "invalid command: %s\n" "$action" >&2
exit 1
;;
@ -2056,12 +2090,12 @@ orcli_export_parse_requirements() {
key="$1"
case "$key" in
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
printf "invalid argument: %s\n" "$key" >&2
@ -2077,14 +2111,20 @@ orcli_export_parse_requirements() {
# :command.parse_requirements
orcli_export_tsv_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_export_tsv_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="export tsv"
@ -2094,7 +2134,7 @@ orcli_export_tsv_parse_requirements() {
key="$1"
case "$key" in
# :flag.case
--output )
--output)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -2109,7 +2149,7 @@ orcli_export_tsv_parse_requirements() {
;;
# :flag.case
--encoding )
--encoding)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -2124,19 +2164,19 @@ orcli_export_tsv_parse_requirements() {
;;
# :flag.case
--quiet | -q )
--quiet | -q)
# :flag.case_no_arg
args[--quiet]=1
shift
;;
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_simple
if [[ -z ${args[project]+x} ]]; then
@ -2167,14 +2207,20 @@ orcli_export_tsv_parse_requirements() {
# :command.parse_requirements
orcli_run_parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--help | -h )
--help | -h)
long_usage=yes
orcli_run_usage
exit
;;
*)
break
;;
esac
done
# :command.command_filter
action="run"
@ -2184,7 +2230,7 @@ orcli_run_parse_requirements() {
key="$1"
case "$key" in
# :flag.case
--memory )
--memory)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -2199,7 +2245,7 @@ orcli_run_parse_requirements() {
;;
# :flag.case
--port )
--port)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
@ -2214,7 +2260,7 @@ orcli_run_parse_requirements() {
;;
# :flag.case
--interactive )
--interactive)
# :flag.case_no_arg
args[--interactive]=1
@ -2222,19 +2268,19 @@ orcli_run_parse_requirements() {
;;
# :flag.case
--quiet | -q )
--quiet | -q)
# :flag.case_no_arg
args[--quiet]=1
shift
;;
-?* )
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
* )
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
if [[ -z ${args[file]+x} ]]; then
@ -2279,97 +2325,111 @@ run() {
normalize_input "$@"
parse_requirements "${input[@]}"
if [[ $action == "completions" ]]; then
case "$action" in
"completions")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_completions_usage
else
orcli_completions_command
fi
;;
elif [[ $action == "import" ]]; then
"import")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_import_usage
else
orcli_import_command
fi
;;
elif [[ $action == "import csv" ]]; then
"import csv")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_import_csv_usage
else
orcli_import_csv_command
fi
;;
elif [[ $action == "import tsv" ]]; then
"import tsv")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_import_tsv_usage
else
orcli_import_tsv_command
fi
;;
elif [[ $action == "list" ]]; then
"list")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_list_usage
else
orcli_list_command
fi
;;
elif [[ $action == "info" ]]; then
"info")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_info_usage
else
orcli_info_command
fi
;;
elif [[ $action == "test" ]]; then
"test")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_test_usage
else
orcli_test_command
fi
;;
elif [[ $action == "transform" ]]; then
"transform")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_transform_usage
else
orcli_transform_command
fi
;;
elif [[ $action == "export" ]]; then
"export")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_export_usage
else
orcli_export_command
fi
;;
elif [[ $action == "export tsv" ]]; then
"export tsv")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_export_tsv_usage
else
orcli_export_tsv_command
fi
;;
elif [[ $action == "run" ]]; then
"run")
if [[ ${args[--help]:-} ]]; then
long_usage=yes
orcli_run_usage
else
orcli_run_command
fi
;;
elif [[ $action == "root" ]]; then
"root")
root_command
fi
;;
esac
}
initialize

View File

@ -118,11 +118,6 @@ commands:
- name: test
help: run functional tests on tmp OpenRefine workspace
args:
- name: file
help: Path to one or more files
default: "tests/*.sh"
repeatable: true
- name: transform
help: apply undo/redo JSON file(s) to an OpenRefine project

View File

@ -1,15 +1,11 @@
# shellcheck shell=bash disable=SC2154
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
# check existence of files
for i in "${!files[@]}"; do
if ! [[ -f "${files[$i]}" ]]; then
error "cannot open ${files[$i]} (no such file)!"
fi
done
if ! [[ -f "tests/help.sh" ]]; then
error "Cannot open test files!"
fi
cd "tests"
files=(*.sh)
# locate orcli and OpenRefine
scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
@ -48,15 +44,10 @@ results=()
for i in "${!files[@]}"; do
set +e
bash -e <(
# support ./orcli
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
# separate subdirectory for each test
echo "mkdir -p ${OPENREFINE_TMPDIR}/${files[$i]}"
echo "cd ${OPENREFINE_TMPDIR}/${files[$i]} || exit 1"
# echo test file
awk 1 "${files[$i]}"
) &>"$OPENREFINE_TMPDIR/test.log"
results+=(${?})

4
tests/data/example.csv Normal file
View File

@ -0,0 +1,4 @@
a,b,c
1,2,3
0,0,0
$,\,'
1 a b c
2 1 2 3
3 0 0 0
4 $ \ '

4
tests/data/example.tsv Normal file
View File

@ -0,0 +1,4 @@
a b c
1 2 3
0 0 0
$ \ '
1 a b c
2 1 2 3
3 0 0 0
4 $ \ '

19
tests/help.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
t="help"
# environment
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
cd "${tmpdir}" || exit 1
# assertion
cat << "DATA" > "${t}.assert"
orcli - OpenRefine command-line interface written in Bash
DATA
# action
orcli --help | head -n1 > "${t}.output"
# test
diff -u "${t}.assert" "${t}.output"

View File

@ -1,23 +1,13 @@
#!/bin/bash
t="import-csv"
# environment
t="$(basename "$(pwd)" .sh)"
# data
cat << "DATA" > "${t}.csv"
a,b,c
1,2,3
0,0,0
$,\,'
DATA
# assertion
cat << "DATA" > "${t}.assert"
a b c
1 2 3
0 0 0
$ \ '
DATA
tmpdir="$(mktemp -d)"
trap '{ rm -rf "${tmpdir}"; }' 0 2 3 15
cp data/example.csv "${tmpdir}"/${t}.csv
cp data/example.tsv "${tmpdir}"/${t}.assert
cd "${tmpdir}" || exit 1
# action
orcli import csv "${t}.csv"