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

Re: [PATCH 6/6] t4124: fix test for non-compliance diff

From
Jeff King <peff@peff.net>
Date
Mar 19, 2020, 16:33 UTC
Message-ID
<20200319163334.GG3513282@coredump.intra.peff.net>
In-Reply-To
<285c6830c5182cb602d4fe559525083f69a158e9.1584625896.git.congdanhqx@gmail.com>
On Thu, Mar 19, 2020 at 09:00:07PM +0700, Đoàn Trần Công Danh wrote:
> POSIX's diff(1) requires output in normal diff format.
> However, busybox's diff's output is written in unified format.

That's a pretty big difference. I'm surprised this only produces one problem in the test scripts. ;)

Show 7 quoted lines
> POSIX requires no option for normal-diff format.
> 
> A hint in test-lib-functions::test_cmp said `diff -u` isn't available
> everywhere.
> 
> Workaround this problem by assuming `diff(1)` output is unified
> if we couldn't make anything from normal-diff format.

I wonder if we could use "git diff" here. We have to be careful about circular reasoning in our tests (i.e., making sure we're not verifying output with the same code that we're testing), but I think here we're checking how "apply --whitespace=fix" works.

But if this is the only spot, then adjusting to handle unified or normal diff isn't too bad.

Show 14 quoted lines
> diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
> index 971a5a7512..2a54ce96b5 100755
> --- a/t/t4124-apply-ws-rule.sh
> +++ b/t/t4124-apply-ws-rule.sh
> @@ -52,6 +52,12 @@ test_fix () {
>  
>  	# find touched lines
>  	$DIFF file target | sed -n -e "s/^> //p" >fixed
> +	if ! test -s fixed; then
> +		$DIFF file target |
> +		grep '^+' |
> +		grep -v '^+++' |
> +		sed -e "s/+//" >fixed
> +	fi
I think those greps could be lumped into sed like:
  sed -ne "s/^+[^+]//p"

(at the cost of missing blank lines, but I think that's OK for our purposes here; it could be fixed with an ERE).

Could we then make a single invocation that covers both diff formats? We can further observe that the only thing we do with the "fixed" file is count the lines, so we can leave the markers. Which means we could ditch sed entirely and use grep. Something like:

diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index 971a5a7512..15cb0c81b7 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -50,8 +50,9 @@ test_fix () {
 	# fix should not barf
 	apply_patch --whitespace=fix || return 1
 
-	# find touched lines
-	$DIFF file target | sed -n -e "s/^> //p" >fixed
+	# find touched lines; handle either normal or unified
+	# diff, as system diff may generate either
+	$DIFF file target | grep '^[>+][^+]' >fixed
 
 	# the changed lines are all expected to change
 	fixed_cnt=$(wc -l <fixed)

seems to work for with both busybox diff and GNU diff.

-Peff
Previous: Đoàn Trần Công DanhNext: Junio C Hamano
Message 27 of 35 in “fix test failure with busybox”
  1. 0/6 fix test failure with busyboxĐoàn Trần Công Danh, Mar 19, 2020
  2. 1/6 t4061: use POSIX compliance regex(7)Đoàn Trần Công Danh, Mar 19, 2020
  3. Jeff KingMar 19, 2020
  4. Eric SunshineMar 19, 2020
  5. Junio C HamanoMar 19, 2020
  6. Danh DoanMar 20, 2020
  7. 2/6 test-lib-functions: test_cmp: eval $GIT_TEST_CMPĐoàn Trần Công Danh, Mar 19, 2020
  8. Jeff KingMar 19, 2020
  9. Eric SunshineMar 19, 2020
  10. Danh DoanMar 20, 2020
  11. 3/6 t5003: skip conversion test if unzip -a is unavailableĐoàn Trần Công Danh, Mar 19, 2020
  12. Jeff KingMar 19, 2020
  13. Danh DoanMar 20, 2020
  14. Jeff KingMar 20, 2020
  15. 4/6 t5616: use rev-parse instead to get HEAD's object_idĐoàn Trần Công Danh, Mar 19, 2020
  16. Jeff KingMar 19, 2020
  17. Danh DoanMar 20, 2020
  18. 5/6 t7063: use POSIX find(1) syntaxĐoàn Trần Công Danh, Mar 19, 2020
  19. Jeff KingMar 19, 2020
  20. Junio C HamanoMar 19, 2020
  21. Danh DoanMar 20, 2020
  22. Danh DoanMar 20, 2020
  23. Jeff KingMar 20, 2020
  24. Danh DoanMar 22, 2020
  25. Jeff KingMar 22, 2020
  26. 6/6 t4124: fix test for non-compliance diffĐoàn Trần Công Danh, Mar 19, 2020
  27. Jeff KingMar 19, 2020
  28. Junio C HamanoMar 19, 2020
  29. Jeff KingMar 20, 2020
  30. Danh DoanMar 20, 2020
  31. Jeff KingMar 20, 2020
  32. Jeff KingMar 19, 2020
  33. Danh DoanMar 20, 2020
  34. Jeff KingMar 20, 2020
  35. Jeff KingMar 19, 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.