threads / patch / 53194

patchmingw: make is_hidden tests in t0001/t5611 more robust

Subject: [PATCH] mingw: make is_hidden tests in t0001/t5611 more robust

## tl;dr

15 messages between Apr 8, 2020 and Apr 11, 2020. Diffs are folded; open one to read it.

replies: 14people: 2as markdown or json

Johannes Schindelin via GitGitGadget· Apr 8, 2020, 19:34 UTC · lore
From: Johannes Schindelin <johannes.schindelin@gmx.de>

We should not actually expect the first `attrib.exe` in the PATH to be the one we are looking for. Or that it is in the PATH, for that matter.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
    Make the tests that test core.hideDotFiles more robust
    
    We have this feature on Windows where the files starting with a dot can
    be marked hidden (whether a file is hidden by default or not is a matter
    of naming convention on Unix, but it is an explicit flag on Windows).
    This patch improves the regression tests of this feature, and it has
    been carried in Git for Windows for over three years.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-603%2Fdscho%2Frobustify-is-hidden-tests-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-603/dscho/robustify-is-hidden-tests-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/603
 t/t0001-init.sh         | 2 +-
 t/t5611-clone-config.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

t/t0001-init.sh, t/t5611-clone-config.sh

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 26f82063267..2456688b281 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -395,7 +395,7 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 # Tests for the hidden file attribute on windows
 is_hidden () {
 	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 	return 1
 }
 
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
index 60c1ba951b7..87b8073cd74 100755
--- a/t/t5611-clone-config.sh
+++ b/t/t5611-clone-config.sh
@@ -95,7 +95,7 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
 # Tests for the hidden file attribute on windows
 is_hidden () {
 	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 	return 1
 }
 

base-commit: 9fadedd637b312089337d73c3ed8447e9f0aa775
-- 
gitgitgadget
Junio C Hamano· Apr 8, 2020, 21:59 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH] mingw: make is_hidden tests in t0001/t5611 more robust

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 12 quoted lines
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index 26f82063267..2456688b281 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -395,7 +395,7 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
>  # Tests for the hidden file attribute on windows
>  is_hidden () {
>  	# Use the output of `attrib`, ignore the absolute path
> -	case "$(attrib "$1")" in *H*?:*) return 0;; esac
> +	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
>  	return 1
>  }

I wondered if this (and the other one) want to be in test-lib but I am on the fence. All three tests that call this helper in t0001 are protected with MINGW prerequisite, but until I realized it, the call to "attrib" (whether it is given as a full path or relies on $PATH lookup) looked like a portability nightmare waiting to happen. It would make it even worse if we moved the above as-is to test-lib, as it is harder to see what the callers are doing once we did so.

With a change like this, however
	is_hidden () {
		if ! test_have_prereq MINGW
		then
			BUG "use of is_hidden outside MINGW prerequisite"
		fi
		case "$("$SYSTEMROOT"/system32/attrib "$1")" in 
		*H*?:*)	return 0 ;;
		*)	return 1 ;;
		esac
	}
I think it is OK to consolidate these two copies into one in test-lib
Thanks.
Show 15 quoted lines
> diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
> index 60c1ba951b7..87b8073cd74 100755
> --- a/t/t5611-clone-config.sh
> +++ b/t/t5611-clone-config.sh
> @@ -95,7 +95,7 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
>  # Tests for the hidden file attribute on windows
>  is_hidden () {
>  	# Use the output of `attrib`, ignore the absolute path
> -	case "$(attrib "$1")" in *H*?:*) return 0;; esac
> +	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
>  	return 1
>  }
>  
>
> base-commit: 9fadedd637b312089337d73c3ed8447e9f0aa775
Junio C Hamano· Apr 9, 2020, 20:11 UTC · re: Junio C Hamano · lore

[PATCH 0/2] make "is_hidden" even more robust

Here are two patches on top of "mingw: make is_hidden tests in t0001/t5611 more robust" from yesterday to clean up the code by consolidating two copies and make it harder to misuse.

