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

Re: [PATCH v3 2/2] tests: add tests for grep --max-count

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 22, 2022, 18:10 UTC
Message-ID
<xmqq7d58603l.fsf@gitster.g>
In-Reply-To
<525958af877c3bbd024ffd3aee0c64e469c3d6cf.1655917628.git.gitgitgadget@gmail.com>
"Carlos López via GitGitGadget"  <gitgitgadget@gmail.com> writes:
Show 10 quoted lines
> From: =?UTF-8?q?Carlos=20L=C3=B3pez?= <00xc@protonmail.com>
>
> Add tests for grep's -m / --max-count to check if the option correctly
> outputs limited results, and that it interacts properly with other flags
> that could likely be used in conjunction.
>
> Signed-off-by: Carlos López 00xc@protonmail.com
> ---
>  t/t7810-grep.sh | 83 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 83 insertions(+)

This is better done as part of the previous patch. The new tests protect the new code from future breakage.

Show 23 quoted lines
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 69356011713..7b1b8a3cd93 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -77,6 +77,7 @@ test_expect_success setup '
>  	# Say hello.
>  	function hello() {
>  	  echo "Hello world."
> +	  echo "Hello again."
>  	} # hello
>  
>  	# Still a no-op.
> @@ -595,6 +596,88 @@ test_expect_success 'grep --files-without-match --quiet' '
>  	test_must_be_empty actual
>  '
>  
> +cat >expected <<EOF &&
> +EOF
> +
> +test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
> +	test_must_fail git grep --max-count 0 foo >actual &&
> +	test_cmp expected actual
> +'

For this particular one, "test_must_be_empty actual" would suffice, without comparing with the expected output.

Show 8 quoted lines
> +cat >expected <<EOF &&
> +file:foo mmap bar
> +EOF
> +
> +test_expect_success 'grep --max-count 1' '
> +	git grep --max-count 1 foo >actual &&
> +	test_cmp expected actual
> +'

Writing expected output outside test_expect_success that uses it is a quite old style but that is because this test script is pretty much ancient, so mimicking it is OK. We'd need to come back later when the tree is quiescent to clean them up, though (#leftoverbits).

> ...
> +	test_cmp expected actual
> +'

The new tests seem to give us a reasonable test coverage. We could discard one of the "-m1" vs "-m3" in the early ones, as they do not give much extra test coverage over the other, to reduce repetition.

We do not test a case where we pick up-to N matches each from multiple files, though. Perhaps

    git grep -m1 -e o -- hello.\*

may stop after hitting "No-op." in hello.ps1 and "stdio" in hello.c, which may make a good test, perhaps?

Thanks.
Previous: Carlos López via GitGitGadgetNext: Carlos López via GitGitGadget
Message 11 of 14 in “grep: add --max-count command line option”
  1. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 20, 2022
  2. Paul EggertJun 20, 2022
  3. Carlos L.Jun 20, 2022
  4. Paul EggertJun 20, 2022
  5. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 21, 2022
  6. Junio C HamanoJun 21, 2022
  7. Carlos L.Jun 22, 2022
  8. Junio C HamanoJun 22, 2022
  9. 0/2 grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 22, 2022
  10. 2/2 tests: add tests for grep --max-countCarlos López via GitGitGadget, Jun 22, 2022
  11. Junio C HamanoJun 22, 2022
  12. 1/2 grep: add --max-count command line optionCarlos López via GitGitGadget, Jun 22, 2022
  13. grep: add --max-count command line optionCarlos L. via GitGitGadget, Jun 22, 2022
  14. Carlos L.Jun 22, 2022

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.