Reduce how much a test can interfere other tests:

  * Move setup code that multiple tests depend on to the 'setup' test
    case.
  * Run 'git reset --hard' after every test (pass or fail) to clean up
    in case the test fails and leaves the repository in a strange
    state.
  * If the repository must be in a particular state (beyond what is
    already done by the 'setup' test case) before the test can run,
    make the necessary repository changes in the test script even if
    it means duplicating some lines of code from the previous test
    case.
  * Never assume that a particular commit is checked out.
  * Always work on a test-specific branch when the test might create a
    commit.  This is not always necessary for correctness, but it
    improves debuggability by ensuring a commit created by test #N
    shows up on the testN branch, not the branch for test #N-1.

Signed-off-by: Richard Hansen <hans...@google.com>
---
 t/t7610-mergetool.sh | 146 +++++++++++++++++++++++++++++----------------------
 1 file changed, 84 insertions(+), 62 deletions(-)

diff --git a/t/t7610-mergetool.sh b/t/t7610-mergetool.sh
index 14090739f..2c06cf73f 100755
--- a/t/t7610-mergetool.sh
+++ b/t/t7610-mergetool.sh
@@ -55,6 +55,22 @@ test_expect_success 'setup' '
        git rm file12 &&
        git commit -m "branch1 changes" &&
 
+       git checkout -b delete-base branch1 &&
+       mkdir -p a/a &&
+       (echo one; echo two; echo 3; echo 4) >a/a/file.txt &&
+       git add a/a/file.txt &&
+       git commit -m"base file" &&
+       git checkout -b move-to-b delete-base &&
+       mkdir -p b/b &&
+       git mv a/a/file.txt b/b/file.txt &&
+       (echo one; echo two; echo 4) >b/b/file.txt &&
+       git commit -a -m"move to b" &&
+       git checkout -b move-to-c delete-base &&
+       mkdir -p c/c &&
+       git mv a/a/file.txt c/c/file.txt &&
+       (echo one; echo two; echo 3) >c/c/file.txt &&
+       git commit -a -m"move to c" &&
+
        git checkout -b stash1 master &&
        echo stash1 change file11 >file11 &&
        git add file11 &&
@@ -86,6 +102,23 @@ test_expect_success 'setup' '
        git rm file11 &&
        git commit -m "master updates" &&
 
+       git clean -fdx &&
+       git checkout -b order-file-start master &&
+       echo start >a &&
+       echo start >b &&
+       git add a b &&
+       git commit -m start &&
+       git checkout -b order-file-side1 order-file-start &&
+       echo side1 >a &&
+       echo side1 >b &&
+       git add a b &&
+       git commit -m side1 &&
+       git checkout -b order-file-side2 order-file-start &&
+       echo side2 >a &&
+       echo side2 >b &&
+       git add a b &&
+       git commit -m side2 &&
+
        git config merge.tool mytool &&
        git config mergetool.mytool.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
        git config mergetool.mytool.trustExitCode true &&
@@ -94,6 +127,7 @@ test_expect_success 'setup' '
 '
 
 test_expect_success 'custom mergetool' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        test_must_fail git merge master >/dev/null 2>&1 &&
@@ -112,6 +146,7 @@ test_expect_success 'custom mergetool' '
 '
 
 test_expect_success 'mergetool crlf' '
+       test_when_finished "git reset --hard" &&
        test_config core.autocrlf true &&
        git checkout -b test$test_count branch1 &&
        test_must_fail git merge master >/dev/null 2>&1 &&
@@ -129,11 +164,11 @@ test_expect_success 'mergetool crlf' '
        git submodule update -N &&
        test "$(cat submod/bar)" = "master submodule" &&
        git commit -m "branch1 resolved with mergetool - autocrlf" &&
