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

[PATCH 04/21] completion tests: consolidate getting path of current working directory

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Feb 25, 2016, 22:50 UTC
Message-ID
<1456440650-32623-5-git-send-email-szeder@ira.uka.de>
In-Reply-To
<1456440650-32623-1-git-send-email-szeder@ira.uka.de>

Some tests of the __gitdir() helper function use the $TRASH_DIRECTORY variable in direct path comparisons. In general this should be avoided, because it might contain symbolic links. There happens to be no issues with this here, however, because those tests use $TRASH_DIRECTORY both for specifying the expected result and for specifying input which in turn is just 'echo'ed verbatim.

Other __gitdir() tests ask for the path of the trash directory by running $(pwd -P) in each test, sometimes even twice in a single test.

Run $(pwd) only once at the beginning of the test script to store the path of the trash directory in a variable, and use that variable in all __gitdir() tests.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 t/t9902-completion.sh | 46 ++++++++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 24 deletions(-)
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 9f591c361fab..447f57b89291 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -124,15 +124,22 @@ invalid_variable_name='${foo.bar}'
 
 actual="$TRASH_DIRECTORY/actual"
 
+if test_have_prereq MINGW
+then
+	ROOT="$(pwd -W)"
+else
+	ROOT="$(pwd)"
+fi
+
 test_expect_success 'setup for __gitdir tests' '
 	mkdir -p subdir/subsubdir &&
 	git init otherrepo
 '
 
 test_expect_success '__gitdir - from command line (through $__git_dir)' '
-	echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+	echo "$ROOT/otherrepo/.git" >expected &&
 	(
-		__git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
+		__git_dir="$ROOT/otherrepo/.git" &&
 		__gitdir >"$actual"
 	) &&
 	test_cmp expected "$actual"
@@ -157,7 +164,7 @@ test_expect_success '__gitdir - .git directory in cwd' '
 '
 
 test_expect_success '__gitdir - .git directory in parent' '
