t7800: modernize tests

Eliminate a lot of redundant work by using test_config().
Catch more return codes by more use of temporary files
and test_cmp.

The original tests relied upon restore_test_defaults()
from the previous test to provide the next test with a sane
environment.  Make the tests do their own setup so that they
are not dependent on the success of the previous test.
The end result is shorter tests and better test isolation.

Signed-off-by: David Aguilar <davvid@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
David Aguilar 2013-02-20 20:03:47 -08:00 committed by Junio C Hamano
parent 9e5a86f204
commit e42360c48e
1 changed files with 168 additions and 198 deletions

View File

@ -10,43 +10,25 @@ Testing basic diff tool invocation


. ./test-lib.sh . ./test-lib.sh


remove_config_vars() difftool_test_setup ()
{ {
# Unset all config variables used by git-difftool test_config diff.tool test-tool &&
git config --unset diff.tool test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
git config --unset diff.guitool test_config difftool.bogus-tool.cmd false
git config --unset difftool.test-tool.cmd
git config --unset difftool.prompt
git config --unset merge.tool
git config --unset mergetool.test-tool.cmd
git config --unset mergetool.prompt
return 0
} }


restore_test_defaults() prompt_given ()
{
# Restores the test defaults used by several tests
remove_config_vars
unset GIT_DIFF_TOOL
unset GIT_DIFFTOOL_PROMPT
unset GIT_DIFFTOOL_NO_PROMPT
git config diff.tool test-tool &&
git config difftool.test-tool.cmd 'cat $LOCAL'
git config difftool.bogus-tool.cmd false
}

prompt_given()
{ {
prompt="$1" prompt="$1"
test "$prompt" = "Launch 'test-tool' [Y/n]: branch" test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
} }


stdin_contains() stdin_contains ()
{ {
grep >/dev/null "$1" grep >/dev/null "$1"
} }


stdin_doesnot_contain() stdin_doesnot_contain ()
{ {
! stdin_contains "$1" ! stdin_contains "$1"
} }
@ -65,249 +47,237 @@ test_expect_success PERL 'setup' '


# Configure a custom difftool.<tool>.cmd and use it # Configure a custom difftool.<tool>.cmd and use it
test_expect_success PERL 'custom commands' ' test_expect_success PERL 'custom commands' '
restore_test_defaults && difftool_test_setup &&
git config difftool.test-tool.cmd "cat \$REMOTE" && test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
echo master >expect &&
git difftool --no-prompt branch >actual &&
test_cmp expect actual &&


diff=$(git difftool --no-prompt branch) && test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
test "$diff" = "master" && echo branch >expect &&

git difftool --no-prompt branch >actual &&
restore_test_defaults && test_cmp expect actual
diff=$(git difftool --no-prompt branch) &&
test "$diff" = "branch"
' '


# Ensures that a custom difftool.<tool>.cmd overrides built-ins test_expect_success PERL 'custom tool commands override built-ins' '
test_expect_success PERL 'custom commands override built-ins' ' test_config difftool.defaults.cmd "cat \"\$REMOTE\"" &&
restore_test_defaults && echo master >expect &&
git config difftool.defaults.cmd "cat \$REMOTE" && git difftool --tool defaults --no-prompt branch >actual &&

test_cmp expect actual
diff=$(git difftool --tool defaults --no-prompt branch) &&
test "$diff" = "master" &&

git config --unset difftool.defaults.cmd
' '


# Ensures that git-difftool ignores bogus --tool values
test_expect_success PERL 'difftool ignores bad --tool values' ' test_expect_success PERL 'difftool ignores bad --tool values' '
diff=$(git difftool --no-prompt --tool=bad-tool branch) : >expect &&
test "$?" = 1 && test_expect_code 1 \
test "$diff" = "" git difftool --no-prompt --tool=bad-tool branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool forwards arguments to diff' ' test_expect_success PERL 'difftool forwards arguments to diff' '
difftool_test_setup &&
>for-diff && >for-diff &&
git add for-diff && git add for-diff &&
echo changes>for-diff && echo changes>for-diff &&
git add for-diff && git add for-diff &&
diff=$(git difftool --cached --no-prompt -- for-diff) && : >expect &&
test "$diff" = "" && git difftool --cached --no-prompt -- for-diff >actual &&
test_cmp expect actual &&
git reset -- for-diff && git reset -- for-diff &&
rm for-diff rm for-diff
' '


test_expect_success PERL 'difftool honors --gui' ' test_expect_success PERL 'difftool honors --gui' '
git config merge.tool bogus-tool && difftool_test_setup &&
git config diff.tool bogus-tool && test_config merge.tool bogus-tool &&
git config diff.guitool test-tool && test_config diff.tool bogus-tool &&
test_config diff.guitool test-tool &&


