git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 11/11] t7001: move cleanup code from outside the tests into them

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 25, 2020, 20:54 UTC
Message-ID
<xmqqlfgxk266.fsf@gitster.c.googlers.com>
In-Reply-To
<20200925170256.11490-12-shubhunic@gmail.com>
shubham verma <shubhunic@gmail.com> writes:
Show 22 quoted lines
> From: Shubham Verma <shubhunic@gmail.com>
>
> Let's use test_when_finished() to include cleanup code inside the tests,
> as it's cleaner and safer to not have any code outside the tests.
>
> Signed-off-by: shubham verma <shubhunic@gmail.com>
> ---
>  t/t7001-mv.sh | 25 +++++++++++--------------
>  1 file changed, 11 insertions(+), 14 deletions(-)
>
> diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
> index 7bb4a7b759..b4d04ceaf8 100755
> --- a/t/t7001-mv.sh
> +++ b/t/t7001-mv.sh
> @@ -32,6 +32,7 @@ test_expect_success 'commiting the change' '
>  '
>  
>  test_expect_success 'checking the commit' '
> +	test_when_finished "rmdir path1" &&
>  	git diff-tree -r -M --name-status  HEAD^ HEAD >actual &&
>  	grep "^R100..*path1/COPYING..*path0/COPYING" actual
>  '

Sorry, but why in this test? It only runs diff-tree and runs grep, neither of which changes any state in the repository. Because the test does *not* create path1, and having or not having path1 on the filesystem would not affect the outcome of the test, I do not see how it makes sense to use test_when_finished in here.

If you are saying that path1 will no longer be used after this test finishes, test_when_finished should be done in the test before this one that used path1 the last, because this test does not care. It is probably the one that moves the file out of path1 back to path0 and records the result as a commit with title "move-in" (although if I were writing this test today, I would merge the "move and commit" into one step).

Show 7 quoted lines
> @@ -43,6 +44,7 @@ test_expect_success 'mv --dry-run does not move file' '
>  '
>  
>  test_expect_success 'checking -k on non-existing file' '
> +	test_when_finished "rm -f idontexist path0/idontexist" &&
>  	git mv -k idontexist path0
>  '

I do not see the point of "rm -f idontexist" in the post-test-cleanup at all. Some might see that path0/ideontexist is worth having there, in case the "mv" command gets so broken that it creates such a file by mistake, but Personally I'd prefer to use test_when_finished to clean up the side effects we _expect_ to cause. We cannot anticipate each and every breakage.

The other side of the coin is that this test DEPENDS ON the fact that idontexist does *NOT* exist before it runs "git mv -k idontexist path0", but nobody before us gives us any explicitly guarantee. This test also depends on the presence of path0 directory the same way. Instead of relying on others that came before us to have cleaned after themselves for us, we can more explicitly protect ourselves by making sure the pre-condition we depend on holds. I.e.

    test_expect_success 'mv -k on non-exising file would not fail' '
	mkdir -p path0 &&
	rm -f idontexist path0/idontexist &&
	git mv -k idontexist path0
    '

A broken "git mv" may or may not leave path0/idontexist behind, but as long as the tests that come after us protect themselves with the same principle of making sure the preconditions they care about do hold, we do not necessarily have to clean after ourselves. Since we expect we do not leave any side effect, I'd rather not to use test_when_finished here.

@@ -55,6 +57,7 @@ test_expect_success 'checking -k on untracked file' '
>  
>  test_expect_success 'checking -k on multiple untracked files' '
>  	: > untracked2 &&
> +	test_when_finished "rm -f untracked2 path0/untracked2" &&
>  	git mv -k untracked1 untracked2 path0 &&
>  	test -f untracked1 &&
>  	test -f untracked2 &&

An exercise to readers.  Explain why

 - we want to move test_when_finished _before_ ">untracked2" is created;

 - "rm -f untrackd2" in test_when_finished is a good idea;

 - "rm -f path0/untracked2" is not a good idea;

 - we may want to do

	>untracked1 &&
	mkdir -p path0 &&

   before "git mv -k ..." is tested.

> -# clean up the mess in case bad things happen
> -rm -f idontexist untracked1 untracked2 \
> -     path0/idontexist path0/untracked1 path0/untracked2 \
> -     .git/index.lock
> -rmdir path1
> -
>  test_expect_success 'moving to absent target with trailing slash' '
>  	test_must_fail git mv path0/COPYING no-such-dir/ &&
>  	test_must_fail git mv path0/COPYING no-such-dir// &&

It may be a better approach to move the above removals at the
beginning of this test, just before the first test_must_fail line.

> -rm -fr papers partA path?
> -
>  test_expect_success "Sergey Vlasov's test case" '

Likewise.
Previous: Eric SunshineNext: Eric Sunshine
Message 24 of 27 in “Modernizing the t7001 test script”
  1. 00/11 Modernizing the t7001 test scriptshubham verma, Sep 25, 2020
  2. 01/11 t7001: convert tests from the old style to the current styleshubham verma, Sep 25, 2020
  3. Eric SunshineSep 25, 2020
  4. 02/11 t7001: use TAB instead of spacesshubham verma, Sep 25, 2020
  5. Eric SunshineSep 25, 2020
  6. 03/11 t7001: remove unnecessary blank linesshubham verma, Sep 25, 2020
  7. Eric SunshineSep 25, 2020
  8. Junio C HamanoSep 25, 2020
  9. 04/11 t7001: change the style for cd according to subshellshubham verma, Sep 25, 2020
  10. Eric SunshineSep 25, 2020
  11. 05/11 t7001: remove whitespace after redirect operatorsshubham verma, Sep 25, 2020
  12. 06/11 t7001: change (cd <path> && git foo) to (git -C <path> foo)shubham verma, Sep 25, 2020
  13. Eric SunshineSep 25, 2020
  14. 07/11 t7001: use ': >' rather than 'touch'shubham verma, Sep 25, 2020
  15. Eric SunshineSep 25, 2020
  16. Junio C HamanoSep 25, 2020
  17. 08/11 t7001: put each command on a separate lineshubham verma, Sep 25, 2020
  18. Eric SunshineSep 25, 2020
  19. 09/11 t7001: use here-docs instead of echoshubham verma, Sep 25, 2020
  20. Junio C HamanoSep 25, 2020
  21. 10/11 t7001: use `test` rather than `[`shubham verma, Sep 25, 2020
  22. 11/11 t7001: move cleanup code from outside the tests into themshubham verma, Sep 25, 2020
  23. Eric SunshineSep 25, 2020
  24. Junio C HamanoSep 25, 2020
  25. Eric SunshineSep 25, 2020
  26. Shubham VermaOct 1, 2020
  27. Junio C HamanoDec 22, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.