Fully fixed up ll escaping, added lots of tests
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
script: ./test.sh
|
||||
script: ./run_tests.sh
|
||||
before_script:
|
||||
- sudo apt-get install zsh
|
||||
|
||||
@@ -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 <<EOF
|
||||
echo "$ll_output" | ruby -e "$( \cat <<EOF
|
||||
output = STDIN.read
|
||||
e = 1
|
||||
re = /^(([^ ]* +){8})/
|
||||
@@ -143,17 +150,14 @@ EOF
|
||||
local file=''
|
||||
|
||||
if [ -z $_ls_bsd ]; then
|
||||
ll_files="$(ls -v --group-directories-first --color=never "$@")"
|
||||
ll_files="$(\ls -v --group-directories-first --color=never "$@")"
|
||||
else
|
||||
ll_files="$(ls "$@")"
|
||||
ll_files="$(\ls "$@")"
|
||||
fi
|
||||
# Escape single and double quotes
|
||||
ll_files=$(echo "$ll_files" | \sed -e 's/"/\\\\\\"/g' -e "s/'"'/\\\\'"'/g")
|
||||
|
||||
OLDIFS="$IFS"; IFS=$'\n'
|
||||
if [[ $shell == "zsh" ]]; then
|
||||
# Ensure sh_word_split is on
|
||||
zsh_compat
|
||||
setopt shwordsplit
|
||||
fi
|
||||
for file in $ll_files; do
|
||||
if [ -n "$rel_path" ]; then file="$rel_path/$file"; fi
|
||||
export $git_env_char$e="$(eval $_abs_path_command \"$file\")"
|
||||
@@ -161,7 +165,9 @@ EOF
|
||||
let e++
|
||||
done
|
||||
IFS="$OLDIFS"
|
||||
if [[ $shell == "zsh" ]]; then unsetopt shwordsplit; fi
|
||||
|
||||
# Turn off shwordsplit unless it was on previously
|
||||
if [[ $shell == "zsh" ]] && [ -z "$SHWORDSPLIT_ON" ]; then unsetopt shwordsplit; fi
|
||||
}
|
||||
|
||||
# Setup aliases
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 "$@")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user