diff --git a/.travis.yml b/.travis.yml index 64ef4bf..3d40388 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ -script: ./test.sh +script: ./run_tests.sh before_script: - sudo apt-get install zsh diff --git a/lib/git/shell_shortcuts.sh b/lib/git/shell_shortcuts.sh index f2c615b..865f0e1 100644 --- a/lib/git/shell_shortcuts.sh +++ b/lib/git/shell_shortcuts.sh @@ -91,14 +91,20 @@ fi # Adds numbered shortcuts to output of ls -l, just like 'git status' unalias ll > /dev/null 2>&1; unset -f ll > /dev/null 2>&1 function ls_with_file_shortcuts { - if [ -z $_ls_bsd ]; then - local ll_output="$(ls -lhv --group-directories-first --color "$@")" + local ll_output + if [ "$_ls_bsd" != "BSD" ]; then + ll_output="$(\ls -lhv --group-directories-first --color "$@")" else - local ll_output="$(CLICOLOR_FORCE=1 ls -l -G "$@")" + ll_output="$(CLICOLOR_FORCE=1 \ls -l -G "$@")" + fi + + if [[ $shell == "zsh" ]]; then + # Ensure sh_word_split is on + if setopt | grep -q shwordsplit; then SHWORDSPLIT_ON=true; fi + setopt shwordsplit fi # Parse path from args - zsh_compat # Ensure sh_word_split is on OLDIFS="$IFS"; IFS=$'\n' for arg in $@; do if [ -d "$arg" ]; then local rel_path="${arg%/}"; fi @@ -115,7 +121,8 @@ function ls_with_file_shortcuts { u,g,s=o.lines.map{|l|l[re,3]}.compact.map(&:split).transpose.map{|a|a.map(&:size).max+1};\ puts o.lines.map{|l|l.sub(re){|m|\"%s%-#{u}s %-#{g}s%#{s}s \"%[\$1,*\$3.split]}}" } - ll_output=$(echo "$ll_output" | sed "s/ $USER/ $(/bin/cat $HOME/.user_sym)/g" | rejustify_ls_columns) + + ll_output=$(echo "$ll_output" | \sed "s/ $USER/ $(/bin/cat $HOME/.user_sym)/g" | rejustify_ls_columns) fi if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then @@ -125,7 +132,7 @@ function ls_with_file_shortcuts { fi # Use ruby to inject numbers into ls output - echo "$ll_output" | ruby -e "$( cat < $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" diff --git a/test/lib/git/status_shortcuts_test.sh b/test/lib/git/status_shortcuts_test.sh index 137ed07..626873d 100755 --- a/test/lib/git/status_shortcuts_test.sh +++ b/test/lib/git/status_shortcuts_test.sh @@ -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) diff --git a/test/support/test_helper b/test/support/test_helper.sh similarity index 73% rename from test/support/test_helper rename to test/support/test_helper.sh index c7281e8..bf371d0 100644 --- a/test/support/test_helper +++ b/test/support/test_helper.sh @@ -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 "$@") }