Skip to contentSkip to conversation

test-lib.sh: new test_commit args, simplification & fixes

PATCH Superseded Ævar Arnfjörð Bjarmason 8 participants 175 messages Apr 12, 2021 – May 13, 2021

lore
Share & tools
lore
#0 lore raw

cover test-lib.sh: new test_commit args, simplification & fixes

This is a "various small fixes" series to test-lib.sh, test-lib-functions.sh.

I have a couple of outstanding patch serieses that I've pulled this from (ab/describe-tests-fix, ab/pickaxe-pcre2), as well as some other local work I've been having a chicken and egg problem with.

I think the approach of pulling out these various miscellaneous test fixes into their own topic makes the most sense, I'll then submit re-rolled versions of other topics that e.g. need these new test_commit arguments on top of this.

Ævar Arnfjörð Bjarmason (16):
check-non-portable-shell: complain about "test" a/-o instead of &&/||
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated-tag option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: modernize test_create_repo() function
test-lib-functions: normalize test_path_is_missing() debugging
test-lib-functions: use "return 1" instead of "false" Revert and amend "test-lib-functions: assert correct parameter count"
test-lib-functions: remove last two parameter count assertions

 t/check-non-portable-shell.pl       |   4 +
 t/lib-submodule-update.sh           |   3 +-
 t/t0000-basic.sh                    |   4 -
 t/t1307-config-blob.sh              |   4 +-
 t/t1403-show-ref.sh                 |   6 +-
 t/t2030-unresolve-info.sh           |   3 +-
 t/t4006-diff-mode.sh                |   6 +-
 t/t4030-diff-textconv.sh            |   8 +-
 t/t5406-remote-rejects.sh           |   1 -
 t/t5407-post-rewrite-hook.sh        |   2 -
 t/t5409-colorize-remote-messages.sh |   1 -
 t/t5520-pull.sh                     |  10 +--
 t/t6120-describe.sh                 |  58 +++----------
 t/test-lib-functions.sh             | 125 ++++++++++++++--------------
 t/test-lib.sh                       |  36 ++++----
 15 files changed, 113 insertions(+), 158 deletions(-)
Signature · 1 line
2.31.1.634.gb41287a30b0
#1 lore raw

PATCH 1/16 check-non-portable-shell: complain about "test" a/-o instead of &&/||

Expand the t/check-non-portable-shell.pl checks to complain about the use of "-a" and "-o" to the "test" shell built-in to to mean "and" and "or", as opposed to using two "test" invocations with "&&" or "||".

There aren't any portability issues with using that construct that I know of, but since Junio expressed a dislike of it in [1] and we've currently got no such constructs let's add it to the lint checking. I had various in-flight and WIP patches that used this construct.

1. https://lore.kernel.org/git/[email protected]/

---
 t/check-non-portable-shell.pl | 4 ++++
 1 file changed, 4 insertions(+)