Junio C Hamano (2):
  mingw: refactor test_path_is_hidden out to t/test-lib-functions.sh
  t: protect against use of test_path_is_hidden outside MINGW
 t/t0001-init.sh         | 13 +++----------
 t/t5611-clone-config.sh | 13 +++----------
 t/test-lib-functions.sh | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 20 deletions(-)
-- 
2.26.0-106-g9fadedd637
Junio C Hamano· Apr 9, 2020, 20:11 UTC · re: Junio C Hamano · lore

[PATCH 1/2] mingw: refactor test_path_is_hidden out to t/test-lib-functions.sh

Two scripts had a copy of the same helper function, which needed the same fix at the same time. Let's move it to a common place.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t0001-init.sh         | 13 +++----------
 t/t5611-clone-config.sh | 13 +++----------
 t/test-lib-functions.sh |  9 +++++++++
 3 files changed, 15 insertions(+), 20 deletions(-)
Show changes to 3 files +15 −20

t/t0001-init.sh, t/t5611-clone-config.sh, t/test-lib-functions.sh

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 2456688b28..6b2e2e3dc2 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -392,13 +392,6 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 	test_path_is_dir realgitdir/refs
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW '.git hidden' '
 	rm -rf newdir &&
 	(
@@ -406,7 +399,7 @@ test_expect_success MINGW '.git hidden' '
 		mkdir newdir &&
 		cd newdir &&
 		git init &&
-		is_hidden .git
+		test_path_is_hidden .git
 	) &&
 	check_config newdir/.git false unset
 '
@@ -419,7 +412,7 @@ test_expect_success MINGW 'bare git dir not hidden' '
 		cd newdir &&
 		git --bare init
 	) &&
-	! is_hidden newdir
+	! test_path_is_hidden newdir
 '
 
 test_expect_success 'remote init from does not use config from cwd' '
@@ -456,7 +449,7 @@ test_expect_success MINGW 'core.hidedotfiles = false' '
 		sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
 		git -C newdir init
 	) &&
-	! is_hidden newdir/.git
+	! test_path_is_hidden newdir/.git
 '
 
 test_expect_success MINGW 'redirect std handles' '
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
index 87b8073cd7..8e0fd39823 100755
--- a/t/t5611-clone-config.sh
+++ b/t/t5611-clone-config.sh
@@ -92,24 +92,17 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
 	test_cmp expect actual
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW 'clone -c core.hideDotFiles' '
 	test_commit attributes .gitattributes "" &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=false . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=dotGitOnly . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=true . child &&
-	is_hidden child/.gitattributes
+	test_path_is_hidden child/.gitattributes
 '
 
 test_done
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 352c213d52..39b478e731 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -760,6 +760,15 @@ test_path_is_missing () {
 	fi
 }
 
+# Tests for the hidden file attribute on windows
+test_path_is_hidden () {
+	# Use the output of `attrib`, ignore the absolute path
+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in
+	*H*?:*)		return 0;;
+	esac
+	return 1
+}
+
 # test_line_count checks that a file has the number of lines it
 # ought to. For example:
 #
-- 
2.26.0-106-g9fadedd637
Junio C Hamano· Apr 9, 2020, 20:11 UTC · re: Junio C Hamano · lore

[PATCH 2/2] t: protect against use of test_path_is_hidden outside MINGW

This test helper function is meant to test if the "hidden" atttribute is set (or unset) as expected on Windows, and use of it outside MINGW prerequisite is an error. Ensure we have the prereq and trigger a BUG otherwise.

It is tempting to instead replace its implementation with something like

	if test_have_prereq MINGW
	then
		... current Windows specific code ...
	else
		# ls without -a/-A hides paths that begin with a dot
		case "$(basename "$1")" in
		.*) return 0 ;;
		esac
	fi
	return 1

but one test in t0001 is designed specifically that with an option "repository/.git" of a newly created repository is *not* hidden on Windows, which means that it is impossible to make that feature work on POSIX systems and the above won't test what we want to test.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/test-lib-functions.sh | 5 +++++
 1 file changed, 5 insertions(+)
