t7005: sanitize test environment for subsequent tests
Some of the editor tests manipulate the environment or config in ways that affect future tests, but those modifications are visible to future tests and create a footgun for them. Use test_config, subshells, single-command environment overrides, and test helpers to automatically undo environment and config modifications once finished. Best-viewed-with: --ignore-all-space Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
D. Ben Knoble committed
Aug 13, 2025 at 13:50 UTC
a60203a01512c7544e5e10b9e8b936a30ac4455c
1 file changed
+39
-44
t/t7005-editor.sh
+39
-44
@@ -34,61 +34,57 @@ test_expect_success setup '
34
test_commit_message HEAD -m "$msg"
35
'
36
37
-TERM=dumb
38
-export TERM
37
test_expect_success 'dumb should error out when falling back on vi' '
40
- test_must_fail git commit --amend
38
+ test_must_fail env TERM=dumb git commit --amend
39
'
40
41
test_expect_success 'dumb should prefer EDITOR to VISUAL' '
44
- EDITOR=./e-EDITOR.sh &&
45
- VISUAL=./e-VISUAL.sh &&
46
- export EDITOR VISUAL &&
47
- git commit --amend &&
42
+ TERM=dumb EDITOR=./e-EDITOR.sh VISUAL=./e-VISUAL.sh \
43
+ git commit --amend &&
44
test_commit_message HEAD -m "Edited by EDITOR"
45
'
46
51
-TERM=vt100
52
-export TERM
47
for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
48
do
55
- echo "Edited by $i" >expect
56
- unset EDITOR VISUAL GIT_EDITOR
57
- git config --unset-all core.editor
58
- case "$i" in
59
- core_editor)
60
- git config core.editor ./e-core_editor.sh
61
- ;;
62
- [A-Z]*)
63
- eval "$i=./e-$i.sh"
64
- export $i
65
- ;;
66
- esac
49
test_expect_success "Using $i" '
68
- PATH="$PWD:$PATH" git commit --amend &&
69
- test_commit_message HEAD expect
50
+ if test "$i" = core_editor
51
+ then
52
+ test_config core.editor ./e-core_editor.sh
53
+ fi &&
54
+ (
55
+ case "$i" in
56
+ [A-Z]*)
57
+ eval "$i=./e-$i.sh" &&
58
+ export $i
59
+ ;;
60
+ esac &&
61
+ PATH="$PWD:$PATH" TERM=vt100 git commit --amend
62
+ ) &&
63
+ test_commit_message HEAD -m "Edited by $i"
64
'
65
done
66
73
-unset EDITOR VISUAL GIT_EDITOR
74
-git config --unset-all core.editor
75
-for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
76
-do
77
- echo "Edited by $i" >expect
78
- case "$i" in
79
- core_editor)
80
- git config core.editor ./e-core_editor.sh
81
- ;;
82
- [A-Z]*)
83
- eval "$i=./e-$i.sh"
84
- export $i
85
- ;;
86
- esac
87
- test_expect_success "Using $i (override)" '
88
- PATH="$PWD:$PATH" git commit --amend &&
89
- test_commit_message HEAD expect
90
- '
91
-done
67
+test_expect_success 'Using editors with overrides' '
68
+ (
69
+ TERM=vt100 &&
70
+ export TERM &&
71
+ for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
72
+ do
73
+ echo "Edited by $i" >expect &&
74
+ case "$i" in
75
+ core_editor)
76
+ git config core.editor ./e-core_editor.sh
77
+ ;;
78
+ [A-Z]*)
79
+ eval "$i=./e-$i.sh" &&
80
+ export $i
81
+ ;;
82
+ esac &&
83
+ PATH="$PWD:$PATH" git commit --amend &&
84
+ test_commit_message HEAD expect || exit 1
85
+ done
86
+ )
87
+'
88
89
test_expect_success 'editor with a space' '
90
echo "echo space >\"\$1\"" >"e space.sh" &&
@@ -97,9 +93,8 @@ test_expect_success 'editor with a space' '
93
test_commit_message HEAD -m space
94
'
95
100
-unset GIT_EDITOR
96
test_expect_success 'core.editor with a space' '
102
- git config core.editor \"./e\ space.sh\" &&
97
+ test_config core.editor \"./e\ space.sh\" &&
98
git commit --amend &&
99
test_commit_message HEAD -m space
100
'