[shell_aliases] try fix array looping

This commit is contained in:
Will Owens
2024-02-23 04:05:51 -05:00
parent fd4096ed98
commit 71f97bf3cf
2 changed files with 57 additions and 52 deletions

View File

@@ -7,11 +7,13 @@
#
# Unit tests for shell command wrapping
export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
export scmbDir="$(cd -P "$(dirname "$0")" && pwd)/../../.."
# Zsh compatibility
if [ -n "${ZSH_VERSION:-}" ]; then
shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit
shell="zsh"
SHUNIT_PARENT=$0
setopt shwordsplit
else
# Bash needs this option so that 'alias' works in a non-interactive shell
shopt -s expand_aliases
@@ -25,7 +27,7 @@ source "$scmbDir/lib/scm_breeze.sh"
#-----------------------------------------------------------------------------
oneTimeSetUp() {
export shell_command_wrapping_enabled="true"
export scmb_wrapped_shell_commands="not_found cat rm cp mv ln cd sed"
export scmb_wrapped_shell_commands=("not_found" "cat" "rm" "cp" "mv" "ln" "cd" "sed")
export shell_ls_aliases_enabled="true"
alias rvm="test" # Ensure tests run if RVM isn't loaded but $HOME/.rvm is present
@@ -52,37 +54,35 @@ oneTimeSetUp() {
# Define 'whence' function for Bash.
# Must come after sourcing shell_shortcuts
type whence > /dev/null 2>&1 || function whence() { type "$@" | sed -e "s/.*is aliased to \`//" -e "s/'$//"; }
type whence >/dev/null 2>&1 || function whence() { type "$@" | sed -e "s/.*is aliased to \`//" -e "s/'$//"; }
}
# Helper function to test that alias is defined properly.
# (Works for both zsh and bash)
assertAliasEquals(){
assertAliasEquals() {
assertEquals "$1" "$(whence $2)"
}
#-----------------------------------------------------------------------------
# Setup and tear down
#-----------------------------------------------------------------------------
setUp() {
unset QUOTING_STYLE # Use default quoting style for ls
unset QUOTING_STYLE # Use default quoting style for ls
}
#-----------------------------------------------------------------------------
# Unit tests
#-----------------------------------------------------------------------------
test_shell_command_wrapping() {
assertAliasEquals "exec_scmb_expand_args nocorrect $mv_path" "mv"
assertAliasEquals "exec_scmb_expand_args $rm_path --option" "rm"
assertAliasEquals "exec_scmb_expand_args $sed_path" "sed"
assertAliasEquals "exec_scmb_expand_args $cat_path" "cat"
assertAliasEquals "exec_scmb_expand_args builtin cd" "cd"
assertIncludes "$(declare -f ln)" "ln ()"
assertIncludes "$(declare -f ln)" "exec_scmb_expand_args __original_ln"
assertAliasEquals "exec_scmb_expand_args $rm_path --option" "rm"
assertAliasEquals "exec_scmb_expand_args $sed_path" "sed"
assertAliasEquals "exec_scmb_expand_args $cat_path" "cat"
assertAliasEquals "exec_scmb_expand_args builtin cd" "cd"
assertIncludes "$(declare -f ln)" "ln ()"
assertIncludes "$(declare -f ln)" "exec_scmb_expand_args __original_ln"
}
test_ls_with_file_shortcuts() {
@@ -105,7 +105,7 @@ test_ls_with_file_shortcuts() {
# Run command in shell, load output from temp file into variable
# (This is needed so that env variables are exported in the current shell)
temp_file=$(mktemp -t scm_breeze.XXXXXXXXXX)
ls_with_file_shortcuts > "$temp_file"
ls_with_file_shortcuts >"$temp_file"
ls_output=$(<"$temp_file" strip_colors)
# Compare as fixed strings (F), instead of normal grep behavior
@@ -116,14 +116,14 @@ test_ls_with_file_shortcuts() {
assertIncludes "$ls_output" '[5] test_file' F
# Test filenames with single or double quotes escaped
assertEquals "$TEST_DIR/"'a "b"' "$e1"
assertEquals "$TEST_DIR/a 'b'" "$e2"
assertEquals "$TEST_DIR/a [b]" "$e3"
assertEquals "$TEST_DIR/"'a "b"' "$e1"
assertEquals "$TEST_DIR/a 'b'" "$e2"
assertEquals "$TEST_DIR/a [b]" "$e3"
assertEquals "$TEST_DIR/test file" "$e4"
assertEquals "$TEST_DIR/test_file" "$e5"
# Test ls with subdirectory
ls_with_file_shortcuts 'a "b"' > $temp_file
ls_with_file_shortcuts 'a "b"' >$temp_file
ls_output=$(<$temp_file strip_colors)
assertIncludes "$ls_output" '[1] c' F
# Test that env variable is set correctly