Show changes to t/test-lib-functions.sh +5 −0
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 39b478e731..ad54863166 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -762,6 +762,11 @@ test_path_is_missing () {
 
 # Tests for the hidden file attribute on windows
 test_path_is_hidden () {
+	if ! test_have_prereq MINGW
+	then
+		BUG "use of test_path_is_hidden without MINGW prerequisite"
+	fi
+
 	# Use the output of `attrib`, ignore the absolute path
 	case "$("$SYSTEMROOT"/system32/attrib "$1")" in
 	*H*?:*)		return 0;;
-- 
2.26.0-106-g9fadedd637
Johannes Schindelin via GitGitGadget· Apr 10, 2020, 11:03 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 0/3] Make the tests that test core.hideDotFiles more robust

We have this feature on Windows where the files starting with a dot can be marked hidden (whether a file is hidden by default or not is a matter of naming convention on Unix, but it is an explicit flag on Windows). This patch improves the regression tests of this feature, and it has been carried in Git for Windows for over three years.

Junio, I'm sorry that I did not get to send v2 yesterday, and that you spent time on the two add-on patches even after I finalized this second iteration (but I was first waiting for the PR build to pass and while that happened, I got stuck in meeting after meeting).

Changes since v1:
 * A preparatory patch now moves and renames is_hidden to 
   test-lib-functions.sh
 * A typo in the function's comment is fixed, while at it.
Johannes Schindelin (3):
  t: consolidate the `is_hidden` functions
  mingw: make test_path_is_hidden tests in t0001/t5611 more robust
  t: fix casing of the operating system `Windows`
 t/t0001-init.sh         |  9 +--------
 t/t5611-clone-config.sh | 13 +++----------
 t/test-lib-functions.sh | 10 ++++++++++
 3 files changed, 14 insertions(+), 18 deletions(-)
base-commit: 9fadedd637b312089337d73c3ed8447e9f0aa775
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-603%2Fdscho%2Frobustify-is-hidden-tests-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-603/dscho/robustify-is-hidden-tests-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/603
Range-diff vs v1:
 -:  ----------- > 1:  dd81ea68d6b t: consolidate the `is_hidden` functions
 1:  889f7c21333 ! 2:  fc4202cb548 mingw: make is_hidden tests in t0001/t5611 more robust
     @@ Metadata
      Author: Johannes Schindelin <Johannes.Schindelin@gmx.de>
      
       ## Commit message ##
     -    mingw: make is_hidden tests in t0001/t5611 more robust
     +    mingw: make test_path_is_hidden tests in t0001/t5611 more robust
     +
     +    This function uses Windows' system tool `attrib` to determine the state
     +    of the hidden flag of a file or directory.
      
          We should not actually expect the first `attrib.exe` in the PATH to
          be the one we are looking for. Or that it is in the PATH, for that
          matter.
      
     +    Let's use the full path to the tool instead.
     +
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
     - ## t/t0001-init.sh ##
     -@@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '
     - # Tests for the hidden file attribute on windows
     - is_hidden () {
     - 	# Use the output of `attrib`, ignore the absolute path
     --	case "$(attrib "$1")" in *H*?:*) return 0;; esac
     -+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
     - 	return 1
     - }
     + ## t/test-lib-functions.sh ##
     +@@ t/test-lib-functions.sh: test_path_is_hidden () {
     + 	BUG "test_path_is_hidden can only be used on Windows"
       
     -
     - ## t/t5611-clone-config.sh ##
     -@@ t/t5611-clone-config.sh: test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
     - # Tests for the hidden file attribute on windows
     - is_hidden () {
       	# Use the output of `attrib`, ignore the absolute path
      -	case "$(attrib "$1")" in *H*?:*) return 0;; esac
      +	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
       	return 1
       }
     - 
 -:  ----------- > 3:  d0c0767a2aa t: fix casing of the operating system `Windows`
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 10, 2020, 11:03 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 2/3] mingw: make test_path_is_hidden tests in t0001/t5611 more robust

