[zsh] fix missing history

This commit is contained in:
Will Owens
2024-02-23 05:25:15 -05:00
parent 953e79dd70
commit 2a05bcc79f

View File

@@ -7,10 +7,15 @@
# #
# Unit tests for git shell scripts # Unit tests for git shell scripts
export scmbDir="$( cd -P "$( dirname "$0" )" && pwd )/../../.." export scmbDir="$(cd -P "$(dirname "$0")" && pwd)/../../.."
# Zsh compatibility # Zsh compatibility
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
setopt append_history
fi
# Load test helpers # Load test helpers
source "$scmbDir/test/support/test_helper.sh" source "$scmbDir/test/support/test_helper.sh"
@@ -19,7 +24,6 @@ source "$scmbDir/test/support/test_helper.sh"
source "$scmbDir/lib/scm_breeze.sh" source "$scmbDir/lib/scm_breeze.sh"
source "$scmbDir/lib/git/status_shortcuts.sh" source "$scmbDir/lib/git/status_shortcuts.sh"
# Setup and tear down # Setup and tear down
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
oneTimeSetUp() { oneTimeSetUp() {
@@ -40,10 +44,9 @@ setupTestRepo() {
rm -rf "${testRepo}" rm -rf "${testRepo}"
mkdir -p "$testRepo" mkdir -p "$testRepo"
cd "$testRepo" cd "$testRepo"
git init > /dev/null git init >/dev/null
} }
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Unit tests # Unit tests
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
@@ -52,19 +55,37 @@ test_scmb_expand_args() {
local e1="one" e2="two" e3="three" e4="four" e5="five" e6="six" e7='$dollar' e8='two words' local e1="one" e2="two" e3="three" e4="four" e5="five" e6="six" e7='$dollar' e8='two words'
local error="Args not expanded correctly" local error="Args not expanded correctly"
assertEquals "$error" 'one three six' \ assertEquals "$error" 'one three six' \
"$(eval args="$(scmb_expand_args 1 3 6)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 1 3 6)"
token_quote "${args[@]}"
)"
assertEquals "$error" 'one two three five' \ assertEquals "$error" 'one two three five' \
"$(eval args="$(scmb_expand_args 1-3 5)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 1-3 5)"
token_quote "${args[@]}"
)"
assertEquals "$error" '\$dollar two three four one' \ assertEquals "$error" '\$dollar two three four one' \
"$(eval args="$(scmb_expand_args 7 2-4 1)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 7 2-4 1)"
token_quote "${args[@]}"
)"
# Test that any args with spaces remain quoted # Test that any args with spaces remain quoted
assertEquals "$error" '-m Test\ Commit\ Message one' \ assertEquals "$error" '-m Test\ Commit\ Message one' \
"$(eval args="$(scmb_expand_args -m "Test Commit Message" 1)"; token_quote "${args[@]}")" "$(
assertEquals "$error" '-ma Test\ Commit\ Message Unquoted'\ eval args="$(scmb_expand_args -m "Test Commit Message" 1)"
"$(eval args="$(scmb_expand_args -ma "Test Commit Message" "Unquoted")"; token_quote "${args[@]}")" token_quote "${args[@]}"
)"
assertEquals "$error" '-ma Test\ Commit\ Message Unquoted' \
"$(
eval args="$(scmb_expand_args -ma "Test Commit Message" "Unquoted")"
token_quote "${args[@]}"
)"
assertEquals "$error" '\$dollar one two\ words' \ assertEquals "$error" '\$dollar one two\ words' \
"$(eval args="$(scmb_expand_args 7 1-1 8)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 7 1-1 8)"
token_quote "${args[@]}"
)"
# Keep this code for use when minimum versions of {ba,z}sh can be increased. # Keep this code for use when minimum versions of {ba,z}sh can be increased.
# See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260 # See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260
@@ -89,14 +110,23 @@ test_scmb_expand_args() {
test_exec_scmb_expand_args() { test_exec_scmb_expand_args() {
local e1="one" e2="a b c" e3='$dollar' e4="single'quote" e5='double"quote' e6='a(){:;};a&' local e1="one" e2="a b c" e3='$dollar' e4="single'quote" e5='double"quote' e6='a(){:;};a&'
assertEquals "literals with spaces not preserved" 'foo bar\ baz' \ assertEquals "literals with spaces not preserved" 'foo bar\ baz' \
"$(eval args="$(scmb_expand_args foo 'bar baz')"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args foo 'bar baz')"
token_quote "${args[@]}"
)"
assertEquals "variables with spaces not preserved" 'one a\ b\ c' \ assertEquals "variables with spaces not preserved" 'one a\ b\ c' \
"$(eval args="$(scmb_expand_args 1-2)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 1-2)"
token_quote "${args[@]}"
)"
# Expecting text: '$dollar' "single'quote" 'double"quote' # Expecting text: '$dollar' "single'quote" 'double"quote'
# Generate quoted expected string with: token_quote "$(cat)" then copy/paste, ^D # Generate quoted expected string with: token_quote "$(cat)" then copy/paste, ^D
assertEquals "special characters are preserved" \ assertEquals "special characters are preserved" \
'\$dollar single\'\''quote double\"quote a\(\)\{:\;\}\;a\&' \ '\$dollar single\'\''quote double\"quote a\(\)\{:\;\}\;a\&' \
"$(eval args="$(scmb_expand_args 3-6)"; token_quote "${args[@]}")" "$(
eval args="$(scmb_expand_args 3-6)"
token_quote "${args[@]}"
)"
# Keep this code for use when minimum versions of {ba,z}sh can be increased. # Keep this code for use when minimum versions of {ba,z}sh can be increased.
# See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260 # See token_quote() source and https://github.com/scmbreeze/scm_breeze/issues/260
@@ -113,8 +143,8 @@ test_exec_scmb_expand_args() {
} }
test_command_wrapping_escapes_special_characters() { test_command_wrapping_escapes_special_characters() {
assertEquals 'should escape | the pipe' "$(exec_scmb_expand_args echo should escape '|' the pipe)" assertEquals 'should escape | the pipe' "$(exec_scmb_expand_args echo should escape '|' the pipe)"
assertEquals 'should escape ; the semicolon' "$(exec_scmb_expand_args echo should escape ';' the semicolon)" assertEquals 'should escape ; the semicolon' "$(exec_scmb_expand_args echo should escape ';' the semicolon)"
} }
test_git_status_shortcuts() { test_git_status_shortcuts() {
@@ -129,7 +159,7 @@ test_git_status_shortcuts() {
touch new_file touch new_file
touch untracked_file touch untracked_file
git add new_file git add new_file
echo "changed" > new_file echo "changed" >new_file
rm deleted_file rm deleted_file
verboseGitCommands verboseGitCommands
@@ -153,19 +183,19 @@ test_git_status_shortcuts() {
# 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) # (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)
assertIncludes "$git_status" "new file: *\[1\] *new_file" || return assertIncludes "$git_status" "new file: *\[1\] *new_file" || return
assertIncludes "$git_status" "deleted: *\[2\] *deleted_file" || return assertIncludes "$git_status" "deleted: *\[2\] *deleted_file" || return
assertIncludes "$git_status" "modified: *\[3\] *new_file" || return assertIncludes "$git_status" "modified: *\[3\] *new_file" || return
assertIncludes "$git_status" "untracked: *\[4\] *untracked_file" || return assertIncludes "$git_status" "untracked: *\[4\] *untracked_file" || return
# Test that shortcut env variables are set with full path # Test that shortcut env variables are set with full path
local error="Env variable was not set" local error="Env variable was not set"
assertEquals "$error" "$testRepo/new_file" "$e1" || return assertEquals "$error" "$testRepo/new_file" "$e1" || return
assertEquals "$error" "$testRepo/deleted_file" "$e2" || return assertEquals "$error" "$testRepo/deleted_file" "$e2" || return
assertEquals "$error" "$testRepo/new_file" "$e3" || return assertEquals "$error" "$testRepo/new_file" "$e3" || return
assertEquals "$error" "$testRepo/untracked_file" "$e4" || return assertEquals "$error" "$testRepo/untracked_file" "$e4" || return
} }
@@ -181,28 +211,27 @@ test_git_status_produces_relative_paths() {
git add . git add .
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "dir1/sub1/subsub1/testfile" || return assertIncludes "$git_status" "dir1/sub1/subsub1/testfile" || return
cd $testRepo/dir1 cd $testRepo/dir1
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " sub1/subsub1/testfile" || return assertIncludes "$git_status" " sub1/subsub1/testfile" || return
assertIncludes "$git_status" " sub2/testfile" || return assertIncludes "$git_status" " sub2/testfile" || return
assertIncludes "$git_status" "../dir2/testfile" || return assertIncludes "$git_status" "../dir2/testfile" || return
cd $testRepo/dir1/sub1 cd $testRepo/dir1/sub1
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " subsub1/testfile" || return assertIncludes "$git_status" " subsub1/testfile" || return
assertIncludes "$git_status" " ../sub2/testfile" || return assertIncludes "$git_status" " ../sub2/testfile" || return
assertIncludes "$git_status" "../../dir2/testfile" || return assertIncludes "$git_status" "../../dir2/testfile" || return
cd $testRepo/dir1/sub1/subsub1 cd $testRepo/dir1/sub1/subsub1
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " testfile" || return assertIncludes "$git_status" " testfile" || return
assertIncludes "$git_status" " ../../sub2/testfile" || return assertIncludes "$git_status" " ../../sub2/testfile" || return
assertIncludes "$git_status" "../../../dir2/testfile" || return assertIncludes "$git_status" "../../../dir2/testfile" || return
} }
test_git_status_shortcuts_merge_conflicts() { test_git_status_shortcuts_merge_conflicts() {
setupTestRepo setupTestRepo
@@ -210,23 +239,23 @@ test_git_status_shortcuts_merge_conflicts() {
# Set up every possible merge conflict # Set up every possible merge conflict
touch both_modified both_deleted deleted_by_them deleted_by_us touch both_modified both_deleted deleted_by_them deleted_by_us
echo "renamed file needs some content" > renamed_file echo "renamed file needs some content" >renamed_file
git add both_modified both_deleted renamed_file deleted_by_them deleted_by_us git add both_modified both_deleted renamed_file deleted_by_them deleted_by_us
git commit -m "First commit" git commit -m "First commit"
git checkout -b conflict_branch git checkout -b conflict_branch
echo "added by branch" > both_added echo "added by branch" >both_added
echo "branch line" > both_modified echo "branch line" >both_modified
echo "deleted by us" > deleted_by_us echo "deleted by us" >deleted_by_us
git rm deleted_by_them both_deleted git rm deleted_by_them both_deleted
git mv renamed_file renamed_file_on_branch git mv renamed_file renamed_file_on_branch
git add both_added both_modified deleted_by_us git add both_added both_modified deleted_by_us
git commit -m "Branch commit" git commit -m "Branch commit"
git checkout master git checkout master
echo "added by master" > both_added echo "added by master" >both_added
echo "master line" > both_modified echo "master line" >both_modified
echo "deleted by them" > deleted_by_them echo "deleted by them" >deleted_by_them
git rm deleted_by_us both_deleted git rm deleted_by_us both_deleted
git mv renamed_file renamed_file_on_master git mv renamed_file renamed_file_on_master
git add both_added both_modified deleted_by_them git add both_added both_modified deleted_by_them
@@ -238,16 +267,15 @@ test_git_status_shortcuts_merge_conflicts() {
# Test output without stripped color codes # Test output without stripped color codes
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "both added: *\[[0-9]*\] *both_added" || return assertIncludes "$git_status" "both added: *\[[0-9]*\] *both_added" || return
assertIncludes "$git_status" "both modified: *\[[0-9]*\] *both_modified" || return assertIncludes "$git_status" "both modified: *\[[0-9]*\] *both_modified" || return
assertIncludes "$git_status" "deleted by them: *\[[0-9]*\] *deleted_by_them" || return assertIncludes "$git_status" "deleted by them: *\[[0-9]*\] *deleted_by_them" || return
assertIncludes "$git_status" "deleted by us: *\[[0-9]*\] *deleted_by_us" || return assertIncludes "$git_status" "deleted by us: *\[[0-9]*\] *deleted_by_us" || return
assertIncludes "$git_status" "both deleted: *\[[0-9]*\] *renamed_file" || return assertIncludes "$git_status" "both deleted: *\[[0-9]*\] *renamed_file" || return
assertIncludes "$git_status" "added by them: *\[[0-9]*\] *renamed_file_on_branch" || return assertIncludes "$git_status" "added by them: *\[[0-9]*\] *renamed_file_on_branch" || return
assertIncludes "$git_status" "added by us: *\[[0-9]*\] *renamed_file_on_master" || return assertIncludes "$git_status" "added by us: *\[[0-9]*\] *renamed_file_on_master" || return
} }
test_git_status_shortcuts_max_changes() { test_git_status_shortcuts_max_changes() {
setupTestRepo setupTestRepo
@@ -257,30 +285,29 @@ test_git_status_shortcuts_max_changes() {
touch a b c d e touch a b c d e
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
for i in {1..5}; do for i in {1..5}; do
assertIncludes "$git_status" "\[$i\]" || return assertIncludes "$git_status" "\[$i\]" || return
done done
# 6 untracked files is more than $gs_max_changes # 6 untracked files is more than $gs_max_changes
touch f touch f
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertNotIncludes "$git_status" "\[[0-9]*\]" || return assertNotIncludes "$git_status" "\[[0-9]*\]" || return
assertIncludes "$git_status" "There were more than 5 changed files." || return assertIncludes "$git_status" "There were more than 5 changed files." || return
export gs_max_changes="20" export gs_max_changes="20"
} }
test_git_add_shortcuts() { test_git_add_shortcuts() {
setupTestRepo setupTestRepo
touch a b c d e f g h i j touch a b c d e f g h i j
# Show git status, which sets up env variables # Show git status, which sets up env variables
git_status_shortcuts > /dev/null git_status_shortcuts >/dev/null
git_add_shortcuts 2-4 7 8 > /dev/null git_add_shortcuts 2-4 7 8 >/dev/null
git_status=$(git_status_shortcuts 1 | strip_colors) git_status=$(git_status_shortcuts 1 | strip_colors)
for c in b c d g h; do for c in b c d g h; do
assertIncludes "$git_status" "\[[0-9]*\] $c" || return assertIncludes "$git_status" "\[[0-9]*\] $c" || return
done done
} }
@@ -294,28 +321,39 @@ test_git_commit_prompt() {
export HISTFILESIZE=1000 export HISTFILESIZE=1000
export HISTSIZE=1000 export HISTSIZE=1000
if [[ $shell == "zsh" ]]; then
export SAVEHIST=1000
zsh_appendhistory=$( (setopt | grep -q appendhistory) && echo "true")
if [ "$zsh_appendhistory" != "true" ]; then
setopt appendhistory
trap "unsetopt appendhistory" EXIT
fi
fi
touch a b c d touch a b c d
git add . > /dev/null git add . >/dev/null
# Zsh 'vared' doesn't handle input via pipe, so replace with function that reads into commit_msg variable. # Zsh 'vared' doesn't handle input via pipe, so replace with function that reads into commit_msg variable.
function vared(){ read commit_msg; } function vared() { read commit_msg; }
# Test the git commit prompt, by piping a commit message # Test the git commit prompt, by piping a commit message
# instead of user input. # instead of user input.
echo "$commit_msg" | git_commit_prompt > /dev/null echo "$commit_msg" | git_commit_prompt >/dev/null
git_show_output=$(git show --oneline --name-only) git_show_output=$(git show --oneline --name-only)
assertIncludes "$git_show_output" "$commit_msg" assertIncludes "$git_show_output" "$commit_msg"
# Test that history was appended correctly. # Test that history was appended correctly.
if [[ $shell == "zsh" ]]; then if [[ $shell == "zsh" ]]; then
setopt
cat $HISTFILE
test_history="$(history)" test_history="$(history)"
else else
# Need to load history from $HISTFILE # Need to load history from $HISTFILE
# (Couldn't get the 'history' builtin to work during tests.) # (Couldn't get the 'history' builtin to work during tests.)
test_history="$(cat $HISTFILE)" test_history="$(cat $HISTFILE)"
fi fi
assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\"" assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\""
} }
test_git_commit_prompt_with_append() { test_git_commit_prompt_with_append() {
@@ -324,22 +362,23 @@ test_git_commit_prompt_with_append() {
commit_msg="Updating README, no build please" commit_msg="Updating README, no build please"
# Create temporary history file # Create temporary history file
HISTFILE=$(mktemp -t scm_breeze.XXXXXXXXXX) export HISTFILE=$(mktemp -t scm_breeze.XXXXXXXXXX)
HISTFILESIZE=1000 export HISTFILESIZE=1000
HISTSIZE=1000 export HISTSIZE=1000
export SAVEHIST=1000
touch a b c touch a b c
git add . > /dev/null git add . >/dev/null
# Zsh 'vared' doesn't handle input via pipe, so replace with function that reads into commit_msg variable. # Zsh 'vared' doesn't handle input via pipe, so replace with function that reads into commit_msg variable.
function vared(){ read commit_msg; } function vared() { read commit_msg; }
# Test the git commit prompt, by piping a commit message # Test the git commit prompt, by piping a commit message
# instead of user input. # instead of user input.
echo "$commit_msg" | GIT_COMMIT_MSG_SUFFIX="[ci skip]" git_commit_prompt > /dev/null echo "$commit_msg" | GIT_COMMIT_MSG_SUFFIX="[ci skip]" git_commit_prompt >/dev/null
git_show_output=$(git show --oneline --name-only) git_show_output=$(git show --oneline --name-only)
assertIncludes "$git_show_output" "$commit_msg \[ci skip\]" assertIncludes "$git_show_output" "$commit_msg \[ci skip\]"
# Test that history was appended correctly. # Test that history was appended correctly.
if [[ $shell == "zsh" ]]; then if [[ $shell == "zsh" ]]; then
@@ -347,8 +386,8 @@ test_git_commit_prompt_with_append() {
else else
test_history="$(cat $HISTFILE)" test_history="$(cat $HISTFILE)"
fi fi
assertIncludes "$test_history" "$commit_msg \[ci skip\]" assertIncludes "$test_history" "$commit_msg \[ci skip\]"
assertIncludes "$test_history" "git commit -m \"$commit_msg \[ci skip\]\"" assertIncludes "$test_history" "git commit -m \"$commit_msg \[ci skip\]\""
} }
test_adding_files_with_spaces() { test_adding_files_with_spaces() {
@@ -358,14 +397,12 @@ test_adding_files_with_spaces() {
touch "$test_file" touch "$test_file"
e1="$testRepo/$test_file" e1="$testRepo/$test_file"
git_add_shortcuts 1 > /dev/null git_add_shortcuts 1 >/dev/null
# Test that file is added by looking at git status # Test that file is added by looking at git status
git_status=$(git_status_shortcuts | strip_colors) git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "new file: \[1\] \"$test_file" assertIncludes "$git_status" "new file: \[1\] \"$test_file"
} }
# load and run shUnit2 # load and run shUnit2
source "$scmbDir/test/support/shunit2" source "$scmbDir/test/support/shunit2"