Skip to content

Commit

Permalink
dorothy-config: fix tests, restore output revised items
Browse files Browse the repository at this point in the history
config-helper: restore outputting config was updated line to stderr
echo-lines: support --quote flag
  • Loading branch information
balupton committed Sep 16, 2024
1 parent 7deab4e commit ecee164
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion commands/config-helper
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ function config_helper() (
act "${option_args[@]}"

# we do our own processing, as we need to support multiline matches
if test "$UPDATED" = 'yes' -a "$option_quiet" = 'no'; then
if test "$UPDATED" = 'yes' -a "$option_quiet" != 'yes'; then
echo-style --success="Updated configuration file: $option_file" >/dev/stderr
fi
)
Expand Down
41 changes: 22 additions & 19 deletions commands/dorothy-config
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ function dorothy_config_test() (
# remove
fs-rm --quiet --no-confirm -- "$DOROTHY/user/config/dorothy-config-testing.bash"

local invented_packages util_packages expected_stdout expected_stderr
local invented_packages util_packages expected_stdout expected_stderr expected_file
invented_packages=(
"dorothy-config-testing-$(get-random-number)"
"dorothy-config-testing-$(get-random-number)"
# 0 and 1 prefixes for sorting consistency
"dorothy-config-testing-value-0$(get-random-number)"
"dorothy-config-testing-value-1$(get-random-number)"
)
util_packages=(
curl
ripgrep
sd
)
expected_file="$DOROTHY/user/config/setup.bash"

# add DOROTHY_CONFIG_TESTING_LIST, ignore stderr as it will vary based on whether choose was used or not
expected_stdout="$(__print_lines "${invented_packages[@]}")"
expected_stderr="$(
cat <<-EOF
Moved [curl] from [DOROTHY_CONFIG_TESTING] to [SETUP_UTILS] as [curl].
Moved [ripgrep] from [DOROTHY_CONFIG_TESTING] to [SETUP_UTILS] as [ripgrep].
Moved [sd] from [DOROTHY_CONFIG_TESTING] to [SETUP_UTILS] as [sd].
Updated configuration file: $DOROTHY/user/config/setup.bash
EOF
)"
eval-tester --name='util packages were removed from custom config' --stdout="$expected_stdout" --stderr="$expected_stderr" \
-- eval-no-color -- dorothy-config --packages-var='DOROTHY_CONFIG_TESTING' --prefer=public -- "${invented_packages[@]}" "${util_packages[@]}"
eval-tester --name='add and remove packages' --stdout="$expected_stdout" --ignore-stderr \
-- eval-no-color -- dorothy-config --packages-var='DOROTHY_CONFIG_TESTING_LIST' --prefer=public -- "${invented_packages[@]}" "${util_packages[@]}"

# remove DOROTHY_CONFIG_TESTING
expected_stdout=''
# read DOROTHY_CONFIG_TESTING_LIST
expected_stdout=$'(\n'"$(echo-lines --quote --indent=$'\t' -- "${invented_packages[@]}")"$'\n)'
eval-tester --name='read packages' --stdout="$expected_stdout" --ignore-stderr \
-- eval-no-color -- config-helper --file="$expected_file" -- --field='DOROTHY_CONFIG_TESTING_LIST'

# remove DOROTHY_CONFIG_TESTING_LIST
expected_stderr="Updated configuration file: $DOROTHY/user/config/setup.bash"
eval-tester --name='custom config was removed' --stdout="$expected_stdout" --stderr="$expected_stderr" \
-- eval-no-color -- dorothy-config 'setup.bash' --prefer=public -- --field='DOROTHY_CONFIG_TESTING' --replace=
eval-tester --name='remove config' --stderr="$expected_stderr" \
-- eval-no-color -- dorothy-config 'setup.bash' --prefer=public -- --field='DOROTHY_CONFIG_TESTING_LIST' --replace=

echo-style --g1="TEST: $0"
return 0
Expand Down Expand Up @@ -109,15 +109,15 @@ function dorothy_config() (
if test -n "$option_packages_var"; then
option_filename='setup.bash'
else
help "A filename must be provided."
help 'A filename must be provided.'
fi
fi

# check extension
local extension # this is used later too
extension="$(fs-extension -- "$option_filename")"
if ! [[ $extension =~ ^(bash|zsh|sh|fish|nu)$ ]]; then
help "The file extension of [$option_filename] is not yet supported."
help 'The file extension of ' --code="$option_filename" ' is not yet supported.'
fi

# =================================
Expand Down Expand Up @@ -165,6 +165,9 @@ function dorothy_config() (
--field="$option_packages_var" --array="$(__print_lines "${revised_items[@]}" | sort --ignore-case | uniq)" \
--field='SETUP_UTILS' --array="$(__print_lines "${SETUP_UTILS[@]}" | sort --ignore-case | uniq)"
fi

# output the revised items, so that the caller has them
__print_lines "${revised_items[@]}"
}

function update_configuration {
Expand Down
4 changes: 2 additions & 2 deletions commands/echo-lines
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function echo_lines() (
--suffix=<suffix>
suffix each input with this
--quoted=<yes/no>
--quote=<yes/NO> | --quoted=<yes/NO>
if yes, the arguments will be quoted if necessary
$(stdinargs_options_help --)
Expand Down Expand Up @@ -232,7 +232,7 @@ function echo_lines() (
'--no-shrink'* | '--shrink'*)
option_shrink="$(get-flag-value --affirmative --fallback="$option_shrink" -- "$item")"
;;
'--no-quoted'* | '--quoted'*)
'--no-quote'* | '--quote'*) # will also support quoted
option_quoted="$(get-flag-value --affirmative --fallback="$option_quoted" -- "$item")"
;;
# forward to stdinargs, however support mixing and matching of our options, with stdinarg options
Expand Down

0 comments on commit ecee164

Please sign in to comment.