From: Johannes Schindelin <johannes.schindelin@gmx.de>

This function uses Windows' system tool `attrib` to determine the state of the hidden flag of a file or directory.

We should not actually expect the first `attrib.exe` in the PATH to be the one we are looking for. Or that it is in the PATH, for that matter.

Let's use the full path to the tool instead.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/test-lib-functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to t/test-lib-functions.sh +1 −1
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 09a2479fd38..09e2cebb456 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1550,6 +1550,6 @@ test_path_is_hidden () {
 	BUG "test_path_is_hidden can only be used on Windows"
 
 	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 	return 1
 }
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 10, 2020, 11:03 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 3/3] t: fix casing of the operating system `Windows`

From: Johannes Schindelin <johannes.schindelin@gmx.de>
The lower-case `windows` refers to something different.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/test-lib-functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to t/test-lib-functions.sh +1 −1
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 09e2cebb456..139647a6341 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1544,7 +1544,7 @@ test_bitmap_traversal () {
 	rm -f "$1.normalized" "$2.normalized"
 }
 
-# Tests for the hidden file attribute on windows
+# Tests for the hidden file attribute on Windows
 test_path_is_hidden () {
 	test_have_prereq MINGW ||
 	BUG "test_path_is_hidden can only be used on Windows"
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 10, 2020, 11:03 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 1/3] t: consolidate the `is_hidden` functions

From: Johannes Schindelin <johannes.schindelin@gmx.de>

The `is_hidden` function can be used (only on Windows) to determine whether a directory or file have their `hidden` flag set.

This function is duplicated between two test scripts. It is better to move it into `test-lib-functions.sh` so that it is reused.

To make it safer to use, we specifically test for the `MINGW` prereq now, so that it is not used on a non-Windows platform by mistake.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t0001-init.sh         |  9 +--------
 t/t5611-clone-config.sh | 13 +++----------
 t/test-lib-functions.sh | 10 ++++++++++
 3 files changed, 14 insertions(+), 18 deletions(-)
Show changes to 3 files +14 −18

t/t0001-init.sh, t/t5611-clone-config.sh, t/test-lib-functions.sh

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 26f82063267..1edd5aeb8f0 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -392,13 +392,6 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 	test_path_is_dir realgitdir/refs
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW '.git hidden' '
 	rm -rf newdir &&
 	(
@@ -406,7 +399,7 @@ test_expect_success MINGW '.git hidden' '
 		mkdir newdir &&
 		cd newdir &&
 		git init &&
-		is_hidden .git
+		test_path_is_hidden .git
 	) &&
 	check_config newdir/.git false unset
 '
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
index 60c1ba951b7..8e0fd398236 100755
--- a/t/t5611-clone-config.sh
+++ b/t/t5611-clone-config.sh
@@ -92,24 +92,17 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
 	test_cmp expect actual
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW 'clone -c core.hideDotFiles' '
 	test_commit attributes .gitattributes "" &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=false . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=dotGitOnly . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=true . child &&
-	is_hidden child/.gitattributes
+	test_path_is_hidden child/.gitattributes
 '
 
 test_done
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 352c213d52e..09a2479fd38 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1543,3 +1543,13 @@ test_bitmap_traversal () {
 	test_cmp "$1.normalized" "$2.normalized" &&
 	rm -f "$1.normalized" "$2.normalized"
 }
+
+# Tests for the hidden file attribute on windows
+test_path_is_hidden () {
+	test_have_prereq MINGW ||
+	BUG "test_path_is_hidden can only be used on Windows"
+
+	# Use the output of `attrib`, ignore the absolute path
+	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	return 1
+}
-- 
gitgitgadget
Junio C Hamano· Apr 10, 2020, 11:14 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH v2 0/3] Make the tests that test core.hideDotFiles more robust

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Junio, I'm sorry that I did not get to send v2 yesterday, and that you spent
> time on the two add-on patches even after I finalized this second iteration
> (but I was first waiting for the PR build to pass and while that happened, I
> got stuck in meeting after meeting).