diff=$(git difftool --no-prompt --gui branch) && echo branch >expect &&
test "$diff" = "branch" && git difftool --no-prompt --gui branch >actual &&

test_cmp expect actual
restore_test_defaults
' '


test_expect_success PERL 'difftool --gui last setting wins' ' test_expect_success PERL 'difftool --gui last setting wins' '
git config diff.guitool bogus-tool && difftool_test_setup &&
git difftool --no-prompt --gui --no-gui && : >expect &&
git difftool --no-prompt --gui --no-gui >actual &&
test_cmp expect actual &&


git config merge.tool bogus-tool && test_config merge.tool bogus-tool &&
git config diff.tool bogus-tool && test_config diff.tool bogus-tool &&
git config diff.guitool test-tool && test_config diff.guitool test-tool &&
diff=$(git difftool --no-prompt --no-gui --gui branch) && echo branch >expect &&
test "$diff" = "branch" && git difftool --no-prompt --no-gui --gui branch >actual &&

test_cmp expect actual
restore_test_defaults
' '


test_expect_success PERL 'difftool --gui works without configured diff.guitool' ' test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
git config diff.tool test-tool && difftool_test_setup &&

echo branch >expect &&
diff=$(git difftool --no-prompt --gui branch) && git difftool --no-prompt --gui branch >actual &&
test "$diff" = "branch" && test_cmp expect actual

restore_test_defaults
' '


# Specify the diff tool using $GIT_DIFF_TOOL # Specify the diff tool using $GIT_DIFF_TOOL
test_expect_success PERL 'GIT_DIFF_TOOL variable' ' test_expect_success PERL 'GIT_DIFF_TOOL variable' '
test_might_fail git config --unset diff.tool && difftool_test_setup &&
GIT_DIFF_TOOL=test-tool && git config --unset diff.tool &&
export GIT_DIFF_TOOL && echo branch >expect &&

GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
diff=$(git difftool --no-prompt branch) && test_cmp expect actual
test "$diff" = "branch" &&

restore_test_defaults
' '


# Test the $GIT_*_TOOL variables and ensure # Test the $GIT_*_TOOL variables and ensure
# that $GIT_DIFF_TOOL always wins unless --tool is specified # that $GIT_DIFF_TOOL always wins unless --tool is specified
test_expect_success PERL 'GIT_DIFF_TOOL overrides' ' test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
git config diff.tool bogus-tool && difftool_test_setup &&
git config merge.tool bogus-tool && test_config diff.tool bogus-tool &&
test_config merge.tool bogus-tool &&


GIT_DIFF_TOOL=test-tool && echo branch >expect &&
export GIT_DIFF_TOOL && GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
test_cmp expect actual &&


diff=$(git difftool --no-prompt branch) && test_config diff.tool bogus-tool &&
test "$diff" = "branch" && test_config merge.tool bogus-tool &&

GIT_DIFF_TOOL=bogus-tool \
GIT_DIFF_TOOL=bogus-tool && git difftool --no-prompt --tool=test-tool branch >actual &&
export GIT_DIFF_TOOL && test_cmp expect actual

diff=$(git difftool --no-prompt --tool=test-tool branch) &&
test "$diff" = "branch" &&

restore_test_defaults
' '


# Test that we don't have to pass --no-prompt to difftool # Test that we don't have to pass --no-prompt to difftool
# when $GIT_DIFFTOOL_NO_PROMPT is true # when $GIT_DIFFTOOL_NO_PROMPT is true
test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' ' test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
GIT_DIFFTOOL_NO_PROMPT=true && difftool_test_setup &&
export GIT_DIFFTOOL_NO_PROMPT && echo branch >expect &&

GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
diff=$(git difftool branch) && test_cmp expect actual
test "$diff" = "branch" &&

restore_test_defaults
' '


# git-difftool supports the difftool.prompt variable. # git-difftool supports the difftool.prompt variable.
# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' ' test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
git config difftool.prompt false && difftool_test_setup &&
GIT_DIFFTOOL_PROMPT=true && test_config difftool.prompt false &&
export GIT_DIFFTOOL_PROMPT && echo >input &&

GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
prompt=$(echo | git difftool branch | tail -1) && prompt=$(tail -1 <output) &&
prompt_given "$prompt" && prompt_given "$prompt"

restore_test_defaults
' '


# Test that we don't have to pass --no-prompt when difftool.prompt is false # Test that we don't have to pass --no-prompt when difftool.prompt is false
test_expect_success PERL 'difftool.prompt config variable is false' ' test_expect_success PERL 'difftool.prompt config variable is false' '
git config difftool.prompt false && difftool_test_setup &&