t/check-non-portable-shell.pl +4 -0
index fd3303552be..a5367346255 100755
@@ -35,6 +35,7 @@ sub err {
 	next if $line =~ s/\\$//;
 
 	$_ = $line;
+	# Portability issues
 	/\bcp\s+-a/ and err 'cp -a is not portable';
 	/\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
 	/\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
@@ -48,6 +49,9 @@ sub err {
 	/\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
 	/^\s*([A-Z0-9_]+=(\w*|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
 		err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
+	# Coding style preferences
+	/\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+	/\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
 	$line = '';
 	# this resets our $. for each file
 	close ARGV if eof;
Signature · 1 line
2.31.1.634.gb41287a30b0
#2 lore raw

Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

Quoted 8 lines · Ævar Arnfjörð Bjarmason

Expand the t/check-non-portable-shell.pl checks to complain about the use of "-a" and "-o" to the "test" shell built-in to to mean "and" and "or", as opposed to using two "test" invocations with "&&" or "||".

There aren't any portability issues with using that construct that I know of, but since Junio expressed a dislike of it in [1] and we've currently got no such constructs let's add it to the lint checking. I had various in-flight and WIP patches that used this construct.

It's not only Junio's dislike of -a and -o but also that they have long been considered obsolescent by POSIX[1]. GNU has likewise warned against it[2] for a few decades.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
[2]: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

t/check-non-portable-shell.pl sub err { · line 49

Quoted 7 lines

---

t/check-non-portable-shell.pl +2 -0
@@ -35,6 +35,7 @@ sub err {
+       # Portability issues
@@ -48,6 +49,9 @@ sub err {
+       # Coding style preferences

Given that these flags are considered obsolescent, thus may someday be dropped from some implementations, these seem a better fit for the "portability issues" section than the "coding style preferences" section. (I'd probably just drop those section titles, though.)

t/check-non-portable-shell.pl sub err { · line 54

Quoted 2 lines
t/check-non-portable-shell.pl +2 -0
+       /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+       /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. Crafting a regex to match more generally would be non-trivial, so this simpler match is a reasonable start. Okay.

#3 lore raw

Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

Quoted 11 lines · Eric Sunshine
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Expand the t/check-non-portable-shell.pl checks to complain about the use of "-a" and "-o" to the "test" shell built-in to to mean "and" and "or", as opposed to using two "test" invocations with "&&" or "||".

There aren't any portability issues with using that construct that I know of, but since Junio expressed a dislike of it in [1] and we've currently got no such constructs let's add it to the lint checking. I had various in-flight and WIP patches that used this construct.

Quoted 8 lines

It's not only Junio's dislike of -a and -o but also that they have long been considered obsolescent by POSIX[1]. GNU has likewise warned against it[2] for a few decades.

[1]:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
[2]:
https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

And also in Git's Documentation/CodingGuidelines, which motivates the advice with an example:

     test -n "$x" -a "$a" = "$b"

is buggy and breaks when $x is "=", but

     test -n "$x" && test "$a" = "$b"

does not have such a problem.

Signature · 2 lines
Matthieu Moy
https://matthieu-moy.fr/
#4 lore raw

Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

Quoted 9 lines · Matthieu Moy

And also in Git's Documentation/CodingGuidelines, which motivates the advice with an example:

     test -n "$x" -a "$a" = "$b"

is buggy and breaks when $x is "=", but

     test -n "$x" && test "$a" = "$b"

does not have such a problem.

Good advice to cite Documentation/CodingGuidelines in the log message.

Thanks (and nice to hear from you again ;-).

#5 lore raw

Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

In reply to Eric Sunshine
Quoted 5 lines · Eric Sunshine

Given that these flags are considered obsolescent, thus may someday be dropped from some implementations, these seem a better fit for the "portability issues" section than the "coding style preferences" section. (I'd probably just drop those section titles, though.)

Quoted 7 lines

t/check-non-portable-shell.pl sub err { · line 54

Quoted 2 lines
t/check-non-portable-shell.pl +2 -0
+       /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+       /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. Crafting a regex to match more generally would be non-trivial, so this simpler match is a reasonable start. Okay.

Would it be a trivial improvement to do

'test', followed by anything other than '&' or '|', and then followed by '-a' or '-o'

instead?

#6 lore raw

Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Eric Sunshine

t/check-non-portable-shell.pl sub err { · line 54

Quoted 2 lines
t/check-non-portable-shell.pl +2 -0
+       /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+       /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. Crafting a regex to match more generally would be non-trivial, so this simpler match is a reasonable start. Okay.

Quoted 6 lines

Would it be a trivial improvement to do

'test', followed by anything other than '&' or '|', and then followed by '-a' or '-o'

instead?

That seems plausible and trivial enough. In fact, I think it eliminates a source of false-positives that Ævar's pattern can report, such as:

    test whatever && ls -a foo

and

    test whatever && foo -o outfile

among others.

#7 lore raw

PATCH 2/16 test-lib: bring $remove_trash out of retirement

In reply to Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests.

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
t/test-lib.sh +17 -14
index d3f6af6a654..a8869eee58f 100644
@@ -1167,7 +1167,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1332,6 +1332,21 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1339,10 +1354,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1354,15 +1366,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.634.gb41287a30b0
#8 lore raw

Re: [PATCH 02/16] test-lib: bring $remove_trash out of retirement

Quoted 11 lines · Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests.

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

Quoted 6 lines

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---

I am not clear what the reasoning behind this change is. Is it correct to say that the idea flows like the following?

0. If we do the $GIT_SKIP_TESTS check early, specifically, before we create the trash directory to run the tests in, we do not have to create and then remove it.

1. If we naïvely do the above, test_done called in the "ah, the entirety of test is skipped" case will try to see if the trash directory exists and complain, so we need to add more code in that test_done codepath to avoid complaints.

2. As a part of that "more code", we mark the fact that we created a new repository with $remove_trash variable.

Assuming that the above flow of thought is what is behind this patch, I think the patch is mostly sensible.

remove_trash needs to be cleared at the beginning, perhaps where store_arg_to and opt_required_arg are cleared, though, to protect us from a stray environment variable.

Thanks.

#9 lore raw

Re: [PATCH 02/16] test-lib: bring $remove_trash out of retirement

Quoted 32 lines · Junio C Hamano
Quoted 11 lines · Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests.

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

Quoted 6 lines

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---

I am not clear what the reasoning behind this change is. Is it correct to say that the idea flows like the following?

0. If we do the $GIT_SKIP_TESTS check early, specifically, before we create the trash directory to run the tests in, we do not have to create and then remove it.

1. If we naïvely do the above, test_done called in the "ah, the entirety of test is skipped" case will try to see if the trash directory exists and complain, so we need to add more code in that test_done codepath to avoid complaints.

Quoted 5 lines

2. As a part of that "more code", we mark the fact that we created a new repository with $remove_trash variable.

Assuming that the above flow of thought is what is behind this patch, I think the patch is mostly sensible.

By the way, I did wonder if the call to test_done is even needed, or if it is even ready to be called (in other words, the machineries it tries to finalize like junit and HARNESS_ACTIVE are even initialized yet). I verified that calling test_done this early is fine with respect to the call it makes to finalize_junit_xml, though.

Quoted 5 lines

remove_trash needs to be cleared at the beginning, perhaps where store_arg_to and opt_required_arg are cleared, though, to protect us from a stray environment variable.

Thanks.

#10 lore raw

PATCH 3/16 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27a..2c6e34b9478 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.634.gb41287a30b0
#11 lore raw

PATCH 4/16 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index 6348e8d7339..d169fb2f597 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.634.gb41287a30b0
#12 lore raw

PATCH 5/16 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 3 +++
 1 file changed, 3 insertions(+)
t/test-lib-functions.sh +3 -0
index d169fb2f597..fa3e3e975fd 100644
@@ -177,6 +177,9 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit, if supplied giving the
+#	optional "<tag>" argument is an error.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.634.gb41287a30b0
#13 lore raw

Re: [PATCH 05/16] test-lib-functions: document test_commit --no-tag

t/test-lib-functions.sh debug () { · line 179

Quoted 13 lines · Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---

t/test-lib-functions.sh +3 -0
@@ -177,6 +177,9 @@ debug () {
+#   --no-tag
+#      Do not tag the resulting commit, if supplied giving the
+#      optional "<tag>" argument is an error.

This is difficult to understand for a native English speaker/reader due to the comma-splice[1]. A period or semicolon in place of the comma would fix it.

[1]: https://lore.kernel.org/git/CAPx1GvfFPWvJsj+uJV7RZrv1rgEpio=pk6rKF2UrjHebVY=LPA@mail.gmail.com/

#14 lore raw

PATCH 6/16 test-lib functions: add an --annotated-tag option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated-tag option to test_commit. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c3..17d3cc14050 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index fa3e3e975fd..a0fcc383d0b 100644
@@ -180,6 +180,10 @@ debug () {
 #   --no-tag
 #	Do not tag the resulting commit, if supplied giving the
 #	optional "<tag>" argument is an error.
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag unless --notick is
+#	given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -192,7 +196,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -220,7 +224,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -244,10 +251,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test "$tag$notick" = "annotate"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.634.gb41287a30b0
#15 lore raw

Re: [PATCH 06/16] test-lib functions: add an --annotated-tag option to "test_commit"

t/test-lib-functions.sh debug () { · line 182

Quoted 9 lines · Ævar Arnfjörð Bjarmason
(quoted diff) +0 -0
 	git branch B A^0
t/test-lib-functions.sh +0 -0
index fa3e3e975fd..a0fcc383d0b 100644
@@ -180,6 +180,10 @@ debug () {
 #   --no-tag
 #	Do not tag the resulting commit, if supplied giving the
 #	optional "<tag>" argument is an error.

This comment applied to previous patch, but I think we need this patch to justify for optional "<tag>" argument is an error for "--no-tag"

----8<----

t/test-lib-functions.sh +4 -0
index a0fcc383d0..ed0a4e5e5d 100644
@@ -253,6 +253,10 @@ test_commit () {
 	    $signoff -m "$1" &&
 	case "$tag" in
 	none)
+		if test -z "$4"
+		then
+			BUG "<tag> given to test_commit --no-tag"
+		fi
 		;;
 	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"

----8<----

Quoted 33 lines
t/test-lib-functions.sh +9 -3
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag unless --notick is
+#	given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -192,7 +196,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -220,7 +224,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -244,10 +251,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"

t/test-lib-functions.sh test_commit () { · line 261

Quoted 10 lines
t/test-lib-functions.sh +7 -2
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test "$tag$notick" = "annotate"

Perhap I'm missing something, however, I couldn't get the reason for inserting "$tag" here. I wonder what wrong with:

	if test -z "$notick"
	then
		test_tick
	fi &&
Quoted 12 lines
t/test-lib-functions.sh +6 -0
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.634.gb41287a30b0
Signature · 1 line
Danh
#16 lore raw

PATCH 7/16 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747bee..88fddc91424 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.634.gb41287a30b0
#17 lore raw

PATCH 8/16 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388e..930dce06f0f 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a2..f691e6d9032 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15b..6cdee2a2164 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60d..a39a626664d 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f9..e2c0c510222 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index a0fcc383d0b..a90a6b2cc27 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -192,6 +196,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -203,6 +208,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -239,9 +247,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.634.gb41287a30b0
#18 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 9 lines · Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

---

Just a bit of pure bikeshedding...

t/test-lib-functions.sh debug () { · line 176

Quoted 6 lines
t/test-lib-functions.sh +4 -0
@@ -173,6 +173,10 @@ debug () {
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).

The name "printf" has such strong association in programmer's minds with "%" and argument consumption that the name of this option alone almost begs people to take advantage of argument interpolation even though it's documented here as not allowing it. Taking into consideration that people often do not read documentation, --printf as the name of the option may be an unfortunate one. Perhaps it could be called --raw or something less likely to suggest argument interpolation.

t/test-lib-functions.sh debug () { · line 199

Quoted 5 lines
t/test-lib-functions.sh +1 -0
@@ -192,6 +196,7 @@ debug () {
 
 test_commit () {
        notick= &&
+       echo=echo &&

This could be slightly confusing. I wonder if naming this variable emit would be clearer.

#19 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 8 lines · Eric Sunshine

The name "printf" has such strong association in programmer's minds with "%" and argument consumption that the name of this option alone almost begs people to take advantage of argument interpolation even though it's documented here as not allowing it. Taking into consideration that people often do not read documentation, --printf as the name of the option may be an unfortunate one. Perhaps it could be called --raw or something less likely to suggest argument interpolation.

The reason we want to use 'printf' instead of 'echo' is because only some implementations of 'echo' honors '\t\n\r' etc., and 'echo' by others show these literally. Using printf(1) allows us to write these backslashed special characters universally.

So, I find 'raw' equally confusing, if not more.

Quoted 8 lines

t/test-lib-functions.sh debug () { · line 199

Quoted 5 lines
t/test-lib-functions.sh +1 -0
@@ -192,6 +196,7 @@ debug () {
 
 test_commit () {
        notick= &&
+       echo=echo &&

This could be slightly confusing. I wonder if naming this variable emit would be clearer.

Perhaps.

#20 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Eric Sunshine

The name "printf" has such strong association in programmer's minds with "%" and argument consumption that the name of this option alone almost begs people to take advantage of argument interpolation even though it's documented here as not allowing it. Taking into consideration that people often do not read documentation, --printf as the name of the option may be an unfortunate one. Perhaps it could be called --raw or something less likely to suggest argument interpolation.

Quoted 6 lines

The reason we want to use 'printf' instead of 'echo' is because only some implementations of 'echo' honors '\t\n\r' etc., and 'echo' by others show these literally. Using printf(1) allows us to write these backslashed special characters universally.

So, I find 'raw' equally confusing, if not more.

I don't care for --raw either but couldn't think of anything better at the time. But perhaps a name such as --allow-escapes would be clearer, or perhaps not. --c-style-escapes?

Anyhow, it's pure bikeshedding as I mentioned in my original email, so not a big deal. I brought it up only because the very first thought that popped into my head when reading the commit message saying it was adding --printf was "oh, interesting; how do I specify the arguments to interpolate into the printf format string?".

#21 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 3 lines · Eric Sunshine

I don't care for --raw either but couldn't think of anything better at the time. But perhaps a name such as --allow-escapes would be clearer, or perhaps not. --c-style-escapes?

It's printf(1) style escapes ;-)

Quoted 5 lines

Anyhow, it's pure bikeshedding as I mentioned in my original email, so not a big deal. I brought it up only because the very first thought that popped into my head when reading the commit message saying it was adding --printf was "oh, interesting; how do I specify the arguments to interpolate into the printf format string?".

#22 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 13 lines · Junio C Hamano
Quoted 3 lines · Eric Sunshine

I don't care for --raw either but couldn't think of anything better at the time. But perhaps a name such as --allow-escapes would be clearer, or perhaps not. --c-style-escapes?

It's printf(1) style escapes ;-)

Quoted 5 lines

Anyhow, it's pure bikeshedding as I mentioned in my original email, so not a big deal. I brought it up only because the very first thought that popped into my head when reading the commit message saying it was adding --printf was "oh, interesting; how do I specify the arguments to interpolate into the printf format string?".

So, the conclusion of this thread is let's keep it as --printf?

#23 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Eric Sunshine

I don't care for --raw either but couldn't think of anything better at the time. But perhaps a name such as --allow-escapes would be clearer, or perhaps not. --c-style-escapes?

It's printf(1) style escapes ;-)

So, the conclusion of this thread is let's keep it as --printf?

It was bikeshedding on my part, so I don't feel strongly. As mentioned, I only brought it up because my first thought was to wonder how interpolation would work. One might suggest --printf-escapes or --string-escapes but the audience for this is so narrow (Git developers) that the short and concise --printf is probably preferable.

#24 lore raw

Re: [PATCH 08/16] test-lib functions: add --printf option to test_commit

Quoted 10 lines · Eric Sunshine
Quoted 8 lines · Ævar Arnfjörð Bjarmason
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Eric Sunshine

I don't care for --raw either but couldn't think of anything better at the time. But perhaps a name such as --allow-escapes would be clearer, or perhaps not. --c-style-escapes?

It's printf(1) style escapes ;-)

Quoted 8 lines
Quoted 1 line

So, the conclusion of this thread is let's keep it as --printf?

It was bikeshedding on my part, so I don't feel strongly. As mentioned, I only brought it up because my first thought was to wonder how interpolation would work. One might suggest --printf-escapes or --string-escapes but the audience for this is so narrow (Git developers) that the short and concise --printf is probably preferable.

After seeing you raised the issue, I wish we had a better option name than --printf to be even more clear, but the wish is not strong enough to make me say "let's stop until we come up with the perfect name". I am OK with --printf

Thanks.

#25 lore raw

PATCH 9/16 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e93083..f7c7df0ca42 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.634.gb41287a30b0
#26 lore raw

PATCH 10/16 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index a90a6b2cc27..674b865a20d 100644
@@ -1258,8 +1258,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.634.gb41287a30b0
#27 lore raw

PATCH 11/16 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 674b865a20d..c81726acb9e 100644
@@ -1259,7 +1259,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index a8869eee58f..0057dfa69d0 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.634.gb41287a30b0
#28 lore raw

Re: [PATCH 11/16] test-lib: do not show advice about init.defaultBranch under --verbose

t/test-lib-functions.sh test_create_repo () { · line 1262

Quoted 10 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +0 -1
index 674b865a20d..c81726acb9e 100644
@@ -1259,7 +1259,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \

This change got my hopes up that

GIT_TEST_INSTALLED=/.../v1.6.0/bin ./t9999-foo.sh

will work again (it didn't yet support 'git init -c <...>'), but, alas, already the next patch will turn this into 'git init ... <trash-dir>', and v1.6.0 didn't support that, either, so it will remain broken... Oh, well.

Quoted 21 lines
t/test-lib-functions.sh +0 -0
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index a8869eee58f..0057dfa69d0 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.634.gb41287a30b0
#29 lore raw

PATCH 12/16 test-lib: modernize test_create_repo() function

In reply to Ævar Arnfjörð Bjarmason

Remove redundant "mkdir -p", argument number checking', test environment sanity checking, and disabling of hooks from test_create_repo(). As we'll see below these were all either redundant to other test other framework code, or to changes in git itself.

Respectively:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. We don't need to check the number of arguments anymore, instead we'll feed "git init" with "$@". It will die if given too many arguments.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

In the end it turns out that all we needed was a plain "git init" invocation with a custom --template directory.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 +++------------
 4 files changed, 3 insertions(+), 16 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649e..5c509db6fc3 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4e..6da8d760e28 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8ec..9f1a483f426 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +3 -12
index c81726acb9e..1258329fdd8 100644
@@ -1252,18 +1252,9 @@ test_atexit () {
 # Most tests can use the created repository, but some may need to create more.
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+		init \
+		"--template=$GIT_BUILD_DIR/templates/blt/" "$@" >&3 2>&4
 }
 
 # This function helps on symlink challenged file systems when it is not
Signature · 1 line
2.31.1.634.gb41287a30b0
#30 lore raw

Re: [PATCH 12/16] test-lib: modernize test_create_repo() function

Quoted 1 line · Ævar Arnfjörð Bjarmason

4. We don't need to move .git/hooks out of the way.

It might be unsafe to assume that we'd never have an 'enabled-by-default' hook in blt/ template directory forever, though.

#31 lore raw

Re: [PATCH 12/16] test-lib: modernize test_create_repo() function

Quoted 7 lines · Junio C Hamano
Quoted 1 line · Ævar Arnfjörð Bjarmason

4. We don't need to move .git/hooks out of the way.

It might be unsafe to assume that we'd never have an 'enabled-by-default' hook in blt/ template directory forever, though.

I would think that if we injected run-by-default hooks into the top-level repo creation any such breakage in the test suite would be a feature, i.e. we'd want to see and adjust for the breakage, because surely the same sort of breakage would occur in the wild.

FWIW I've got some WIP local patches for the "just ship a README in hooks/":
https://lore.kernel.org/git/[email protected]/

From memory the cumultive footprint of foot a -d run on the whole test suite is on the order of ~1GB, and ~100MB is just the hooks over and over again...

#32 lore raw

Re: [PATCH 12/16] test-lib: modernize test_create_repo() function

In reply to Ævar Arnfjörð Bjarmason
Quoted 5 lines · Ævar Arnfjörð Bjarmason

Remove redundant "mkdir -p", argument number checking', test environment sanity checking, and disabling of hooks from test_create_repo(). As we'll see below these were all either redundant to other test other framework code, or to changes in git itself.

Quoted 8 lines

Respectively:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. We don't need to check the number of arguments anymore, instead we'll feed "git init" with "$@". It will die if given too many arguments.

Or it will succeed if invoked as e.g. 'test_create_repo --bare repo'.

Quoted 2 lines

3. We won't ever hit that "Cannot setup test environment" error.

ENOSPC? Some rogue background process on Windows still desperately clinging to an open file descriptor to some file in the same directory, preventing 'rm -rf "$TRASH_DIRECTORY"' near the beginning of 'test-lib.sh' and interfering with 'git init'?

Quoted 13 lines

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

Quoted 7 lines

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

I agree that if we already have a 'git' binary that can run 'git version', then we can safely assume that it will be able to run 'git init' as well. It might be that 'git init' is buggy and segfaults, but that is not a "have you built things yet?" kind of error.

Quoted 6 lines

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

Quoted 6 lines

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

We needed the subshell because we changed directory in 'test_create_repo' (even you referenced 0d314ce834d above), not because we moved the hooks directory.

Quoted 2 lines

In the end it turns out that all we needed was a plain "git init" invocation with a custom --template directory.

We don't need '--template', either; see below.

Quoted 10 lines
t/test-lib-functions.sh +0 -2
index c81726acb9e..1258329fdd8 100644
@@ -1252,18 +1252,9 @@ test_atexit () {
 # Most tests can use the created repository, but some may need to create more.
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"

t/test-lib-functions.sh test_atexit () { · line 1266

Quoted 10 lines
t/test-lib-functions.sh +0 -10
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit

This patch removes this '|| exit', which is...

- good: if 'test_create_repo' is invoked in a test case (and not in a subshell) and if it were to fail for some reason, then it won't abort the whole test script, but will fail only that test case.

- bad: 'test_create_repo' is responsible for creating the repository in the trash directory as well; if that were to fail for any reason, then the test script will not be aborted early.

I think the 'exit' on error should be removed from 'test_create_repo', but the callsite in 'test-lib.sh' should become 'test_create_repo || exit 1'.

In any case, removing this '|| exit' is not mentioned in the commit message.

t/test-lib-functions.sh test_atexit () { · line 1255

Quoted 1 line
t/test-lib-functions.sh +1 -0
+	"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \

PATH is already set up to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before 'test_create_repo' is called to init the repo in the trash directory, so we could simply run 'git' and rely on PATH lookup choosing the right executable.

t/test-lib-functions.sh test_atexit () { · line 1257

Quoted 2 lines
t/test-lib-functions.sh +2 -0
+		init \
+		"--template=$GIT_BUILD_DIR/templates/blt/" "$@" >&3 2>&4

Likewise, GIT_TEMPLATE_DIR is already set up to the same value as in this '--template=...' option, so we could omit this option as well.

And after that all that would remain in this function is:

git init "$1" >&3 2>&4

And those redirections are only needed when this function is called to initialize the repo in the trash directory, but not when it creates a repo in a test case. This makes me question the value of having this 'test_create_repo' helper function at all; there are already over 800 cases where we run plain 'git init' outside of 't0001-init.sh' instead of 'test_create_repo', though removing it would be definitely cause some churn with over 300 callsites.

Quoted 6 lines
t/test-lib-functions.sh +0 -0
 }
 
 # This function helps on symlink challenged file systems when it is not
Signature · 1 line
2.31.1.634.gb41287a30b0
#33 lore raw

Re: [PATCH 12/16] test-lib: modernize test_create_repo() function

Quoted 50 lines · SZEDER Gábor
Quoted 2 lines

3. We won't ever hit that "Cannot setup test environment" error.

ENOSPC? Some rogue background process on Windows still desperately clinging to an open file descriptor to some file in the same directory, preventing 'rm -rf "$TRASH_DIRECTORY"' near the beginning of 'test-lib.sh' and interfering with 'git init'?

Quoted 13 lines

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

Quoted 7 lines

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

I agree that if we already have a 'git' binary that can run 'git version', then we can safely assume that it will be able to run 'git init' as well. It might be that 'git init' is buggy and segfaults, but that is not a "have you built things yet?" kind of error.

t/test-lib-functions.sh test_atexit () { · line 1266

Quoted 9 lines
t/test-lib-functions.sh +0 -9
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit

This patch removes this '|| exit', which is...

- good: if 'test_create_repo' is invoked in a test case (and not in a subshell) and if it were to fail for some reason, then it won't abort the whole test script, but will fail only that test case.

Quoted 7 lines

- bad: 'test_create_repo' is responsible for creating the repository in the trash directory as well; if that were to fail for any reason, then the test script will not be aborted early.

I think the 'exit' on error should be removed from 'test_create_repo', but the callsite in 'test-lib.sh' should become 'test_create_repo || exit 1'.

Case in point: the bug I just reported in

https://public-inbox.org/git/[email protected]/

does break Git in a way that in one of our CI jobs 'git init' is unable to create the repository in the trash directory.

#34 lore raw

Re: [PATCH 12/16] test-lib: modernize test_create_repo() function

In reply to SZEDER Gábor
Quoted 8 lines · SZEDER Gábor
Quoted 7 lines · Ævar Arnfjörð Bjarmason

Remove redundant "mkdir -p", argument number checking', test environment sanity checking, and disabling of hooks from test_create_repo(). As we'll see below these were all either redundant to other test other framework code, or to changes in git itself.

Respectively:

Quoted 8 lines
Quoted 6 lines

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. We don't need to check the number of arguments anymore, instead we'll feed "git init" with "$@". It will die if given too many arguments.

Or it will succeed if invoked as e.g. 'test_create_repo --bare repo'.

Which is fine, no? I'll make it clearer in the commit message, but I'm aiming to address plausible bugs due to these functions not erroring out in one way or another, if you call it with "--bare repo" that's clearly what you wanted, as opposed to "foo bar" and we don't create a repo under "bar".

Quoted 7 lines
Quoted 2 lines

3. We won't ever hit that "Cannot setup test environment" error.

ENOSPC? Some rogue background process on Windows still desperately clinging to an open file descriptor to some file in the same directory, preventing 'rm -rf "$TRASH_DIRECTORY"' near the beginning of 'test-lib.sh' and interfering with 'git init'?

I mean to say we won't hit it for the reasons we'd have that error there in the first place.

Of course "git init" can still error, but we'll catch that in other ways.

Quoted 18 lines
Quoted 18 lines

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

Quoted 7 lines
Quoted 2 lines

So all of that's been checked already, and we don't need to re-check it here.

I agree that if we already have a 'git' binary that can run 'git version', then we can safely assume that it will be able to run 'git init' as well. It might be that 'git init' is buggy and segfaults, but that is not a "have you built things yet?" kind of error.

*nod*

Quoted 10 lines
Quoted 10 lines

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

Quoted 6 lines
Quoted 2 lines

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

We needed the subshell because we changed directory in 'test_create_repo' (even you referenced 0d314ce834d above), not because we moved the hooks directory.

Well, yes and no, a minor point but we'd need to do some juggling with saving the $PWD in a variable etc. if it wasn't for the subshell, I think that's why it got added in the first place.

Quoted 4 lines
Quoted 2 lines

In the end it turns out that all we needed was a plain "git init" invocation with a custom --template directory.

We don't need '--template', either; see below.

Ah, interesting.

Quoted 23 lines
Quoted 10 lines
t/test-lib-functions.sh +0 -2
index c81726acb9e..1258329fdd8 100644
@@ -1252,18 +1252,9 @@ test_atexit () {
 # Most tests can use the created repository, but some may need to create more.
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"

t/test-lib-functions.sh test_atexit () { · line 1266

Quoted 10 lines
t/test-lib-functions.sh +0 -10
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit

This patch removes this '|| exit', which is...

Quoted 3 lines

- good: if 'test_create_repo' is invoked in a test case (and not in a subshell) and if it were to fail for some reason, then it won't abort the whole test script, but will fail only that test case.

*nod*

Quoted 10 lines

- bad: 'test_create_repo' is responsible for creating the repository in the trash directory as well; if that were to fail for any reason, then the test script will not be aborted early.

I think the 'exit' on error should be removed from 'test_create_repo', but the callsite in 'test-lib.sh' should become 'test_create_repo || exit 1'.

In any case, removing this '|| exit' is not mentioned in the commit message.

I'll fix that code. FWIW I the "|| exit 1" is effectively not removed in almost all cases where "git init" would error, because in test-lib.sh we cd to the just-init-ed directory, and if we can't we'll fail.

But yes, it won't catch e.g. git-init dying midway through, us running out of space while populating that directory etc.

Quoted 17 lines

t/test-lib-functions.sh test_atexit () { · line 1255

Quoted 1 line
t/test-lib-functions.sh +1 -0
+	"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \

PATH is already set up to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before 'test_create_repo' is called to init the repo in the trash directory, so we could simply run 'git' and rely on PATH lookup choosing the right executable.

t/test-lib-functions.sh test_atexit () { · line 1257

Quoted 2 lines
t/test-lib-functions.sh +2 -0
+		init \
+		"--template=$GIT_BUILD_DIR/templates/blt/" "$@" >&3 2>&4

Likewise, GIT_TEMPLATE_DIR is already set up to the same value as in this '--template=...' option, so we could omit this option as well.

And after that all that would remain in this function is:

git init "$1" >&3 2>&4

Quoted 7 lines

And those redirections are only needed when this function is called to initialize the repo in the trash directory, but not when it creates a repo in a test case. This makes me question the value of having this 'test_create_repo' helper function at all; there are already over 800 cases where we run plain 'git init' outside of 't0001-init.sh' instead of 'test_create_repo', though removing it would be definitely cause some churn with over 300 callsites.

I'd missed how GIT_TEMPLATE_DIR was being set, that allows us to make this much simpler indeed!

Quoted 6 lines
Quoted 6 lines
t/test-lib-functions.sh +0 -0
 }
 
 # This function helps on symlink challenged file systems when it is not
Signature · 1 line
2.31.1.634.gb41287a30b0
#35 lore raw

PATCH 13/16 test-lib-functions: normalize test_path_is_missing() debugging

In reply to Ævar Arnfjörð Bjarmason

Change the test_path_is_missing() to be consistent with related functions. Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists. Let's just say that it exists instead.

---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index 1258329fdd8..2518a9b8274 100644
@@ -811,12 +811,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
Signature · 1 line
2.31.1.634.gb41287a30b0
#36 lore raw

PATCH 14/16 test-lib-functions: use "return 1" instead of "false"

In reply to Ævar Arnfjörð Bjarmason

Change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

---
 t/test-lib-functions.sh | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
t/test-lib-functions.sh +4 -4
index 2518a9b8274..c46bf0ff09c 100644
@@ -763,7 +763,7 @@ test_path_is_file () {
 	if ! test -f "$1"
 	then
 		echo "File $1 doesn't exist"
-		false
+		return 1
 	fi
 }
 
@@ -772,7 +772,7 @@ test_path_is_dir () {
 	if ! test -d "$1"
 	then
 		echo "Directory $1 doesn't exist"
-		false
+		return 1
 	fi
 }
 
@@ -781,7 +781,7 @@ test_path_exists () {
 	if ! test -e "$1"
 	then
 		echo "Path $1 doesn't exist"
-		false
+		return 1
 	fi
 }
 
@@ -803,7 +803,7 @@ test_file_not_empty () {
 	if ! test -s "$1"
 	then
 		echo "'$1' is not a non-empty file."
-		false
+		return 1
 	fi
 }
 
Signature · 1 line
2.31.1.634.gb41287a30b0
#37 lore raw

Re: [PATCH 14/16] test-lib-functions: use "return 1" instead of "false"

Quoted 3 lines · Ævar Arnfjörð Bjarmason

Change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

While not wrong per-se, this and a few other changes in this series are typical of your multi-patch series that unnecessarily consume reviewer bandwidth that could be better spent on other more important and correctness sensitive steps (like 07/16) in the same series.

You should find a good balance to avoid wearing out and distracting your reviewers---they are the most scarce resource around here that must be shared by other people, not just with other topics of your own.

Thanks.

#38 lore raw

PATCH 15/16 Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

When I added these BUG assertions in e7884b353b7 I missed that this made the -x output much more verbose.

E.g. for each test_cmp invocation we'd now emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Let's instead use "$@" instead of "$1" to achieve the same ends with no extra -x output verbosity. The "test" operator will die with an error if given more than one argument in these contexts, so using "$@" achieves the same goal.

The same goes for "cmp" and "diff -u" (which we typically use for test_cmp).

---
 t/test-lib-functions.sh | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)
t/test-lib-functions.sh +16 -25
index c46bf0ff09c..2cf72b56851 100644
@@ -759,39 +759,35 @@ test_external_without_stderr () {
 # debugging-friendly alternatives to "test [-f|-d|-e]"
 # The commands test the existence or non-existence of $1
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
+		echo "Directory $@ doesn't exist"
 		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
+		echo "Path $@ doesn't exist"
 		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$@' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -799,17 +795,15 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
+		echo "'$@' is not a non-empty file."
 		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
 		echo "Path $1 exists!"
 		false
@@ -1013,7 +1007,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1043,7 +1036,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1104,12 +1096,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$@' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.634.gb41287a30b0
#39 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 8 lines · Ævar Arnfjörð Bjarmason

test_path_is_file () {

t/test-lib-functions.sh +2 -3
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1

What does it even mean to call

test_path_is_file Documentation/ Makefile

with this patch applied?

If there were three files "COPYING Makefile", "COPYING", and "Makefile", what would happen when you did

test_path_is_file COPYING Makefile

(without dq around them)?

I think this particular medicine is far worse than the symptom it tries to cure.

#40 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 20 lines · Junio C Hamano

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 8 lines · Ævar Arnfjörð Bjarmason

test_path_is_file () {

t/test-lib-functions.sh +2 -3
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1

What does it even mean to call

test_path_is_file Documentation/ Makefile

with this patch applied?

If there were three files "COPYING Makefile", "COPYING", and "Makefile", what would happen when you did

Quoted 6 lines

test_path_is_file COPYING Makefile

(without dq around them)?

I think this particular medicine is far worse than the symptom it tries to cure.

We'll error with:

test: foo: unexpected operator

And then say:

File COPYING Makefile doesn't exist

I thought guarding just for the one-off development error of not using the function correctly wasn't worth it, but I thought it made sense not to litter all of this with:

	diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
	index 28b8826e565..0bd7367a07e 100644
	--- a/t/test-lib-functions.sh
	+++ b/t/test-lib-functions.sh
	@@ -690,6 +690,7 @@ test_expect_success () {
	 test_path_is_file () {
	 	if ! test -f "$@"
	 	then
	+		test $# -eq 1 || BUG "Do not call test_path_is_file() with more than one argument!"
	 		echo "File $@ doesn't exist"
	 		return 1
	 	fi

But I could do that if you think it's better.

We could also just emit $1 in the "echo". I don't feel strongly about that, but think that's arguably worse, since having it be $@ means the developer is more likely to notice the invalid usage right away.

#41 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 24 lines · Ævar Arnfjörð Bjarmason
Quoted 22 lines · Junio C Hamano

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 8 lines · Ævar Arnfjörð Bjarmason

test_path_is_file () {

t/test-lib-functions.sh +2 -3
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1

What does it even mean to call

test_path_is_file Documentation/ Makefile

with this patch applied?

If there were three files "COPYING Makefile", "COPYING", and "Makefile", what would happen when you did

test_path_is_file COPYING Makefile

Quoted 8 lines
Quoted 4 lines

(without dq around them)?

I think this particular medicine is far worse than the symptom it tries to cure.

We'll error with:

test: foo: unexpected operator

If you want a single argument you should use "$*", not "$@".

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#42 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason
Quoted 24 lines · Ævar Arnfjörð Bjarmason
Quoted 22 lines · Junio C Hamano

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 8 lines · Ævar Arnfjörð Bjarmason

test_path_is_file () {

t/test-lib-functions.sh +2 -3
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1

What does it even mean to call

test_path_is_file Documentation/ Makefile

with this patch applied?

If there were three files "COPYING Makefile", "COPYING", and "Makefile", what would happen when you did

test_path_is_file COPYING Makefile

Quoted 8 lines
Quoted 4 lines

(without dq around them)?

I think this particular medicine is far worse than the symptom it tries to cure.

We'll error with:

test: foo: unexpected operator

Ah, so use of "$@" was intentional. That's clever (I thought it was a common typo people make when they mean "$*").

Of course, it would not work if the caller did a nonsense like so:

test_path_is_file foo -o ok

but as long as we trust that the callers would not make stupid mistakes, this is OK. Is that the reasoning behind this removal of the BUG?

Quoted 4 lines

I thought guarding just for the one-off development error of not using the function correctly wasn't worth it, but I thought it made sense not to litter all of this with:

Quoted 9 lines
	diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
	index 28b8826e565..0bd7367a07e 100644
	--- a/t/test-lib-functions.sh
	+++ b/t/test-lib-functions.sh
	@@ -690,6 +690,7 @@ test_expect_success () {
	 test_path_is_file () {
	 	if ! test -f "$@"
	 	then
	+		test $# -eq 1 || BUG "Do not call test_path_is_file() with more than one argument!"

But this breaks our assumption that the caller would not be making stupid mistakes, so I am not sure if it is worth it. If we were to have a sanity check, shouldn't we do the check upfront, like the original?

Thanks.

#43 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 38 lines · Junio C Hamano
Quoted 24 lines · Ævar Arnfjörð Bjarmason
Quoted 22 lines · Junio C Hamano

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 8 lines · Ævar Arnfjörð Bjarmason

test_path_is_file () {

t/test-lib-functions.sh +2 -3
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1

What does it even mean to call

test_path_is_file Documentation/ Makefile

with this patch applied?

If there were three files "COPYING Makefile", "COPYING", and "Makefile", what would happen when you did

test_path_is_file COPYING Makefile

Quoted 8 lines
Quoted 4 lines

(without dq around them)?

I think this particular medicine is far worse than the symptom it tries to cure.

We'll error with:

test: foo: unexpected operator

Ah, so use of "$@" was intentional. That's clever (I thought it was a common typo people make when they mean "$*").

Quoted 7 lines

Of course, it would not work if the caller did a nonsense like so:

test_path_is_file foo -o ok

but as long as we trust that the callers would not make stupid mistakes, this is OK. Is that the reasoning behind this removal of the BUG?

The reasoning is to get rid of verbosity in the trace output, while still effectively retaining the error checking.

Yes you could do "foo -o ok", but as my already-on-master fixes to the few misuses showed we only realistically have to worry about them being used with many normal looking file names (if that).

Quoted 14 lines
Quoted 4 lines

I thought guarding just for the one-off development error of not using the function correctly wasn't worth it, but I thought it made sense not to litter all of this with:

Quoted 9 lines
	diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
	index 28b8826e565..0bd7367a07e 100644
	--- a/t/test-lib-functions.sh
	+++ b/t/test-lib-functions.sh
	@@ -690,6 +690,7 @@ test_expect_success () {
	 test_path_is_file () {
	 	if ! test -f "$@"
	 	then
	+		test $# -eq 1 || BUG "Do not call test_path_is_file() with more than one argument!"
Quoted 4 lines

But this breaks our assumption that the caller would not be making stupid mistakes, so I am not sure if it is worth it. If we were to have a sanity check, shouldn't we do the check upfront, like the original?

It's fine to do sanity checking if it's in the "else" branch where we're already emitting an error, and we only do so in the BUG case.

I.e. if it's written like e.g. this:

test_path_is_file () {
if test $# -ne 1
then
BUG "Do not call test_path_is_file() with more than one argument!"
elif ! test -f "$@"
then
echo "File $@ doesn't exist"

Then with e.g.:

    ./t3600-rm.sh  --run=1-3 -vx

We get:

+ test_path_is_file foo
+ test 1 -ne 1
+ test -f foo
+ git ls-files --error-unmatch foo

But if we, as my patch does, piggy-pack on the test-built in to panic on too many arguments we get the much more succinct:

+ test_path_is_file foo
+ test -f foo
+ git ls-files --error-unmatch foo

I think that's the only trace output that matters, having "test 2 -ne 1" or whatever in the case where we're just about to invoke BUG anyway is fine.

#44 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason
Quoted 19 lines · Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

When I added these BUG assertions in e7884b353b7 I missed that this made the -x output much more verbose.

E.g. for each test_cmp invocation we'd now emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Quoted 4 lines

Let's instead use "$@" instead of "$1" to achieve the same ends with no extra -x output verbosity. The "test" operator will die with an error if given more than one argument in these contexts, so using "$@" achieves the same goal.

I prefer the current check for its explicitness over the implicit and somewhat cryptic approach introduced in this patch. I hope that sooner or later I'll finish up my patch series to suppress '-x' output from test helper functions, and then this issue will become moot anyway.

Quoted 118 lines

The same goes for "cmp" and "diff -u" (which we typically use for test_cmp).

---
 t/test-lib-functions.sh | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)
t/test-lib-functions.sh +16 -25
index c46bf0ff09c..2cf72b56851 100644
@@ -759,39 +759,35 @@ test_external_without_stderr () {
 # debugging-friendly alternatives to "test [-f|-d|-e]"
 # The commands test the existence or non-existence of $1
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
+		echo "File $@ doesn't exist"
 		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
+		echo "Directory $@ doesn't exist"
 		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
+		echo "Path $@ doesn't exist"
 		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$@' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -799,17 +795,15 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
+		echo "'$@' is not a non-empty file."
 		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
 		echo "Path $1 exists!"
 		false
@@ -1013,7 +1007,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1043,7 +1036,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1104,12 +1096,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$@' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.634.gb41287a30b0
#45 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 3 lines · SZEDER Gábor

... suppress '-x' output
from test helper functions, and then this issue will become moot anyway.

That is certainly a better approach than tweaking each call site of BUG.

The interface to BUG is "write a code to determine condition and then die by calling BUG", which means under '-x' you are bound to see the trace from "code to determine condition" part.

I wonder if introducing a BUG_ON helper function that

- turns off '-x' trace upon entry;
- takes a condition as one of its arguments and evals it;
- issues a message and dies if needed;
- otherwise arranges to turn '-x' trace on and return.

would solve it well?

Thanks.

#46 lore raw

Re: [PATCH 15/16] Revert and amend "test-lib-functions: assert correct parameter count"

In reply to SZEDER Gábor
Quoted 20 lines · SZEDER Gábor
Quoted 19 lines · Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

When I added these BUG assertions in e7884b353b7 I missed that this made the -x output much more verbose.

E.g. for each test_cmp invocation we'd now emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Quoted 7 lines
Quoted 4 lines

Let's instead use "$@" instead of "$1" to achieve the same ends with no extra -x output verbosity. The "test" operator will die with an error if given more than one argument in these contexts, so using "$@" achieves the same goal.

I prefer the current check for its explicitness over the implicit and somewhat cryptic approach introduced in this patch.

Fair enough, I think it's worth it to have a bit of a non-obvious pattern there for less trace verbosity across the board.

Quoted 3 lines

I hope that sooner or later I'll finish up my patch series to suppress '-x' output from test helper functions, and then this issue will become moot anyway.

That sounds like an interesting feature for those who want it, but it's entirely orthagonal to the direction this patch is taking.

I'd like to have trace output for when tests descend into test-lib-functions.sh and friends, I'd just like the most frequently used code there to not be needlessly verbose.

That's not the same as wanting to hide it entirely, i.e. treat test_path_is_file et al as though they were shell built-ins.

#47 lore raw

PATCH 16/16 test-lib-functions: remove last two parameter count assertions

In reply to Ævar Arnfjörð Bjarmason

Remove a couple of parameter count assertions where we'll now silently do the wrong thing if given too many parameters, unlike the "$@" cases in the preceding commit where "test" etc. handle the check for usi.

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just use "$@" in that case anyway, there's only a few callers, and eventually those should probably be moved to use the test-tool directly.

That only leaves test_line_count, I suppose I could leave that one alone, but since it's the only common function left that does this assertion let's remove it for the brevity of the -x output and consistency with other functions.

---
 t/test-lib-functions.sh | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
t/test-lib-functions.sh +2 -6
index 2cf72b56851..32d6b98b972 100644
@@ -822,10 +822,7 @@ test_path_is_missing () {
 # output through when the number of lines is wrong.
 
 test_line_count () {
-	if test $# != 3
-	then
-		BUG "not 3 parameters to test_line_count"
-	elif ! test $(wc -l <"$3") "$1" "$2"
+	if ! test $(wc -l <"$3") "$1" "$2"
 	then
 		echo "test_line_count: line count for $3 !$1 $2"
 		cat "$3"
@@ -834,8 +831,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
Signature · 1 line
2.31.1.634.gb41287a30b0
#48 lore raw

Re: [PATCH 16/16] test-lib-functions: remove last two parameter count assertions

Quoted 3 lines · Ævar Arnfjörð Bjarmason

Remove a couple of parameter count assertions where we'll now silently do the wrong thing if given too many parameters, unlike the "$@" cases in the preceding commit where "test" etc. handle the check for usi.

What's "usi" (not a "cow", I presume)?

If test_line_count is given 2 or 4 parameters, we'd call BUG---does that "silently do the wrong thing"? Why?

Quoted 42 lines

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just use "$@" in that case anyway, there's only a few callers, and eventually those should probably be moved to use the test-tool directly.

That only leaves test_line_count, I suppose I could leave that one alone, but since it's the only common function left that does this assertion let's remove it for the brevity of the -x output and consistency with other functions.

---
 t/test-lib-functions.sh | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
t/test-lib-functions.sh +2 -6
index 2cf72b56851..32d6b98b972 100644
@@ -822,10 +822,7 @@ test_path_is_missing () {
 # output through when the number of lines is wrong.
 
 test_line_count () {
-	if test $# != 3
-	then
-		BUG "not 3 parameters to test_line_count"
-	elif ! test $(wc -l <"$3") "$1" "$2"
+	if ! test $(wc -l <"$3") "$1" "$2"
 	then
 		echo "test_line_count: line count for $3 !$1 $2"
 		cat "$3"
@@ -834,8 +831,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
#49 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 9 lines · Ævar Arnfjörð Bjarmason

This is a "various small fixes" series to test-lib.sh, test-lib-functions.sh.

I have a couple of outstanding patch serieses that I've pulled this from (ab/describe-tests-fix, ab/pickaxe-pcre2), as well as some other local work I've been having a chicken and egg problem with.

I think the approach of pulling out these various miscellaneous test fixes into their own topic makes the most sense,...

I guess I'm expected to discard the other topics that have been expecting rerolls from my tree to make room for this, queue this topic and wait it to stabilize, before taking the rerools of these other topics.

I find it a bit distracting to retract multiple topics that have been already reviewed and reorganizing them in a new topic. The reviewer resource spent for the last round might not be completely lost, but for reviewers it makes it unnecessarily harder to verify which review comments have been addressed and which parts haven't changed, so the net effect is to require them a fresh review anyway. If you can entice reviewers to review this new series and making them giving it priority over other topics in flight, that is.

Quoted 17 lines

Ævar Arnfjörð Bjarmason (16):
check-non-portable-shell: complain about "test" a/-o instead of &&/||
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated-tag option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: modernize test_create_repo() function
test-lib-functions: normalize test_path_is_missing() debugging
test-lib-functions: use "return 1" instead of "false" Revert and amend "test-lib-functions: assert correct parameter count"
test-lib-functions: remove last two parameter count assertions

#50 lore raw

v2 cover test-lib.sh: new test_commit args, simplification & fixes

In reply to Ævar Arnfjörð Bjarmason

This is a "various small fixes" series to test-lib.sh, test-lib-functions.sh.

I've addressed the feedback on v1, the "test_create_repo" function is now purely a dumb "git init" wrapper without any arguments, thanks to SZEDER's observation.

The trickery with "test" arguments and "$@" later in the series seemed more controversial, so I've peeled those patches off. I'll submit them as another series, I've got various other serieses waiting on the "test_commit" functions here so I'd like to get this in sooner than later, rather than having it hang on the discussion around that.

Ævar Arnfjörð Bjarmason (12):
check-non-portable-shell: check for "test <cond> -a/-o <cond>"
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/check-non-portable-shell.pl       |  2 +
 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 +++++++---------------------
 t/test-lib-functions.sh             | 60 ++++++++++++++++++-----------
 t/test-lib.sh                       | 41 ++++++++++++--------
 15 files changed, 90 insertions(+), 119 deletions(-)

Range-diff against v1:
1: 8e4b4a2a216 ! 1: a8b483bc771 check-non-portable-shell: complain about "test" a/-o instead of &&/||
@@ Metadata
Author: Ævar Arnfjörð Bjarmason <[email protected]>

## Commit message ##
- check-non-portable-shell: complain about "test" a/-o instead of &&/||
+ check-non-portable-shell: check for "test <cond> -a/-o <cond>"

- Expand the t/check-non-portable-shell.pl checks to complain about the
- use of "-a" and "-o" to the "test" shell built-in to to mean "and" and
- "or", as opposed to using two "test" invocations with "&&" or "||".
+ Add a check for -a/-o in "test", as a follow-up to the
+ CodingGuidelines having recommended against their use since
+ 897f964c0dc (CodingGuidelines: avoid "test <cond> -a/-o <cond>",
+ 2014-05-20).

- There aren't any portability issues with using that construct that I
- know of, but since Junio expressed a dislike of it in [1] and we've
- currently got no such constructs let's add it to the lint checking. I
- had various in-flight and WIP patches that used this construct.
+ These constructs are considered obsolescent by POSIX[1]. GNU has
+ likewise warned against them[2] for a few decades.

- 1. https://lore.kernel.org/git/[email protected]/
+ These will only match the simplistic forms of test -X blah (where
+ "-X" is some single letter option), but will miss expressions such as
+ test "$foo" = bar. We stop at "&" or "|" to try not to overmatch
+ things like:
+
+ test whatever && ls -a foo
+ test whatever && foo -o outfile
+
+ 1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
+ 2. https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

## t/check-non-portable-shell.pl ##
@@ t/check-non-portable-shell.pl: sub err {
- next if $line =~ s/\\$//;
-
- $_ = $line;
-+ # Portability issues
- /\bcp\s+-a/ and err 'cp -a is not portable';
- /\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
- /\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
-@@ t/check-non-portable-shell.pl: sub err {
- /\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
- /^\s*([A-Z0-9_]+=(\w*|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
- err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
-+ # Coding style preferences
-+ /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
-+ /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
- $line = '';
- # this resets our $. for each file
- close ARGV if eof;
+ /^\s*declare\s+/ and err 'arrays/declare not portable';
+ /^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
+ /\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
++ /\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
++ /\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
+ /\bwc -l.*"\s*=/ and err '"$(wc -l)" is not portable (use test_line_count)';
+ /\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
+ /(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
2: 44223ae777e ! 2: 39759d00adf test-lib: bring $remove_trash out of retirement
@@ Commit message
test-lib: bring $remove_trash out of retirement

There's no point in creating a repository or directory only to decide
- right afterwards that we're skipping all the tests.
+ right afterwards that we're skipping all the tests. We can save
+ ourselves the redundant "git init" or "mkdir" and "rm -rf" in this
+ case.
+
+ We carry around the "$remove_trash" variable because if the directory
+ is unexpectedly gone at test_done time we'll hit the error about it
+ being unexpectedly gone added in df4c0d1a792 (test-lib: abort when
+ can't remove trash directory, 2017-04-20).

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all

 3:  3d04553d710 =  3:  d669ce31961 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 4:  4ecce153b99 =  4:  9313d35bf86 test-lib-functions: reword "test_commit --append" docs
 5:  6df03776940 !  5:  c916d648e49 test-lib-functions: document test_commit --no-tag

@@ t/test-lib-functions.sh: debug () {
# --author <author>
# Invoke "git commit" with --author <author>
+# --no-tag
-+# Do not tag the resulting commit, if supplied giving the
-+# optional "<tag>" argument is an error.
++# Do not tag the resulting commit
#
# This will commit a file with the given contents and the given commit
# message, and tag the resulting commit with the given tag name.
6: 8d43fdd5865 ! 6: 9b6bf65ce1c test-lib functions: add an --annotated-tag option to "test_commit"
@@ Metadata
Author: Ævar Arnfjörð Bjarmason <[email protected]>

## Commit message ##
- test-lib functions: add an --annotated-tag option to "test_commit"
+ test-lib functions: add an --annotated option to "test_commit"

- Add an --annotated-tag option to test_commit. The tag will share the
- same message as the commit, and we'll call test_tick before creating
- it (unless --notick) is provided.
+ Add an --annotated option to test_commit to create annotated tags. The
+ tag will share the same message as the commit, and we'll call
+ test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a
@@ t/t1403-show-ref.sh: export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME

## t/test-lib-functions.sh ##
@@ t/test-lib-functions.sh: debug () {
+ # Invoke "git commit" with --author <author>
# --no-tag
- # Do not tag the resulting commit, if supplied giving the
- # optional "<tag>" argument is an error.
+ # Do not tag the resulting commit
+# --annotate
+# Create an annotated tag with "--annotate -m <message>". Calls
-+# test_tick between making the commit and tag unless --notick is
-+# given.
++# test_tick between making the commit and tag, unless --notick
++# is given.
#
# This will commit a file with the given contents and the given commit # message, and tag the resulting commit with the given tag name.
@@ t/test-lib-functions.sh: test_commit () {
- fi
+ ;;
+ annotate)
-+ if test "$tag$notick" = "annotate"
++ if test -z "$notick"
+ then
+ test_tick
+ fi &&

 7:  ec4809e7466 =  7:  683b3ba3dd9 describe tests: convert setup to use test_commit
 8:  352eeff41c9 =  8:  dc0a863db72 test-lib functions: add --printf option to test_commit
 9:  e7432294215 =  9:  90bf55d2d42 submodule tests: use symbolic-ref --short to discover branch name
10:  634038c3a8d = 10:  e0a1e2fd529 test-lib: reformat argument list in test_create_repo()
11:  cdbff4df362 = 11:  cedf5d383b0 test-lib: do not show advice about init.defaultBranch under --verbose
12:  424caad189f ! 12:  a3e20ef18f7 test-lib: modernize test_create_repo() function

@@ Metadata
Author: Ævar Arnfjörð Bjarmason <[email protected]>

## Commit message ##
- test-lib: modernize test_create_repo() function
+ test-lib: split up and deprecate test_create_repo()

- Remove redundant "mkdir -p", argument number checking', test
- environment sanity checking, and disabling of hooks from
- test_create_repo(). As we'll see below these were all either redundant
- to other test other framework code, or to changes in git itself.
+ Remove various redundant or obsolete code from the test_create_repo()
+ function, and split up its use in test-lib.sh from what tests need
+ from it, leaving us with a pass-through wrapper for "git init" in
+ test-lib-functions.sh

- Respectively:
+ Reasons for why we can remove various code from test_create_repo():

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

- 2. We don't need to check the number of arguments anymore, instead
- we'll feed "git init" with "$@". It will die if given too many
- arguments.
+ 2. Since we're now a simple wrapper for "git init" we don't need to
+ check that we have only one argument. If someone wants to run
+ "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.
@@ Commit message
5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

+ That wasn't really needed for the .git/hooks either, but was being
+ done for the convenience of not having to quote the path to the
+ repository as we moved the hooks.
+
+ 6. We can drop the --template argument and instead rely on the
+ GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See
+ 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)
+
+ 7. We only needed that ">&3 2>&4" redirection when invoked from
+ test-lib.sh, and the same goes for needing the full path to "git".
+
+ Let's move that special behavior into test-lib.sh itself.
+
In the end it turns out that all we needed was a plain "git init"
- invocation with a custom --template directory.
+ invocation.

@@ t/t5409-colorize-remote-messages.sh: test_description='remote messages are color

## t/test-lib-functions.sh ##
@@ t/test-lib-functions.sh: test_atexit () {
- # Most tests can use the created repository, but some may need to create more.
+ } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
+ }
+
+-# Most tests can use the created repository, but some may need to create more. ++# Deprecated wrapper for "git init", use "git init" directly instead
# Usage: test_create_repo <directory>
test_create_repo () {
- test "$#" = 1 ||
@@ t/test-lib-functions.sh: test_atexit () {
- error "cannot run git init -- have you built things yet?"
- mv .git/hooks .git/hooks-disabled
- ) || exit
-+ "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-+ init \
-+ "--template=$GIT_BUILD_DIR/templates/blt/" "$@" >&3 2>&4
++ git init "$@"
}

# This function helps on symlink challenged file systems when it is not
+
+ ## t/test-lib.sh ##
+@@ t/test-lib.sh: rm -fr "$TRASH_DIRECTORY" || {
+ remove_trash=t
+ if test -z "$TEST_NO_CREATE_REPO"
+ then
+- test_create_repo "$TRASH_DIRECTORY"
++ "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" \
++ init \
++ "$TRASH_DIRECTORY" >&3 2>&4 ||
++ error "cannot run git init"
+ else
+ mkdir -p "$TRASH_DIRECTORY"
+ fi

13:  0813aa8e34e <  -:  ----------- test-lib-functions: normalize test_path_is_missing() debugging
14:  b6e9d971b40 <  -:  ----------- test-lib-functions: use "return 1" instead of "false"
15:  0cd511206c4 <  -:  ----------- Revert and amend "test-lib-functions: assert correct parameter count"
16:  b4a018a63f3 <  -:  ----------- test-lib-functions: remove last two parameter count assertions
Signature · 1 line
2.31.1.722.g788886f50a2
#51 lore raw

PATCH v2 1/12 check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Add a check for -a/-o in "test", as a follow-up to the CodingGuidelines having recommended against their use since 897f964c0dc (CodingGuidelines: avoid "test <cond> -a/-o <cond>", 2014-05-20).

These constructs are considered obsolescent by POSIX[1]. GNU has likewise warned against them[2] for a few decades.

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
2. https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

---
 t/check-non-portable-shell.pl | 2 ++
 1 file changed, 2 insertions(+)
t/check-non-portable-shell.pl +2 -0
index fd3303552be..894aa0a4f92 100755
@@ -41,6 +41,8 @@ sub err {
 	/^\s*declare\s+/ and err 'arrays/declare not portable';
 	/^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
 	/\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
 	/\bwc -l.*"\s*=/ and err '`"$(wc -l)"` is not portable (use test_line_count)';
 	/\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
 	/(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
Signature · 1 line
2.31.1.722.g788886f50a2
#52 lore raw

PATCH v2 2/12 test-lib: bring $remove_trash out of retirement

In reply to Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
t/test-lib.sh +17 -14
index 3dec266221c..7522faf39fc 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,21 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1356,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1368,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.722.g788886f50a2
#53 lore raw

PATCH v2 3/12 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27a..2c6e34b9478 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.722.g788886f50a2
#54 lore raw

PATCH v2 4/12 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index 6348e8d7339..d169fb2f597 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.722.g788886f50a2
#55 lore raw

PATCH v2 5/12 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 2 ++
 1 file changed, 2 insertions(+)
t/test-lib-functions.sh +2 -0
index d169fb2f597..d0f4f3885d6 100644
@@ -177,6 +177,8 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.722.g788886f50a2
#56 lore raw

PATCH v2 6/12 test-lib functions: add an --annotated option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated option to test_commit to create annotated tags. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c3..17d3cc14050 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index d0f4f3885d6..6e2332a324a 100644
@@ -179,6 +179,10 @@ debug () {
 #	Invoke "git commit" with --author <author>
 #   --no-tag
 #	Do not tag the resulting commit
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag, unless --notick
+#	is given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -191,7 +195,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -219,7 +223,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -243,10 +250,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test -z "$notick"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.722.g788886f50a2
#57 lore raw

PATCH v2 7/12 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747bee..88fddc91424 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.722.g788886f50a2
#58 lore raw

PATCH v2 8/12 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388e..930dce06f0f 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a2..f691e6d9032 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15b..6cdee2a2164 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60d..a39a626664d 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f9..e2c0c510222 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index 6e2332a324a..dbeb3d92628 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -191,6 +195,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -202,6 +207,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -238,9 +246,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.722.g788886f50a2
#59 lore raw

PATCH v2 9/12 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e93083..f7c7df0ca42 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.722.g788886f50a2
#60 lore raw

PATCH v2 10/12 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index dbeb3d92628..21271020c79 100644
@@ -1257,8 +1257,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.722.g788886f50a2
#61 lore raw

PATCH v2 11/12 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 21271020c79..8e75a013a43 100644
@@ -1258,7 +1258,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index 7522faf39fc..9ebb595c335 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.722.g788886f50a2
#62 lore raw

PATCH v2 12/12 test-lib: split up and deprecate test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it, leaving us with a pass-through wrapper for "git init" in test-lib-functions.sh

Reasons for why we can remove various code from test_create_repo():

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

That wasn't really needed for the .git/hooks either, but was being done for the convenience of not having to quote the path to the repository as we moved the hooks.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh, and the same goes for needing the full path to "git".

Let's move that special behavior into test-lib.sh itself.

In the end it turns out that all we needed was a plain "git init" invocation.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 ++-------------
 t/test-lib.sh                       |  5 ++++-
 5 files changed, 6 insertions(+), 18 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649e..5c509db6fc3 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4e..6da8d760e28 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8ec..9f1a483f426 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +2 -13
index 8e75a013a43..bd64a15c731 100644
@@ -1248,21 +1248,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	git init "$@"
 }
 
 # This function helps on symlink challenged file systems when it is not
t/test-lib.sh +4 -1
index 9ebb595c335..f73c3c6fc72 100644
@@ -1364,7 +1364,10 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" \
+		init \
+		"$TRASH_DIRECTORY" >&3 2>&4 ||
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.722.g788886f50a2
#63 lore raw

Re: [PATCH v2 12/12] test-lib: split up and deprecate test_create_repo()

Quoted 44 lines · Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it, leaving us with a pass-through wrapper for "git init" in test-lib-functions.sh

Reasons for why we can remove various code from test_create_repo():

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

Quoted 5 lines

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory

Since we don't change directory anymore...

Quoted 6 lines

we don't need
the subshell here either.

That wasn't really needed for the .git/hooks either, but was being done for the convenience of not having to quote the path to the repository as we moved the hooks.

And then this dubious explanation will not be necessary.

Quoted 8 lines

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh, and the same goes for needing the full path to "git".

Let's move that special behavior into test-lib.sh itself.

Quoting myself from my review of the previous version of this patch:

PATH is already set up to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before 'test_create_repo' is called to init the repo in the trash directory, so we could simply run 'git' and rely on PATH lookup choosing the right executable.

t/test-lib.sh rm -fr "$TRASH_DIRECTORY" || { · line 1369

Quoted 12 lines
t/test-lib.sh +3 -1
index 9ebb595c335..f73c3c6fc72 100644
@@ -1364,7 +1364,10 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" \
+		init \
+		"$TRASH_DIRECTORY" >&3 2>&4 ||

So this could be just:

git init "$TRASH_DIRECTORY" >&3 2>&4 ||

Quoted 7 lines
t/test-lib.sh +1 -0
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.722.g788886f50a2
#64 lore raw

Re: [PATCH v2 12/12] test-lib: split up and deprecate test_create_repo()

Quoted 53 lines · SZEDER Gábor
Quoted 44 lines · Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it, leaving us with a pass-through wrapper for "git init" in test-lib-functions.sh

Reasons for why we can remove various code from test_create_repo():

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

Quoted 5 lines

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory

Since we don't change directory anymore...

Quoted 8 lines
Quoted 6 lines

we don't need
the subshell here either.

That wasn't really needed for the .git/hooks either, but was being done for the convenience of not having to quote the path to the repository as we moved the hooks.

And then this dubious explanation will not be necessary.

Why dubious? That's why we had the subshell-ing. See 0d314ce834 (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

I don't mind rewording or not including some of this verbosity per-se, but I wonder why you're honing in on the subshell part in particular. Maybe there's something I'm missing...

The goal of the commit messsage is to point-by-point tear apart facets of the previous behavior, and assure the reader that e.g. the subshelling isn't needed for some other subtle reason.

Quoted 31 lines
Quoted 8 lines

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh, and the same goes for needing the full path to "git".

Let's move that special behavior into test-lib.sh itself.

Quoting myself from my review of the previous version of this patch:

PATH is already set up to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before 'test_create_repo' is called to init the repo in the trash directory, so we could simply run 'git' and rely on PATH lookup choosing the right executable.

t/test-lib.sh rm -fr "$TRASH_DIRECTORY" || { · line 1369

Quoted 12 lines
t/test-lib.sh +3 -1
index 9ebb595c335..f73c3c6fc72 100644
@@ -1364,7 +1364,10 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" \
+		init \
+		"$TRASH_DIRECTORY" >&3 2>&4 ||

So this could be just:

Quoted 1 line

git init "$TRASH_DIRECTORY" >&3 2>&4 ||

Ah yes, I see that now. FWIW I managed to misread that in the last round as it applying only once we were calling the test-lib-functions.sh helper, but I see it's finishe setting up a few lines before the test_create_repo in test-lib.sh too, nice.

#65 lore raw

Re: [PATCH v2 12/12] test-lib: split up and deprecate test_create_repo()

Quoted 11 lines · Ævar Arnfjörð Bjarmason
Quoted 11 lines
Quoted 1 line

5. Since we don't need to move the .git/hooks directory

Since we don't change directory anymore...

Quoted 6 lines

we don't need
the subshell here either.

That wasn't really needed for the .git/hooks either, but was being done for the convenience of not having to quote the path to the repository as we moved the hooks.

Quoted 5 lines
Quoted 1 line

And then this dubious explanation will not be necessary.

Why dubious? That's why we had the subshell-ing. See 0d314ce834 (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

That commit doesn't mention hooks at all, so I'm not sure what I should see there. OTOH, you did specifically write "use a subshell instead of keeping track of the old working directory and cd-ing back when it's done"... which is in line with our best practices on how to change directories in our tests in order to reliably change back even in case of an error.

And that cd-ing back and forth was not added because of the hooks either, but because back in the day when 'test_create_repo' was created 'git init-db' could only create a new repository in the current directory.

Quoted 6 lines

I don't mind rewording or not including some of this verbosity per-se, but I wonder why you're honing in on the subshell part in particular. Maybe there's something I'm missing...

The goal of the commit messsage is to point-by-point tear apart facets of the previous behavior,

Agreed.

Quoted 2 lines

and assure the reader that e.g. the subshelling isn't needed for some other subtle reason.

The role of moving the hooks with respect to the subshell is not a subtlety, it is simply irrelevant. And while you are so focused on this irrevelancy you keep forgetting to mention the factor that actually does matter: that we won't change directories anymore.

#66 lore raw

cover test-lib-functions.sh: trickery to make -x less verbose

In reply to Ævar Arnfjörð Bjarmason

This series makes the "-x" output less verbose for our various helper functions, mainly using the trickery of feeding "$@" to "test" to rely on it to die when given too many arguments.

It was split off from v2 of my larger test-lib.sh series as noted in its CL:
https://lore.kernel.org/git/[email protected]/

The range-diff below is to v1 of the relevant part of that series, showing what's changed just for these commits.

Ævar Arnfjörð Bjarmason (3):
test-lib-functions: normalize test_path_is_missing() debugging Revert and amend "test-lib-functions: assert correct parameter count"
test-lib-functions: remove last two parameter count assertions

 t/test-lib-functions.sh | 64 +++++++++++++++--------------------------
 1 file changed, 23 insertions(+), 41 deletions(-)

Range-diff:

1:  0813aa8e34e = 1:  c22e3f7764f test-lib-functions: normalize test_path_is_missing() debugging
2:  b6e9d971b40 < -:  ----------- test-lib-functions: use "return 1" instead of "false"
3:  0cd511206c4 ! 2:  6f9e09a2017 Revert and amend "test-lib-functions: assert correct parameter count"

@@ Commit message
assert correct parameter count, 2021-02-12) in order to improve the -x output.

- When I added these BUG assertions in e7884b353b7 I missed that this
- made the -x output much more verbose.
+ The goal here is to get rid of the verbosity of having e.g. a "test 2
+ -ne 2" line for every "test_cmp". We use "$@" as an argument to "test"
+ to intentionally feed the "test" operator too many arguments if the
+ functions are called with too many arguments, thus piggy-backing on it
+ to check the number of arguments we get.

- E.g. for each test_cmp invocation we'd now emit:
+ Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

- That "test 2 -ne 2" line is new in e7884b353b7. As noted in
+ That "test 2 -ne 2" line is new in my e7884b353b7. As noted in
45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

- Let's instead use "$@" instead of "$1" to achieve the same ends with
- no extra -x output verbosity. The "test" operator will die with an
- error if given more than one argument in these contexts, so using "$@"
- achieves the same goal.
+ Now we'll get just:

- The same goes for "cmp" and "diff -u" (which we typically use for
- test_cmp).
+ + test_cmp expect actual
+ + eval diff -u "$@"
+ + diff -u expect actual
+
+ This does not to the "right" thing in cases like:
+
+ test_path_is_file x -a y
+
+ Which will now turn into:
+
+ test -f x -a y
+
+ I consider that to be OK given the trade-off that any extra checking
+ would produce more verbose trace output. As shown in 45a2686441b we
+ had issues with these functions being invoked with multiple
+ parameters (e.g. a glob) by accident, we don't need to be paranoid in
+ guarding against hostile misuse from our own test suite.
+
+ While I'm at it change a few functions that relied on a "false" being
+ the last statement in the function to use an explicit "return 1" like
+ the other functions in this file.

@@ t/test-lib-functions.sh: test_external_without_stderr () {
+ if ! test -f "$@"
then
- echo "File $1 doesn't exist"
-+ echo "File $@ doesn't exist"
- return 1
+- false
++ echo "File $* doesn't exist"
++ return 1
fi
}

@@ t/test-lib-functions.sh: test_external_without_stderr () {
+ if ! test -d "$@"
then
- echo "Directory $1 doesn't exist"
-+ echo "Directory $@ doesn't exist"
- return 1
+- false
++ echo "Directory $* doesn't exist"
++ return 1
fi
}

@@ t/test-lib-functions.sh: test_external_without_stderr () {
+ if ! test -e "$@"
then
- echo "Path $1 doesn't exist"
-+ echo "Path $@ doesn't exist"
- return 1
+- false
++ echo "Path $* doesn't exist"
++ return 1
fi
}

@@ t/test-lib-functions.sh: test_external_without_stderr () {
+ if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
then
- echo "Directory '$1' is not empty, it contains:"
-+ echo "Directory '$@' is not empty, it contains:"
++ echo "Directory '$*' is not empty, it contains:"
ls -la "$1"
return 1
fi
@@ t/test-lib-functions.sh: test_dir_is_empty () {
+ if ! test -s "$@"
then
- echo "'$1' is not a non-empty file."
-+ echo "'$@' is not a non-empty file."
- return 1
+- false
++ echo "'$*' is not a non-empty file."
++ return 1
fi
}

@@ t/test-lib-functions.sh: test_dir_is_empty () {
- if test -e "$1"
+ if test -e "$@"
then
- echo "Path $1 exists!"
+- echo "Path $1 exists!"
++ echo "Path $* exists!"
false
+ fi
+ }
@@ t/test-lib-functions.sh: test_expect_code () {
# - not all diff versions understand "-u"

@@ t/test-lib-functions.sh: verbose () {
then
- echo "'$1' is not empty, it contains:"
- cat "$1"
-+ echo "'$@' is not empty, it contains:"
++ echo "'$*' is not empty, it contains:"
+ cat "$@"
return 1
fi
4: b4a018a63f3 ! 3: b7b11a60bcd test-lib-functions: remove last two parameter count assertions
@@ Metadata
## Commit message ##
test-lib-functions: remove last two parameter count assertions

- Remove a couple of parameter count assertions where we'll now silently
- do the wrong thing if given too many parameters, unlike the "$@" cases
- in the preceding commit where "test" etc. handle the check for usi.
+ Remove a couple of parameter count assertions where, unlike the
+ preceding commit's migration to 'test -$x "$@"', we'll now silently do
+ the "wrong" thing if given too many parameters. The benefit is less
+ verbose trace output, as noted in the preceding commit.

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just
- use "$@" in that case anyway, there's only a few callers, and
+ use "$@" in that case anyway. There's only a few callers, and
eventually those should probably be moved to use the test-tool directly.

Signature · 1 line
2.31.1.722.g788886f50a2
#67 lore raw

PATCH 1/3 test-lib-functions: normalize test_path_is_missing() debugging

Change the test_path_is_missing() to be consistent with related functions. Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists. Let's just say that it exists instead.

---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index bd64a15c731..0232cc9f46d 100644
@@ -810,12 +810,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
Signature · 1 line
2.31.1.722.g788886f50a2
#68 lore raw

PATCH 2/3 Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in my e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Now we'll get just:

+ test_cmp expect actual
+ eval diff -u "$@"
+ diff -u expect actual

This does not to the "right" thing in cases like:

test_path_is_file x -a y

Which will now turn into:

test -f x -a y

I consider that to be OK given the trade-off that any extra checking would produce more verbose trace output. As shown in 45a2686441b we had issues with these functions being invoked with multiple parameters (e.g. a glob) by accident, we don't need to be paranoid in guarding against hostile misuse from our own test suite.

While I'm at it change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

---
 t/test-lib-functions.sh | 51 +++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)
t/test-lib-functions.sh +21 -30
index 0232cc9f46d..9f6d46b8b1b 100644
@@ -758,39 +758,35 @@ test_external_without_stderr () {
 # debugging-friendly alternatives to "test [-f|-d|-e]"
 # The commands test the existence or non-existence of $1
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
-		false
+		echo "File $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
-		false
+		echo "Directory $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
-		false
+		echo "Path $* doesn't exist"
+		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$*' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -798,19 +794,17 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
-		false
+		echo "'$*' is not a non-empty file."
+		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
-		echo "Path $1 exists!"
+		echo "Path $* exists!"
 		false
 	fi
 }
@@ -1012,7 +1006,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1042,7 +1035,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1103,12 +1095,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$*' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.722.g788886f50a2
#69 lore raw

Re: [PATCH 2/3] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 6 lines · Ævar Arnfjörð Bjarmason

[...]
The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

My one concern about this change is that it enters the realm of "huh?". Although it's dead-simple to understand what this code is doing:

    test "$#" -ne 1 && BUG "1 param"
    if ! test -f "$1"

the replacement code:

    if ! test -f "$@"

can easily lead to head-scratching, wondering why the author chose to use "$@" rather than the more obvious "$1". This sort of unusual local idiom might normally deserve an in-code comment explaining why the more obvious code is not employed. However, adding a comment at each site would be overkill, so it might be the sort of thing to explain in documentation somewhere ("In order to make -x output less noisy, employ "$@" rather than explicitly checking function arguments when writing new test functions...".) Otherwise, the reader is forced to consult the commit message.

Not a major objection; just voicing the bit of unease I feel about it.

Quoted 1 line

This does not to the "right" thing in cases like:

Channeling Dscho's inner Eric Sunshine[1]: s/to/do/

[1]: https://lore.kernel.org/git/[email protected]/

#70 lore raw

Re: [PATCH 2/3] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 30 lines · Eric Sunshine
Quoted 6 lines · Ævar Arnfjörð Bjarmason

[...]
The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

My one concern about this change is that it enters the realm of "huh?". Although it's dead-simple to understand what this code is doing:

    test "$#" -ne 1 && BUG "1 param"
    if ! test -f "$1"

the replacement code:

    if ! test -f "$@"

can easily lead to head-scratching, wondering why the author chose to use "$@" rather than the more obvious "$1". This sort of unusual local idiom might normally deserve an in-code comment explaining why the more obvious code is not employed. However, adding a comment at each site would be overkill, so it might be the sort of thing to explain in documentation somewhere ("In order to make -x output less noisy, employ "$@" rather than explicitly checking function arguments when writing new test functions...".) Otherwise, the reader is forced to consult the commit message.

Quoted 1 line

Not a major objection; just voicing the bit of unease I feel about it.

I'm including a well-placed comment in the re-roll of this. FWIW this would be much easier to follow with my rejected split-up of test-lib-functions.sh, then we could group these functions into one file:
https://lore.kernel.org/git/[email protected]/

Quoted 3 lines
Quoted 1 line

This does not to the "right" thing in cases like:

Channeling Dscho's inner Eric Sunshine[1]: s/to/do/

Sunshine recursion error? :)

Quoted 1 line

[1]: https://lore.kernel.org/git/[email protected]/

#71 lore raw

PATCH 3/3 test-lib-functions: remove last two parameter count assertions

In reply to Ævar Arnfjörð Bjarmason

Remove a couple of parameter count assertions where, unlike the preceding commit's migration to 'test -$x "$@"', we'll now silently do the "wrong" thing if given too many parameters. The benefit is less verbose trace output, as noted in the preceding commit.

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just use "$@" in that case anyway. There's only a few callers, and eventually those should probably be moved to use the test-tool directly.

That only leaves test_line_count, I suppose I could leave that one alone, but since it's the only common function left that does this assertion let's remove it for the brevity of the -x output and consistency with other functions.

---
 t/test-lib-functions.sh | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
t/test-lib-functions.sh +2 -6
index 9f6d46b8b1b..a2081ebcdea 100644
@@ -821,10 +821,7 @@ test_path_is_missing () {
 # output through when the number of lines is wrong.
 
 test_line_count () {
-	if test $# != 3
-	then
-		BUG "not 3 parameters to test_line_count"
-	elif ! test $(wc -l <"$3") "$1" "$2"
+	if ! test $(wc -l <"$3") "$1" "$2"
 	then
 		echo "test_line_count: line count for $3 !$1 $2"
 		cat "$3"
@@ -833,8 +830,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
Signature · 1 line
2.31.1.722.g788886f50a2
#72 lore raw

v3 cover test-lib.sh: new test_commit args, simplification & fixes

In reply to Ævar Arnfjörð Bjarmason

This is a "various small fixes" series to test-lib.sh, test-lib-functions.sh.

Since v2 the only changes are to 12/12, per SZEDER's comments we can count on $PATH this late in test-lib.sh, so do that, and clarify the commit message for that change per the discussion on v2.

Ævar Arnfjörð Bjarmason (12):
check-non-portable-shell: check for "test <cond> -a/-o <cond>"
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/check-non-portable-shell.pl       |  2 +
 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 +++++++---------------------
 t/test-lib-functions.sh             | 60 ++++++++++++++++++-----------
 t/test-lib.sh                       | 39 +++++++++++--------
 15 files changed, 88 insertions(+), 119 deletions(-)

Range-diff against v2:

 -:  ---------- >  1:  a8b483bc77 check-non-portable-shell: check for "test <cond> -a/-o <cond>"
 -:  ---------- >  2:  39759d00ad test-lib: bring $remove_trash out of retirement
 -:  ---------- >  3:  d669ce3196 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 -:  ---------- >  4:  9313d35bf8 test-lib-functions: reword "test_commit --append" docs
 -:  ---------- >  5:  c916d648e4 test-lib-functions: document test_commit --no-tag
 -:  ---------- >  6:  9b6bf65ce1 test-lib functions: add an --annotated option to "test_commit"
 -:  ---------- >  7:  683b3ba3dd describe tests: convert setup to use test_commit
 -:  ---------- >  8:  dc0a863db7 test-lib functions: add --printf option to test_commit
 -:  ---------- >  9:  90bf55d2d4 submodule tests: use symbolic-ref --short to discover branch name
 -:  ---------- > 10:  e0a1e2fd52 test-lib: reformat argument list in test_create_repo()
 -:  ---------- > 11:  cedf5d383b test-lib: do not show advice about init.defaultBranch under --verbose
 1:  a3e20ef18f ! 12:  0dc0da9490 test-lib: split up and deprecate test_create_repo()

@@ Commit message

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need
- from it, leaving us with a pass-through wrapper for "git init" in
- test-lib-functions.sh
+ from it.

- Reasons for why we can remove various code from test_create_repo():
+ This leave us with a pass-through wrapper for "git init" in
+ test-lib-functions.sh, in test-lib.sh we have the same, except for
+ needing to redirect stdout/stderr, and emitting an error ourselves if
+ it fails. We don't need to error() ourselves when test_create_repo()
+ is invoked, as the invocation will be a part of a test's "&&"-chain.
+
+ Everything below this paragraph is a detailed summary of the history
+ of test_create_repo() explaining why it's safe to remove the various
+ things it was doing:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.
@@ Commit message
5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

- That wasn't really needed for the .git/hooks either, but was being
- done for the convenience of not having to quote the path to the
- repository as we moved the hooks.
+ See 0d314ce834 for when the subshell use got introduced for the
+ convenience of not having to "cd" back and forth while setting up
+ the hooks.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from
- test-lib.sh, and the same goes for needing the full path to "git".
+ test-lib.sh.
+
+ We could still invoke test_create_repo() there, but as the
+ invocation is now trivial and we don't have a good reason to use
+ test_create_repo() elsewhere let's call "git init" there
+ ourselves.

- Let's move that special behavior into test-lib.sh itself.
+ 8. We didn't need to resolve "git" as
+ "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(),
+ even for the use of test-lib.sh

- In the end it turns out that all we needed was a plain "git init"
- invocation.
+ PATH is already set up in test-lib.sh to start with
+ GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before
+ test_create_repo() (now "git init") is called.. So we can simply
+ run "git" and rely on the PATH lookup choosing the right
+ executable.

@@ t/test-lib.sh: rm -fr "$TRASH_DIRECTORY" || {
if test -z "$TEST_NO_CREATE_REPO"
then
- test_create_repo "$TRASH_DIRECTORY"
-+ "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" \
-+ init \
-+ "$TRASH_DIRECTORY" >&3 2>&4 ||
++ git init "$TRASH_DIRECTORY" >&3 2>&4 ||
+ error "cannot run git init"
else
mkdir -p "$TRASH_DIRECTORY"

Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#73 lore raw

PATCH v3 1/12 check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Add a check for -a/-o in "test", as a follow-up to the CodingGuidelines having recommended against their use since 897f964c0dc (CodingGuidelines: avoid "test <cond> -a/-o <cond>", 2014-05-20).

These constructs are considered obsolescent by POSIX[1]. GNU has likewise warned against them[2] for a few decades.

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16
2. https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html

---
 t/check-non-portable-shell.pl | 2 ++
 1 file changed, 2 insertions(+)
t/check-non-portable-shell.pl +2 -0
index fd3303552b..894aa0a4f9 100755
@@ -41,6 +41,8 @@ sub err {
 	/^\s*declare\s+/ and err 'arrays/declare not portable';
 	/^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
 	/\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"';
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
 	/\bwc -l.*"\s*=/ and err '`"$(wc -l)"` is not portable (use test_line_count)';
 	/\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
 	/(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#74 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Quoted 7 lines · Ævar Arnfjörð Bjarmason

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

I still do not understand why you have to insist on dashed operator as the first thing given to "test", like this:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" ...
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" ...

IOW, what over-matching would we get if we simplified the condition like so?

    /\btest\s+[^&|]+\s+-a\s/
    /\btest\s+[^&|]+\s+-o\s/

The one in the patch would miss things like

	test "$a" = "$b" -o "$a" -lt "$b"
	test "$n" -a "$n" -lt 4

but the only thing that we care about is that a command that started with "test " has "-a" or "-o" before we see "&" or "|", no?

#75 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Quoted 22 lines · Junio C Hamano
Quoted 7 lines · Ævar Arnfjörð Bjarmason

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

I still do not understand why you have to insist on dashed operator as the first thing given to "test", like this:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" ...
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" ...

IOW, what over-matching would we get if we simplified the condition like so?

    /\btest\s+[^&|]+\s+-a\s/
    /\btest\s+[^&|]+\s+-o\s/
Quoted 7 lines

The one in the patch would miss things like

	test "$a" = "$b" -o "$a" -lt "$b"
	test "$n" -a "$n" -lt 4

but the only thing that we care about is that a command that started with "test " has "-a" or "-o" before we see "&" or "|", no?

Applying your suggestion results in these false positives:

	t4038-diff-combined.sh:135: error: "test A && test B" preferred to "test A -a B": git commit -m "test space change" -a &&
	t4038-diff-combined.sh:147: error: "test A && test B" preferred to "test A -a B": git commit -m "test other space changes" -a &&
	t6400-merge-df.sh:89: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:91: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:110: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:112: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6402-merge-rename.sh:639: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:646: error: "test A || test B" preferred to "test A -o B": test 2 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:686: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)" &&
	t6402-merge-rename.sh:865: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l) &&
	annotate-tests.sh:201: error: "test A && test B" preferred to "test A -a B": GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" git commit -a -m "norobots"

I'll just drop this from the re-roll of this series. Maybe you/someone has a better suggestion for something that's simple but still catches these cases, but in any case I'd like to not have this series blocked on this minor thing (which none of the rest of it needs).

#76 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Quoted 35 lines · Ævar Arnfjörð Bjarmason
Quoted 22 lines · Junio C Hamano
Quoted 7 lines · Ævar Arnfjörð Bjarmason

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

I still do not understand why you have to insist on dashed operator as the first thing given to "test", like this:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" ...
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" ...

IOW, what over-matching would we get if we simplified the condition like so?

    /\btest\s+[^&|]+\s+-a\s/
    /\btest\s+[^&|]+\s+-o\s/
Quoted 7 lines

The one in the patch would miss things like

	test "$a" = "$b" -o "$a" -lt "$b"
	test "$n" -a "$n" -lt 4

but the only thing that we care about is that a command that started with "test " has "-a" or "-o" before we see "&" or "|", no?

Applying your suggestion results in these false positives:

Quoted 10 lines
	t4038-diff-combined.sh:135: error: "test A && test B" preferred to "test A -a B": git commit -m "test space change" -a &&
	t4038-diff-combined.sh:147: error: "test A && test B" preferred to "test A -a B": git commit -m "test other space changes" -a &&
	t6400-merge-df.sh:89: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:91: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:110: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:112: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6402-merge-rename.sh:639: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:646: error: "test A || test B" preferred to "test A -o B": test 2 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:686: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)" &&
	t6402-merge-rename.sh:865: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l) &&

With: 7dbe8c8003, (check-non-portable-shell.pl: wc -l may have leading WS, 2017-12-21)
Unless the situation has been changed, since.
I think those tests with quoted "$(.. | wc -l)" has been deemed unportable and should be replaced with test_line_count anyway? Does "test -eq" strip spaces from integer strings?

And I think we're working on moving "git" command to its own commmand instead of put it in the left of a pipe.

2 followed patch will clean them out

Signature · 1 line
Danh
#77 lore raw

PATCH 1/2 t6400: check exit status of ls-files

We will lose the exit status of "git ls-files" if it's being run in anywhere-but-not-final part of a pipe.

Let's send the output of "git ls-files" to a file first, and adjust the expected result for "git ls-files -o" since a new untracked file will be created as a side effect.

---
 t/t6400-merge-df.sh | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
t/t6400-merge-df.sh +14 -8
index 38700d29b5..fd99a78360 100755
@@ -82,13 +82,16 @@ test_expect_success 'modify/delete + directory/file conflict' '
 	git checkout delete^0 &&
 	test_must_fail git merge modify &&
 
-	test 5 -eq $(git ls-files -s | wc -l) &&
-	test 4 -eq $(git ls-files -u | wc -l) &&
+	git ls-files -s >out &&
+	test_line_count = 5 out &&
+	git ls-files -u >out &&
+	test_line_count = 4 out &&
+	git ls-files -o >out &&
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 0 -eq $(git ls-files -o | wc -l)
+		test_line_count = 1 out
 	else
-		test 1 -eq $(git ls-files -o | wc -l)
+		test_line_count = 2 out
 	fi &&
 
 	test_path_is_file letters/file &&
@@ -103,13 +106,16 @@ test_expect_success 'modify/delete + directory/file conflict; other way' '
 
 	test_must_fail git merge delete &&
 
-	test 5 -eq $(git ls-files -s | wc -l) &&
-	test 4 -eq $(git ls-files -u | wc -l) &&
+	git ls-files -s >out &&
+	test_line_count = 5 out &&
+	git ls-files -u >out &&
+	test_line_count = 4 out &&
+	git ls-files -o >out &&
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 0 -eq $(git ls-files -o | wc -l)
+		test_line_count = 1 out
 	else
-		test 1 -eq $(git ls-files -o | wc -l)
+		test_line_count = 2 out
 	fi &&
 
 	test_path_is_file letters/file &&
Signature · 1 line
2.31.1.192.g0881477623
#78 lore raw

PATCH 2/2 t6402: check exit status of ls-files

We will lose the exit status of "git ls-files" if it's being run in anywhere-but-not-final part of a pipe.

Let's send the output of "git ls-files" to a file first, and adjust the expected result for "git ls-files -o" since a new untracked file will be created as a side effect.

---
 t/t6402-merge-rename.sh | 148 +++++++++++++++++++++++++++-------------
 1 file changed, 101 insertions(+), 47 deletions(-)
t/t6402-merge-rename.sh +101 -47
index 425dad97d5..d705847f26 100755
@@ -330,8 +330,10 @@ test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
 		test_i18ngrep "Adding as dir~HEAD instead" output
 	fi &&
 
-	test 3 -eq "$(git ls-files -u | wc -l)" &&
-	test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 3 output &&
+	git ls-files -u dir/file-in-the-way >output &&
+	test_line_count = 2 output &&
 
 	test_must_fail git diff --quiet &&
 	test_must_fail git diff --cached --quiet &&
@@ -357,8 +359,10 @@ test_expect_success 'Same as previous, but merged other way' '
 		test_i18ngrep "Adding as dir~renamed-file-has-no-conflicts instead" output
 	fi &&
 
-	test 3 -eq "$(git ls-files -u | wc -l)" &&
-	test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 3 output &&
+	git ls-files -u dir/file-in-the-way >output &&
+	test_line_count = 2 output &&
 
 	test_must_fail git diff --quiet &&
 	test_must_fail git diff --cached --quiet &&
@@ -374,8 +378,10 @@ test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in
 	git checkout -q renamed-file-has-conflicts^0 &&
 	test_must_fail git merge --strategy=recursive dir-not-in-way &&
 
-	test 3 -eq "$(git ls-files -u | wc -l)" &&
-	test 3 -eq "$(git ls-files -u dir | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 3 output &&
+	git ls-files -u dir >output &&
+	test_line_count = 3 output &&
 
 	test_must_fail git diff --quiet &&
 	test_must_fail git diff --cached --quiet &&
@@ -409,14 +415,19 @@ test_expect_success 'Rename+D/F conflict; renamed file cannot merge and dir in t
 	git checkout -q renamed-file-has-conflicts^0 &&
 	test_must_fail git merge --strategy=recursive dir-in-way &&
 
-	test 5 -eq "$(git ls-files -u | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 5 output &&
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 3 -eq "$(git ls-files -u dir~HEAD | wc -l)"
+		git ls-files -u dir~HEAD >output &&
+		test_line_count = 3 output
 	else
-		test 3 -eq "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)"
+		git ls-files -u dir >output &&
+		grep -v file-in-the-way output >filtered
+		test_line_count = 3 filtered
 	fi &&
-	test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+	git ls-files -u dir/file-in-the-way >output &&
+	test_line_count = 2 output &&
 
 	test_must_fail git diff --quiet &&
 	test_must_fail git diff --cached --quiet &&
@@ -432,14 +443,19 @@ test_expect_success 'Same as previous, but merged other way' '
 	git checkout -q dir-in-way^0 &&
 	test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
 
-	test 5 -eq "$(git ls-files -u | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 5 output &&
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 3 -eq "$(git ls-files -u dir~renamed-file-has-conflicts | wc -l)"
+		git ls-files -u dir~renamed-file-has-conflicts >output &&
+		test_line_count = 3 output
 	else
-		test 3 -eq "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)"
+		git ls-files -u dir >output &&
+		grep -v file-in-the-way output >filtered
+		test_line_count = 3 filtered
 	fi &&
-	test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
+	git ls-files -u dir/file-in-the-way >output &&
+	test_line_count = 2 output &&
 
 	test_must_fail git diff --quiet &&
 	test_must_fail git diff --cached --quiet &&
@@ -494,11 +510,12 @@ test_expect_success 'both rename source and destination involved in D/F conflict
 	git checkout -q rename-dest^0 &&
 	test_must_fail git merge --strategy=recursive source-conflict &&
 
+	git ls-files -u >output &&
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 2 -eq "$(git ls-files -u | wc -l)"
+		test_line_count = 2 output
 	else
-		test 1 -eq "$(git ls-files -u | wc -l)"
+		test_line_count = 1 output
 	fi &&
 
 	test_must_fail git diff --quiet &&
@@ -540,9 +557,13 @@ then
 		mkdir one &&
 		test_must_fail git merge --strategy=recursive rename-two &&
 
-		test 4 -eq "$(git ls-files -u | wc -l)" &&
-		test 2 -eq "$(git ls-files -u one | wc -l)" &&
-		test 2 -eq "$(git ls-files -u two | wc -l)" &&
+		git ls-files -u >output &&
+		test_line_count = 4 output &&
+		git ls-files -u one >output &&
+		test_line_count = 2 output &&
+		git ls-files -u two >output &&
+		test_line_count = 2 output &&
+		rm -f output &&
 
 		test_must_fail git diff --quiet &&
 
@@ -559,9 +580,13 @@ else
 		mkdir one &&
 		test_must_fail git merge --strategy=recursive rename-two &&
 
-		test 2 -eq "$(git ls-files -u | wc -l)" &&
-		test 1 -eq "$(git ls-files -u one | wc -l)" &&
-		test 1 -eq "$(git ls-files -u two | wc -l)" &&
+		git ls-files -u >output &&
+		test_line_count = 2 output &&
+		git ls-files -u one >output &&
+		test_line_count = 1 output &&
+		git ls-files -u two >output &&
+		test_line_count = 1 output &&
+		rm -f output &&
 
 		test_must_fail git diff --quiet &&
 
@@ -582,14 +607,21 @@ test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean sta
 
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 4 -eq "$(git ls-files -u | wc -l)" &&
-		test 2 -eq "$(git ls-files -u one | wc -l)" &&
-		test 2 -eq "$(git ls-files -u two | wc -l)"
+		git ls-files -u >output &&
+		test_line_count = 4 output &&
+		git ls-files -u one >output &&
+		test_line_count = 2 output &&
+		git ls-files -u two >output &&
+		test_line_count = 2 output
 	else
-		test 2 -eq "$(git ls-files -u | wc -l)" &&
-		test 1 -eq "$(git ls-files -u one | wc -l)" &&
-		test 1 -eq "$(git ls-files -u two | wc -l)"
+		git ls-files -u >output &&
+		test_line_count = 2 output &&
+		git ls-files -u one >output &&
+		test_line_count = 1 output &&
+		git ls-files -u two >output &&
+		test_line_count = 1 output
 	fi &&
+	rm -f output &&
 
 	test_must_fail git diff --quiet &&
 
@@ -631,20 +663,33 @@ test_expect_success 'check handling of differently renamed file with D/F conflic
 
 	if test "$GIT_TEST_MERGE_ALGORITHM" = ort
 	then
-		test 5 -eq "$(git ls-files -s | wc -l)" &&
-		test 3 -eq "$(git ls-files -u | wc -l)" &&
-		test 1 -eq "$(git ls-files -u one~HEAD | wc -l)" &&
-		test 1 -eq "$(git ls-files -u two~second-rename | wc -l)" &&
-		test 1 -eq "$(git ls-files -u original | wc -l)" &&
-		test 0 -eq "$(git ls-files -o | wc -l)"
+		git ls-files -s >output &&
+		test_line_count = 5 output &&
+		git ls-files -u >output &&
+		test_line_count = 3 output &&
+		git ls-files -u one~HEAD >output &&
+		test_line_count = 1 output &&
+		git ls-files -u two~second-rename >output &&
+		test_line_count = 1 output &&
+		git ls-files -u original >output &&
+		test_line_count = 1 output &&
+		git ls-files -o >output &&
+		test_line_count = 1 output
 	else
-		test 5 -eq "$(git ls-files -s | wc -l)" &&
-		test 3 -eq "$(git ls-files -u | wc -l)" &&
-		test 1 -eq "$(git ls-files -u one | wc -l)" &&
-		test 1 -eq "$(git ls-files -u two | wc -l)" &&
-		test 1 -eq "$(git ls-files -u original | wc -l)" &&
-		test 2 -eq "$(git ls-files -o | wc -l)"
+		git ls-files -s >output &&
+		test_line_count = 5 output &&
+		git ls-files -u >output &&
+		test_line_count = 3 output &&
+		git ls-files -u one >output &&
+		test_line_count = 1 output &&
+		git ls-files -u two >output &&
+		test_line_count = 1 output &&
+		git ls-files -u original >output &&
+		test_line_count = 1 output &&
+		git ls-files -o >output &&
+		test_line_count = 3 output
 	fi &&
+	rm -f output &&
 
 	test_path_is_file one/file &&
 	test_path_is_file two/file &&
@@ -679,11 +724,17 @@ test_expect_success 'check handling of differently renamed file with D/F conflic
 	git checkout -q first-rename-redo^0 &&
 	test_must_fail git merge --strategy=recursive second-rename-redo &&
 
-	test 3 -eq "$(git ls-files -u | wc -l)" &&
-	test 1 -eq "$(git ls-files -u one | wc -l)" &&
-	test 1 -eq "$(git ls-files -u two | wc -l)" &&
-	test 1 -eq "$(git ls-files -u original | wc -l)" &&
-	test 0 -eq "$(git ls-files -o | wc -l)" &&
+	git ls-files -u >output &&
+	test_line_count = 3 output &&
+	git ls-files -u one >output &&
+	test_line_count = 1 output &&
+	git ls-files -u two >output &&
+	test_line_count = 1 output &&
+	git ls-files -u original >output &&
+	test_line_count = 1 output &&
+	git ls-files -o >output &&
+	test_line_count = 1 output &&
+	rm -f output &&
 
 	test_path_is_file one &&
 	test_path_is_file two &&
@@ -861,8 +912,11 @@ test_expect_success 'setup merge of rename + small change' '
 test_expect_success 'merge rename + small change' '
 	git merge rename_branch &&
 
-	test 1 -eq $(git ls-files -s | wc -l) &&
-	test 0 -eq $(git ls-files -o | wc -l) &&
+	git ls-files -s >output &&
+	test_line_count = 1 output &&
+	git ls-files -o >output &&
+	test_line_count = 1 output &&
+	rm -f output &&
 	test $(git rev-parse HEAD:renamed_file) = $(git rev-parse HEAD~1:file)
 '
 
Signature · 1 line
2.31.1.192.g0881477623
#79 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 11 lines · Đoàn Trần Công Danh

We will lose the exit status of "git ls-files" if it's being run in anywhere-but-not-final part of a pipe.

Let's send the output of "git ls-files" to a file first, and adjust the expected result for "git ls-files -o" since a new untracked file will be created as a side effect.

---

t/t6402-merge-rename.sh test_expect_success 'check handling of differently renamed file with D/F conflic · line 668

Quoted 9 lines
t/t6402-merge-rename.sh +6 -3
@@ -631,20 +663,33 @@ test_expect_success 'check handling of differently renamed file with D/F conflic
-               test 5 -eq "$(git ls-files -s | wc -l)" &&
-               test 3 -eq "$(git ls-files -u | wc -l)" &&
-               test 1 -eq "$(git ls-files -u one~HEAD | wc -l)" &&
+               git ls-files -s >output &&
+               test_line_count = 5 output &&
+               git ls-files -u >output &&
+               test_line_count = 3 output &&
+               git ls-files -u one~HEAD >output &&
+               test_line_count = 1 output &&

This idiom crops up so frequently in this test script that it almost begs for the introduction of a helper function rather than applying the manual transformation repeatedly. For instance, the helper might be called like this:

    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

#80 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 5 lines · Eric Sunshine
    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

The frequency with which this idiom crops up with commands beyond git-ls-files suggests the more general solution of supporting it directly in test-lib-functions.sh for any command. For instance:

    test_cmd_line_count = 3 git ls-files -u &&

Or, perhaps, a new mode of test_line_count():

    test_line_count = 3 -c git ls-files -u &&
#81 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 18 lines · Eric Sunshine
Quoted 5 lines · Eric Sunshine
    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

The frequency with which this idiom crops up with commands beyond git-ls-files suggests the more general solution of supporting it directly in test-lib-functions.sh for any command. For instance:

Quoted 5 lines
    test_cmd_line_count = 3 git ls-files -u &&

Or, perhaps, a new mode of test_line_count():

    test_line_count = 3 -c git ls-files -u &&

That looks nice on paper, but may be going too far.

We may want to count the lines in the error message, or we may want to count the lines after filtering the output with pipe.

A test file that is dedicated to test ls-files with a file local helper "count_ls_files" smells like a better place to stop, at least to me.

#82 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Eric Sunshine
Quoted 5 lines · Eric Sunshine
    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

The frequency with which this idiom crops up with commands beyond git-ls-files suggests the more general solution of supporting it directly in test-lib-functions.sh for any command. For instance:

Quoted 5 lines
Quoted 5 lines
    test_cmd_line_count = 3 git ls-files -u &&

Or, perhaps, a new mode of test_line_count():

    test_line_count = 3 -c git ls-files -u &&

Hm, I'm not sure how would we implement such mode:

* Will we run such command in a subprocess and and pipe to "wc -l" directly to avoid a temporary file, but we will lose the exit code of running command in process?
* Will we run in a separated chain? Thus, a temporary file would be created, skimming over test-lib-functions.sh, I couldn't find any convention for creating such files, except for specific use cases, let's say "*.config".
* Another clever hacks that I don't know *shrug*

Quoted 4 lines

That looks nice on paper, but may be going too far.

We may want to count the lines in the error message,

Let's assume that we solve above puzzle.

Count the lines in the error messages is not too hard to be imagined, let's say by -c2 or something like that.

Quoted 2 lines

or we may want
to count the lines after filtering the output with pipe.

However, when it involved a pipe, things becomes complicated.

Quoted 3 lines

A test file that is dedicated to test ls-files with a file local helper "count_ls_files" smells like a better place to stop, at least to me.

Hence, I'll stick with local help "count_ls_files" for now.

Signature · 1 line
Danh
#83 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 25 lines · Đoàn Trần Công Danh
Quoted 24 lines · Junio C Hamano
Quoted 22 lines · Eric Sunshine
Quoted 5 lines · Eric Sunshine
    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

The frequency with which this idiom crops up with commands beyond git-ls-files suggests the more general solution of supporting it directly in test-lib-functions.sh for any command. For instance:

    test_cmd_line_count = 3 git ls-files -u &&

Or, perhaps, a new mode of test_line_count():

Quoted 7 lines
Quoted 1 line
Quoted 1 line
    test_line_count = 3 -c git ls-files -u &&

Hm, I'm not sure how would we implement such mode:

* Will we run such command in a subprocess and and pipe to "wc -l" directly to avoid a temporary file, but we will lose the exit code of running command in process?

Just use a tempfile.

Quoted 4 lines

* Will we run in a separated chain? Thus, a temporary file would be created, skimming over test-lib-functions.sh, I couldn't find any convention for creating such files, except for specific use cases, let's say "*.config".

Yeah we happen not to have one of those in test-lib-functions.sh, but a bunch of helpers create those.

Quoted 9 lines

* Another clever hacks that I don't know *shrug*

Quoted 4 lines

That looks nice on paper, but may be going too far.

We may want to count the lines in the error message,

Let's assume that we solve above puzzle.

Quoted 7 lines

Count the lines in the error messages is not too hard to be imagined, let's say by -c2 or something like that.

Quoted 2 lines

or we may want
to count the lines after filtering the output with pipe.

However, when it involved a pipe, things becomes complicated.

Yes, there's no portable way to both stream stdout/stderr in the correct order and to intercept it. You'd need a mkfifo. See also how the "--tee" mode works (i.e. they're squashed).

Quoted 5 lines
Quoted 3 lines

A test file that is dedicated to test ls-files with a file local helper "count_ls_files" smells like a better place to stop, at least to me.

Hence, I'll stick with local help "count_ls_files" for now.

Here's an implementation that works, sans the fancy opts parsing etc. I just use -1 for "don't care". You can see "test_commit" etc. for how to do that.

It doesn't emit output, but that's just a matter of re-cat-ing the relevant files.

Yes we wouldn't have combined output when you do that, but I don't think we care, you can chain this thing, and most thing that would do so would only care about piping stdout/stderr to their own files anyway, and not that we emit one line of stdout, then another of stderr, another of stdout etc.

t/t6402-merge-rename.sh +4 -8
index 425dad97d54..c7251102a3d 100755
@@ -154,14 +154,10 @@ test_expect_success 'pull conflicting renames' \
 	git reset --hard &&
 	git show-branch &&
 	test_expect_code 1 git pull . blue &&
-	git ls-files -u A >a.stages &&
-	test_line_count = 1 a.stages &&
-	git ls-files -u B >b.stages &&
-	test_line_count = 1 b.stages &&
-	git ls-files -u C >c.stages &&
-	test_line_count = 1 c.stages &&
-	git ls-files -s N >n.stages &&
-	test_line_count = 1 n.stages &&
+	test_line_count_command = 1 0 git ls-files -u A &&
+	test_line_count_command = 1 0 git ls-files -u B &&
+	test_line_count_command = 1 0 git ls-files -u C &&
+	test_line_count_command = 1 0 git ls-files -s N &&
 	sed -ne "/^g/{
 	p
 	q
t/test-lib-functions.sh +18 -0
index b823c140271..10e3cbe0d47 100644
@@ -817,6 +817,24 @@ test_line_count () {
 	fi
 }
 
+test_line_count_command () {
+	local op=$1
+	local ocnt=$2
+	local ecnt=$3
+	shift 3
+	"$@" 2>cnt.err >cnt.out
+	local exit_code=$?
+	if test "$ocnt" -gt -1
+	then
+		test_line_count "$op" "$ocnt" cnt.out
+	fi &&
+	if test "$ecnt" -gt -1
+	then
+		test_line_count "$op" "$ecnt" cnt.err
+	fi &&
+	return "$exit_code"
+}
+
 test_file_size () {
 	test "$#" -ne 1 && BUG "1 param"
 	test-tool path-utils file-size "$1"
#84 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

Quoted 8 lines · Ævar Arnfjörð Bjarmason
Quoted 5 lines · Đoàn Trần Công Danh

Hm, I'm not sure how would we implement such mode:

* Will we run such command in a subprocess and and pipe to "wc -l" directly to avoid a temporary file, but we will lose the exit code of running command in process?

Just use a tempfile.

That's what I had in mind when I suggested the function. The function would remove the tempfile if the check succeeded.

Quoted 13 lines

Here's an implementation that works, sans the fancy opts parsing etc. I just use -1 for "don't care". You can see "test_commit" etc. for how to do that.

t/test-lib-functions.sh +7 -0
@@ -817,6 +817,24 @@ test_line_count () {
+test_line_count_command () {
+       local op=$1
+       local ocnt=$2
+       local ecnt=$3
+       shift 3
+       "$@" 2>cnt.err >cnt.out
+       local exit_code=$?

t/test-lib-functions.sh test_line_count () { · line 833

Quoted 10 lines
t/test-lib-functions.sh +10 -0
+       if test "$ocnt" -gt -1
+       then
+               test_line_count "$op" "$ocnt" cnt.out
+       fi &&
+       if test "$ecnt" -gt -1
+       then
+               test_line_count "$op" "$ecnt" cnt.err
+       fi &&
+       return "$exit_code"
+}

It's not clear why you manually catch the exit code of the command being run but then use &&-chain for the other parts.

An important reason I suggested creating a function was because the function could clean up after itself thus freeing the test author from worrying about adjusting counts in later tests (or later in the same test). So, just before this function returns, I'd expect to see:

    rm -f cnt.out cnt.err &&
#85 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

In reply to Đoàn Trần Công Danh
Quoted 29 lines · Đoàn Trần Công Danh
Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Eric Sunshine
Quoted 5 lines · Eric Sunshine
    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

The frequency with which this idiom crops up with commands beyond git-ls-files suggests the more general solution of supporting it directly in test-lib-functions.sh for any command. For instance:

Quoted 5 lines
Quoted 5 lines
    test_cmd_line_count = 3 git ls-files -u &&

Or, perhaps, a new mode of test_line_count():

    test_line_count = 3 -c git ls-files -u &&

Hm, I'm not sure how would we implement such mode:

Quoted 8 lines

* Will we run such command in a subprocess and and pipe to "wc -l" directly to avoid a temporary file, but we will lose the exit code of running command in process?
* Will we run in a separated chain? Thus, a temporary file would be created, skimming over test-lib-functions.sh, I couldn't find any convention for creating such files, except for specific use cases, let's say "*.config".
* Another clever hacks that I don't know *shrug*

Hm, I figured out, just a bit insane for reviewing. -----8<--------
Subject: [PATCH] test-lib-functions: add test_line_count_in helper

---
 t/t0000-basic.sh        |  7 +++++
 t/test-lib-functions.sh | 62 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
t/t0000-basic.sh +7 -0
index 705d62cc27..2ddb50e919 100755
@@ -60,6 +60,13 @@ test_expect_success '.git/objects should have 3 subdirectories' '
 	test_line_count = 3 full-of-directories
 '
 
+test_expect_success 'check test_line_count_in' '
+	test_line_count_in --stdout = 4 --stderr = 3 -- sh -c "
+		printf \"%s\\n\" 1 2 3 4
+		printf >&2 \"%s\\n\" a b c
+		"
+'
+
 ################################################################
 # Test harness
 test_expect_success 'success is reported like this' '
t/test-lib-functions.sh +62 -0

68 lines not on this page · raw message

Signature · 1 line
2.31.1.500.gbc6bbdd36b
#86 lore raw

Re: [PATCH 2/2] t6402: check exit status of ls-files

In reply to Eric Sunshine
Quoted 33 lines · Eric Sunshine
Quoted 11 lines · Đoàn Trần Công Danh

We will lose the exit status of "git ls-files" if it's being run in anywhere-but-not-final part of a pipe.

Let's send the output of "git ls-files" to a file first, and adjust the expected result for "git ls-files -o" since a new untracked file will be created as a side effect.

---

t/t6402-merge-rename.sh test_expect_success 'check handling of differently renamed file with D/F conflic · line 668

Quoted 9 lines
t/t6402-merge-rename.sh +6 -3
@@ -631,20 +663,33 @@ test_expect_success 'check handling of differently renamed file with D/F conflic
-               test 5 -eq "$(git ls-files -s | wc -l)" &&
-               test 3 -eq "$(git ls-files -u | wc -l)" &&
-               test 1 -eq "$(git ls-files -u one~HEAD | wc -l)" &&
+               git ls-files -s >output &&
+               test_line_count = 5 output &&
+               git ls-files -u >output &&
+               test_line_count = 3 output &&
+               git ls-files -u one~HEAD >output &&
+               test_line_count = 1 output &&

This idiom crops up so frequently in this test script that it almost begs for the introduction of a helper function rather than applying the manual transformation repeatedly. For instance, the helper might be called like this:

    count_ls_files 5 -s &&
    count_ls_files 3 -u &&
    count_ls_files 1 -u one~HEAD &&
    ...
Quoted 7 lines

The nice thing about having a helper function is that it can clean up after itself by not leaving a new file lying around, thus you wouldn't have to make adjustments to the expected number of untracked files (as mentioned in the commit message). If this is the sort of thing which comes up often enough (if there are more such cases beyond the two scripts you changed in this series), then it might make sense to promote the helper function to test-lib-functions.sh.

Agreed, but I'd saythat it makes more sense to have something like:

test_line_count = 1 -- git ls-files

Or maybe another function, but in any case not something specific to ls-files, but just a wrapper like the various "$@" wrappers in test-lib-functions.sh to count the lines emitted by an arbitrary command.

It would also leave things open for:

    test_line_count --stout=1 --stderr=2 -- git ls-files
    test_line_count --combined=3 -- git ls-files

Or whatever, to count the different output streams.

#87 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

In reply to Đoàn Trần Công Danh
Quoted 54 lines · Đoàn Trần Công Danh
Quoted 35 lines · Ævar Arnfjörð Bjarmason
Quoted 22 lines · Junio C Hamano
Quoted 7 lines · Ævar Arnfjörð Bjarmason

These will only match the simplistic forms of test -X blah (where "-X" is some single letter option), but will miss expressions such as test "$foo" = bar. We stop at "&" or "|" to try not to overmatch things like:

    test whatever && ls -a foo
    test whatever && foo -o outfile

I still do not understand why you have to insist on dashed operator as the first thing given to "test", like this:

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/\btest\s+-[a-z]\s+[^&|]+\s+-a\s+/ and err '"test A && test B" ...
+	/\btest\s+-[a-z]\s+[^&|]+\s+-o\s+/ and err '"test A || test B" ...

IOW, what over-matching would we get if we simplified the condition like so?

    /\btest\s+[^&|]+\s+-a\s/
    /\btest\s+[^&|]+\s+-o\s/
Quoted 7 lines

The one in the patch would miss things like

	test "$a" = "$b" -o "$a" -lt "$b"
	test "$n" -a "$n" -lt 4

but the only thing that we care about is that a command that started with "test " has "-a" or "-o" before we see "&" or "|", no?

Applying your suggestion results in these false positives:

Quoted 10 lines
	t4038-diff-combined.sh:135: error: "test A && test B" preferred to "test A -a B": git commit -m "test space change" -a &&
	t4038-diff-combined.sh:147: error: "test A && test B" preferred to "test A -a B": git commit -m "test other space changes" -a &&
	t6400-merge-df.sh:89: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:91: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:110: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l)
	t6400-merge-df.sh:112: error: "test A || test B" preferred to "test A -o B": test 1 -eq $(git ls-files -o | wc -l)
	t6402-merge-rename.sh:639: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:646: error: "test A || test B" preferred to "test A -o B": test 2 -eq "$(git ls-files -o | wc -l)"
	t6402-merge-rename.sh:686: error: "test A || test B" preferred to "test A -o B": test 0 -eq "$(git ls-files -o | wc -l)" &&
	t6402-merge-rename.sh:865: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l) &&

With: 7dbe8c8003, (check-non-portable-shell.pl: wc -l may have leading WS, 2017-12-21)
Unless the situation has been changed, since.
I think those tests with quoted "$(.. | wc -l)" has been deemed unportable and should be replaced with test_line_count anyway? Does "test -eq" strip spaces from integer strings?

Quoted 4 lines

And I think we're working on moving "git" command to its own commmand instead of put it in the left of a pipe.

2 followed patch will clean them out

I think those patches are good in their own right, i.e. replacing things with more incremental helpers and test_cmp-like functions.

But I believe the code you're changing is not non-portable. It was using the output of "wc -l" with the "=" operator that wasn't portable.

These ones are all occurances that use "-eq".

And:

    test "0" -eq " 0"

etc., is true, which is why these pass on OSX and beyond.

#88 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

Quoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 3 lines
Quoted 1 line
	t6402-merge-rename.sh:865: error: "test A || test B" preferred to "test A -o B": test 0 -eq $(git ls-files -o | wc -l) &&

With: 7dbe8c8003, (check-non-portable-shell.pl: wc -l may have

...
I think those patches are good in their own right, i.e. replacing things with more incremental helpers and test_cmp-like functions.

But I believe the code you're changing is not non-portable. It was using the output of "wc -l" with the "=" operator that wasn't portable.

Not necessarily. The last one shown above

	test 0 -eq $(git ls-files -o | wc -l) &&

is still valid and correct with s/-eq/=/. It lets shell to remove excess whitespaces around output from some implementations of "wc", so textual '=' is perfectly fine.

Quoted 7 lines

These ones are all occurances that use "-eq".

And:

    test "0" -eq " 0"

etc., is true, which is why these pass on OSX and beyond.

Perhaps, but I am not sure how portable it is to rely on "-eq" ignoring the leading whitespaces.

A more conventional (read: time tested) way is to $(... | wc -l) *NOT* enclosed inside dq, so that the shell will strip out any excess spaces around it, and use '=', i.e.

    test 3 = $(... | wc -l)

and that is what we end up evaluating when we write

test_line_count = 3 file

This does rely on that "wc -l" will run and produce _some_ output to be syntactically correct, though.

#89 lore raw

Re: [PATCH v3 01/12] check-non-portable-shell: check for "test <cond> -a/-o <cond>"

In reply to Ævar Arnfjörð Bjarmason
Quoted 3 lines · Ævar Arnfjörð Bjarmason

Applying your suggestion results in these false positives:

	t4038-diff-combined.sh:135: error: "test A && test B" preferred to "test A -a B": git commit -m "test space change" -a &&

This is utterly amusing. Consider the suggestion withdrawn.

#90 lore raw

PATCH v3 2/12 test-lib: bring $remove_trash out of retirement

In reply to Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
t/test-lib.sh +17 -14
index 3dec266221..7522faf39f 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,21 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1356,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1368,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#91 lore raw

Re: [PATCH v3 02/12] test-lib: bring $remove_trash out of retirement

Quoted 4 lines · Ævar Arnfjörð Bjarmason

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

Makes sense, but then remove_trash should be initialized to a non-empty string at the beginning, to avoid it leaking from end-user environment, just like the way store_arg_to and opt_required_arg are protected?

Quoted 63 lines
t/test-lib.sh +17 -14
index 3dec266221..7522faf39f 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,21 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1356,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1368,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
#92 lore raw

PATCH v3 3/12 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27..2c6e34b947 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#93 lore raw

PATCH v3 4/12 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index 6348e8d733..d169fb2f59 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#94 lore raw

PATCH v3 5/12 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 2 ++
 1 file changed, 2 insertions(+)
t/test-lib-functions.sh +2 -0
index d169fb2f59..d0f4f3885d 100644
@@ -177,6 +177,8 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#95 lore raw

PATCH v3 6/12 test-lib functions: add an --annotated option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated option to test_commit to create annotated tags. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c..17d3cc1405 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index d0f4f3885d..6e2332a324 100644
@@ -179,6 +179,10 @@ debug () {
 #	Invoke "git commit" with --author <author>
 #   --no-tag
 #	Do not tag the resulting commit
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag, unless --notick
+#	is given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -191,7 +195,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -219,7 +223,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -243,10 +250,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test -z "$notick"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#96 lore raw

PATCH v3 7/12 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747be..88fddc9142 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#97 lore raw

PATCH v3 8/12 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388..930dce06f0 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a..f691e6d903 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15..6cdee2a216 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60..a39a626664 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f..e2c0c51022 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index 6e2332a324..dbeb3d9262 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -191,6 +195,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -202,6 +207,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -238,9 +246,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#98 lore raw

PATCH v3 9/12 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e9308..f7c7df0ca4 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#99 lore raw

PATCH v3 10/12 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index dbeb3d9262..21271020c7 100644
@@ -1257,8 +1257,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#100 lore raw

PATCH v3 11/12 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 21271020c7..8e75a013a4 100644
@@ -1258,7 +1258,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index 7522faf39f..9ebb595c33 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#101 lore raw

PATCH v3 12/12 test-lib: split up and deprecate test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it.

This leave us with a pass-through wrapper for "git init" in test-lib-functions.sh, in test-lib.sh we have the same, except for needing to redirect stdout/stderr, and emitting an error ourselves if it fails. We don't need to error() ourselves when test_create_repo() is invoked, as the invocation will be a part of a test's "&&"-chain.

Everything below this paragraph is a detailed summary of the history of test_create_repo() explaining why it's safe to remove the various things it was doing:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

See 0d314ce834 for when the subshell use got introduced for the convenience of not having to "cd" back and forth while setting up the hooks.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh.

We could still invoke test_create_repo() there, but as the invocation is now trivial and we don't have a good reason to use test_create_repo() elsewhere let's call "git init" there ourselves.

8. We didn't need to resolve "git" as "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(), even for the use of test-lib.sh

PATH is already set up in test-lib.sh to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before test_create_repo() (now "git init") is called.. So we can simply run "git" and rely on the PATH lookup choosing the right executable.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 ++-------------
 t/test-lib.sh                       |  3 ++-
 5 files changed, 4 insertions(+), 18 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649..5c509db6fc 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4..6da8d760e2 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8e..9f1a483f42 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +2 -13
index 8e75a013a4..bd64a15c73 100644
@@ -1248,21 +1248,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	git init "$@"
 }
 
 # This function helps on symlink challenged file systems when it is not
t/test-lib.sh +2 -1
index 9ebb595c33..06026bbe75 100644
@@ -1364,7 +1364,8 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	git init "$TRASH_DIRECTORY" >&3 2>&4 ||
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#102 lore raw

v4 cover test-lib.sh: new test_commit args, simplification & fixes

In reply to Ævar Arnfjörð Bjarmason

A fast re-roll of this test topic that a few things depend on, per
https://lore.kernel.org/git/[email protected]/

Changes since v3:

* Dropped the check-non-portable-shell change

As noted in
https://lore.kernel.org/git/[email protected]/ it wasn't needed for the rest of this series, so let's not hold it up for the discussion of if/how to check "-a" and "-o" invocations of "test".

* Set the remove_trash= variable so we won't leak it from the environment.

Ævar Arnfjörð Bjarmason (11):
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 +++++++---------------------
 t/test-lib-functions.sh             | 60 ++++++++++++++++++-----------
 t/test-lib.sh                       | 40 +++++++++++--------
 14 files changed, 87 insertions(+), 119 deletions(-)

Range-diff against v3:

 1:  a8b483bc77 <  -:  ---------- check-non-portable-shell: check for "test <cond> -a/-o <cond>"
 2:  39759d00ad !  1:  a76ea749bb test-lib: bring $remove_trash out of retirement

@@ Commit message
variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

+ Let's also fix a bug that was with us since abc5d372ec (Enable
+ parallel tests, 2008-08-08): we would leak $remove_trash from the
+ environment. We don't want this to error out, so let's reset it to the
+ empty string first:
+
+ remove_trash=t GIT_SKIP_TESTS=t0001 ./t0001-init.sh
+
I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.
@@ t/test-lib.sh: then
fi

+# Are we running this test at all?
++remove_trash=
+this_test=${0##*/} +this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS

 3:  d669ce3196 =  2:  de7be7844e test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 4:  9313d35bf8 =  3:  709bc773fb test-lib-functions: reword "test_commit --append" docs
 5:  c916d648e4 =  4:  b67654334e test-lib-functions: document test_commit --no-tag
 6:  9b6bf65ce1 =  5:  3a166c9206 test-lib functions: add an --annotated option to "test_commit"
 7:  683b3ba3dd =  6:  981fc43ee6 describe tests: convert setup to use test_commit
 8:  dc0a863db7 =  7:  15057cdecf test-lib functions: add --printf option to test_commit
 9:  90bf55d2d4 =  8:  5d437f53ec submodule tests: use symbolic-ref --short to discover branch name
10:  e0a1e2fd52 =  9:  9ee13ee71b test-lib: reformat argument list in test_create_repo()
11:  cedf5d383b = 10:  6ba568df9f test-lib: do not show advice about init.defaultBranch under --verbose
12:  0dc0da9490 = 11:  311a9dba36 test-lib: split up and deprecate test_create_repo()
Signature · 1 line
2.31.1.721.gbeb6a21927
#103 lore raw

PATCH v4 1/11 test-lib: bring $remove_trash out of retirement

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

Let's also fix a bug that was with us since abc5d372ec (Enable parallel tests, 2008-08-08): we would leak $remove_trash from the environment. We don't want this to error out, so let's reset it to the empty string first:

     remove_trash=t GIT_SKIP_TESTS=t0001 ./t0001-init.sh

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
t/test-lib.sh +18 -14
index 3dec266221..105c424bf5 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,22 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+remove_trash=
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1357,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1369,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.721.gbeb6a21927
#104 lore raw

PATCH v4 2/11 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27..2c6e34b947 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.721.gbeb6a21927
#105 lore raw

PATCH v4 3/11 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index 6348e8d733..d169fb2f59 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.721.gbeb6a21927
#106 lore raw

PATCH v4 4/11 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 2 ++
 1 file changed, 2 insertions(+)
t/test-lib-functions.sh +2 -0
index d169fb2f59..d0f4f3885d 100644
@@ -177,6 +177,8 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.721.gbeb6a21927
#107 lore raw

PATCH v4 5/11 test-lib functions: add an --annotated option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated option to test_commit to create annotated tags. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c..17d3cc1405 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index d0f4f3885d..6e2332a324 100644
@@ -179,6 +179,10 @@ debug () {
 #	Invoke "git commit" with --author <author>
 #   --no-tag
 #	Do not tag the resulting commit
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag, unless --notick
+#	is given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -191,7 +195,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -219,7 +223,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -243,10 +250,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test -z "$notick"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.721.gbeb6a21927
#108 lore raw

PATCH v4 6/11 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747be..88fddc9142 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.721.gbeb6a21927
#109 lore raw

PATCH v4 7/11 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388..930dce06f0 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a..f691e6d903 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15..6cdee2a216 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60..a39a626664 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f..e2c0c51022 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index 6e2332a324..dbeb3d9262 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -191,6 +195,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -202,6 +207,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -238,9 +246,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.721.gbeb6a21927
#110 lore raw

PATCH v4 8/11 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e9308..f7c7df0ca4 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.721.gbeb6a21927
#111 lore raw

PATCH v4 9/11 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index dbeb3d9262..21271020c7 100644
@@ -1257,8 +1257,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.721.gbeb6a21927
#112 lore raw

PATCH v4 10/11 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 21271020c7..8e75a013a4 100644
@@ -1258,7 +1258,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index 105c424bf5..ab1fe98ccc 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.721.gbeb6a21927
#113 lore raw

PATCH v4 11/11 test-lib: split up and deprecate test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it.

This leave us with a pass-through wrapper for "git init" in test-lib-functions.sh, in test-lib.sh we have the same, except for needing to redirect stdout/stderr, and emitting an error ourselves if it fails. We don't need to error() ourselves when test_create_repo() is invoked, as the invocation will be a part of a test's "&&"-chain.

Everything below this paragraph is a detailed summary of the history of test_create_repo() explaining why it's safe to remove the various things it was doing:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

5. Since we don't need to move the .git/hooks directory we don't need the subshell here either.

See 0d314ce834 for when the subshell use got introduced for the convenience of not having to "cd" back and forth while setting up the hooks.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh.

We could still invoke test_create_repo() there, but as the invocation is now trivial and we don't have a good reason to use test_create_repo() elsewhere let's call "git init" there ourselves.

8. We didn't need to resolve "git" as "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(), even for the use of test-lib.sh

PATH is already set up in test-lib.sh to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before test_create_repo() (now "git init") is called.. So we can simply run "git" and rely on the PATH lookup choosing the right executable.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 ++-------------
 t/test-lib.sh                       |  3 ++-
 5 files changed, 4 insertions(+), 18 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649..5c509db6fc 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4..6da8d760e2 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8e..9f1a483f42 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +2 -13
index 8e75a013a4..bd64a15c73 100644
@@ -1248,21 +1248,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	git init "$@"
 }
 
 # This function helps on symlink challenged file systems when it is not
t/test-lib.sh +2 -1
index ab1fe98ccc..5fd968d11b 100644
@@ -1365,7 +1365,8 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	git init "$TRASH_DIRECTORY" >&3 2>&4 ||
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.721.gbeb6a21927
#114 lore raw

Re: [PATCH v4 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 1 line · Ævar Arnfjörð Bjarmason

5. Since we don't need to move the .git/hooks directory we don't need

Again: Since we don't need to change directories....

Quoted 5 lines

the subshell here either.

See 0d314ce834 for when the subshell use got introduced for the convenience of not having to "cd" back and forth while setting up the hooks.

I think this is worse and even more misleading than even v2 was, for the reasons I mentioned earlier.

#115 lore raw

Re: [PATCH v4 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 5 lines · SZEDER Gábor
Quoted 1 line · Ævar Arnfjörð Bjarmason

5. Since we don't need to move the .git/hooks directory we don't need

Again: Since we don't need to change directories....

Quoted 8 lines
Quoted 5 lines

the subshell here either.

See 0d314ce834 for when the subshell use got introduced for the convenience of not having to "cd" back and forth while setting up the hooks.

I think this is worse and even more misleading than even v2 was, for the reasons I mentioned earlier.

Between this & the thread at
http://lore.kernel.org/git/[email protected] I'm not sure what wording you'd be OK with, would you mind suggesting an alternative?

As far as I can see it's some subtlety of how we explain the cd-ing & subshelling we did as a combination of eedf8f97e58 (Abstract test_create_repo out for use in tests., 2006-02-17) and my 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

Maybe you'd be happy with this:

In the code being removed here we both "cd"'d to the repository before calling "init", and did so in a subshell. It's not important to do either, so both of those can be removed. We cd'd because this code grew from test-lib.sh code where we'd have done so already, see 0d314ce834d, and later "cd"'d inside a subshell since 0d314ce834d to avoid having to keep track of an "old pwd" variable to cd back after the setup.

Being in the repository directory made moving the hooks around easier (we wouldn't have to fully qualify the path), but since we're not moving the hooks anymore we don't need it for that reason either.

?

#116 lore raw

v5 cover test-lib.sh: new test_commit args, simplification & fixes

In reply to Ævar Arnfjörð Bjarmason

Changes since v4: Only a commit message change/re-wording per <[email protected]>.

Ævar Arnfjörð Bjarmason (11):
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 +++++++---------------------
 t/test-lib-functions.sh             | 60 ++++++++++++++++++-----------
 t/test-lib.sh                       | 40 +++++++++++--------
 14 files changed, 87 insertions(+), 119 deletions(-)

Range-diff against v4:

 1:  a76ea749bb6 =  1:  75667f98f3a test-lib: bring $remove_trash out of retirement
 2:  de7be7844ea =  2:  55c9413f9cd test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 3:  709bc773fb0 =  3:  361e34654e9 test-lib-functions: reword "test_commit --append" docs
 4:  b67654334e6 =  4:  2db68a4ac7c test-lib-functions: document test_commit --no-tag
 5:  3a166c92063 =  5:  4ceba3d404b test-lib functions: add an --annotated option to "test_commit"
 6:  981fc43ee69 =  6:  589eaf7a078 describe tests: convert setup to use test_commit
 7:  15057cdecfe =  7:  a0fe0640148 test-lib functions: add --printf option to test_commit
 8:  5d437f53ec8 =  8:  7fb8849ce66 submodule tests: use symbolic-ref --short to discover branch name
 9:  9ee13ee71bc =  9:  f67245ba40d test-lib: reformat argument list in test_create_repo()
10:  6ba568df9f4 = 10:  37338c88300 test-lib: do not show advice about init.defaultBranch under --verbose
11:  311a9dba36b ! 11:  7793311e5f1 test-lib: split up and deprecate test_create_repo()

@@ Commit message
from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

- 5. Since we don't need to move the .git/hooks directory we don't need
- the subshell here either.
-
- See 0d314ce834 for when the subshell use got introduced for the
- convenience of not having to "cd" back and forth while setting up
- the hooks.
+ This makes us implicitly depend on the default hooks being
+ disabled, which is a good thing. If and when we'd have any
+ on-by-default hooks (I see no reason we ever would) we'd want to
+ see the subtle and not so subtle ways that would break the test
+ suite.
+
+ 5. We don't need to "cd" to the "$repo" directory at all anymore.
+
+ In the code being removed here we both "cd"'d to the repository
+ before calling "init", and did so in a subshell.
+
+ It's not important to do either, so both of those can be
+ removed. We cd'd because this code grew from test-lib.sh code
+ where we'd have done so already, see eedf8f97e58 (Abstract
+ test_create_repo out for use in tests., 2006-02-17), and later
+ "cd"'d inside a subshell since 0d314ce834d to avoid having to keep
+ track of an "old pwd" variable to cd back after the setup.
+
+ Being in the repository directory made moving the hooks around
+ easier (we wouldn't have to fully qualify the path). Since we're
+ not moving the hooks per #4 above we don't need to "cd" for that
+ reason either.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See

Signature · 1 line
2.31.1.737.g98b508eba36
#117 lore raw

PATCH v5 1/11 test-lib: bring $remove_trash out of retirement

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

Let's also fix a bug that was with us since abc5d372ec (Enable parallel tests, 2008-08-08): we would leak $remove_trash from the environment. We don't want this to error out, so let's reset it to the empty string first:

     remove_trash=t GIT_SKIP_TESTS=t0001 ./t0001-init.sh

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
t/test-lib.sh +18 -14
index 3dec266221c..105c424bf56 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,22 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+remove_trash=
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1357,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1369,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.737.g98b508eba36
#118 lore raw

Re: [PATCH v5 01/11] test-lib: bring $remove_trash out of retirement

Quoted 9 lines · Ævar Arnfjörð Bjarmason

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll hit the error about it being unexpectedly gone added in df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20).

s/test_done time/&,/ perhaps?

I guess it maybe only me (I'm not a native speaker of any Indo-European languages). But "because if" sounds weird to me. In my native tongue, I would say something like:

We carry around ... variable because we'll hit the error ... if the directory is expectedly gone ...

Quoted 9 lines

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

Let's also fix a bug that was with us since abc5d372ec (Enable parallel tests, 2008-08-08): we would leak $remove_trash from the environment. We don't want this to error out, so let's reset it to the empty string first:

Quoted 7 lines
     remove_trash=t GIT_SKIP_TESTS=t0001 ./t0001-init.sh

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it let's move the HOME assignment to just before

s/at it/&,/ too?

Quoted 75 lines

test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
t/test-lib.sh +18 -14
index 3dec266221c..105c424bf56 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,22 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+remove_trash=
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1357,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1369,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.737.g98b508eba36
Signature · 1 line
Danh
#119 lore raw

PATCH v5 2/11 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27a..2c6e34b9478 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.737.g98b508eba36
#120 lore raw

PATCH v5 3/11 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index b823c140271..1f787ff0726 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.737.g98b508eba36
#121 lore raw

PATCH v5 4/11 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 2 ++
 1 file changed, 2 insertions(+)
t/test-lib-functions.sh +2 -0
index 1f787ff0726..c6d07f4ce32 100644
@@ -177,6 +177,8 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.737.g98b508eba36
#122 lore raw

PATCH v5 5/11 test-lib functions: add an --annotated option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated option to test_commit to create annotated tags. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c3..17d3cc14050 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index c6d07f4ce32..827c8502b10 100644
@@ -179,6 +179,10 @@ debug () {
 #	Invoke "git commit" with --author <author>
 #   --no-tag
 #	Do not tag the resulting commit
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag, unless --notick
+#	is given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -191,7 +195,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -219,7 +223,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -243,10 +250,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test -z "$notick"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.737.g98b508eba36
#123 lore raw

PATCH v5 6/11 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747bee..88fddc91424 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.737.g98b508eba36
#124 lore raw

Re: [PATCH v5 06/11] describe tests: convert setup to use test_commit

Quoted 2 lines · Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotated-tag option to

s/--annotated-tag/--annotate/

Otherwise, looks correct.

Quoted 15 lines

test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

Signature · 1 line
Danh
#125 lore raw

PATCH v5 7/11 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388e..930dce06f0f 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a2..f691e6d9032 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15b..6cdee2a2164 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60d..a39a626664d 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f9..e2c0c510222 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index 827c8502b10..701518f5fd3 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -191,6 +195,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -202,6 +207,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -238,9 +246,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.737.g98b508eba36
#126 lore raw

Re: [PATCH v5 07/11] test-lib functions: add --printf option to test_commit

t/test-lib-functions.sh debug () { · line 179

Quoted 12 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +4 -0
index 827c8502b10..701518f5fd3 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).

This is the second appearance of "You" in this file, and it's the first in the usage of test_commit
Perhaps:

Trailing "\n" won't be added automatically. This option supports nothing but the FORMAT of printf(1), not even its ARGUMENT(s).

Or something along the line.

Quoted 3 lines
t/test-lib-functions.sh +0 -0
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
Danh
#127 lore raw

PATCH v5 8/11 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e93083..f7c7df0ca42 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.737.g98b508eba36
#128 lore raw

PATCH v5 9/11 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index 701518f5fd3..7101030f042 100644
@@ -1250,8 +1250,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.737.g98b508eba36
#129 lore raw

PATCH v5 10/11 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 7101030f042..c9f33b79151 100644
@@ -1251,7 +1251,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index 105c424bf56..ab1fe98ccce 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.737.g98b508eba36
#130 lore raw

PATCH v5 11/11 test-lib: split up and deprecate test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it.

This leave us with a pass-through wrapper for "git init" in test-lib-functions.sh, in test-lib.sh we have the same, except for needing to redirect stdout/stderr, and emitting an error ourselves if it fails. We don't need to error() ourselves when test_create_repo() is invoked, as the invocation will be a part of a test's "&&"-chain.

Everything below this paragraph is a detailed summary of the history of test_create_repo() explaining why it's safe to remove the various things it was doing:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

This makes us implicitly depend on the default hooks being disabled, which is a good thing. If and when we'd have any on-by-default hooks (I see no reason we ever would) we'd want to see the subtle and not so subtle ways that would break the test suite.

5. We don't need to "cd" to the "$repo" directory at all anymore.

In the code being removed here we both "cd"'d to the repository before calling "init", and did so in a subshell.

It's not important to do either, so both of those can be removed. We cd'd because this code grew from test-lib.sh code where we'd have done so already, see eedf8f97e58 (Abstract test_create_repo out for use in tests., 2006-02-17), and later "cd"'d inside a subshell since 0d314ce834d to avoid having to keep track of an "old pwd" variable to cd back after the setup.

Being in the repository directory made moving the hooks around easier (we wouldn't have to fully qualify the path). Since we're not moving the hooks per #4 above we don't need to "cd" for that reason either.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh.

We could still invoke test_create_repo() there, but as the invocation is now trivial and we don't have a good reason to use test_create_repo() elsewhere let's call "git init" there ourselves.

8. We didn't need to resolve "git" as "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(), even for the use of test-lib.sh

PATH is already set up in test-lib.sh to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before test_create_repo() (now "git init") is called.. So we can simply run "git" and rely on the PATH lookup choosing the right executable.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 ++-------------
 t/test-lib.sh                       |  3 ++-
 5 files changed, 4 insertions(+), 18 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649e..5c509db6fc3 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4e..6da8d760e28 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8ec..9f1a483f426 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +2 -13
index c9f33b79151..213b1988aa4 100644
@@ -1241,21 +1241,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	git init "$@"
 }
 
 # This function helps on symlink challenged file systems when it is not
t/test-lib.sh +2 -1
index ab1fe98ccce..5fd968d11b2 100644
@@ -1365,7 +1365,8 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	git init "$TRASH_DIRECTORY" >&3 2>&4 ||
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.737.g98b508eba36
#131 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 15 lines · Ævar Arnfjörð Bjarmason

Changes since v4: Only a commit message change/re-wording per <[email protected]>.

Ævar Arnfjörð Bjarmason (11):
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

I wasn't paying much attention to this series, and didn't look at the last step at all (as I saw somebody else is already deeply biting into it), but the earlier parts looked good to me.

It was painful to come back to the pile of topics after a week's interruption, though. While juggling ~50 topics, it is unfair to expect the maintainer to remember that a topic that is still going through iterations are depended on two other topics in flight (it is easier for contributors, who will be juggling far fewer number of topics at one time).

But I think I've sorted out the duplicates caused by forgetting that I had to rebase ab/pickaxe-pcre2 and ab/describe-tests-fix on top of this topic. Will be pushing out the result of today's integration sometime this evening, which is far from complete as I am still in the "catch up with what happened during my absense" mode.

Thanks.

#132 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 2 lines · Ævar Arnfjörð Bjarmason

Changes since v4: Only a commit message change/re-wording per <[email protected]>.

SZEDER, Eric, Đoàn et al: does this v5 look good to you? Junio's suggested in [1] that this needs some final review feedback before advancing to 'next'. If you could take a look that would be great!

1. https://lore.kernel.org/git/[email protected]/

Quoted 76 lines

Ævar Arnfjörð Bjarmason (11):
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 +++++++---------------------
 t/test-lib-functions.sh             | 60 ++++++++++++++++++-----------
 t/test-lib.sh                       | 40 +++++++++++--------
 14 files changed, 87 insertions(+), 119 deletions(-)

Range-diff against v4:

 1:  a76ea749bb6 =  1:  75667f98f3a test-lib: bring $remove_trash out of retirement
 2:  de7be7844ea =  2:  55c9413f9cd test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 3:  709bc773fb0 =  3:  361e34654e9 test-lib-functions: reword "test_commit --append" docs
 4:  b67654334e6 =  4:  2db68a4ac7c test-lib-functions: document test_commit --no-tag
 5:  3a166c92063 =  5:  4ceba3d404b test-lib functions: add an --annotated option to "test_commit"
 6:  981fc43ee69 =  6:  589eaf7a078 describe tests: convert setup to use test_commit
 7:  15057cdecfe =  7:  a0fe0640148 test-lib functions: add --printf option to test_commit
 8:  5d437f53ec8 =  8:  7fb8849ce66 submodule tests: use symbolic-ref --short to discover branch name
 9:  9ee13ee71bc =  9:  f67245ba40d test-lib: reformat argument list in test_create_repo()
10:  6ba568df9f4 = 10:  37338c88300 test-lib: do not show advice about init.defaultBranch under --verbose
11:  311a9dba36b ! 11:  7793311e5f1 test-lib: split up and deprecate test_create_repo()

@@ Commit message
from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

- 5. Since we don't need to move the .git/hooks directory we don't need
- the subshell here either.
-
- See 0d314ce834 for when the subshell use got introduced for the
- convenience of not having to "cd" back and forth while setting up
- the hooks.
+ This makes us implicitly depend on the default hooks being
+ disabled, which is a good thing. If and when we'd have any
+ on-by-default hooks (I see no reason we ever would) we'd want to
+ see the subtle and not so subtle ways that would break the test
+ suite.
+
+ 5. We don't need to "cd" to the "$repo" directory at all anymore.
+
+ In the code being removed here we both "cd"'d to the repository
+ before calling "init", and did so in a subshell.
+
+ It's not important to do either, so both of those can be
+ removed. We cd'd because this code grew from test-lib.sh code
+ where we'd have done so already, see eedf8f97e58 (Abstract
+ test_create_repo out for use in tests., 2006-02-17), and later
+ "cd"'d inside a subshell since 0d314ce834d to avoid having to keep
+ track of an "old pwd" variable to cd back after the setup.
+
+ Being in the repository directory made moving the hooks around
+ easier (we wouldn't have to fully qualify the path). Since we're
+ not moving the hooks per #4 above we don't need to "cd" for that
+ reason either.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See

#133 lore raw
Quoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Ævar Arnfjörð Bjarmason

Changes since v4: Only a commit message change/re-wording per <[email protected]>.

SZEDER, Eric, Đoàn et al: does this v5 look good to you? Junio's suggested in [1] that this needs some final review feedback before advancing to 'next'. If you could take a look that would be great!

I have some minor nit-picks here and there, I didn't look into 11/11. Otherwise, I think the series is fine, now.

Signature · 1 line
Danh
#134 lore raw

v6 cover test-lib.sh: new test_commit args, simplification & fixes

In reply to Ævar Arnfjörð Bjarmason

Changes since v5 only trivial commit message / comment fixes suggested by Đoàn Trần Công Danh. Thanks! Link to v5:
http://lore.kernel.org/git/[email protected]

Ævar Arnfjörð Bjarmason (11):
test-lib: bring $remove_trash out of retirement
test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
test-lib-functions: reword "test_commit --append" docs
test-lib-functions: document test_commit --no-tag
test-lib functions: add an --annotated option to "test_commit" describe tests: convert setup to use test_commit
test-lib functions: add --printf option to test_commit submodule tests: use symbolic-ref --short to discover branch name
test-lib: reformat argument list in test_create_repo()
test-lib: do not show advice about init.defaultBranch under --verbose
test-lib: split up and deprecate test_create_repo()

 t/lib-submodule-update.sh           |  3 +-
 t/t0000-basic.sh                    |  4 --
 t/t1307-config-blob.sh              |  4 +-
 t/t1403-show-ref.sh                 |  6 +--
 t/t2030-unresolve-info.sh           |  3 +-
 t/t4006-diff-mode.sh                |  6 +--
 t/t4030-diff-textconv.sh            |  8 +---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 -
 t/t5409-colorize-remote-messages.sh |  1 -
 t/t5520-pull.sh                     | 10 +----
 t/t6120-describe.sh                 | 58 ++++++---------------------
 t/test-lib-functions.sh             | 62 ++++++++++++++++++-----------
 t/test-lib.sh                       | 40 ++++++++++++-------
 14 files changed, 89 insertions(+), 119 deletions(-)

Range-diff against v5:
1: 75667f98f3a ! 1: febf98e87a1 test-lib: bring $remove_trash out of retirement
@@ Commit message
case.

We carry around the "$remove_trash" variable because if the directory
- is unexpectedly gone at test_done time we'll hit the error about it
- being unexpectedly gone added in df4c0d1a792 (test-lib: abort when
- can't remove trash directory, 2017-04-20).
+ is unexpectedly gone at test_done time we'll still want to hit the
+ "trash directory already removed" error, but not if we never created
+ the trash directory. See df4c0d1a792 (test-lib: abort when can't
+ remove trash directory, 2017-04-20) for the addition of that error.

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all
@@ Commit message
under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

- While I'm at it let's move the HOME assignment to just before
+ While I'm at it, let's move the HOME assignment to just before
test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

 2:  55c9413f9cd =  2:  6f360e37e84 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable
 3:  361e34654e9 =  3:  c2a0140fcd9 test-lib-functions: reword "test_commit --append" docs
 4:  2db68a4ac7c =  4:  1a675a6cfe5 test-lib-functions: document test_commit --no-tag
 5:  4ceba3d404b =  5:  2e0d69209e9 test-lib functions: add an --annotated option to "test_commit"
 6:  589eaf7a078 !  6:  07e8b63596b describe tests: convert setup to use test_commit

@@ Commit message
describe tests: convert setup to use test_commit

Convert the setup of the describe tests to use test_commit when
- possible. This makes use of the new --annotated-tag option to
- test_commit.
+ possible. This makes use of the new --annotate option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done
7: a0fe0640148 ! 7: 5aaeadf01ff test-lib functions: add --printf option to test_commit

    @@ t/test-lib-functions.sh: debug () {
      #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
     +#   --printf
     +#       Use "printf" instead of "echo" when writing "<contents>" to
    -+#       "<file>". You will need to provide your own trailing "\n". You
    -+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
    ++#       "<file>", use this to write escape sequences such as "\0", a
    ++#       trailing "\n" won't be added automatically. This option
    ++#       supports nothing but the FORMAT of printf(1), i.e. no custom
    ++#       ARGUMENT(s).
      #   --signoff
      #	Invoke "git commit" with --signoff
      #   --author <author>
 8:  7fb8849ce66 =  8:  0b31b1ffeff submodule tests: use symbolic-ref --short to discover branch name
 9:  f67245ba40d =  9:  c806f3888c5 test-lib: reformat argument list in test_create_repo()
10:  37338c88300 = 10:  105a95fb7a0 test-lib: do not show advice about init.defaultBranch under --verbose
11:  7793311e5f1 = 11:  217c5aed491 test-lib: split up and deprecate test_create_repo()
Signature · 1 line
2.31.1.838.g924d365b763
#135 lore raw

PATCH v6 1/11 test-lib: bring $remove_trash out of retirement

There's no point in creating a repository or directory only to decide right afterwards that we're skipping all the tests. We can save ourselves the redundant "git init" or "mkdir" and "rm -rf" in this case.

We carry around the "$remove_trash" variable because if the directory is unexpectedly gone at test_done time we'll still want to hit the "trash directory already removed" error, but not if we never created the trash directory. See df4c0d1a792 (test-lib: abort when can't remove trash directory, 2017-04-20) for the addition of that error.

So let's partially revert 06478dab4c (test-lib: retire $remove_trash variable, 2017-04-23) and move the decision about whether to skip all tests earlier.

Let's also fix a bug that was with us since abc5d372ec (Enable parallel tests, 2008-08-08): we would leak $remove_trash from the environment. We don't want this to error out, so let's reset it to the empty string first:

     remove_trash=t GIT_SKIP_TESTS=t0001 ./t0001-init.sh

I tested this with --debug, see 4d0912a206 (test-lib.sh: do not barf under --debug at the end of the test, 2017-04-24) for a bug we don't want to re-introduce.

While I'm at it, let's move the HOME assignment to just before test_create_repo, it could be lower, but it seems better to set it before calling anything in test-lib-functions.sh

---
 t/test-lib.sh | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
t/test-lib.sh +18 -14
index adaa2db6014..7647afcfd0a 100644
@@ -1169,7 +1169,7 @@ test_done () {
 			esac
 		fi
 
-		if test -z "$debug"
+		if test -z "$debug" && test -n "$remove_trash"
 		then
 			test -d "$TRASH_DIRECTORY" ||
 			error "Tests passed but trash directory already removed before test cleanup; aborting"
@@ -1334,6 +1334,22 @@ then
 	exit 1
 fi
 
+# Are we running this test at all?
+remove_trash=
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" $GIT_SKIP_TESTS
+then
+	say_color info >&3 "skipping test $this_test altogether"
+	skip_all="skip all tests in $this_test"
+	test_done
+fi
+
+# Last-minute variable setup
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME
+
 # Test repository
 rm -fr "$TRASH_DIRECTORY" || {
 	GIT_EXIT_OK=t
@@ -1341,10 +1357,7 @@ rm -fr "$TRASH_DIRECTORY" || {
 	exit 1
 }
 
-HOME="$TRASH_DIRECTORY"
-GNUPGHOME="$HOME/gnupg-home-not-used"
-export HOME GNUPGHOME
-
+remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
 	test_create_repo "$TRASH_DIRECTORY"
@@ -1356,15 +1369,6 @@ fi
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || exit 1
 
-this_test=${0##*/}
-this_test=${this_test%%-*}
-if match_pattern_list "$this_test" $GIT_SKIP_TESTS
-then
-	say_color info >&3 "skipping test $this_test altogether"
-	skip_all="skip all tests in $this_test"
-	test_done
-fi
-
 if test -n "$write_junit_xml"
 then
 	junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
Signature · 1 line
2.31.1.838.g924d365b763
#136 lore raw

PATCH v6 2/11 test-lib tests: remove dead GIT_TEST_FRAMEWORK_SELFTEST variable

In reply to Ævar Arnfjörð Bjarmason

Stop setting the GIT_TEST_FRAMEWORK_SELFTEST variable. This was originally needed back in 4231d1ba99 (t0000: do not get self-test disrupted by environment warnings, 2018-09-20).

It hasn't been needed since I deleted the relevant code in test-lib.sh in c0eedbc009 (test-lib: remove check_var_migration, 2021-02-09), I just didn't notice that it was set here.

---
 t/t0000-basic.sh | 4 ----
 1 file changed, 4 deletions(-)
t/t0000-basic.sh +0 -4
index 705d62cc27a..2c6e34b9478 100755
@@ -84,10 +84,6 @@ _run_sub_test_lib_test_common () {
 		passing metrics
 		'
 
-		# Tell the framework that we are self-testing to make sure
-		# it yields a stable result.
-		GIT_TEST_FRAMEWORK_SELFTEST=t &&
-
 		# Point to the t/test-lib.sh, which isn't in ../ as usual
 		. "\$TEST_DIRECTORY"/test-lib.sh
 		EOF
Signature · 1 line
2.31.1.838.g924d365b763
#137 lore raw

PATCH v6 3/11 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index b823c140271..1f787ff0726 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.31.1.838.g924d365b763
#138 lore raw

PATCH v6 4/11 test-lib-functions: document test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option. Let's fix that.

---
 t/test-lib-functions.sh | 2 ++
 1 file changed, 2 insertions(+)
t/test-lib-functions.sh +2 -0
index 1f787ff0726..c6d07f4ce32 100644
@@ -177,6 +177,8 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
Signature · 1 line
2.31.1.838.g924d365b763
#139 lore raw

PATCH v6 5/11 test-lib functions: add an --annotated option to "test_commit"

In reply to Ævar Arnfjörð Bjarmason

Add an --annotated option to test_commit to create annotated tags. The tag will share the same message as the commit, and we'll call test_tick before creating it (unless --notick) is provided.

There's quite a few tests that could be simplified with this construct. I've picked one to convert in this change as a demonstration.

---
 t/t1403-show-ref.sh     |  6 ++----
 t/test-lib-functions.sh | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 9 deletions(-)
t/t1403-show-ref.sh +2 -4
index 6ce62f878c3..17d3cc14050 100755
@@ -7,11 +7,9 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	test_commit A &&
-	git tag -f -a -m "annotated A" A &&
+	test_commit --annotate A &&
 	git checkout -b side &&
-	test_commit B &&
-	git tag -f -a -m "annotated B" B &&
+	test_commit --annotate B &&
 	git checkout main &&
 	test_commit C &&
 	git branch B A^0
t/test-lib-functions.sh +22 -5
index c6d07f4ce32..827c8502b10 100644
@@ -179,6 +179,10 @@ debug () {
 #	Invoke "git commit" with --author <author>
 #   --no-tag
 #	Do not tag the resulting commit
+#   --annotate
+#	Create an annotated tag with "--annotate -m <message>". Calls
+#	test_tick between making the commit and tag, unless --notick
+#	is given.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -191,7 +195,7 @@ test_commit () {
 	author= &&
 	signoff= &&
 	indir= &&
-	no_tag= &&
+	tag=light &&
 	while test $# != 0
 	do
 		case "$1" in
@@ -219,7 +223,10 @@ test_commit () {
 			shift
 			;;
 		--no-tag)
-			no_tag=yes
+			tag=none
+			;;
+		--annotate)
+			tag=annotate
 			;;
 		*)
 			break
@@ -243,10 +250,20 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
-	then
+	case "$tag" in
+	none)
+		;;
+	light)
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
-	fi
+		;;
+	annotate)
+		if test -z "$notick"
+		then
+			test_tick
+		fi &&
+		git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+		;;
+	esac
 }
 
 # Call test_merge with the arguments "<message> <commit>", where <commit>
Signature · 1 line
2.31.1.838.g924d365b763
#140 lore raw

PATCH v6 6/11 describe tests: convert setup to use test_commit

In reply to Ævar Arnfjörð Bjarmason

Convert the setup of the describe tests to use test_commit when possible. This makes use of the new --annotate option to test_commit.

Some of the setup here could simply be removed since the data being created wasn't important to any of the subsequent tests, so I've done so. E.g. assigning to the "one" variable was always useless, and just checking that we can describe HEAD after the first commit wasn't useful.

In the case of the "two" variable we could instead use the tag we just created. See 5312ab11fbf (Add describe test., 2007-01-13) for the initial version of this code. There's other cases here like redundant "test_tick" invocations, or the simplification of not echoing "X" to a file we're about to tag as "x", now we just use "x" in both cases.

---
 t/t6120-describe.sh | 58 ++++++++++-----------------------------------
 1 file changed, 13 insertions(+), 45 deletions(-)
t/t6120-describe.sh +13 -45
index e89b6747bee..88fddc91424 100755
@@ -31,64 +31,32 @@ check_describe () {
 }
 
 test_expect_success setup '
+	test_commit initial file one &&
+	test_commit second file two &&
+	test_commit third file three &&
+	test_commit --annotate A file A &&
+	test_commit c file c &&
 
-	test_tick &&
-	echo one >file && git add file && git commit -m initial &&
-	one=$(git rev-parse HEAD) &&
-
-	git describe --always HEAD &&
-
-	test_tick &&
-	echo two >file && git add file && git commit -m second &&
-	two=$(git rev-parse HEAD) &&
-
-	test_tick &&
-	echo three >file && git add file && git commit -m third &&
-
-	test_tick &&
-	echo A >file && git add file && git commit -m A &&
-	test_tick &&
-	git tag -a -m A A &&
-
-	test_tick &&
-	echo c >file && git add file && git commit -m c &&
-	test_tick &&
-	git tag c &&
-
-	git reset --hard $two &&
-	test_tick &&
-	echo B >side && git add side && git commit -m B &&
-	test_tick &&
-	git tag -a -m B B &&
+	git reset --hard second &&
+	test_commit --annotate B side B &&
 
 	test_tick &&
 	git merge -m Merged c &&
 	merged=$(git rev-parse HEAD) &&
 
-	git reset --hard $two &&
-	test_tick &&
-	echo D >another && git add another && git commit -m D &&
-	test_tick &&
-	git tag -a -m D D &&
-	test_tick &&
-	git tag -a -m R R &&
-
-	test_tick &&
-	echo DD >another && git commit -a -m another &&
+	git reset --hard second &&
+	test_commit --no-tag D another D &&
 
 	test_tick &&
-	git tag e &&
+	git tag -a -m R R &&
 
-	test_tick &&
-	echo DDD >another && git commit -a -m "yet another" &&
+	test_commit e another DD &&
+	test_commit --no-tag "yet another" another DDD &&
 
 	test_tick &&
 	git merge -m Merged $merged &&
 
-	test_tick &&
-	echo X >file && echo X >side && git add file side &&
-	git commit -m x
-
+	test_commit --no-tag x file
 '
 
 check_describe A-* HEAD
Signature · 1 line
2.31.1.838.g924d365b763
#141 lore raw

PATCH v6 7/11 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 14 ++++++++++++--
 6 files changed, 20 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388e..930dce06f0f 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a2..f691e6d9032 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit --printf binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 275ce5fa15b..6cdee2a2164 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index c906320b60d..a39a626664d 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f9..e2c0c510222 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +12 -2
index 827c8502b10..b242ecf5cd2 100644
@@ -173,6 +173,12 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>", use this to write escape sequences such as "\0", a
+#       trailing "\n" won't be added automatically. This option
+#       supports nothing but the FORMAT of printf(1), i.e. no custom
+#       ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -191,6 +197,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -202,6 +209,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -238,9 +248,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.31.1.838.g924d365b763
#142 lore raw

PATCH v6 8/11 submodule tests: use symbolic-ref --short to discover branch name

In reply to Ævar Arnfjörð Bjarmason

Change a use of $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME added in 704fed9ea22 (tests: start moving to a different default main branch name, 2020-10-23) to simply discover the initial branch name of a repository set up in this function with "symbolic-ref --short".

That's something done in another test in 704fed9ea22, so doing it like this seems like an omission, or rather an overly eager search/replacement instead of fixing the test logic.

There are only three uses of the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable in the test suite, this gets rid of one of those.

---
 t/lib-submodule-update.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
t/lib-submodule-update.sh +2 -1
index 4b714e93083..f7c7df0ca42 100644
@@ -63,6 +63,7 @@ create_lib_submodule_repo () {
 	git init submodule_update_repo &&
 	(
 		cd submodule_update_repo &&
+		branch=$(git symbolic-ref --short HEAD) &&
 		echo "expect" >>.gitignore &&
 		echo "actual" >>.gitignore &&
 		echo "x" >file1 &&
@@ -144,7 +145,7 @@ create_lib_submodule_repo () {
 		git checkout -b valid_sub1 &&
 		git revert HEAD &&
 
-		git checkout "${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}"
+		git checkout "$branch"
 	)
 }
 
Signature · 1 line
2.31.1.838.g924d365b763
#143 lore raw

PATCH v6 9/11 test-lib: reformat argument list in test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Reformat an argument list changed in 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) to have the "-c" on the same line as the argument it sets. This whitespace-only change makes it easier to review a subsequent commit.

---
 t/test-lib-functions.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/test-lib-functions.sh +2 -2
index b242ecf5cd2..661597e9192 100644
@@ -1252,8 +1252,8 @@ test_create_repo () {
 	mkdir -p "$repo"
 	(
 		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
-			init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
+		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
+			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
Signature · 1 line
2.31.1.838.g924d365b763
#144 lore raw

PATCH v6 10/11 test-lib: do not show advice about init.defaultBranch under --verbose

In reply to Ævar Arnfjörð Bjarmason

Arrange for the advice about naming the initial branch not to be shown in the --verbose output of the test suite.

Since 675704c74dd (init: provide useful advice about init.defaultBranch, 2020-12-11) some tests have been very chatty with repeated occurrences of this multi-line advice. Having it be this verbose isn't helpful for anyone in the context of git's own test suite, and it makes debugging tests that use their own "git init" invocations needlessly distracting.

By setting the GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME variable early in test-lib.sh itself we'll squash the warning not only for test_create_repo(), as 675704c74dd explicitly intended, but also for other "git init" invocations.

And once we'd like to have this configuration set for all "git init" invocations in the test suite we can get rid of the init.defaultBranch configuration setting in test_create_repo(), as repo_default_branch_name() in refs.c will take the GIT_TEST_* variable over it being set.

---
 t/test-lib-functions.sh | 1 -
 t/test-lib.sh           | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
t/test-lib-functions.sh +0 -1
index 661597e9192..669645a812f 100644
@@ -1253,7 +1253,6 @@ test_create_repo () {
 	(
 		cd "$repo" || error "Cannot setup test environment"
 		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			-c init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
 			init \
 			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 		error "cannot run git init -- have you built things yet?"
t/test-lib.sh +5 -0
index 7647afcfd0a..f3671b1766d 100644
@@ -64,6 +64,11 @@ then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
Signature · 1 line
2.31.1.838.g924d365b763
#145 lore raw

PATCH v6 11/11 test-lib: split up and deprecate test_create_repo()

In reply to Ævar Arnfjörð Bjarmason

Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it.

This leave us with a pass-through wrapper for "git init" in test-lib-functions.sh, in test-lib.sh we have the same, except for needing to redirect stdout/stderr, and emitting an error ourselves if it fails. We don't need to error() ourselves when test_create_repo() is invoked, as the invocation will be a part of a test's "&&"-chain.

Everything below this paragraph is a detailed summary of the history of test_create_repo() explaining why it's safe to remove the various things it was doing:

1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed.

2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK.

3. We won't ever hit that "Cannot setup test environment" error.

Checking the test environment sanity when doing "git init" dates back to eea420693be (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd2027b01 (trivial: check, if t/trash directory was successfully created, 2006-01-05).

We can also see it in another form a bit later in my own 0d314ce834d (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30).

But since 2006f0adaee (t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier.

The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since 3af4c7156c4 (tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init".

So all of that's been checked already, and we don't need to re-check it here.

4. We don't need to move .git/hooks out of the way.

That dates back to c09a69a83e3 (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cbac01 (Ship sample hooks with .sample suffix, 2008-06-24).

So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do.

This makes us implicitly depend on the default hooks being disabled, which is a good thing. If and when we'd have any on-by-default hooks (I see no reason we ever would) we'd want to see the subtle and not so subtle ways that would break the test suite.

5. We don't need to "cd" to the "$repo" directory at all anymore.

In the code being removed here we both "cd"'d to the repository before calling "init", and did so in a subshell.

It's not important to do either, so both of those can be removed. We cd'd because this code grew from test-lib.sh code where we'd have done so already, see eedf8f97e58 (Abstract test_create_repo out for use in tests., 2006-02-17), and later "cd"'d inside a subshell since 0d314ce834d to avoid having to keep track of an "old pwd" variable to cd back after the setup.

Being in the repository directory made moving the hooks around easier (we wouldn't have to fully qualify the path). Since we're not moving the hooks per #4 above we don't need to "cd" for that reason either.

6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See 8683a45d669 (Introduce GIT_TEMPLATE_DIR, 2006-12-19)

7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh.

We could still invoke test_create_repo() there, but as the invocation is now trivial and we don't have a good reason to use test_create_repo() elsewhere let's call "git init" there ourselves.

8. We didn't need to resolve "git" as "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(), even for the use of test-lib.sh

PATH is already set up in test-lib.sh to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before test_create_repo() (now "git init") is called.. So we can simply run "git" and rely on the PATH lookup choosing the right executable.

---
 t/t5406-remote-rejects.sh           |  1 -
 t/t5407-post-rewrite-hook.sh        |  2 --
 t/t5409-colorize-remote-messages.sh |  1 -
 t/test-lib-functions.sh             | 15 ++-------------
 t/test-lib.sh                       |  3 ++-
 5 files changed, 4 insertions(+), 18 deletions(-)
t/t5406-remote-rejects.sh +0 -1
index ff06f99649e..5c509db6fc3 100755
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	exit 1
 	EOF
t/t5407-post-rewrite-hook.sh +0 -2
index 5bb23cc3a4e..6da8d760e28 100755
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
 	git checkout main
 '
 
-mkdir .git/hooks
-
 cat >.git/hooks/post-rewrite <<EOF
 #!/bin/sh
 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
t/t5409-colorize-remote-messages.sh +0 -1
index 5d8f401d8ec..9f1a483f426 100755
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
 . ./test-lib.sh
 
 test_expect_success 'setup' '
-	mkdir .git/hooks &&
 	write_script .git/hooks/update <<-\EOF &&
 	echo error: error
 	echo ERROR: also highlighted
t/test-lib-functions.sh +2 -13
index 669645a812f..f0448daa74b 100644
@@ -1243,21 +1243,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {
-	test "$#" = 1 ||
-	BUG "not 1 parameter to test-create-repo"
-	repo="$1"
-	mkdir -p "$repo"
-	(
-		cd "$repo" || error "Cannot setup test environment"
-		"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
-			init \
-			"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
-		error "cannot run git init -- have you built things yet?"
-		mv .git/hooks .git/hooks-disabled
-	) || exit
+	git init "$@"
 }
 
 # This function helps on symlink challenged file systems when it is not
t/test-lib.sh +2 -1
index f3671b1766d..e6ca7a22826 100644
@@ -1365,7 +1365,8 @@ rm -fr "$TRASH_DIRECTORY" || {
 remove_trash=t
 if test -z "$TEST_NO_CREATE_REPO"
 then
-	test_create_repo "$TRASH_DIRECTORY"
+	git init "$TRASH_DIRECTORY" >&3 2>&4 ||
+	error "cannot run git init"
 else
 	mkdir -p "$TRASH_DIRECTORY"
 fi
Signature · 1 line
2.31.1.838.g924d365b763
#146 lore raw

RE: [PATCH v6 11/11] test-lib: split up and deprecate test_create_repo()

t/test-lib-functions.sh test_atexit () { · line 1248

Quoted 10 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +1 -1
@@ -1243,21 +1243,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {

If this is deprecated why not add a warning?

echo "warning: test_create_repo is deprecated in favor of git init" >&2

Signature · 1 line
Felipe Contreras
#147 lore raw

Re: [PATCH v6 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 7 lines · Felipe Contreras
Quoted 6 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +0 -0
@@ -1243,21 +1243,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
Quoted 8 lines

t/test-lib-functions.sh test_atexit () { · line 1248

Quoted 4 lines
t/test-lib-functions.sh +1 -1
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {

If this is deprecated why not add a warning?

echo "warning: test_create_repo is deprecated in favor of git init" >&2

Because like test_i18ncmp, test_i18ngrep or whatever this is in the state of "don't use this for new code", but annoying everyone who runs the test suite with loads of this output under -v would be too distracting.

The attention of the developer community is much better spent on one person doing a s/test_create_repo/git init/ patch than having everyone see this warning until somebody does that.

#148 lore raw

Re: [PATCH v6 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 19 lines · Ævar Arnfjörð Bjarmason
Quoted 7 lines · Felipe Contreras
Quoted 6 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +0 -0
@@ -1243,21 +1243,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
Quoted 8 lines

t/test-lib-functions.sh test_atexit () { · line 1248

Quoted 4 lines
t/test-lib-functions.sh +1 -1
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {

If this is deprecated why not add a warning?

echo "warning: test_create_repo is deprecated in favor of git init" >&2

Quoted 8 lines

Because like test_i18ncmp, test_i18ngrep or whatever this is in the state of "don't use this for new code", but annoying everyone who runs the test suite with loads of this output under -v would be too distracting.

The attention of the developer community is much better spent on one person doing a s/test_create_repo/git init/ patch than having everyone see this warning until somebody does that.

Then it's not really deprecated; it's merely disfavored.

To deprecate means to express disapproval of. If we are not going to express disapproval (i.e. annoy the users of the test suite), then it's not really deprecated. You can't eat your cake and have it too.

I agree we shouldn't throw a warning right now, but at some point in the future we should, *then* it will be deprecated.

Cheers.

Signature · 1 line
Felipe Contreras
#149 lore raw

Re: [PATCH v6 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 29 lines · Felipe Contreras
Quoted 19 lines · Ævar Arnfjörð Bjarmason
Quoted 7 lines · Felipe Contreras
Quoted 6 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +0 -0
@@ -1243,21 +1243,10 @@ test_atexit () {
 		} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
 }
 
Quoted 8 lines

t/test-lib-functions.sh test_atexit () { · line 1248

Quoted 4 lines
t/test-lib-functions.sh +1 -1
-# Most tests can use the created repository, but some may need to create more.
+# Deprecated wrapper for "git init", use "git init" directly instead
 # Usage: test_create_repo <directory>
 test_create_repo () {

If this is deprecated why not add a warning?

echo "warning: test_create_repo is deprecated in favor of git init" >&2

Quoted 8 lines

Because like test_i18ncmp, test_i18ngrep or whatever this is in the state of "don't use this for new code", but annoying everyone who runs the test suite with loads of this output under -v would be too distracting.

The attention of the developer community is much better spent on one person doing a s/test_create_repo/git init/ patch than having everyone see this warning until somebody does that.

Quoted 8 lines

Then it's not really deprecated; it's merely disfavored.

To deprecate means to express disapproval of. If we are not going to express disapproval (i.e. annoy the users of the test suite), then it's not really deprecated. You can't eat your cake and have it too.

I agree we shouldn't throw a warning right now, but at some point in the future we should, *then* it will be deprecated.

I don't feel strongly about either term, as long as it's consistent.

I do think that "deprecated" is consistent in the way I'm using it within the git project. If you grep various things we've "deprecated" we usually just do so by noting so in the docs. In this case the docs are the comments in test-lib.sh.

We do also have things like git rebase --preserve-merges which emit a warning, but we use "deprecated" for both. By contrast we don't have a single git for "git grep -i disfavor".

Anyway, for now I'd prefer to just have this land as-is and sort out such minor things later, given the v6 and having two other topics waiting on this...

#150 lore raw

Re: [PATCH v6 11/11] test-lib: split up and deprecate test_create_repo()

Quoted 18 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines · Felipe Contreras

Then it's not really deprecated; it's merely disfavored.

To deprecate means to express disapproval of. If we are not going to express disapproval (i.e. annoy the users of the test suite), then it's not really deprecated. You can't eat your cake and have it too.

I agree we shouldn't throw a warning right now, but at some point in the future we should, *then* it will be deprecated.

I don't feel strongly about either term, as long as it's consistent.

I do think that "deprecated" is consistent in the way I'm using it within the git project. If you grep various things we've "deprecated" we usually just do so by noting so in the docs. In this case the docs are the comments in test-lib.sh.

Quoted 7 lines

We do also have things like git rebase --preserve-merges which emit a warning, but we use "deprecated" for both. By contrast we don't have a single git for "git grep -i disfavor".

Anyway, for now I'd prefer to just have this land as-is and sort out such minor things later, given the v6 and having two other topics waiting on this...

Fair enough.

Signature · 1 line
Felipe Contreras
#153 lore raw

v2 cover test-lib-functions.sh: trickery to make -x less verbose

In reply to Ævar Arnfjörð Bjarmason

This series makes the "-x" output less verbose for our various helper functions, mainly using the trickery of feeding "$@" to "test" to rely on it to die when given too many arguments.

Since v1 the only change is adding a comment as suggested by Eric Sunshine, it's not ideal as noted in <[email protected]> (I wish these were just in one file, so we could have a comment at the top), but will aid future maintenance anyway.

Ævar Arnfjörð Bjarmason (3):
test-lib-functions: normalize test_path_is_missing() debugging Revert and amend "test-lib-functions: assert correct parameter count"
test-lib-functions: remove last two parameter count assertions

 t/test-lib-functions.sh | 72 +++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 42 deletions(-)

Range-diff against v1:

1:  a3e20ef18f < -:  ---------- test-lib: split up and deprecate test_create_repo()
2:  c22e3f7764 = 1:  fd33f6d71f test-lib-functions: normalize test_path_is_missing() debugging
3:  6f9e09a201 ! 2:  67ddd821df Revert and amend "test-lib-functions: assert correct parameter count"

@@ Commit message

## t/test-lib-functions.sh ##
@@ t/test-lib-functions.sh: test_external_without_stderr () {
+ }
+
# debugging-friendly alternatives to "test [-f|-d|-e]"
- # The commands test the existence or non-existence of $1
+-# The commands test the existence or non-existence of $1
++# The commands test the existence or non-existence of
++# a given argument.
++#
++# The pattern of using "$@" to "test" instead of "$1" is not a bug. We ++# are counting on "test" to error on too many arguments if more than
++# one is given. Checking "$#" explicitly would lead to overly verbose
++# -x output.
test_path_is_file () {
- test "$#" -ne 1 && BUG "1 param"
- if ! test -f "$1"
4: b7b11a60bc = 3: 8fd51861b5 test-lib-functions: remove last two parameter count assertions

Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#154 lore raw

PATCH v2 1/3 test-lib-functions: normalize test_path_is_missing() debugging

Change the test_path_is_missing() to be consistent with related functions. Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists. Let's just say that it exists instead.

---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index bd64a15c73..0232cc9f46 100644
@@ -810,12 +810,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#155 lore raw

PATCH v2 2/3 Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in my e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Now we'll get just:

+ test_cmp expect actual
+ eval diff -u "$@"
+ diff -u expect actual

This does not to the "right" thing in cases like:

test_path_is_file x -a y

Which will now turn into:

test -f x -a y

I consider that to be OK given the trade-off that any extra checking would produce more verbose trace output. As shown in 45a2686441b we had issues with these functions being invoked with multiple parameters (e.g. a glob) by accident, we don't need to be paranoid in guarding against hostile misuse from our own test suite.

While I'm at it change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

---
 t/test-lib-functions.sh | 59 +++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 31 deletions(-)
t/test-lib-functions.sh +28 -31
index 0232cc9f46..f8f5bf9de1 100644
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 }
 
 # debugging-friendly alternatives to "test [-f|-d|-e]"
-# The commands test the existence or non-existence of $1
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
-		false
+		echo "File $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
-		false
+		echo "Directory $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
-		false
+		echo "Path $* doesn't exist"
+		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$*' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -798,19 +800,17 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
-		false
+		echo "'$*' is not a non-empty file."
+		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
-		echo "Path $1 exists!"
+		echo "Path $* exists!"
 		false
 	fi
 }
@@ -1012,7 +1012,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1042,7 +1041,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1103,12 +1101,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$*' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#156 lore raw

Re: [PATCH v2 2/3] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 11 lines · Ævar Arnfjörð Bjarmason

[...]
The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

---

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 10 lines
t/test-lib-functions.sh +8 -0
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 # debugging-friendly alternatives to "test [-f|-d|-e]"
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.
 test_path_is_file () {
+       if ! test -f "$@"

Thanks. The new comment makes the intent of "$@" clear.

If you do re-roll for some reason, it might make sense to move the new comment (the one starting "The pattern of...") into the function itself just before the use of "$@". The reasons I suggest this are:
(1) the comment explains an implementation detail, thus is intended for people who might change this function, whereas all the text above the new paragraph is API documentation for callers of the function who need only the black-box description; (2) it places the comment closer to the relevant code, thus is less likely to be overlooked by someone changing the function.

#157 lore raw

Re: [PATCH v2 2/3] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 26 lines · Eric Sunshine
Quoted 11 lines · Ævar Arnfjörð Bjarmason

[...]
The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

---

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 10 lines
t/test-lib-functions.sh +8 -0
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 # debugging-friendly alternatives to "test [-f|-d|-e]"
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.
 test_path_is_file () {
+       if ! test -f "$@"

Thanks. The new comment makes the intent of "$@" clear.

Quoted 9 lines

If you do re-roll for some reason, it might make sense to move the new comment (the one starting "The pattern of...") into the function itself just before the use of "$@". The reasons I suggest this are:
(1) the comment explains an implementation detail, thus is intended for people who might change this function, whereas all the text above the new paragraph is API documentation for callers of the function who need only the black-box description; (2) it places the comment closer to the relevant code, thus is less likely to be overlooked by someone changing the function.

I think moving it would be worse / wouldn't make sense.

The comment doesn't just apply to test_path_is_file(), but the next N functions that use the "$@" pattern. Just like the existing "debugging-friendly alternatives" comment does. I.e. test_path_is_file is just the "-f" part of thta comment, the following test_path_is_dir is the "-d" etc.

#158 lore raw

PATCH v2 3/3 test-lib-functions: remove last two parameter count assertions

In reply to Ævar Arnfjörð Bjarmason

Remove a couple of parameter count assertions where, unlike the preceding commit's migration to 'test -$x "$@"', we'll now silently do the "wrong" thing if given too many parameters. The benefit is less verbose trace output, as noted in the preceding commit.

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just use "$@" in that case anyway. There's only a few callers, and eventually those should probably be moved to use the test-tool directly.

That only leaves test_line_count, I suppose I could leave that one alone, but since it's the only common function left that does this assertion let's remove it for the brevity of the -x output and consistency with other functions.

---
 t/test-lib-functions.sh | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
t/test-lib-functions.sh +2 -6
index f8f5bf9de1..e128b341ff 100644
@@ -827,10 +827,7 @@ test_path_is_missing () {
 # output through when the number of lines is wrong.
 
 test_line_count () {
-	if test $# != 3
-	then
-		BUG "not 3 parameters to test_line_count"
-	elif ! test $(wc -l <"$3") "$1" "$2"
+	if ! test $(wc -l <"$3") "$1" "$2"
 	then
 		echo "test_line_count: line count for $3 !$1 $2"
 		cat "$3"
@@ -839,8 +836,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
Signature · 1 line
2.31.1.723.gf6bad1b9ba1
#159 lore raw

Re: [PATCH v2 3/3] test-lib-functions: remove last two parameter count assertions

Quoted 11 lines · Ævar Arnfjörð Bjarmason

Remove a couple of parameter count assertions where, unlike the preceding commit's migration to 'test -$x "$@"', we'll now silently do the "wrong" thing if given too many parameters. The benefit is less verbose trace output, as noted in the preceding commit.

In the case of "test_file_size", the "test-tool" we're invoking is happy to accept N parameters (it'll print out all N sizes). Let's just use "$@" in that case anyway. There's only a few callers, and eventually those should probably be moved to use the test-tool directly.

Quoted 4 lines

That only leaves test_line_count, I suppose I could leave that one alone, but since it's the only common function left that does this assertion let's remove it for the brevity of the -x output and consistency with other functions.

Unlike the previous step that did not entirely sacrifice the "test writers are human and they need our help in detecting their mistakes", this step feels like "shortening '-x' output is the most important thing in the world, and helping test writers is much less important."

IOW, I am not convinced it is a good change.

Quoted 31 lines
---
 t/test-lib-functions.sh | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
t/test-lib-functions.sh +2 -6
index f8f5bf9de1..e128b341ff 100644
@@ -827,10 +827,7 @@ test_path_is_missing () {
 # output through when the number of lines is wrong.
 
 test_line_count () {
-	if test $# != 3
-	then
-		BUG "not 3 parameters to test_line_count"
-	elif ! test $(wc -l <"$3") "$1" "$2"
+	if ! test $(wc -l <"$3") "$1" "$2"
 	then
 		echo "test_line_count: line count for $3 !$1 $2"
 		cat "$3"
@@ -839,8 +836,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
#160 lore raw

v2 cover test-lib-functions.sh: trickery to make -x less verbose

In reply to Ævar Arnfjörð Bjarmason

This series makes the "-x" output less verbose for our various helper functions, mainly using the trickery of feeding "$@" to "test" to rely on it to die when given too many arguments.

Since v2[1] I ejected the patch to make the "test_line_count" function unsafer per Junio's feedback.

The test_file_size has lost its "BUG" invocation, but is now safe as we do that check in the test helper, as shown in 5/5 we fixed one previous mild regression while doing that.

The 3-4/5 patches are just trivial "while we're at it" work.

1. http://lore.kernel.org/git/[email protected]

Ævar Arnfjörð Bjarmason (5):
test-lib-functions: normalize test_path_is_missing() debugging Revert and amend "test-lib-functions: assert correct parameter count" test helpers: remove unused test-tool path-utils slice-tests
test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"
test-helpers: split "file-size" into "file-size(s)"

 t/helper/test-path-utils.c  | 44 ++++++------------------
 t/t0060-path-utils.sh       |  2 +-
 t/t1050-large.sh            |  2 +-
 t/t5319-multi-pack-index.sh |  6 ++--
 t/test-lib-functions.sh     | 67 ++++++++++++++++---------------------
 5 files changed, 45 insertions(+), 76 deletions(-)

Range-diff against v1:

1:  fd33f6d71f0 = 1:  ba9d6251ad5 test-lib-functions: normalize test_path_is_missing() debugging
2:  67ddd821dfa = 2:  5f8e5ad27f5 Revert and amend "test-lib-functions: assert correct parameter count"
3:  8fd51861b59 < -:  ----------- test-lib-functions: remove last two parameter count assertions
-:  ----------- > 3:  44948c6ace9 test helpers: remove unused test-tool path-utils slice-tests
-:  ----------- > 4:  ae5dc5d03b3 test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"
-:  ----------- > 5:  947791d9535 test-helpers: split "file-size" into "file-size(s)"
Signature · 1 line
2.31.1.721.gbeb6a21927
#161 lore raw

PATCH v2 1/5 test-lib-functions: normalize test_path_is_missing() debugging

Change the test_path_is_missing() to be consistent with related functions. Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists. Let's just say that it exists instead.

---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index bd64a15c731..0232cc9f46d 100644
@@ -810,12 +810,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
Signature · 1 line
2.31.1.721.gbeb6a21927
#162 lore raw

PATCH v2 2/5 Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in my e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Now we'll get just:

+ test_cmp expect actual
+ eval diff -u "$@"
+ diff -u expect actual

This does not to the "right" thing in cases like:

test_path_is_file x -a y

Which will now turn into:

test -f x -a y

I consider that to be OK given the trade-off that any extra checking would produce more verbose trace output. As shown in 45a2686441b we had issues with these functions being invoked with multiple parameters (e.g. a glob) by accident, we don't need to be paranoid in guarding against hostile misuse from our own test suite.

While I'm at it change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

---
 t/test-lib-functions.sh | 59 +++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 31 deletions(-)
t/test-lib-functions.sh +28 -31
index 0232cc9f46d..f8f5bf9de1e 100644
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 }
 
 # debugging-friendly alternatives to "test [-f|-d|-e]"
-# The commands test the existence or non-existence of $1
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
-		false
+		echo "File $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
-		false
+		echo "Directory $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
-		false
+		echo "Path $* doesn't exist"
+		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$*' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -798,19 +800,17 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
-		false
+		echo "'$*' is not a non-empty file."
+		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
-		echo "Path $1 exists!"
+		echo "Path $* exists!"
 		false
 	fi
 }
@@ -1012,7 +1012,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1042,7 +1041,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1103,12 +1101,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$*' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.721.gbeb6a21927
#163 lore raw

PATCH v2 3/5 test helpers: remove unused test-tool path-utils slice-tests

In reply to Ævar Arnfjörð Bjarmason

This utility was last used in the code removed in 4c2c38e800f (ci:
modification of main.yml to use cmake for vs-build job, 2020-06-26). Let's also remove this now-dead code.

---
 t/helper/test-path-utils.c | 31 -------------------------------
 1 file changed, 31 deletions(-)
t/helper/test-path-utils.c +0 -31
index 313a153209c..3d7c0f1d19d 100644
@@ -177,14 +177,6 @@ static int is_dotgitmodules(const char *path)
 	return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path);
 }
 
-static int cmp_by_st_size(const void *a, const void *b)
-{
-	intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
-	intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
-
-	return x > y ? -1 : (x < y ? +1 : 0);
-}
-
 /*
  * A very simple, reproducible pseudo-random generator. Copied from
  * `test-genrandom.c`.
@@ -428,29 +420,6 @@ int cmd__path_utils(int argc, const char **argv)
 		return 0;
 	}
 
-	if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
-		int res = 0;
-		long offset, stride, i;
-		struct string_list list = STRING_LIST_INIT_NODUP;
-		struct stat st;
-
-		offset = strtol(argv[2], NULL, 10);
-		stride = strtol(argv[3], NULL, 10);
-		if (stride < 1)
-			stride = 1;
-		for (i = 4; i < argc; i++)
-			if (stat(argv[i], &st))
-				res = error_errno("Cannot stat '%s'", argv[i]);
-			else
-				string_list_append(&list, argv[i])->util =
-					(void *)(intptr_t)st.st_size;
-		QSORT(list.items, list.nr, cmp_by_st_size);
-		for (i = offset; i < list.nr; i+= stride)
-			printf("%s\n", list.items[i].string);
-
-		return !!res;
-	}
-
 	if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
 		return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
 
Signature · 1 line
2.31.1.721.gbeb6a21927
#164 lore raw

Re: [PATCH v2 3/5] test helpers: remove unused test-tool path-utils slice-tests

Quoted 3 lines · Ævar Arnfjörð Bjarmason

This utility was last used in the code removed in 4c2c38e800f (ci:
modification of main.yml to use cmake for vs-build job, 2020-06-26). Let's also remove this now-dead code.

This patch needs to be dropped, turns out it's used outside of t/ in ci/, didn't think to grep for test helpers there. Will hold off on a re-roll for a bit pending further feedback.

Junio, unless you'd like to have it sooner than later?

#165 lore raw

PATCH v2 4/5 test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"

In reply to Ævar Arnfjörð Bjarmason

Rename this utility added in d2c84dad1c8 (mingw: refuse to access paths with trailing spaces or periods, 2019-09-05) to make it clear that it expects N number of arguments, and for consistency with dc2d9ba3187 (is_{hfs,ntfs}_dotgitmodules: add tests, 2018-05-12).

Let's also make it a dashed "is-valid-paths" instead of "is_valid_path*" to make it easier to grep the helper v.s. the C function it calls.

---
 t/helper/test-path-utils.c | 2 +-
 t/t0060-path-utils.sh      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
t/helper/test-path-utils.c +1 -1
index 3d7c0f1d19d..af7d7fd5454 100644
@@ -423,7 +423,7 @@ int cmd__path_utils(int argc, const char **argv)
 	if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
 		return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
 
-	if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
+	if (argc > 1 && !strcmp(argv[1], "is-valid-paths")) {
 		int res = 0, expect = 1, i;
 
 		for (i = 2; i < argc; i++)
t/t0060-path-utils.sh +1 -1
index 0ff06b5d1b3..e7fb748b19c 100755
@@ -469,7 +469,7 @@ test_expect_success 'match .gitmodules' '
 '
 
 test_expect_success MINGW 'is_valid_path() on Windows' '
-	test-tool path-utils is_valid_path \
+	test-tool path-utils is-valid-paths \
 		win32 \
 		"win32 x" \
 		../hello.txt \
Signature · 1 line
2.31.1.721.gbeb6a21927
#166 lore raw

PATCH v2 5/5 test-helpers: split "file-size" into "file-size(s)"

In reply to Ævar Arnfjörð Bjarmason

Split the "file-size" helper into two helpers, one that takes 1 argument, and another one that takes N.

This allows us to remove the "BUG" check in test-lib-functions.sh for briefer -x output, and more importantly will clearly distinguish those cases where we expect one argument v.s. many.

The use in t1050-large.sh wants to check just one pack, we used to check that explicitly before 53b67a801bb (tests: consolidate the file_size function into test-lib-functions.sh, 2020-11-07), now we do so again.

---
 t/helper/test-path-utils.c  | 11 ++++++++++-
 t/t1050-large.sh            |  2 +-
 t/t5319-multi-pack-index.sh |  6 +++---
 t/test-lib-functions.sh     |  3 +--
 4 files changed, 15 insertions(+), 7 deletions(-)
t/helper/test-path-utils.c +10 -1
index af7d7fd5454..23970db8183 100644
@@ -272,6 +272,8 @@ static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
 
 int cmd__path_utils(int argc, const char **argv)
 {
+	int is_file_size, is_file_sizes;
+
 	if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
 		char *buf = xmallocz(strlen(argv[2]));
 		int rv = normalize_path_copy(buf, argv[2]);
@@ -387,10 +389,17 @@ int cmd__path_utils(int argc, const char **argv)
 		return !!res;
 	}
 
-	if (argc > 2 && !strcmp(argv[1], "file-size")) {
+	is_file_size = !strcmp(argv[1], "file-size");
+	is_file_sizes = !strcmp(argv[1], "file-sizes");
+	if (argc > 2 && (is_file_size || is_file_sizes)) {
 		int res = 0, i;
 		struct stat st;
 
+		if (is_file_size && argc > 3) {
+			res = error("too many arguments to is-file-size, use is-file-sizes?");
+			return res;
+		}
+
 		for (i = 2; i < argc; i++)
 			if (stat(argv[i], &st))
 				res = error_errno("Cannot stat '%s'", argv[i]);
t/t1050-large.sh +1 -1
index 4bab6a513c5..0b76ab0e3f9 100755
@@ -23,7 +23,7 @@ do
 	test_expect_success "add with $config" '
 		test_when_finished "rm -f .git/objects/pack/pack-*.* .git/index" &&
 		git $config add large1 &&
-		sz=$(test_file_size .git/objects/pack/pack-*.pack) &&
+		sz=$(test-tool path-utils file-size .git/objects/pack/pack-*.pack) &&
 		case "$expect" in
 		small) test "$sz" -le 100000 ;;
 		large) test "$sz" -ge 100000 ;;
t/t5319-multi-pack-index.sh +3 -3
index 5641d158dfc..baeda407b90 100755
@@ -648,7 +648,7 @@ test_expect_success 'repack with minimum size does not alter existing packs' '
 		test-tool chmtime =-3 .git/objects/pack/pack-B* &&
 		test-tool chmtime =-2 .git/objects/pack/pack-A* &&
 		ls .git/objects/pack >expect &&
-		MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
+		MINSIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | head -n 1) &&
 		git multi-pack-index repack --batch-size=$MINSIZE &&
 		ls .git/objects/pack >actual &&
 		test_cmp expect actual
@@ -672,7 +672,7 @@ test_expect_success 'repack respects repack.packKeptObjects=false' '
 		test_line_count = 5 idx-list &&
 		test-tool read-midx .git/objects | grep idx >midx-list &&
 		test_line_count = 5 midx-list &&
-		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
+		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | sed -n 3p) &&
 		BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
 		git multi-pack-index repack --batch-size=$BATCH_SIZE &&
 		ls .git/objects/pack/*idx >idx-list &&
@@ -687,7 +687,7 @@ test_expect_success 'repack creates a new pack' '
 		cd dup &&
 		ls .git/objects/pack/*idx >idx-list &&
 		test_line_count = 5 idx-list &&
-		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
+		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
 		BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
 		git multi-pack-index repack --batch-size=$BATCH_SIZE &&
 		ls .git/objects/pack/*idx >idx-list &&
t/test-lib-functions.sh +1 -2
index f8f5bf9de1e..20d1f94fd97 100644
@@ -839,8 +839,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
Signature · 1 line
2.31.1.721.gbeb6a21927
#167 lore raw

v3 cover test-lib-functions.sh: trickery to make -x less verbose

In reply to Ævar Arnfjörð Bjarmason

This series makes the "-x" output less verbose for our various helper functions, mainly using the trickery of feeding "$@" to "test" to rely on it to die when given too many arguments.

Since v2[1] I ejected the 3rd patch. I thought some test-lib code was removed, but was only grepping t/, turned out it was used in ci/.

https://lore.kernel.org/git/[email protected]

Ævar Arnfjörð Bjarmason (4):
test-lib-functions: normalize test_path_is_missing() debugging Revert and amend "test-lib-functions: assert correct parameter count"
test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"
test-helpers: split "file-size" into "file-size(s)"

 t/helper/test-path-utils.c  | 13 +++++--
 t/t0060-path-utils.sh       |  2 +-
 t/t1050-large.sh            |  2 +-
 t/t5319-multi-pack-index.sh |  6 ++--
 t/test-lib-functions.sh     | 67 ++++++++++++++++---------------------
 5 files changed, 45 insertions(+), 45 deletions(-)

Range-diff against v2:

1:  ba9d6251ad5 = 1:  26a3d552d88 test-lib-functions: normalize test_path_is_missing() debugging
2:  5f8e5ad27f5 = 2:  3a570321770 Revert and amend "test-lib-functions: assert correct parameter count"
3:  44948c6ace9 < -:  ----------- test helpers: remove unused test-tool path-utils slice-tests
4:  ae5dc5d03b3 = 3:  bf35091a112 test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"
5:  947791d9535 = 4:  ac0b920c041 test-helpers: split "file-size" into "file-size(s)"
Signature · 1 line
2.31.1.734.gdef39492517
#168 lore raw

PATCH v3 1/4 test-lib-functions: normalize test_path_is_missing() debugging

Change the test_path_is_missing() to be consistent with related functions. Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists. Let's just say that it exists instead.

---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index 213b1988aa4..2914b398470 100644
@@ -810,12 +810,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
Signature · 1 line
2.31.1.734.gdef39492517
#169 lore raw

Re: [PATCH v3 1/4] test-lib-functions: normalize test_path_is_missing() debugging

Quoted 2 lines · Ævar Arnfjörð Bjarmason

Change the test_path_is_missing() to be consistent with related functions.

Not very helpful to readers. It is totally unclear what kind of consistency is being sought by this patch.

Quoted 3 lines

Since 2caf20c52b7 (test-lib: user-friendly alternatives to test [-d|-f|-e], 2010-08-10) we've been ls -ld-ing the bad path and echo-ing $* if it exists.

Yes, and it does not explain what's wrong with that behaviour.

If there were something like "... but no invocation of the shell function passes $2 and subsequent arguments" (I didn't check if that is the case) after the above, it might have been more understandable, though.

Quoted 1 line

Let's just say that it exists instead.

As the earlier parts of the proposed log message are so weak, this conclusion does not sound convincing.

Quoted 23 lines
---
 t/test-lib-functions.sh | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
t/test-lib-functions.sh +1 -6
index 213b1988aa4..2914b398470 100644
@@ -810,12 +810,7 @@ test_path_is_missing () {
 	test "$#" -ne 1 && BUG "1 param"
 	if test -e "$1"
 	then
-		echo "Path exists:"
-		ls -ld "$1"
-		if test $# -ge 1
-		then
-			echo "$*"
-		fi
+		echo "Path $1 exists!"
 		false
 	fi
 }
#170 lore raw

PATCH v3 2/4 Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason

This reverts and amends my my own e7884b353b7 (test-lib-functions:
assert correct parameter count, 2021-02-12) in order to improve the -x output.

The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

That "test 2 -ne 2" line is new in my e7884b353b7. As noted in 45a2686441b (test-lib-functions: remove bug-inducing "diagnostics" helper param, 2021-02-12) we had buggy invocations of some of these functions with too many parameters.

Now we'll get just:

+ test_cmp expect actual
+ eval diff -u "$@"
+ diff -u expect actual

This does not to the "right" thing in cases like:

test_path_is_file x -a y

Which will now turn into:

test -f x -a y

I consider that to be OK given the trade-off that any extra checking would produce more verbose trace output. As shown in 45a2686441b we had issues with these functions being invoked with multiple parameters (e.g. a glob) by accident, we don't need to be paranoid in guarding against hostile misuse from our own test suite.

While I'm at it change a few functions that relied on a "false" being the last statement in the function to use an explicit "return 1" like the other functions in this file.

---
 t/test-lib-functions.sh | 59 +++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 31 deletions(-)
t/test-lib-functions.sh +28 -31
index 2914b398470..0adb9fd124d 100644
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 }
 
 # debugging-friendly alternatives to "test [-f|-d|-e]"
-# The commands test the existence or non-existence of $1
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"
 	then
-		echo "File $1 doesn't exist"
-		false
+		echo "File $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_is_dir () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -d "$1"
+	if ! test -d "$@"
 	then
-		echo "Directory $1 doesn't exist"
-		false
+		echo "Directory $* doesn't exist"
+		return 1
 	fi
 }
 
 test_path_exists () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -e "$1"
+	if ! test -e "$@"
 	then
-		echo "Path $1 doesn't exist"
-		false
+		echo "Path $* doesn't exist"
+		return 1
 	fi
 }
 
 # Check if the directory exists and is empty as expected, barf otherwise.
 test_dir_is_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_dir "$1" &&
-	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	test_path_is_dir "$@" &&
+	if test -n "$(ls -a1 "$@" | egrep -v '^\.\.?$')"
 	then
-		echo "Directory '$1' is not empty, it contains:"
+		echo "Directory '$*' is not empty, it contains:"
 		ls -la "$1"
 		return 1
 	fi
@@ -798,19 +800,17 @@ test_dir_is_empty () {
 
 # Check if the file exists and has a size greater than zero
 test_file_not_empty () {
-	test "$#" = 2 && BUG "2 param"
-	if ! test -s "$1"
+	if ! test -s "$@"
 	then
-		echo "'$1' is not a non-empty file."
-		false
+		echo "'$*' is not a non-empty file."
+		return 1
 	fi
 }
 
 test_path_is_missing () {
-	test "$#" -ne 1 && BUG "1 param"
-	if test -e "$1"
+	if test -e "$@"
 	then
-		echo "Path $1 exists!"
+		echo "Path $* exists!"
 		false
 	fi
 }
@@ -1012,7 +1012,6 @@ test_expect_code () {
 # - not all diff versions understand "-u"
 
 test_cmp () {
-	test "$#" -ne 2 && BUG "2 param"
 	eval "$GIT_TEST_CMP" '"$@"'
 }
 
@@ -1042,7 +1041,6 @@ test_cmp_config () {
 # test_cmp_bin - helper to compare binary files
 
 test_cmp_bin () {
-	test "$#" -ne 2 && BUG "2 param"
 	cmp "$@"
 }
 
@@ -1096,12 +1094,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
-	test "$#" -ne 1 && BUG "1 param"
-	test_path_is_file "$1" &&
-	if test -s "$1"
+	test_path_is_file "$@" &&
+	if test -s "$@"
 	then
-		echo "'$1' is not empty, it contains:"
-		cat "$1"
+		echo "'$*' is not empty, it contains:"
+		cat "$@"
 		return 1
 	fi
 }
Signature · 1 line
2.31.1.734.gdef39492517
#171 lore raw

Re: [PATCH v3 2/4] Revert and amend "test-lib-functions: assert correct parameter count"

Quoted 6 lines · Ævar Arnfjörð Bjarmason

The goal here is to get rid of the verbosity of having e.g. a "test 2 -ne 2" line for every "test_cmp". We use "$@" as an argument to "test" to intentionally feed the "test" operator too many arguments if the functions are called with too many arguments, thus piggy-backing on it to check the number of arguments we get.

Quoted 6 lines

Before this for each test_cmp invocation we'd emit:

+ test_cmp expect actual
+ test 2 -ne 2
+ eval diff -u "$@"
+ diff -u expect actual

Quite honestly, I do not see what's bad about that and am not all that sympathetic to the cause behind this series.

The developer is asking to debug the test script by passing '-x', and see where the test script is broken. Especially since ...

Quoted 5 lines

Now we'll get just:

+ test_cmp expect actual
+ eval diff -u "$@"
+ diff -u expect actual

... we are not removing the 'eval diff -u "$@"' line, I do not think how a few extra trace output lines coming from sanity check hurts all that much. It is worth paying for to have such basic sanity checks expressed in a more natural way (than "oh, this hack would not work when 'one -a two' is used as a path"), I would think.

Thanks.

#172 lore raw

RE: [PATCH v3 2/4] Revert and amend "test-lib-functions: assert correct parameter count"

In reply to Ævar Arnfjörð Bjarmason
Quoted 6 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +0 -0
index 2914b398470..0adb9fd124d 100644
@@ -756,41 +756,43 @@ test_external_without_stderr () {
 }

t/test-lib-functions.sh test_external_without_stderr () { · line 765

Quoted 10 lines
t/test-lib-functions.sh +7 -1
 
 # debugging-friendly alternatives to "test [-f|-d|-e]"
-# The commands test the existence or non-existence of $1
+# The commands test the existence or non-existence of
+# a given argument.
+#
+# The pattern of using "$@" to "test" instead of "$1" is not a bug. We
+# are counting on "test" to error on too many arguments if more than
+# one is given. Checking "$#" explicitly would lead to overly verbose
+# -x output.

I would consider adding an extra line. In my mind a comment without a line is for the next function, one with a line is for the section.

  # function comment
  function_1 ()

Versus:

  # section comment

function_1 ()
function_2 ()

But I agree with the other comments that it's better inside the function. It's an implementation detail.

While I think git tries to follow the self-documenting code style, sometimes it's better to just write the comment.

t/test-lib-functions.sh test_external_without_stderr () { · line 767

Quoted 4 lines
t/test-lib-functions.sh +1 -2
 test_path_is_file () {
-	test "$#" -ne 1 && BUG "1 param"
-	if ! test -f "$1"
+	if ! test -f "$@"

if ! test -f "$@" # Rely on test to ensure 1 param

t/test-lib-functions.sh test_external_without_stderr () { · line 772

Quoted 7 lines
t/test-lib-functions.sh +2 -2
 	then
-		echo "File $1 doesn't exist"
-		false
+		echo "File $* doesn't exist"
+		return 1
 	fi
 }

And yes, I would add that comment to every instance where this trick is used. That way people reading the second or third function don't have to scratch their heads, or miraculously read a comment far above.

Cheers.

Signature · 1 line
Felipe Contreras
#173 lore raw

PATCH v3 3/4 test-helpers: rename "path-utils is_valid_path" to "is-valid-paths"

In reply to Ævar Arnfjörð Bjarmason

Rename this utility added in d2c84dad1c8 (mingw: refuse to access paths with trailing spaces or periods, 2019-09-05) to make it clear that it expects N number of arguments, and for consistency with dc2d9ba3187 (is_{hfs,ntfs}_dotgitmodules: add tests, 2018-05-12).

Let's also make it a dashed "is-valid-paths" instead of "is_valid_path*" to make it easier to grep the helper v.s. the C function it calls.

---
 t/helper/test-path-utils.c | 2 +-
 t/t0060-path-utils.sh      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
t/helper/test-path-utils.c +1 -1
index 313a153209c..eed3068ffb7 100644
@@ -454,7 +454,7 @@ int cmd__path_utils(int argc, const char **argv)
 	if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
 		return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
 
-	if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
+	if (argc > 1 && !strcmp(argv[1], "is-valid-paths")) {
 		int res = 0, expect = 1, i;
 
 		for (i = 2; i < argc; i++)
t/t0060-path-utils.sh +1 -1
index 0ff06b5d1b3..e7fb748b19c 100755
@@ -469,7 +469,7 @@ test_expect_success 'match .gitmodules' '
 '
 
 test_expect_success MINGW 'is_valid_path() on Windows' '
-	test-tool path-utils is_valid_path \
+	test-tool path-utils is-valid-paths \
 		win32 \
 		"win32 x" \
 		../hello.txt \
Signature · 1 line
2.31.1.734.gdef39492517
#174 lore raw

PATCH v3 4/4 test-helpers: split "file-size" into "file-size(s)"

In reply to Ævar Arnfjörð Bjarmason

Split the "file-size" helper into two helpers, one that takes 1 argument, and another one that takes N.

This allows us to remove the "BUG" check in test-lib-functions.sh for briefer -x output, and more importantly will clearly distinguish those cases where we expect one argument v.s. many.

The use in t1050-large.sh wants to check just one pack, we used to check that explicitly before 53b67a801bb (tests: consolidate the file_size function into test-lib-functions.sh, 2020-11-07), now we do so again.

---
 t/helper/test-path-utils.c  | 11 ++++++++++-
 t/t1050-large.sh            |  2 +-
 t/t5319-multi-pack-index.sh |  6 +++---
 t/test-lib-functions.sh     |  3 +--
 4 files changed, 15 insertions(+), 7 deletions(-)
t/helper/test-path-utils.c +10 -1
index eed3068ffb7..17645da4a1c 100644
@@ -280,6 +280,8 @@ static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
 
 int cmd__path_utils(int argc, const char **argv)
 {
+	int is_file_size, is_file_sizes;
+
 	if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
 		char *buf = xmallocz(strlen(argv[2]));
 		int rv = normalize_path_copy(buf, argv[2]);
@@ -395,10 +397,17 @@ int cmd__path_utils(int argc, const char **argv)
 		return !!res;
 	}
 
-	if (argc > 2 && !strcmp(argv[1], "file-size")) {
+	is_file_size = !strcmp(argv[1], "file-size");
+	is_file_sizes = !strcmp(argv[1], "file-sizes");
+	if (argc > 2 && (is_file_size || is_file_sizes)) {
 		int res = 0, i;
 		struct stat st;
 
+		if (is_file_size && argc > 3) {
+			res = error("too many arguments to is-file-size, use is-file-sizes?");
+			return res;
+		}
+
 		for (i = 2; i < argc; i++)
 			if (stat(argv[i], &st))
 				res = error_errno("Cannot stat '%s'", argv[i]);
t/t1050-large.sh +1 -1
index 4bab6a513c5..0b76ab0e3f9 100755
@@ -23,7 +23,7 @@ do
 	test_expect_success "add with $config" '
 		test_when_finished "rm -f .git/objects/pack/pack-*.* .git/index" &&
 		git $config add large1 &&
-		sz=$(test_file_size .git/objects/pack/pack-*.pack) &&
+		sz=$(test-tool path-utils file-size .git/objects/pack/pack-*.pack) &&
 		case "$expect" in
 		small) test "$sz" -le 100000 ;;
 		large) test "$sz" -ge 100000 ;;
t/t5319-multi-pack-index.sh +3 -3
index 5641d158dfc..baeda407b90 100755
@@ -648,7 +648,7 @@ test_expect_success 'repack with minimum size does not alter existing packs' '
 		test-tool chmtime =-3 .git/objects/pack/pack-B* &&
 		test-tool chmtime =-2 .git/objects/pack/pack-A* &&
 		ls .git/objects/pack >expect &&
-		MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
+		MINSIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | head -n 1) &&
 		git multi-pack-index repack --batch-size=$MINSIZE &&
 		ls .git/objects/pack >actual &&
 		test_cmp expect actual
@@ -672,7 +672,7 @@ test_expect_success 'repack respects repack.packKeptObjects=false' '
 		test_line_count = 5 idx-list &&
 		test-tool read-midx .git/objects | grep idx >midx-list &&
 		test_line_count = 5 midx-list &&
-		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
+		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | sed -n 3p) &&
 		BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
 		git multi-pack-index repack --batch-size=$BATCH_SIZE &&
 		ls .git/objects/pack/*idx >idx-list &&
@@ -687,7 +687,7 @@ test_expect_success 'repack creates a new pack' '
 		cd dup &&
 		ls .git/objects/pack/*idx >idx-list &&
 		test_line_count = 5 idx-list &&
-		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
+		THIRD_SMALLEST_SIZE=$(test-tool path-utils file-sizes .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
 		BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
 		git multi-pack-index repack --batch-size=$BATCH_SIZE &&
 		ls .git/objects/pack/*idx >idx-list &&
t/test-lib-functions.sh +1 -2
index 0adb9fd124d..602d77dee37 100644
@@ -839,8 +839,7 @@ test_line_count () {
 }
 
 test_file_size () {
-	test "$#" -ne 1 && BUG "1 param"
-	test-tool path-utils file-size "$1"
+	test-tool path-utils file-size "$@"
 }
 
 # Returns success if a comma separated string of keywords ($1) contains a
Signature · 1 line
2.31.1.734.gdef39492517