Heh, mails cross all the time, so there is not much to be sorry, and it is not a race, and it's not much of an issue who sent an equivalent patch to the list first ;-)

A few issues I noticed that are not worth pointing out inline with the patches are

 [1/3] The "consolidate" is a bit uneven.  It keeps the 'windows'
       (lowercase) to pretend to be a pure code movement, while
       adding the "prereq or die" that makes it an impure code
       movement.  If I were doing this, this step would be pure code
       movement, and [3/3] would have the "prereq or die" as its
       main theme, i.e. make sure it is hard to misuse.  [3/3] would
       also do "s/windows/Windows/" as "while at it".
 [2/3] The test numbers on the title is no longer relevant, and I
       would suggest retitling this step.  No matter which test uses
       it currently, or any new uses added in the future to other
       tests, with this patch the helper is more robust, and it was
       the point of [1/3].
 
Thanks.
Johannes Schindelin via GitGitGadget· Apr 11, 2020, 13:40 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 0/3] Make the tests that test core.hideDotFiles more robust

We have this feature on Windows where the files starting with a dot can be marked hidden (whether a file is hidden by default or not is a matter of naming convention on Unix, but it is an explicit flag on Windows). This patch improves the regression tests of this feature, and it has been carried in Git for Windows for over three years.

Junio, I followed your advice, and did one more thing: the function is now renamed also only in 3/3.

Changes since v2:
 * The first patch is now a true code move, no edits are snuck in.
 * The typo fix, the function rename, and the prereq check are now all done
   as a "cleanup" patch (the third one).
Changes since v1:
 * A preparatory patch now moves and renames is_hidden to 
   test-lib-functions.sh
 * A typo in the function's comment is fixed, while at it.
Johannes Schindelin (3):
  t: consolidate the `is_hidden` functions
  mingw: make test_path_is_hidden more robust
  t: restrict `is_hidden` to be called only on Windows
 t/t0001-init.sh         |  9 +--------
 t/t5611-clone-config.sh | 13 +++----------
 t/test-lib-functions.sh | 10 ++++++++++
 3 files changed, 14 insertions(+), 18 deletions(-)