test_config difftool.prompt false &&
diff=$(git difftool branch) && echo branch >expect &&
test "$diff" = "branch" && git difftool branch >actual &&

test_cmp expect actual
restore_test_defaults
' '


# Test that we don't have to pass --no-prompt when mergetool.prompt is false # Test that we don't have to pass --no-prompt when mergetool.prompt is false
test_expect_success PERL 'difftool merge.prompt = false' ' test_expect_success PERL 'difftool merge.prompt = false' '
difftool_test_setup &&
test_might_fail git config --unset difftool.prompt && test_might_fail git config --unset difftool.prompt &&
git config mergetool.prompt false && test_config mergetool.prompt false &&

echo branch >expect &&
diff=$(git difftool branch) && git difftool branch >actual &&
test "$diff" = "branch" && test_cmp expect actual

restore_test_defaults
' '


# Test that the -y flag can override difftool.prompt = true # Test that the -y flag can override difftool.prompt = true
test_expect_success PERL 'difftool.prompt can overridden with -y' ' test_expect_success PERL 'difftool.prompt can overridden with -y' '
git config difftool.prompt true && difftool_test_setup &&

test_config difftool.prompt true &&
diff=$(git difftool -y branch) && echo branch >expect &&
test "$diff" = "branch" && git difftool -y branch >actual &&

test_cmp expect actual
restore_test_defaults
' '


# Test that the --prompt flag can override difftool.prompt = false # Test that the --prompt flag can override difftool.prompt = false
test_expect_success PERL 'difftool.prompt can overridden with --prompt' ' test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
git config difftool.prompt false && difftool_test_setup &&

test_config difftool.prompt false &&
prompt=$(echo | git difftool --prompt branch | tail -1) && echo >input &&
prompt_given "$prompt" && git difftool --prompt branch <input >output &&

prompt=$(tail -1 <output) &&
restore_test_defaults prompt_given "$prompt"
' '


# Test that the last flag passed on the command-line wins # Test that the last flag passed on the command-line wins
test_expect_success PERL 'difftool last flag wins' ' test_expect_success PERL 'difftool last flag wins' '
diff=$(git difftool --prompt --no-prompt branch) && difftool_test_setup &&
test "$diff" = "branch" && echo branch >expect &&

git difftool --prompt --no-prompt branch >actual &&
restore_test_defaults && test_cmp expect actual &&

echo >input &&
prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) && git difftool --no-prompt --prompt branch <input >output &&
prompt_given "$prompt" && prompt=$(tail -1 <output) &&

prompt_given "$prompt"
restore_test_defaults
' '


# git-difftool falls back to git-mergetool config variables # git-difftool falls back to git-mergetool config variables
# so test that behavior here # so test that behavior here
test_expect_success PERL 'difftool + mergetool config variables' ' test_expect_success PERL 'difftool + mergetool config variables' '
remove_config_vars && test_config merge.tool test-tool &&
git config merge.tool test-tool && test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
git config mergetool.test-tool.cmd "cat \$LOCAL" && echo branch >expect &&

git difftool --no-prompt branch >actual &&
diff=$(git difftool --no-prompt branch) && test_cmp expect actual &&
test "$diff" = "branch" &&


# set merge.tool to something bogus, diff.tool to test-tool # set merge.tool to something bogus, diff.tool to test-tool
git config merge.tool bogus-tool && test_config merge.tool bogus-tool &&
git config diff.tool test-tool && test_config diff.tool test-tool &&

git difftool --no-prompt branch >actual &&
diff=$(git difftool --no-prompt branch) && test_cmp expect actual
test "$diff" = "branch" &&

restore_test_defaults
' '


test_expect_success PERL 'difftool.<tool>.path' ' test_expect_success PERL 'difftool.<tool>.path' '
git config difftool.tkdiff.path echo && test_config difftool.tkdiff.path echo &&
diff=$(git difftool --tool=tkdiff --no-prompt branch) && git difftool --tool=tkdiff --no-prompt branch >output &&
git config --unset difftool.tkdiff.path && lines=$(grep file output | wc -l) &&
lines=$(echo "$diff" | grep file | wc -l) && test "$lines" -eq 1
test "$lines" -eq 1 &&

restore_test_defaults
' '