-	echo "$(pwd -P)/.git" >expected &&
+	echo "$ROOT/.git" >expected &&
 	(
 		cd subdir/subsubdir &&
 		__gitdir >"$actual"
@@ -175,7 +182,7 @@ test_expect_success '__gitdir - cwd is a .git directory' '
 '
 
 test_expect_success '__gitdir - parent is a .git directory' '
-	echo "$(pwd -P)/.git" >expected &&
+	echo "$ROOT/.git" >expected &&
 	(
 		cd .git/refs/heads &&
 		__gitdir >"$actual"
@@ -184,9 +191,9 @@ test_expect_success '__gitdir - parent is a .git directory' '
 '
 
 test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
-	echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+	echo "$ROOT/otherrepo/.git" >expected &&
 	(
-		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		GIT_DIR="$ROOT/otherrepo/.git" &&
 		export GIT_DIR &&
 		__gitdir >"$actual"
 	) &&
@@ -194,9 +201,9 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
 '
 
 test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
-	echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
+	echo "$ROOT/otherrepo/.git" >expected &&
 	(
-		GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
+		GIT_DIR="$ROOT/otherrepo/.git" &&
 		export GIT_DIR &&
 		cd subdir &&
 		__gitdir >"$actual"
@@ -206,24 +213,15 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
 
 test_expect_success '__gitdir - non-existing $GIT_DIR' '
 	(
-		GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
+		GIT_DIR="$ROOT/non-existing" &&
 		export GIT_DIR &&
 		test_must_fail __gitdir
 	)
 '
 
-function pwd_P_W () {
-	if test_have_prereq MINGW
-	then
-		pwd -W
-	else
-		pwd -P
-	fi
-}
-
 test_expect_success '__gitdir - gitfile in cwd' '
-	echo "$(pwd_P_W)/otherrepo/.git" >expected &&
-	echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
+	echo "$ROOT/otherrepo/.git" >expected &&
+	echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
 	test_when_finished "rm -f subdir/.git" &&
 	(
 		cd subdir &&
@@ -233,8 +231,8 @@ test_expect_success '__gitdir - gitfile in cwd' '
 '
 
 test_expect_success '__gitdir - gitfile in parent' '
-	echo "$(pwd_P_W)/otherrepo/.git" >expected &&
-	echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
+	echo "$ROOT/otherrepo/.git" >expected &&
+	echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
 	test_when_finished "rm -f subdir/.git" &&
 	(
 		cd subdir/subsubdir &&
@@ -244,7 +242,7 @@ test_expect_success '__gitdir - gitfile in parent' '
 '
 
 test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
-	echo "$(pwd -P)/otherrepo/.git" >expected &&
+	echo "$ROOT/otherrepo/.git" >expected &&
 	mkdir otherrepo/dir &&
 	test_when_finished "rm -rf otherrepo/dir" &&
 	ln -s otherrepo/dir link &&
@@ -259,7 +257,7 @@ test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
 test_expect_success '__gitdir - not a git repository' '
 	(
 		cd subdir/subsubdir &&
-		GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
+		GIT_CEILING_DIRECTORIES="$ROOT" &&
 		export GIT_CEILING_DIRECTORIES &&
 		test_must_fail __gitdir
 	)
-- 
2.7.2.410.g92cb358
Previous: SZEDER GáborNext: SZEDER Gábor
Message 5 of 25 in “completion: __gitdir()-related improvements”
  1. 00/21 completion: __gitdir()-related improvementsSZEDER Gábor, Feb 25, 2016
  2. 01/21 completion: improve __git_refs()'s in-code documentationSZEDER Gábor, Feb 25, 2016
  3. 02/21 completion tests: don't add test cruft to the test repositorySZEDER Gábor, Feb 25, 2016
  4. 03/21 completion tests: make the $cur variable local to the test helper functionsSZEDER Gábor, Feb 25, 2016
  5. 04/21 completion tests: consolidate getting path of current working directorySZEDER Gábor, Feb 25, 2016
  6. 05/21 completion tests: check __gitdir()'s output in the error casesSZEDER Gábor, Feb 25, 2016
  7. 06/21 completion tests: add tests for the __git_refs() helper functionSZEDER Gábor, Feb 25, 2016
  8. 07/21 completion: ensure that the repository path given on the command line existsSZEDER Gábor, Feb 25, 2016
  9. 08/21 completion: fix most spots not respecting 'git --git-dir=<path>'SZEDER Gábor, Feb 25, 2016
  10. 09/21 completion: respect 'git --git-dir=<path>' when listing remote refsSZEDER Gábor, Feb 25, 2016
  11. 10/21 completion: list refs from remote when remote's name matches a directorySZEDER Gábor, Feb 25, 2016
  12. 11/21 completion: don't list 'HEAD' when trying refs completion outside of a repoSZEDER Gábor, Feb 25, 2016
  13. 12/21 completion: list short refs from a remote given as a URLSZEDER Gábor, Feb 25, 2016
  14. 13/21 rev-parse: add '--absolute-git-dir' optionSZEDER Gábor, Feb 25, 2016
  15. Mike RappazzoApr 26, 2016
  16. Jeff KingApr 26, 2016
  17. SZEDER GáborMay 18, 2016
  18. 14/21 completion: don't offer commands when 'git --opt' needs an argumentSZEDER Gábor, Feb 25, 2016
  19. 15/21 completion: fix completion after 'git -C <path>'SZEDER Gábor, Feb 25, 2016
  20. 16/21 completion: respect 'git -C <path>'SZEDER Gábor, Feb 25, 2016
  21. 17/21 completion: don't use __gitdir() for git commandsSZEDER Gábor, Feb 25, 2016
  22. 18/21 completion: consolidate silencing errors from git commandsSZEDER Gábor, Feb 25, 2016
  23. 19/21 completion: don't guard git executions with __gitdir()SZEDER Gábor, Feb 25, 2016
  24. 20/21 completion: extract repository discovery from __gitdir()SZEDER Gábor, Feb 25, 2016
  25. 21/21 completion: cache the path to the repositorySZEDER Gábor, Feb 25, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.