Fully fixed up ll escaping, added lots of tests

This commit is contained in:
Nathan Broadbent
2012-10-23 12:25:28 +13:00
parent 7c7aedce03
commit 5836617dbf
8 changed files with 65 additions and 32 deletions

View File

@@ -13,7 +13,7 @@ export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../.."
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers
source "$scmbDir/test/support/test_helper"
source "$scmbDir/test/support/test_helper.sh"
# Load functions to test
source "$scmbDir/lib/scm_breeze.sh"

View File

@@ -13,7 +13,7 @@ export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers
source "$scmbDir/test/support/test_helper"
source "$scmbDir/test/support/test_helper.sh"
# Load functions to test
source "$scmbDir/lib/scm_breeze.sh"

View File

@@ -11,31 +11,29 @@ 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
fi
# Load test helpers
source "$scmbDir/test/support/test_helper"
source "$scmbDir/test/support/test_helper.sh"
# Setup
#-----------------------------------------------------------------------------
oneTimeSetUp() {
export shell_command_wrapping_enabled="true"
export scmb_wrapped_shell_commands="not_found cat rm cp mv ln ls cd sed"
export scmb_wrapped_shell_commands="not_found cat rm cp mv ln cd sed"
# Test functions
function ls() { ls $@; }
function ln() { ln $@; }
# Test aliases
alias mv="nocorrect mv"
alias rm="rm --option"
alias sed="sed"
# Test already wrapped commands
alias ln="exec_scmb_expand_args /bin/ln"
alias cat="exec_scmb_expand_args /bin/cat"
# Run shortcut wrapping
source "$scmbDir/lib/git/shell_shortcuts.sh"
@@ -62,11 +60,37 @@ test_shell_command_wrapping() {
assertAliasEquals "exec_scmb_expand_args /bin/sed" "sed"
assertAliasEquals "exec_scmb_expand_args /bin/cat" "cat"
assertAliasEquals "exec_scmb_expand_args builtin cd" "cd"
assertAliasEquals "exec_scmb_expand_args __original_ls" "ls"
assertAliasEquals "exec_scmb_expand_args /bin/ln" "ln"
assertAliasEquals "exec_scmb_expand_args __original_ln" "ln"
}
test_ls_with_file_shortcuts() {
export git_env_char="e"
TEST_DIR=$(mktemp -d -t scm_breeze.XXXXXXXXXX)
cd $TEST_DIR
touch 'test file' 'test_file'
mkdir -p "a [b]" 'a "b"' "a 'b'"
# 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_output=$(<$temp_file strip_colors)
# Compare as fixed strings (F), instead of regex (P)
assertIncludes "$ls_output" '[1] a "b"' F
assertIncludes "$ls_output" "[2] a 'b'" F
assertIncludes "$ls_output" '[3] a [b]' F
assertIncludes "$ls_output" '[4] test file' F
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/test file" "$e4"
assertEquals "$TEST_DIR/test_file" "$e5"
}
# load and run shUnit2
source "$scmbDir/test/support/shunit2"

View File

@@ -13,7 +13,7 @@ export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers
source "$scmbDir/test/support/test_helper"
source "$scmbDir/test/support/test_helper.sh"
# Load functions to test
source "$scmbDir/lib/scm_breeze.sh"
@@ -95,6 +95,7 @@ test_git_status_shortcuts() {
assertNotIncludes "$git_status3" "Untracked files"
# 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)
git_status_shortcuts > $temp_file
git_status=$(<$temp_file strip_colors)

View File

@@ -24,18 +24,20 @@ verboseGitCommands() {
unset -f git
}
# Asserts
#-----------------------------------------------------------------------------
_includes() {
if [ -n "$3" ]; then regex="$3"; else regex=P; fi
if echo "$1" | grep -q$regex "$2"; then echo 0; else echo 1; fi
}
# assert $1 contains $2
assertIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi
assertTrue "'$1' should have contained '$2'" $result
assertTrue "'$1' should have contained '$2'" $(_includes "$@")
}
# assert $1 does not contain $2
assertNotIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi
assertFalse "'$1' should not have contained '$2'" $result
assertFalse "'$1' should not have contained '$2'" $(_includes "$@")
}