From 8b2e35ced2cb2b70f9e6ff31085ddbb84c0c1253 Mon Sep 17 00:00:00 2001 From: felixlohmeier Date: Sat, 3 Dec 2022 00:30:01 +0000 Subject: [PATCH] make test scripts standalone --- orcli | 1340 +++++++++++++++++++++------------------- src/bashly.yml | 5 - src/test_command.sh | 19 +- tests/data/example.csv | 4 + tests/data/example.tsv | 4 + tests/help.sh | 19 + tests/import-csv.sh | 26 +- 7 files changed, 740 insertions(+), 677 deletions(-) create mode 100644 tests/data/example.csv create mode 100644 tests/data/example.tsv create mode 100644 tests/help.sh diff --git a/orcli b/orcli index bdc89f9..9fda828 100755 --- a/orcli +++ b/orcli @@ -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)!" - 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]}")") @@ -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 - case "${1:-}" in - --version | -v ) - version_command - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --version | -v) + version_command + exit + ;; - --help | -h ) - long_usage=yes - orcli_usage - exit - ;; + --help | -h) + long_usage=yes + orcli_usage + exit + ;; - esac + *) + 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,75 +1400,75 @@ parse_requirements() { action=${1:-} case $action in - -* ) - ;; + -*) + ;; - completions ) - action="completions" - shift - orcli_completions_parse_requirements "$@" - shift $# - ;; + completions) + action="completions" + shift + orcli_completions_parse_requirements "$@" + shift $# + ;; - import ) - action="import" - shift - orcli_import_parse_requirements "$@" - shift $# - ;; + import) + action="import" + shift + orcli_import_parse_requirements "$@" + shift $# + ;; - list ) - action="list" - shift - orcli_list_parse_requirements "$@" - shift $# - ;; + list) + action="list" + shift + orcli_list_parse_requirements "$@" + shift $# + ;; - info ) - action="info" - shift - orcli_info_parse_requirements "$@" - shift $# - ;; + info) + action="info" + shift + orcli_info_parse_requirements "$@" + shift $# + ;; - test ) - action="test" - shift - orcli_test_parse_requirements "$@" - shift $# - ;; + test) + action="test" + shift + orcli_test_parse_requirements "$@" + shift $# + ;; - transform ) - action="transform" - shift - orcli_transform_parse_requirements "$@" - shift $# - ;; + transform) + action="transform" + shift + orcli_transform_parse_requirements "$@" + shift $# + ;; - export ) - action="export" - shift - orcli_export_parse_requirements "$@" - shift $# - ;; + export) + action="export" + shift + orcli_export_parse_requirements "$@" + shift $# + ;; - run ) - action="run" - shift - orcli_run_parse_requirements "$@" - shift $# - ;; + run) + action="run" + shift + orcli_run_parse_requirements "$@" + shift $# + ;; - # :command.command_fallback - "" ) - orcli_usage >&2 - exit 1 - ;; + # :command.command_fallback + "") + orcli_usage >&2 + exit 1 + ;; - * ) - printf "invalid command: %s\n" "$action" >&2 - exit 1 - ;; + *) + printf "invalid command: %s\n" "$action" >&2 + exit 1 + ;; esac @@ -1488,18 +1477,18 @@ parse_requirements() { key="$1" case "$key" in - -?* ) - printf "invalid option: %s\n" "$key" >&2 - exit 1 - ;; + -?*) + 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 - exit 1 + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - ;; + ;; esac done @@ -1509,14 +1498,20 @@ parse_requirements() { # :command.parse_requirements orcli_completions_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_completions_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_completions_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="completions" @@ -1526,18 +1521,18 @@ orcli_completions_parse_requirements() { key="$1" case "$key" in - -?* ) - printf "invalid option: %s\n" "$key" >&2 - exit 1 - ;; + -?*) + 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 - exit 1 + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - ;; + ;; esac done @@ -1547,46 +1542,52 @@ orcli_completions_parse_requirements() { # :command.parse_requirements orcli_import_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_import_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_import_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action=${1:-} case $action in - -* ) - ;; + -*) + ;; - csv ) - action="csv" - shift - orcli_import_csv_parse_requirements "$@" - shift $# - ;; + csv) + action="csv" + shift + orcli_import_csv_parse_requirements "$@" + shift $# + ;; - tsv ) - action="tsv" - shift - orcli_import_tsv_parse_requirements "$@" - shift $# - ;; + tsv) + action="tsv" + shift + orcli_import_tsv_parse_requirements "$@" + shift $# + ;; - # :command.command_fallback - "" ) - orcli_import_usage >&2 - exit 1 - ;; + # :command.command_fallback + "") + orcli_import_usage >&2 + exit 1 + ;; - * ) - printf "invalid command: %s\n" "$action" >&2 - exit 1 - ;; + *) + printf "invalid command: %s\n" "$action" >&2 + exit 1 + ;; esac @@ -1595,18 +1596,18 @@ orcli_import_parse_requirements() { key="$1" case "$key" in - -?* ) - printf "invalid option: %s\n" "$key" >&2 - exit 1 - ;; + -?*) + 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 - exit 1 + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - ;; + ;; esac done @@ -1616,14 +1617,20 @@ orcli_import_parse_requirements() { # :command.parse_requirements orcli_import_csv_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_import_csv_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_import_csv_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="import csv" @@ -1632,85 +1639,85 @@ orcli_import_csv_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in - # :flag.case - --separator ) + # :flag.case + --separator) - # :flag.case_arg - if [[ -n ${2+x} ]]; then + # :flag.case_arg + if [[ -n ${2+x} ]]; then - args[--separator]="$2" + args[--separator]="$2" + shift + shift + else + printf "%s\n" "--separator requires an argument: --separator SEPARATOR" >&2 + exit 1 + fi + ;; + + # :flag.case + --encoding) + + # :flag.case_arg + if [[ -n ${2+x} ]]; then + + args[--encoding]="$2" + shift + shift + else + printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 + exit 1 + fi + ;; + + # :flag.case + --trimStrings) + + # :flag.case_no_arg + args[--trimStrings]=1 shift + ;; + + # :flag.case + --projectName) + + # :flag.case_arg + if [[ -n ${2+x} ]]; then + + args[--projectName]="$2" + shift + shift + else + printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" >&2 + exit 1 + fi + ;; + + # :flag.case + --quiet | -q) + + # :flag.case_no_arg + args[--quiet]=1 shift - else - printf "%s\n" "--separator requires an argument: --separator SEPARATOR" >&2 + ;; + + -?*) + printf "invalid option: %s\n" "$key" >&2 exit 1 - fi - ;; + ;; - # :flag.case - --encoding ) + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable + if [[ -z ${args[file]+x} ]]; then - # :flag.case_arg - if [[ -n ${2+x} ]]; then + args[file]="\"$1\"" + shift + else + args[file]="${args[file]} \"$1\"" + shift + fi - args[--encoding]="$2" - shift - shift - else - printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 - exit 1 - fi - ;; - - # :flag.case - --trimStrings ) - - # :flag.case_no_arg - args[--trimStrings]=1 - shift - ;; - - # :flag.case - --projectName ) - - # :flag.case_arg - if [[ -n ${2+x} ]]; then - - args[--projectName]="$2" - shift - shift - else - printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" >&2 - exit 1 - fi - ;; - - # :flag.case - --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 - - args[file]="\"$1\"" - shift - else - args[file]="${args[file]} \"$1\"" - shift - fi - - ;; + ;; esac done @@ -1724,14 +1731,20 @@ orcli_import_csv_parse_requirements() { # :command.parse_requirements orcli_import_tsv_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_import_tsv_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_import_tsv_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="import tsv" @@ -1740,70 +1753,70 @@ orcli_import_tsv_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in - # :flag.case - --encoding ) + # :flag.case + --encoding) - # :flag.case_arg - if [[ -n ${2+x} ]]; then + # :flag.case_arg + if [[ -n ${2+x} ]]; then - args[--encoding]="$2" + args[--encoding]="$2" + shift + shift + else + printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 + exit 1 + fi + ;; + + # :flag.case + --trimStrings) + + # :flag.case_no_arg + args[--trimStrings]=1 shift + ;; + + # :flag.case + --projectName) + + # :flag.case_arg + if [[ -n ${2+x} ]]; then + + args[--projectName]="$2" + shift + shift + else + printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" >&2 + exit 1 + fi + ;; + + # :flag.case + --quiet | -q) + + # :flag.case_no_arg + args[--quiet]=1 shift - else - printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 + ;; + + -?*) + printf "invalid option: %s\n" "$key" >&2 exit 1 - fi - ;; + ;; - # :flag.case - --trimStrings ) + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable + if [[ -z ${args[file]+x} ]]; then - # :flag.case_no_arg - args[--trimStrings]=1 - shift - ;; + args[file]="\"$1\"" + shift + else + args[file]="${args[file]} \"$1\"" + shift + fi - # :flag.case - --projectName ) - - # :flag.case_arg - if [[ -n ${2+x} ]]; then - - args[--projectName]="$2" - shift - shift - else - printf "%s\n" "--projectName requires an argument: --projectName PROJECTNAME" >&2 - exit 1 - fi - ;; - - # :flag.case - --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 - - args[file]="\"$1\"" - shift - else - args[file]="${args[file]} \"$1\"" - shift - fi - - ;; + ;; esac done @@ -1816,14 +1829,20 @@ orcli_import_tsv_parse_requirements() { # :command.parse_requirements orcli_list_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_list_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_list_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="list" @@ -1833,18 +1852,18 @@ orcli_list_parse_requirements() { key="$1" case "$key" in - -?* ) - printf "invalid option: %s\n" "$key" >&2 - exit 1 - ;; + -?*) + 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 - exit 1 + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - ;; + ;; esac done @@ -1854,14 +1873,20 @@ orcli_list_parse_requirements() { # :command.parse_requirements orcli_info_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_info_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_info_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="info" @@ -1871,24 +1896,24 @@ 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 - - args[project]=$1 - shift - else - printf "invalid argument: %s\n" "$key" >&2 + -?*) + printf "invalid option: %s\n" "$key" >&2 exit 1 - fi + ;; - ;; + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + if [[ -z ${args[project]+x} ]]; then + + args[project]=$1 + shift + else + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + fi + + ;; esac done @@ -1904,14 +1929,20 @@ orcli_info_parse_requirements() { # :command.parse_requirements orcli_test_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_test_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_test_usage + exit + ;; - esac + *) + 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 - ;; + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; - * ) - # :command.parse_requirements_case - # :command.parse_requirements_case_repeatable - if [[ -z ${args[file]+x} ]]; then + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - args[file]="\"$1\"" - shift - else - args[file]="${args[file]} \"$1\"" - shift - fi - - ;; + ;; esac done - # :command.default_assignments - [[ -n ${args[file]:-} ]] || args[file]="tests/*.sh" - } # :command.parse_requirements orcli_transform_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_transform_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_transform_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="transform" @@ -1967,36 +1995,36 @@ orcli_transform_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in - # :flag.case - --quiet | -q ) + # :flag.case + --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 - - args[project]=$1 + # :flag.case_no_arg + args[--quiet]=1 shift - elif [[ -z ${args[file]+x} ]]; then + ;; - args[file]="\"$1\"" - shift - else - args[file]="${args[file]} \"$1\"" - shift - fi + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; - ;; + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable + if [[ -z ${args[project]+x} ]]; then + + args[project]=$1 + shift + elif [[ -z ${args[file]+x} ]]; then + + args[file]="\"$1\"" + shift + else + args[file]="${args[file]} \"$1\"" + shift + fi + + ;; esac done @@ -2015,39 +2043,45 @@ orcli_transform_parse_requirements() { # :command.parse_requirements orcli_export_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_export_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_export_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action=${1:-} case $action in - -* ) - ;; + -*) + ;; - tsv ) - action="tsv" - shift - orcli_export_tsv_parse_requirements "$@" - shift $# - ;; + tsv) + action="tsv" + shift + orcli_export_tsv_parse_requirements "$@" + shift $# + ;; - # :command.command_fallback - "" ) - orcli_export_usage >&2 - exit 1 - ;; + # :command.command_fallback + "") + orcli_export_usage >&2 + exit 1 + ;; - * ) - printf "invalid command: %s\n" "$action" >&2 - exit 1 - ;; + *) + printf "invalid command: %s\n" "$action" >&2 + exit 1 + ;; esac @@ -2056,18 +2090,18 @@ orcli_export_parse_requirements() { key="$1" case "$key" in - -?* ) - printf "invalid option: %s\n" "$key" >&2 - exit 1 - ;; + -?*) + 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 - exit 1 + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 - ;; + ;; esac done @@ -2077,14 +2111,20 @@ orcli_export_parse_requirements() { # :command.parse_requirements orcli_export_tsv_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_export_tsv_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_export_tsv_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="export tsv" @@ -2093,62 +2133,62 @@ orcli_export_tsv_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in - # :flag.case - --output ) + # :flag.case + --output) - # :flag.case_arg - if [[ -n ${2+x} ]]; then + # :flag.case_arg + if [[ -n ${2+x} ]]; then - args[--output]="$2" + args[--output]="$2" + shift + shift + else + printf "%s\n" "--output requires an argument: --output FILE" >&2 + exit 1 + fi + ;; + + # :flag.case + --encoding) + + # :flag.case_arg + if [[ -n ${2+x} ]]; then + + args[--encoding]="$2" + shift + shift + else + printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 + exit 1 + fi + ;; + + # :flag.case + --quiet | -q) + + # :flag.case_no_arg + args[--quiet]=1 shift - shift - else - printf "%s\n" "--output requires an argument: --output FILE" >&2 + ;; + + -?*) + printf "invalid option: %s\n" "$key" >&2 exit 1 - fi - ;; + ;; - # :flag.case - --encoding ) + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + if [[ -z ${args[project]+x} ]]; then - # :flag.case_arg - if [[ -n ${2+x} ]]; then + args[project]=$1 + shift + else + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + fi - args[--encoding]="$2" - shift - shift - else - printf "%s\n" "--encoding requires an argument: --encoding ENCODING" >&2 - exit 1 - fi - ;; - - # :flag.case - --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 - - args[project]=$1 - shift - else - printf "invalid argument: %s\n" "$key" >&2 - exit 1 - fi - - ;; + ;; esac done @@ -2167,14 +2207,20 @@ orcli_export_tsv_parse_requirements() { # :command.parse_requirements orcli_run_parse_requirements() { # :command.fixed_flags_filter - case "${1:-}" in - --help | -h ) - long_usage=yes - orcli_run_usage - exit - ;; + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + orcli_run_usage + exit + ;; - esac + *) + break + ;; + + esac + done # :command.command_filter action="run" @@ -2183,70 +2229,70 @@ orcli_run_parse_requirements() { while [[ $# -gt 0 ]]; do key="$1" case "$key" in - # :flag.case - --memory ) + # :flag.case + --memory) - # :flag.case_arg - if [[ -n ${2+x} ]]; then + # :flag.case_arg + if [[ -n ${2+x} ]]; then - args[--memory]="$2" + args[--memory]="$2" + shift + shift + else + printf "%s\n" "--memory requires an argument: --memory RAM" >&2 + exit 1 + fi + ;; + + # :flag.case + --port) + + # :flag.case_arg + if [[ -n ${2+x} ]]; then + + args[--port]="$2" + shift + shift + else + printf "%s\n" "--port requires an argument: --port PORT" >&2 + exit 1 + fi + ;; + + # :flag.case + --interactive) + + # :flag.case_no_arg + args[--interactive]=1 shift + ;; + + # :flag.case + --quiet | -q) + + # :flag.case_no_arg + args[--quiet]=1 shift - else - printf "%s\n" "--memory requires an argument: --memory RAM" >&2 + ;; + + -?*) + printf "invalid option: %s\n" "$key" >&2 exit 1 - fi - ;; + ;; - # :flag.case - --port ) + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable + if [[ -z ${args[file]+x} ]]; then - # :flag.case_arg - if [[ -n ${2+x} ]]; then + args[file]="\"$1\"" + shift + else + args[file]="${args[file]} \"$1\"" + shift + fi - args[--port]="$2" - shift - shift - else - printf "%s\n" "--port requires an argument: --port PORT" >&2 - exit 1 - fi - ;; - - # :flag.case - --interactive ) - - # :flag.case_no_arg - args[--interactive]=1 - shift - ;; - - # :flag.case - --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 - - args[file]="\"$1\"" - shift - else - args[file]="${args[file]} \"$1\"" - shift - fi - - ;; + ;; esac done @@ -2279,97 +2325,111 @@ run() { normalize_input "$@" parse_requirements "${input[@]}" - if [[ $action == "completions" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_completions_usage - else - orcli_completions_command - fi + case "$action" in + "completions") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_completions_usage + else + orcli_completions_command + fi + ;; - elif [[ $action == "import" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_import_usage - else - orcli_import_command - fi + "import") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_import_usage + else + orcli_import_command + fi + ;; - elif [[ $action == "import csv" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_import_csv_usage - else - orcli_import_csv_command - fi + "import csv") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_import_csv_usage + else + orcli_import_csv_command + fi + ;; - elif [[ $action == "import tsv" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_import_tsv_usage - else - orcli_import_tsv_command - fi + "import tsv") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_import_tsv_usage + else + orcli_import_tsv_command + fi + ;; - elif [[ $action == "list" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_list_usage - else - orcli_list_command - fi + "list") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_list_usage + else + orcli_list_command + fi + ;; - elif [[ $action == "info" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_info_usage - else - orcli_info_command - fi + "info") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_info_usage + else + orcli_info_command + fi + ;; - elif [[ $action == "test" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_test_usage - else - orcli_test_command - fi + "test") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_test_usage + else + orcli_test_command + fi + ;; - elif [[ $action == "transform" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_transform_usage - else - orcli_transform_command - fi + "transform") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_transform_usage + else + orcli_transform_command + fi + ;; - elif [[ $action == "export" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_export_usage - else - orcli_export_command - fi + "export") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_export_usage + else + orcli_export_command + fi + ;; - elif [[ $action == "export tsv" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_export_tsv_usage - else - orcli_export_tsv_command - fi + "export tsv") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_export_tsv_usage + else + orcli_export_tsv_command + fi + ;; - elif [[ $action == "run" ]]; then - if [[ ${args[--help]:-} ]]; then - long_usage=yes - orcli_run_usage - else - orcli_run_command - fi + "run") + if [[ ${args[--help]:-} ]]; then + long_usage=yes + orcli_run_usage + else + orcli_run_command + fi + ;; - elif [[ $action == "root" ]]; then - root_command - fi + "root") + root_command + ;; + + esac } initialize diff --git a/src/bashly.yml b/src/bashly.yml index 335e910..aaa23f9 100644 --- a/src/bashly.yml +++ b/src/bashly.yml @@ -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 diff --git a/src/test_command.sh b/src/test_command.sh index 20894b9..77a8b0e 100644 --- a/src/test_command.sh +++ b/src/test_command.sh @@ -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+=(${?}) diff --git a/tests/data/example.csv b/tests/data/example.csv new file mode 100644 index 0000000..8be467b --- /dev/null +++ b/tests/data/example.csv @@ -0,0 +1,4 @@ +a,b,c +1,2,3 +0,0,0 +$,\,' diff --git a/tests/data/example.tsv b/tests/data/example.tsv new file mode 100644 index 0000000..8c6d4cb --- /dev/null +++ b/tests/data/example.tsv @@ -0,0 +1,4 @@ +a b c +1 2 3 +0 0 0 +$ \ ' diff --git a/tests/help.sh b/tests/help.sh new file mode 100644 index 0000000..ee61e27 --- /dev/null +++ b/tests/help.sh @@ -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" diff --git a/tests/import-csv.sh b/tests/import-csv.sh index f9406d5..9c93bf1 100644 --- a/tests/import-csv.sh +++ b/tests/import-csv.sh @@ -1,27 +1,17 @@ #!/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" orcli export tsv "${t} csv" --output "${t}.output" # test -diff -u "${t}.assert" "${t}.output" \ No newline at end of file +diff -u "${t}.assert" "${t}.output"