-       test_config core.autocrlf false &&
-       git reset --hard
+       test_config core.autocrlf false
 '
 
 test_expect_success 'mergetool in subdir' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        (
@@ -145,8 +180,13 @@ test_expect_success 'mergetool in subdir' '
 '
 
 test_expect_success 'mergetool on file in parent dir' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count branch1 &&
+       git submodule update -N &&
        (
                cd subdir &&
+               test_must_fail git merge master >/dev/null 2>&1 &&
+               ( yes "" | git mergetool file3 >/dev/null 2>&1 ) &&
                ( yes "" | git mergetool ../file1 >/dev/null 2>&1 ) &&
                ( yes "" | git mergetool ../file2 ../spaced\ name >/dev/null 
2>&1 ) &&
                ( yes "" | git mergetool ../both >/dev/null 2>&1 ) &&
@@ -161,6 +201,7 @@ test_expect_success 'mergetool on file in parent dir' '
 '
 
 test_expect_success 'mergetool skips autoresolved' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        test_must_fail git merge master &&
@@ -169,11 +210,12 @@ test_expect_success 'mergetool skips autoresolved' '
        ( yes "d" | git mergetool file12 >/dev/null 2>&1 ) &&
        ( yes "l" | git mergetool submod >/dev/null 2>&1 ) &&
        output="$(git mergetool --no-prompt)" &&
-       test "$output" = "No files need merging" &&
-       git reset --hard
+       test "$output" = "No files need merging"
 '
 
 test_expect_success 'mergetool merges all from subdir' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count branch1 &&
        test_config rerere.enabled false &&
        (
                cd subdir &&
@@ -190,6 +232,7 @@ test_expect_success 'mergetool merges all from subdir' '
 '
 
 test_expect_success 'mergetool skips resolved paths when rerere is active' '
+       test_when_finished "git reset --hard" &&
        test_config rerere.enabled true &&
        rm -rf .git/rr-cache &&
        git checkout -b test$test_count branch1 &&
@@ -199,11 +242,11 @@ test_expect_success 'mergetool skips resolved paths when 
rerere is active' '
        ( yes "d" "d" | git mergetool --no-prompt >/dev/null 2>&1 ) &&
        git submodule update -N &&
        output="$(yes "n" | git mergetool --no-prompt)" &&
-       test "$output" = "No files need merging" &&
-       git reset --hard
+       test "$output" = "No files need merging"
 '
 
 test_expect_success 'conflicted stash sets up rerere'  '
+       test_when_finished "git reset --hard" &&
        test_config rerere.enabled true &&
        git checkout stash1 &&
        echo "Conflicting stash content" >file11 &&
@@ -231,7 +274,7 @@ test_expect_success 'conflicted stash sets up rerere'  '
 '
 
 test_expect_success 'mergetool takes partial path' '
-       git reset --hard &&
+       test_when_finished "git reset --hard" &&
        test_config rerere.enabled false &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
@@ -239,26 +282,12 @@ test_expect_success 'mergetool takes partial path' '
 
        ( yes "" | git mergetool subdir ) &&
 
-       test "$(cat subdir/file3)" = "master new sub" &&
-       git reset --hard
+       test "$(cat subdir/file3)" = "master new sub"
 '
 
 test_expect_success 'mergetool delete/delete conflict' '
-       git checkout -b delete-base branch1 &&
-       mkdir -p a/a &&
-       (echo one; echo two; echo 3; echo 4) >a/a/file.txt &&
-       git add a/a/file.txt &&
-       git commit -m"base file" &&
-       git checkout -b move-to-b delete-base &&
-       mkdir -p b/b &&
-       git mv a/a/file.txt b/b/file.txt &&
-       (echo one; echo two; echo 4) >b/b/file.txt &&
-       git commit -a -m"move to b" &&
-       git checkout -b move-to-c delete-base &&
-       mkdir -p c/c &&
-       git mv a/a/file.txt c/c/file.txt &&
-       (echo one; echo two; echo 3) >c/c/file.txt &&
-       git commit -a -m"move to c" &&
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count move-to-c &&
        test_must_fail git merge move-to-b &&
        echo d | git mergetool a/a/file.txt &&
        ! test -f a/a/file.txt &&
@@ -269,29 +298,32 @@ test_expect_success 'mergetool delete/delete conflict' '
        git reset --hard HEAD &&
        test_must_fail git merge move-to-b &&
        ! echo a | git mergetool a/a/file.txt &&
-       ! test -f a/a/file.txt &&
-       git reset --hard HEAD
+       ! test -f a/a/file.txt
 '
 
 test_expect_success 'mergetool produces no errors when keepBackup is used' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count move-to-c &&
        test_config mergetool.keepBackup true &&
        test_must_fail git merge move-to-b &&
        : >expect &&
        echo d | git mergetool a/a/file.txt 2>actual &&
        test_cmp expect actual &&
-       ! test -d a &&
-       git reset --hard HEAD
+       ! test -d a
 '
 
 test_expect_success 'mergetool honors tempfile config for deleted files' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count move-to-c &&
        test_config mergetool.keepTemporaries false &&
        test_must_fail git merge move-to-b &&
        echo d | git mergetool a/a/file.txt &&
-       ! test -d a &&
-       git reset --hard HEAD
+       ! test -d a
 '
 
 test_expect_success 'mergetool keeps tempfiles when aborting delete/delete' '
+       test_when_finished "git reset --hard; git clean -fdx" &&
+       git checkout -b test$test_count move-to-c &&
        test_config mergetool.keepTemporaries true &&
        test_must_fail git merge move-to-b &&
        ! (echo a; echo n) | git mergetool a/a/file.txt &&
@@ -302,12 +334,11 @@ test_expect_success 'mergetool keeps tempfiles when 
aborting delete/delete' '
        file_REMOTE_.txt
        EOF
        ls -1 a/a | sed -e "s/[0-9]*//g" >actual &&
-       test_cmp expect actual &&
-       git clean -fdx &&
-       git reset --hard HEAD
+       test_cmp expect actual
 '
 
 test_expect_success 'deleted vs modified submodule' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        mv submod submod-movedaside &&
@@ -372,11 +403,11 @@ test_expect_success 'deleted vs modified submodule' '
        test "$(cat submod/bar)" = "master submodule" &&
        output="$(git mergetool --no-prompt)" &&
        test "$output" = "No files need merging" &&
-       git commit -m "Merge resolved by keeping module" &&
-       git reset --hard HEAD
+       git commit -m "Merge resolved by keeping module"
 '
 
 test_expect_success 'file vs modified submodule' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        mv submod submod-movedaside &&
@@ -448,6 +479,7 @@ test_expect_success 'file vs modified submodule' '
 '
 
 test_expect_success 'submodule in subdirectory' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        git submodule update -N &&
        (
@@ -509,6 +541,7 @@ test_expect_success 'submodule in subdirectory' '
 '
 
 test_expect_success 'directory vs modified submodule' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        mv submod submod-movedaside &&
        git rm --cached submod &&
@@ -559,34 +592,34 @@ test_expect_success 'directory vs modified submodule' '
 '
 
 test_expect_success 'file with no base' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        test_must_fail git merge master &&
        git mergetool --no-prompt --tool mybase -- both &&
        >expected &&
-       test_cmp both expected &&
-       git reset --hard master >/dev/null 2>&1
+       test_cmp both expected
 '
 
 test_expect_success 'custom commands override built-ins' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        test_config mergetool.defaults.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
        test_config mergetool.defaults.trustExitCode true &&
        test_must_fail git merge master &&
        git mergetool --no-prompt --tool defaults -- both &&
        echo master both added >expected &&
-       test_cmp both expected &&
-       git reset --hard master >/dev/null 2>&1
+       test_cmp both expected
 '
 
 test_expect_success 'filenames seen by tools start with ./' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        test_config mergetool.writeToTemp false &&
        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
        test_config mergetool.myecho.trustExitCode true &&
        test_must_fail git merge master &&
        git mergetool --no-prompt --tool myecho -- both >actual &&
-       grep ^\./both_LOCAL_ actual >/dev/null &&
-       git reset --hard master >/dev/null 2>&1
+       grep ^\./both_LOCAL_ actual >/dev/null
 '
 
 test_lazy_prereq MKTEMP '
@@ -595,6 +628,7 @@ test_lazy_prereq MKTEMP '
 '
 
 test_expect_success MKTEMP 'temporary filenames are used with 
mergetool.writeToTemp' '
+       test_when_finished "git reset --hard" &&
        git checkout -b test$test_count branch1 &&
        test_config mergetool.writeToTemp true &&
        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
@@ -602,31 +636,17 @@ test_expect_success MKTEMP 'temporary filenames are used 
with mergetool.writeToT
        test_must_fail git merge master &&
        git mergetool --no-prompt --tool myecho -- both >actual &&
        test_must_fail grep ^\./both_LOCAL_ actual >/dev/null &&
-       grep /both_LOCAL_ actual >/dev/null &&
-       git reset --hard master >/dev/null 2>&1
+       grep /both_LOCAL_ actual >/dev/null
 '
 
 test_expect_success 'diff.orderFile configuration is honored' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count order-file-side2 &&
        test_config diff.orderFile order-file &&
        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
        test_config mergetool.myecho.trustExitCode true &&
        echo b >order-file &&
        echo a >>order-file &&
-       git checkout -b order-file-start master &&
-       echo start >a &&
-       echo start >b &&
-       git add a b &&
-       git commit -m start &&
-       git checkout -b order-file-side1 order-file-start &&
-       echo side1 >a &&
-       echo side1 >b &&
-       git add a b &&
-       git commit -m side1 &&
-       git checkout -b order-file-side2 order-file-start &&
-       echo side2 >a &&
-       echo side2 >b &&
-       git add a b &&
-       git commit -m side2 &&
        test_must_fail git merge order-file-side1 &&
        cat >expect <<-\EOF &&
                Merging:
@@ -635,13 +655,16 @@ test_expect_success 'diff.orderFile configuration is 
honored' '
        EOF
        git mergetool --no-prompt --tool myecho >output &&
        git grep --no-index -h -A2 Merging: output >actual &&
-       test_cmp expect actual &&
-       git reset --hard >/dev/null
+       test_cmp expect actual
 '
 test_expect_success 'mergetool -Oorder-file is honored' '
+       test_when_finished "git reset --hard" &&
+       git checkout -b test$test_count order-file-side2 &&
        test_config diff.orderFile order-file &&
        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
        test_config mergetool.myecho.trustExitCode true &&
+       echo b >order-file &&
+       echo a >>order-file &&
        test_must_fail git merge order-file-side1 &&
        cat >expect <<-\EOF &&
                Merging:
@@ -662,8 +685,7 @@ test_expect_success 'mergetool -Oorder-file is honored' '
        EOF
        git mergetool -Oorder-file --no-prompt --tool myecho >output &&
        git grep --no-index -h -A2 Merging: output >actual &&
-       test_cmp expect actual &&
-       git reset --hard >/dev/null 2>&1
+       test_cmp expect actual
 '
 
 test_done
-- 
2.11.0.390.gc69c2f50cf-goog

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to