t3206: test_when_finished before dirtying operations, not after

Many existing tests in this script perform operation(s) and then use test_when_finished to define how to undo the effect of the operation(s). This is backwards. When your operation(s) fail before you manage to successfully call test_when_finished (remember, that these commands must be all &&-chained, so a failure of an earlier operation mean your test_when_finished may not be executed at all). You must establish how to clean up your mess with test_when_finished before you create the mess to be cleaned up. Also make sure that the body of test_when_finished deals with case where the cruft it wants to remove failed to be created, by using "rm -f" (instead of "rm") to remove potential cruft files, and having "|| :" after "git notes remove" to remove potential cruft notes---both of these by default fail when asked to remove something that does not exist, instead of being silently idempotent no-ops. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 6, 2024 at 10:04 UTC 6caa96c2049a8f201a48c6890dbdbbe6b61ff06d
1 file changed +26 -26
t/t3206-range-diff.sh
+26 -26
@@ -533,9 +533,9 @@ test_expect_success 'dual-coloring' '
533 for prev in topic main..topic
534 do
535 test_expect_success "format-patch --range-diff=$prev" '
536 + test_when_finished "rm -f 000?-*" &&
537 git format-patch --cover-letter --range-diff=$prev \
538 main..unmodified >actual &&
538 - test_when_finished "rm 000?-*" &&
539 test_line_count = 5 actual &&
540 test_grep "^Range-diff:$" 0000-* &&
541 grep "= 1: .* s/5/A" 0000-* &&
@@ -560,32 +560,32 @@ test_expect_success "explicit --no-cover-letter defeats implied --cover-letter"
560 '
561
562 test_expect_success 'format-patch --range-diff as commentary' '
563 + test_when_finished "rm -f 0001-*" &&
564 git format-patch --range-diff=HEAD~1 HEAD~1 >actual &&
564 - test_when_finished "rm 0001-*" &&
565 test_line_count = 1 actual &&
566 test_grep "^Range-diff:$" 0001-* &&
567 grep "> 1: .* new message" 0001-*
568 '
569
570 test_expect_success 'format-patch --range-diff reroll-count with a non-integer' '
571 + test_when_finished "rm -f v2.9-0001-*" &&
572 git format-patch --range-diff=HEAD~1 -v2.9 HEAD~1 >actual &&
572 - test_when_finished "rm v2.9-0001-*" &&
573 test_line_count = 1 actual &&
574 test_grep "^Range-diff:$" v2.9-0001-* &&
575 grep "> 1: .* new message" v2.9-0001-*
576 '
577
578 test_expect_success 'format-patch --range-diff reroll-count with a integer' '
579 + test_when_finished "rm -f v2-0001-*" &&
580 git format-patch --range-diff=HEAD~1 -v2 HEAD~1 >actual &&
580 - test_when_finished "rm v2-0001-*" &&
581 test_line_count = 1 actual &&
582 test_grep "^Range-diff ..* v1:$" v2-0001-* &&
583 grep "> 1: .* new message" v2-0001-*
584 '
585
586 test_expect_success 'format-patch --range-diff with v0' '
587 + test_when_finished "rm -f v0-0001-*" &&
588 git format-patch --range-diff=HEAD~1 -v0 HEAD~1 >actual &&
588 - test_when_finished "rm v0-0001-*" &&
589 test_line_count = 1 actual &&
590 test_grep "^Range-diff:$" v0-0001-* &&
591 grep "> 1: .* new message" v0-0001-*
@@ -606,9 +606,9 @@ test_expect_success 'basic with modified format.pretty without "commit "' '
606 '
607
608 test_expect_success 'range-diff compares notes by default' '
609 + test_when_finished "git notes remove topic unmodified || :" &&
610 git notes add -m "topic note" topic &&
611 git notes add -m "unmodified note" unmodified &&
611 - test_when_finished git notes remove topic unmodified &&
612 git range-diff --no-color main..topic main..unmodified \
613 >actual &&
614 sed s/Z/\ /g >expect <<-EOF &&
@@ -630,9 +630,9 @@ test_expect_success 'range-diff compares notes by default' '
630 '
631
632 test_expect_success 'range-diff with --no-notes' '
633 + test_when_finished "git notes remove topic unmodified || :" &&
634 git notes add -m "topic note" topic &&
635 git notes add -m "unmodified note" unmodified &&
635 - test_when_finished git notes remove topic unmodified &&
636 git range-diff --no-color --no-notes main..topic main..unmodified \
637 >actual &&
638 cat >expect <<-EOF &&
@@ -645,12 +645,12 @@ test_expect_success 'range-diff with --no-notes' '
645 '
646
647 test_expect_success 'range-diff with multiple --notes' '
648 + test_when_finished "git notes --ref=note1 remove topic unmodified || :" &&
649 git notes --ref=note1 add -m "topic note1" topic &&
650 git notes --ref=note1 add -m "unmodified note1" unmodified &&
650 - test_when_finished git notes --ref=note1 remove topic unmodified &&
651 + test_when_finished "git notes --ref=note2 remove topic unmodified || :" &&
652 git notes --ref=note2 add -m "topic note2" topic &&
653 git notes --ref=note2 add -m "unmodified note2" unmodified &&
653 - test_when_finished git notes --ref=note2 remove topic unmodified &&
654 git range-diff --no-color --notes=note1 --notes=note2 main..topic main..unmodified \
655 >actual &&
656 sed s/Z/\ /g >expect <<-EOF &&
@@ -678,12 +678,12 @@ test_expect_success 'range-diff with multiple --notes' '
678
679 # `range-diff` should act like `log` with regards to notes
680 test_expect_success 'range-diff with --notes=custom does not show default notes' '
681 + test_when_finished "git notes remove topic unmodified || :" &&
682 git notes add -m "topic note" topic &&
683 git notes add -m "unmodified note" unmodified &&
684 + test_when_finished "git notes --ref=custom remove topic unmodified || :" &&
685 git notes --ref=custom add -m "topic note" topic &&
686 git notes --ref=custom add -m "unmodified note" unmodified &&
685 - test_when_finished git notes remove topic unmodified &&
686 - test_when_finished git notes --ref=custom remove topic unmodified &&
687 git range-diff --notes=custom main..topic main..unmodified \
688 >actual &&
689 ! grep "## Notes ##" actual &&
@@ -691,12 +691,12 @@ test_expect_success 'range-diff with --notes=custom does not show default notes'
691 '
692
693 test_expect_success 'format-patch --range-diff does not compare notes by default' '
694 + test_when_finished "git notes remove topic unmodified || :" &&
695 git notes add -m "topic note" topic &&
696 git notes add -m "unmodified note" unmodified &&
696 - test_when_finished git notes remove topic unmodified &&
697 + test_when_finished "rm -f 000?-*" &&
698 git format-patch --cover-letter --range-diff=$prev \
699 main..unmodified >actual &&
699 - test_when_finished "rm 000?-*" &&
700 test_line_count = 5 actual &&
701 test_grep "^Range-diff:$" 0000-* &&
702 grep "= 1: .* s/5/A" 0000-* &&
@@ -708,26 +708,26 @@ test_expect_success 'format-patch --range-diff does not compare notes by default
708 '
709
710 test_expect_success 'format-patch --notes=custom --range-diff only compares custom notes' '
711 + test_when_finished "git notes remove topic unmodified || :" &&
712 git notes add -m "topic note" topic &&
712 - git notes --ref=custom add -m "topic note (custom)" topic &&
713 git notes add -m "unmodified note" unmodified &&
714 + test_when_finished "git notes --ref=custom remove topic unmodified || :" &&
715 + git notes --ref=custom add -m "topic note (custom)" topic &&
716 git notes --ref=custom add -m "unmodified note (custom)" unmodified &&
715 - test_when_finished git notes remove topic unmodified &&
716 - test_when_finished git notes --ref=custom remove topic unmodified &&
717 + test_when_finished "rm -f 000?-*" &&
718 git format-patch --notes=custom --cover-letter --range-diff=$prev \
719 main..unmodified >actual &&
719 - test_when_finished "rm 000?-*" &&
720 grep "## Notes (custom) ##" 0000-* &&
721 ! grep "## Notes ##" 0000-*
722 '
723
724 test_expect_success 'format-patch --range-diff with --no-notes' '
725 + test_when_finished "git notes remove topic unmodified || :" &&
726 git notes add -m "topic note" topic &&
727 git notes add -m "unmodified note" unmodified &&
727 - test_when_finished git notes remove topic unmodified &&
728 + test_when_finished "rm -f 000?-*" &&
729 git format-patch --no-notes --cover-letter --range-diff=$prev \
730 main..unmodified >actual &&
730 - test_when_finished "rm 000?-*" &&
731 test_line_count = 5 actual &&
732 test_grep "^Range-diff:$" 0000-* &&
733 grep "= 1: .* s/5/A" 0000-* &&
@@ -739,12 +739,12 @@ test_expect_success 'format-patch --range-diff with --no-notes' '
739 '
740
741 test_expect_success 'format-patch --range-diff with --notes' '
742 + test_when_finished "git notes remove topic unmodified || :" &&
743 git notes add -m "topic note" topic &&
744 git notes add -m "unmodified note" unmodified &&
744 - test_when_finished git notes remove topic unmodified &&
745 + test_when_finished "rm -f 000?-*" &&
746 git format-patch --notes --cover-letter --range-diff=$prev \
747 main..unmodified >actual &&
747 - test_when_finished "rm 000?-*" &&
748 test_line_count = 5 actual &&
749 test_grep "^Range-diff:$" 0000-* &&
750 grep "= 1: .* s/5/A" 0000-* &&
@@ -767,13 +767,13 @@ test_expect_success 'format-patch --range-diff with --notes' '
767 '
768
769 test_expect_success 'format-patch --range-diff with format.notes config' '
770 + test_when_finished "git notes remove topic unmodified || :" &&
771 git notes add -m "topic note" topic &&
772 git notes add -m "unmodified note" unmodified &&
772 - test_when_finished git notes remove topic unmodified &&
773 test_config format.notes true &&
774 + test_when_finished "rm -f 000?-*" &&
775 git format-patch --cover-letter --range-diff=$prev \
776 main..unmodified >actual &&
776 - test_when_finished "rm 000?-*" &&
777 test_line_count = 5 actual &&
778 test_grep "^Range-diff:$" 0000-* &&
779 grep "= 1: .* s/5/A" 0000-* &&
@@ -796,15 +796,15 @@ test_expect_success 'format-patch --range-diff with format.notes config' '
796 '
797
798 test_expect_success 'format-patch --range-diff with multiple notes' '
799 + test_when_finished "git notes --ref=note1 remove topic unmodified || :" &&
800 git notes --ref=note1 add -m "topic note1" topic &&
801 git notes --ref=note1 add -m "unmodified note1" unmodified &&
801 - test_when_finished git notes --ref=note1 remove topic unmodified &&
802 + test_when_finished "git notes --ref=note2 remove topic unmodified || :" &&
803 git notes --ref=note2 add -m "topic note2" topic &&
804 git notes --ref=note2 add -m "unmodified note2" unmodified &&
804 - test_when_finished git notes --ref=note2 remove topic unmodified &&
805 + test_when_finished "rm -f 000?-*" &&
806 git format-patch --notes=note1 --notes=note2 --cover-letter --range-diff=$prev \
807 main..unmodified >actual &&
807 - test_when_finished "rm 000?-*" &&
808 test_line_count = 5 actual &&
809 test_grep "^Range-diff:$" 0000-* &&
810 grep "= 1: .* s/5/A" 0000-* &&