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

@@ -1,3 +1,3 @@
script: ./test.sh script: ./run_tests.sh
before_script: before_script:
- sudo apt-get install zsh - sudo apt-get install zsh

View File

@@ -91,14 +91,20 @@ fi
# Adds numbered shortcuts to output of ls -l, just like 'git status' # 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 unalias ll > /dev/null 2>&1; unset -f ll > /dev/null 2>&1
function ls_with_file_shortcuts { function ls_with_file_shortcuts {
if [ -z $_ls_bsd ]; then local ll_output
local ll_output="$(ls -lhv --group-directories-first --color "$@")" if [ "$_ls_bsd" != "BSD" ]; then
ll_output="$(\ls -lhv --group-directories-first --color "$@")"
else 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 fi
# Parse path from args # Parse path from args
zsh_compat # Ensure sh_word_split is on
OLDIFS="$IFS"; IFS=$'\n' OLDIFS="$IFS"; IFS=$'\n'
for arg in $@; do for arg in $@; do
if [ -d "$arg" ]; then local rel_path="${arg%/}"; fi 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};\ 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]}}" 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 fi
if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then if [ "$(echo "$ll_output" | wc -l)" -gt "50" ]; then
@@ -125,7 +132,7 @@ function ls_with_file_shortcuts {
fi fi
# Use ruby to inject numbers into ls output # 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 output = STDIN.read
e = 1 e = 1
re = /^(([^ ]* +){8})/ re = /^(([^ ]* +){8})/
@@ -143,17 +150,14 @@ EOF
local file='' local file=''
if [ -z $_ls_bsd ]; then if [ -z $_ls_bsd ]; then
ll_files="$(ls -v --group-directories-first --color=never "$@")" ll_files="$(\ls -v --group-directories-first --color=never "$@")"
else else
ll_files="$(ls "$@")" ll_files="$(\ls "$@")"
fi fi
# Escape single and double quotes
ll_files=$(echo "$ll_files" | \sed -e 's/"/\\\\\\"/g' -e "s/'"'/\\\\'"'/g")
OLDIFS="$IFS"; IFS=$'\n' 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 for file in $ll_files; do
if [ -n "$rel_path" ]; then file="$rel_path/$file"; fi if [ -n "$rel_path" ]; then file="$rel_path/$file"; fi
export $git_env_char$e="$(eval $_abs_path_command \"$file\")" export $git_env_char$e="$(eval $_abs_path_command \"$file\")"
@@ -161,7 +165,9 @@ EOF
let e++ let e++
done done
IFS="$OLDIFS" 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 # Setup aliases

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 if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers # Load test helpers
source "$scmbDir/test/support/test_helper" source "$scmbDir/test/support/test_helper.sh"
# Load functions to test # Load functions to test
source "$scmbDir/lib/scm_breeze.sh" 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 if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers # Load test helpers
source "$scmbDir/test/support/test_helper" source "$scmbDir/test/support/test_helper.sh"
# Load functions to test # Load functions to test
source "$scmbDir/lib/scm_breeze.sh" source "$scmbDir/lib/scm_breeze.sh"

View File

@@ -11,31 +11,29 @@ export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
# Zsh compatibility # Zsh compatibility
if [ -n "${ZSH_VERSION:-}" ]; then if [ -n "${ZSH_VERSION:-}" ]; then
shell="zsh" shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit
SHUNIT_PARENT=$0
setopt shwordsplit
else else
# Bash needs this option so that 'alias' works in a non-interactive shell # Bash needs this option so that 'alias' works in a non-interactive shell
shopt -s expand_aliases shopt -s expand_aliases
fi fi
# Load test helpers # Load test helpers
source "$scmbDir/test/support/test_helper" source "$scmbDir/test/support/test_helper.sh"
# Setup # Setup
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
oneTimeSetUp() { oneTimeSetUp() {
export shell_command_wrapping_enabled="true" 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 # Test functions
function ls() { ls $@; } function ln() { ln $@; }
# Test aliases # Test aliases
alias mv="nocorrect mv" alias mv="nocorrect mv"
alias rm="rm --option" alias rm="rm --option"
alias sed="sed" alias sed="sed"
# Test already wrapped commands # Test already wrapped commands
alias ln="exec_scmb_expand_args /bin/ln" alias cat="exec_scmb_expand_args /bin/cat"
# Run shortcut wrapping # Run shortcut wrapping
source "$scmbDir/lib/git/shell_shortcuts.sh" 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/sed" "sed"
assertAliasEquals "exec_scmb_expand_args /bin/cat" "cat" assertAliasEquals "exec_scmb_expand_args /bin/cat" "cat"
assertAliasEquals "exec_scmb_expand_args builtin cd" "cd" assertAliasEquals "exec_scmb_expand_args builtin cd" "cd"
assertAliasEquals "exec_scmb_expand_args __original_ls" "ls" assertAliasEquals "exec_scmb_expand_args __original_ln" "ln"
assertAliasEquals "exec_scmb_expand_args /bin/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 # load and run shUnit2
source "$scmbDir/test/support/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 if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers # Load test helpers
source "$scmbDir/test/support/test_helper" source "$scmbDir/test/support/test_helper.sh"
# Load functions to test # Load functions to test
source "$scmbDir/lib/scm_breeze.sh" source "$scmbDir/lib/scm_breeze.sh"
@@ -95,6 +95,7 @@ test_git_status_shortcuts() {
assertNotIncludes "$git_status3" "Untracked files" assertNotIncludes "$git_status3" "Untracked files"
# Run command in shell, load output from temp file into variable # 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) temp_file=$(mktemp -t scm_breeze.XXXXXXXXXX)
git_status_shortcuts > $temp_file git_status_shortcuts > $temp_file
git_status=$(<$temp_file strip_colors) git_status=$(<$temp_file strip_colors)

View File

@@ -24,18 +24,20 @@ verboseGitCommands() {
unset -f git unset -f git
} }
# Asserts # 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 # assert $1 contains $2
assertIncludes() { assertIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi assertTrue "'$1' should have contained '$2'" $(_includes "$@")
assertTrue "'$1' should have contained '$2'" $result
} }
# assert $1 does not contain $2 # assert $1 does not contain $2
assertNotIncludes() { assertNotIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi assertFalse "'$1' should not have contained '$2'" $(_includes "$@")
assertFalse "'$1' should not have contained '$2'" $result
} }