t7001: avoid using `cd` outside of subshells

Avoid using `cd` outside of subshells since, if the test fails, there is no guarantee that the current working directory is the expected one, which may cause subsequent tests to run in the wrong directory. While at it, make some other tests more concise by replacing simple subshells with `git -C`. Signed-off-by: Shubham Verma <shubhunic@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shubham Verma committed Feb 12, 2021 at 01:17 UTC 368d2782499af9fbe9db73aed2ffbba53772374a
1 file changed +11 -32
t/t7001-mv.sh
+11 -32
@@ -11,12 +11,12 @@ test_expect_success 'prepare reference tree' '
11 '
12
13 test_expect_success 'moving the file out of subdirectory' '
14 - cd path0 && git mv COPYING ../path1/COPYING
14 + git -C path0 mv COPYING ../path1/COPYING
15 '
16
17 # in path0 currently
18 test_expect_success 'commiting the change' '
19 - cd .. && git commit -m move-out -a
19 + git commit -m move-out -a
20 '
21
22 test_expect_success 'checking the commit' '
@@ -25,12 +25,12 @@ test_expect_success 'checking the commit' '
25 '
26
27 test_expect_success 'moving the file back into subdirectory' '
28 - cd path0 && git mv ../path1/COPYING COPYING
28 + git -C path0 mv ../path1/COPYING COPYING
29 '
30
31 # in path0 currently
32 test_expect_success 'commiting the change' '
33 - cd .. && git commit -m move-in -a
33 + git commit -m move-in -a
34 '
35
36 test_expect_success 'checking the commit' '
@@ -328,10 +328,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and no .gitm
328 git mv sub mod/sub &&
329 ! test -e sub &&
330 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
331 - (
332 - cd mod/sub &&
333 - git status
334 - ) &&
331 + git -C mod/sub status &&
332 git update-index --refresh &&
333 git diff-files --quiet
334 '
@@ -351,10 +348,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
348 git mv sub mod/sub &&
349 ! test -e sub &&
350 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
354 - (
355 - cd mod/sub &&
356 - git status
357 - ) &&
351 + git -C mod/sub status &&
352 echo mod/sub >expected &&
353 git config -f .gitmodules submodule.sub.path >actual &&
354 test_cmp expected actual &&
@@ -368,16 +362,10 @@ test_expect_success 'git mv moves a submodule with gitfile' '
362 git submodule update &&
363 entry="$(git ls-files --stage sub | cut -f 1)" &&
364 mkdir mod &&
371 - (
372 - cd mod &&
373 - git mv ../sub/ .
374 - ) &&
365 + git -C mod mv ../sub/ . &&
366 ! test -e sub &&
367 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
377 - (
378 - cd mod/sub &&
379 - git status
380 - ) &&
368 + git -C mod/sub status &&
369 echo mod/sub >expected &&
370 git config -f .gitmodules submodule.sub.path >actual &&
371 test_cmp expected actual &&
@@ -396,10 +384,7 @@ test_expect_success 'mv does not complain when no .gitmodules file is found' '
384 test_must_be_empty actual.err &&
385 ! test -e sub &&
386 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
399 - (
400 - cd mod/sub &&
401 - git status
402 - ) &&
387 + git -C mod/sub status &&
388 git update-index --refresh &&
389 git diff-files --quiet
390 '
@@ -420,10 +405,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
405 test_must_be_empty actual.err &&
406 ! test -e sub &&
407 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
423 - (
424 - cd mod/sub &&
425 - git status
426 - ) &&
408 + git -C mod/sub status &&
409 git update-index --refresh &&
410 git diff-files --quiet
411 '
@@ -441,10 +423,7 @@ test_expect_success 'mv issues a warning when section is not found in .gitmodule
423 test_i18ncmp expect.err actual.err &&
424 ! test -e sub &&
425 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
444 - (
445 - cd mod/sub &&
446 - git status
447 - ) &&
426 + git -C mod/sub status &&
427 git update-index --refresh &&
428 git diff-files --quiet
429 '