test_expect_success PERL 'difftool --extcmd=cat' ' test_expect_success PERL 'difftool --extcmd=cat' '
diff=$(git difftool --no-prompt --extcmd=cat branch) && echo branch >expect &&
test "$diff" = branch"$LF"master echo master >>expect &&
git difftool --no-prompt --extcmd=cat branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool --extcmd cat' ' test_expect_success PERL 'difftool --extcmd cat' '
diff=$(git difftool --no-prompt --extcmd cat branch) && echo branch >expect &&
test "$diff" = branch"$LF"master echo master >>expect &&
git difftool --no-prompt --extcmd=cat branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool -x cat' ' test_expect_success PERL 'difftool -x cat' '
diff=$(git difftool --no-prompt -x cat branch) && echo branch >expect &&
test "$diff" = branch"$LF"master echo master >>expect &&
git difftool --no-prompt -x cat branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool --extcmd echo arg1' ' test_expect_success PERL 'difftool --extcmd echo arg1' '
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) && echo file >expect &&
test "$diff" = file git difftool --no-prompt \
--extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool --extcmd cat arg1' ' test_expect_success PERL 'difftool --extcmd cat arg1' '
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) && echo master >expect &&
test "$diff" = master git difftool --no-prompt \
--extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
test_cmp expect actual
' '


test_expect_success PERL 'difftool --extcmd cat arg2' ' test_expect_success PERL 'difftool --extcmd cat arg2' '
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) && echo branch >expect &&
test "$diff" = branch git difftool --no-prompt \
--extcmd sh\ -c\ \"cat\ \$2\" branch >actual &&
test_cmp expect actual
' '


# Create a second file on master and a different version on branch # Create a second file on master and a different version on branch
@ -324,26 +294,26 @@ test_expect_success PERL 'setup with 2 files different' '
' '


test_expect_success PERL 'say no to the first file' ' test_expect_success PERL 'say no to the first file' '
diff=$( (echo n; echo) | git difftool -x cat branch ) && (echo n && echo) >input &&

git difftool -x cat branch <input >output &&
echo "$diff" | stdin_contains m2 && stdin_contains m2 <output &&
echo "$diff" | stdin_contains br2 && stdin_contains br2 <output &&
echo "$diff" | stdin_doesnot_contain master && stdin_doesnot_contain master <output &&
echo "$diff" | stdin_doesnot_contain branch stdin_doesnot_contain branch <output
' '


test_expect_success PERL 'say no to the second file' ' test_expect_success PERL 'say no to the second file' '
diff=$( (echo; echo n) | git difftool -x cat branch ) && (echo && echo n) >input &&

git difftool -x cat branch <input >output &&
echo "$diff" | stdin_contains master && stdin_contains master <output &&
echo "$diff" | stdin_contains branch && stdin_contains branch <output &&
echo "$diff" | stdin_doesnot_contain m2 && stdin_doesnot_contain m2 <output &&
echo "$diff" | stdin_doesnot_contain br2 stdin_doesnot_contain br2 <output
' '


test_expect_success PERL 'difftool --tool-help' ' test_expect_success PERL 'difftool --tool-help' '
tool_help=$(git difftool --tool-help) && git difftool --tool-help >output &&
echo "$tool_help" | stdin_contains tool stdin_contains tool <output
' '


test_expect_success PERL 'setup change in subdirectory' ' test_expect_success PERL 'setup change in subdirectory' '
@ -359,29 +329,29 @@ test_expect_success PERL 'setup change in subdirectory' '
' '


test_expect_success PERL 'difftool -d' ' test_expect_success PERL 'difftool -d' '
diff=$(git difftool -d --extcmd ls branch) && git difftool -d --extcmd ls branch >output &&
echo "$diff" | stdin_contains sub && stdin_contains sub <output &&
echo "$diff" | stdin_contains file stdin_contains file <output
' '


test_expect_success PERL 'difftool --dir-diff' ' test_expect_success PERL 'difftool --dir-diff' '
diff=$(git difftool --dir-diff --extcmd ls branch) && git difftool --dir-diff --extcmd ls branch >output &&
echo "$diff" | stdin_contains sub && stdin_contains sub <output &&
echo "$diff" | stdin_contains file stdin_contains file <output
' '


test_expect_success PERL 'difftool --dir-diff ignores --prompt' ' test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
diff=$(git difftool --dir-diff --prompt --extcmd ls branch) && git difftool --dir-diff --prompt --extcmd ls branch >output &&
echo "$diff" | stdin_contains sub && stdin_contains sub <output &&
echo "$diff" | stdin_contains file stdin_contains file <output
' '


test_expect_success PERL 'difftool --dir-diff from subdirectory' ' test_expect_success PERL 'difftool --dir-diff from subdirectory' '
( (
cd sub && cd sub &&
diff=$(git difftool --dir-diff --extcmd ls branch) && git difftool --dir-diff --extcmd ls branch >output &&
echo "$diff" | stdin_contains sub && stdin_contains sub <output &&
echo "$diff" | stdin_contains file stdin_contains file <output
) )
' '