/
shell_shortcuts_test.sh
executable file
·111 lines (92 loc) · 3.81 KB
/
shell_shortcuts_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# ------------------------------------------------------------------------------
# SCM Breeze - Streamline your SCM workflow.
# Copyright 2011 Nathan Broadbent (http://madebynathan.com). All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# ------------------------------------------------------------------------------
#
# Unit tests for shell command wrapping
export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.."
# Zsh compatibility
if [ -n "${ZSH_VERSION:-}" ]; then
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 and core functions
source "$scmbDir/test/support/test_helper.sh"
source "$scmbDir/lib/scm_breeze.sh"
# Setup
#-----------------------------------------------------------------------------
oneTimeSetUp() {
export shell_command_wrapping_enabled="true"
export scmb_wrapped_shell_commands="not_found cat rm cp mv ln cd sed"
alias rvm="test" # Ensure tests run if RVM isn't loaded but $HOME/.rvm is present
# Test functions
function ln() { ln $@; }
# Test aliases
alias mv="nocorrect mv"
alias rm="rm --option"
alias sed="sed"
# Test already wrapped commands
alias cat="exec_scmb_expand_args /bin/cat"
# Run shortcut wrapping
source "$scmbDir/lib/git/shell_shortcuts.sh"
# 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/'$//"; }
}
# Helper function to test that alias is defined properly.
# (Works for both zsh and bash)
assertAliasEquals(){
assertEquals "$1" "$(whence $2)"
}
#-----------------------------------------------------------------------------
# Unit tests
#-----------------------------------------------------------------------------
test_shell_command_wrapping() {
assertAliasEquals "exec_scmb_expand_args /bin/rm --option" "rm"
assertAliasEquals "exec_scmb_expand_args nocorrect /bin/mv" "mv"
assertAliasEquals "exec_scmb_expand_args /bin/sed" "sed"
assertAliasEquals "exec_scmb_expand_args /bin/cat" "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() {
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'"
touch "a \"b\"/c"
# 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"
# Test ls with subdirectory
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
assertEquals "$TEST_DIR/a \"b\"/c" "$e1"
# Test arg with no quotes
ls_output=$(ls_with_file_shortcuts a\ \"b\" | strip_colors)
assertIncludes "$ls_output" '[1] c' F
}
# load and run shUnit2
source "$scmbDir/test/support/shunit2"