base-commit: 9fadedd637b312089337d73c3ed8447e9f0aa775
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-603%2Fdscho%2Frobustify-is-hidden-tests-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-603/dscho/robustify-is-hidden-tests-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/603
Range-diff vs v2:
 1:  dd81ea68d6b ! 1:  f13f9f78cda t: consolidate the `is_hidden` functions
     @@ Commit message
          This function is duplicated between two test scripts. It is better to
          move it into `test-lib-functions.sh` so that it is reused.
      
     -    To make it safer to use, we specifically test for the `MINGW` prereq
     -    now, so that it is not used on a non-Windows platform by mistake.
     +    This patch is best viewed with `--color-moved`.
      
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
     @@ t/t0001-init.sh: test_expect_success SYMLINKS 're-init to move gitdir symlink' '
       test_expect_success MINGW '.git hidden' '
       	rm -rf newdir &&
       	(
     -@@ t/t0001-init.sh: test_expect_success MINGW '.git hidden' '
     - 		mkdir newdir &&
     - 		cd newdir &&
     - 		git init &&
     --		is_hidden .git
     -+		test_path_is_hidden .git
     - 	) &&
     - 	check_config newdir/.git false unset
     - '
      
       ## t/t5611-clone-config.sh ##
      @@ t/t5611-clone-config.sh: test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
     @@ t/t5611-clone-config.sh: test_expect_success 'clone -c remote.<remote>.fetch=<re
       test_expect_success MINGW 'clone -c core.hideDotFiles' '
       	test_commit attributes .gitattributes "" &&
       	rm -rf child &&
     - 	git clone -c core.hideDotFiles=false . child &&
     --	! is_hidden child/.gitattributes &&
     -+	! test_path_is_hidden child/.gitattributes &&
     - 	rm -rf child &&
     - 	git clone -c core.hideDotFiles=dotGitOnly . child &&
     --	! is_hidden child/.gitattributes &&
     -+	! test_path_is_hidden child/.gitattributes &&
     - 	rm -rf child &&
     - 	git clone -c core.hideDotFiles=true . child &&
     --	is_hidden child/.gitattributes
     -+	test_path_is_hidden child/.gitattributes
     - '
     - 
     - test_done
      
       ## t/test-lib-functions.sh ##
      @@ t/test-lib-functions.sh: test_bitmap_traversal () {
     @@ t/test-lib-functions.sh: test_bitmap_traversal () {
       }
      +
      +# Tests for the hidden file attribute on windows
     -+test_path_is_hidden () {
     -+	test_have_prereq MINGW ||
     -+	BUG "test_path_is_hidden can only be used on Windows"
     -+
     ++is_hidden () {
      +	# Use the output of `attrib`, ignore the absolute path
      +	case "$(attrib "$1")" in *H*?:*) return 0;; esac
      +	return 1
 2:  fc4202cb548 ! 2:  8650936b8d4 mingw: make test_path_is_hidden tests in t0001/t5611 more robust
     @@ Metadata
      Author: Johannes Schindelin <Johannes.Schindelin@gmx.de>
      
       ## Commit message ##
     -    mingw: make test_path_is_hidden tests in t0001/t5611 more robust
     +    mingw: make test_path_is_hidden more robust
      
          This function uses Windows' system tool `attrib` to determine the state
          of the hidden flag of a file or directory.
     @@ Commit message
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
       ## t/test-lib-functions.sh ##
     -@@ t/test-lib-functions.sh: test_path_is_hidden () {
     - 	BUG "test_path_is_hidden can only be used on Windows"
     - 
     +@@ t/test-lib-functions.sh: test_bitmap_traversal () {
     + # Tests for the hidden file attribute on windows
     + is_hidden () {
       	# Use the output of `attrib`, ignore the absolute path
      -	case "$(attrib "$1")" in *H*?:*) return 0;; esac
      +	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 3:  d0c0767a2aa < -:  ----------- t: fix casing of the operating system `Windows`
 -:  ----------- > 3:  b2604459917 t: restrict `is_hidden` to be called only on Windows
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 11, 2020, 13:40 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 2/3] mingw: make test_path_is_hidden more robust

From: Johannes Schindelin <johannes.schindelin@gmx.de>

This function uses Windows' system tool `attrib` to determine the state of the hidden flag of a file or directory.

We should not actually expect the first `attrib.exe` in the PATH to be the one we are looking for. Or that it is in the PATH, for that matter.

Let's use the full path to the tool instead.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/test-lib-functions.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to t/test-lib-functions.sh +1 −1
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 7a7e7a38312..2f81463a240 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1547,6 +1547,6 @@ test_bitmap_traversal () {
 # Tests for the hidden file attribute on windows
 is_hidden () {
 	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 	return 1
 }
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 11, 2020, 13:40 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 3/3] t: restrict `is_hidden` to be called only on Windows

From: Johannes Schindelin <johannes.schindelin@gmx.de>

The function won't work anywhere else, so let's mark it as an explicit bug if it is called on a non-Windows platform.

Let's also rename the function to avoid cluttering the global namespace with an overly-generic function name.

While at it, we also fix the code comment above that function: the lower-case `windows` refers to something different than `Windows`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t0001-init.sh         | 2 +-
 t/t5611-clone-config.sh | 6 +++---
 t/test-lib-functions.sh | 7 +++++--
 3 files changed, 9 insertions(+), 6 deletions(-)
Show changes to 3 files +9 −6

t/t0001-init.sh, t/t5611-clone-config.sh, t/test-lib-functions.sh

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 9cc919d8d1a..1edd5aeb8f0 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -399,7 +399,7 @@ test_expect_success MINGW '.git hidden' '
 		mkdir newdir &&
 		cd newdir &&
 		git init &&
-		is_hidden .git
+		test_path_is_hidden .git
 	) &&
 	check_config newdir/.git false unset
 '
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
index c861e12ea44..8e0fd398236 100755
--- a/t/t5611-clone-config.sh
+++ b/t/t5611-clone-config.sh
@@ -96,13 +96,13 @@ test_expect_success MINGW 'clone -c core.hideDotFiles' '
 	test_commit attributes .gitattributes "" &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=false . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=dotGitOnly . child &&
-	! is_hidden child/.gitattributes &&
+	! test_path_is_hidden child/.gitattributes &&
 	rm -rf child &&
 	git clone -c core.hideDotFiles=true . child &&
-	is_hidden child/.gitattributes
+	test_path_is_hidden child/.gitattributes
 '
 
 test_done
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 2f81463a240..139647a6341 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1544,8 +1544,11 @@ test_bitmap_traversal () {
 	rm -f "$1.normalized" "$2.normalized"
 }
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
+# Tests for the hidden file attribute on Windows
+test_path_is_hidden () {
+	test_have_prereq MINGW ||
+	BUG "test_path_is_hidden can only be used on Windows"
+
 	# Use the output of `attrib`, ignore the absolute path
 	case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
 	return 1
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Apr 11, 2020, 13:40 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v3 1/3] t: consolidate the `is_hidden` functions

From: Johannes Schindelin <johannes.schindelin@gmx.de>

The `is_hidden` function can be used (only on Windows) to determine whether a directory or file have their `hidden` flag set.

This function is duplicated between two test scripts. It is better to move it into `test-lib-functions.sh` so that it is reused.

This patch is best viewed with `--color-moved`.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t0001-init.sh         | 7 -------
 t/t5611-clone-config.sh | 7 -------
 t/test-lib-functions.sh | 7 +++++++
 3 files changed, 7 insertions(+), 14 deletions(-)
Show changes to 3 files +7 −14

t/t0001-init.sh, t/t5611-clone-config.sh, t/test-lib-functions.sh

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 26f82063267..9cc919d8d1a 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -392,13 +392,6 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 	test_path_is_dir realgitdir/refs
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW '.git hidden' '
 	rm -rf newdir &&
 	(
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
index 60c1ba951b7..c861e12ea44 100755
--- a/t/t5611-clone-config.sh
+++ b/t/t5611-clone-config.sh
@@ -92,13 +92,6 @@ test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
 	test_cmp expect actual
 '
 
-# Tests for the hidden file attribute on windows
-is_hidden () {
-	# Use the output of `attrib`, ignore the absolute path
-	case "$(attrib "$1")" in *H*?:*) return 0;; esac
-	return 1
-}
-
 test_expect_success MINGW 'clone -c core.hideDotFiles' '
 	test_commit attributes .gitattributes "" &&
 	rm -rf child &&
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 352c213d52e..7a7e7a38312 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1543,3 +1543,10 @@ test_bitmap_traversal () {
 	test_cmp "$1.normalized" "$2.normalized" &&
 	rm -f "$1.normalized" "$2.normalized"
 }
+
+# Tests for the hidden file attribute on windows
+is_hidden () {
+	# Use the output of `attrib`, ignore the absolute path
+	case "$(attrib "$1")" in *H*?:*) return 0;; esac
+	return 1
+}
-- 
gitgitgadget
Junio C Hamano· Apr 11, 2020, 21:27 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH v3 0/3] Make the tests that test core.hideDotFiles more robust

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 8 quoted lines
> We have this feature on Windows where the files starting with a dot can be
> marked hidden (whether a file is hidden by default or not is a matter of
> naming convention on Unix, but it is an explicit flag on Windows). This
> patch improves the regression tests of this feature, and it has been carried
> in Git for Windows for over three years.
>
> Junio, I followed your advice, and did one more thing: the function is now
> renamed also only in 3/3.
Looks good.  

I would have renamed it when making it global, exactly for the reason you stated in [3/3], but it's not like we would merge only the first two without the fixup, so it does not matter in practice (as long as other people won't pick up a bad habit from the example, that is).

Thanks.  Let's start merging a lot of things down to 'next'.

← back to recent threads