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

[PATCH 4/6] t7810: improve check of -W with user-defined function lines

From
René Scharfe <l.s.r@web.de>
Date
Nov 18, 2017, 18:06 UTC
Message-ID
<c89b1e5f-0145-ec93-c7fa-53fac1d429da@web.de>
In-Reply-To
<790c2344-a71e-7089-9000-f9b37a4a5cd9@web.de>

The check for function context (-W) together with user-defined function line patterns reuses hello.c and pretends it's written in a language in which function lines contain either "printf" or a trailing curly brace. That's a bit obscure.

Make the test easier to read by adding a small PowerShell script, using a simple, but meaningful expression, and separating out checks for different aspects into dedicated tests instead of simply matching the whole output byte for byte.

Also include a test for showing comments before function lines like git diff -W does, which is currently failing because that functionality is not implemented for git grep, yet.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 t/t7810-grep.sh | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 2a6679c2f5..632b905611 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -60,6 +60,18 @@ test_expect_success setup '
 		echo " line with leading space3"
 		echo "line without leading space2"
 	} >space &&
+	cat >hello.ps1 <<-\EOF &&
+	# No-op.
+	function dummy() {}
+
+	# Say hello.
+	function hello() {
+	  echo "Hello world."
+	} # hello
+
+	# Still a no-op.
+	function dummy() {}
+	EOF
 	git add . &&
 	test_tick &&
 	git commit -m initial
@@ -766,18 +778,27 @@ test_expect_success 'grep -W shows no trailing empty lines' '
 	test_cmp expected actual
 '
 
-cat >expected <<EOF
-hello.c=	printf("Hello world.\n");
-hello.c:	return 0;
-hello.c-	/* char ?? */
-EOF
-
 test_expect_success 'grep -W with userdiff' '
 	test_when_finished "rm -f .gitattributes" &&
-	git config diff.custom.xfuncname "(printf.*|})$" &&
-	echo "hello.c diff=custom" >.gitattributes &&
-	git grep -W return >actual &&
-	test_cmp expected actual
+	git config diff.custom.xfuncname "^function .*$" &&
+	echo "hello.ps1 diff=custom" >.gitattributes &&
+	git grep -W echo >function-context-userdiff-actual
+'
+
+test_expect_failure ' includes preceding comment' '
+	grep "# Say hello" function-context-userdiff-actual
+'
+
+test_expect_success ' includes function line' '
+	grep "=function hello" function-context-userdiff-actual
+'
+
+test_expect_success ' includes matching line' '
+	grep ":  echo" function-context-userdiff-actual
+'
+
+test_expect_success ' includes last line of the function' '
+	grep "} # hello" function-context-userdiff-actual
 '
 
 for threads in $(test_seq 0 10)
-- 
2.15.0
Previous: René ScharfeNext: René Scharfe
Message 10 of 15 in “show non-empty lines before functions with diff/grep -W”
  1. 0/6 show non-empty lines before functions with diff/grep -WRené Scharfe, Nov 18, 2017
  2. 1/6 t4051: add test for comments preceding function linesRené Scharfe, Nov 18, 2017
  3. Eric SunshineNov 19, 2017
  4. René ScharfeNov 19, 2017
  5. Junio C HamanoNov 20, 2017
  6. René ScharfeNov 20, 2017
  7. Junio C HamanoNov 21, 2017
  8. 3/6 xdiff: show non-empty lines before functions with -WRené Scharfe, Nov 18, 2017
  9. 5/6 grep: update boundary variable for pre-contextRené Scharfe, Nov 18, 2017
  10. 4/6 t7810: improve check of -W with user-defined function linesRené Scharfe, Nov 18, 2017
  11. 2/6 xdiff: factor out is_func_rec()René Scharfe, Nov 18, 2017
  12. 6/6 grep: show non-empty lines before functions with -WRené Scharfe, Nov 18, 2017
  13. Stefan BellerNov 20, 2017
  14. René ScharfeNov 20, 2017
  15. Stefan BellerNov 21, 2017

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.