Skip to content

Commit f0d4d39

Browse files
avargitster
authored andcommitted
test-lib: split up and deprecate test_create_repo()
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 eea4206 (t0000: catch trivial pilot errors., 2005-12-10) and 2ccd202 (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 0d314ce (test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30). But since 2006f0a (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 3af4c71 (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 c09a69a (Disable hooks during tests., 2005-10-16), since then hooks became disabled by default in f98f8cb (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 eedf8f9 (Abstract test_create_repo out for use in tests., 2006-02-17), and later "cd"'d inside a subshell since 0d314ce 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 8683a45 (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. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 97c8aac commit f0d4d39

File tree

5 files changed

+4
-18
lines changed

5 files changed

+4
-18
lines changed

t/t5406-remote-rejects.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ test_description='remote push rejects are reported by client'
55
. ./test-lib.sh
66

77
test_expect_success 'setup' '
8-
mkdir .git/hooks &&
98
write_script .git/hooks/update <<-\EOF &&
109
exit 1
1110
EOF

t/t5407-post-rewrite-hook.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ test_expect_success 'setup' '
2020
git checkout main
2121
'
2222

23-
mkdir .git/hooks
24-
2523
cat >.git/hooks/post-rewrite <<EOF
2624
#!/bin/sh
2725
echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args

t/t5409-colorize-remote-messages.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ test_description='remote messages are colorized on the client'
55
. ./test-lib.sh
66

77
test_expect_success 'setup' '
8-
mkdir .git/hooks &&
98
write_script .git/hooks/update <<-\EOF &&
109
echo error: error
1110
echo ERROR: also highlighted

t/test-lib-functions.sh

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,21 +1250,10 @@ test_atexit () {
12501250
} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
12511251
}
12521252

1253-
# Most tests can use the created repository, but some may need to create more.
1253+
# Deprecated wrapper for "git init", use "git init" directly instead
12541254
# Usage: test_create_repo <directory>
12551255
test_create_repo () {
1256-
test "$#" = 1 ||
1257-
BUG "not 1 parameter to test-create-repo"
1258-
repo="$1"
1259-
mkdir -p "$repo"
1260-
(
1261-
cd "$repo" || error "Cannot setup test environment"
1262-
"${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" \
1263-
init \
1264-
"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
1265-
error "cannot run git init -- have you built things yet?"
1266-
mv .git/hooks .git/hooks-disabled
1267-
) || exit
1256+
git init "$@"
12681257
}
12691258

12701259
# This function helps on symlink challenged file systems when it is not

t/test-lib.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ rm -fr "$TRASH_DIRECTORY" || {
13631363
remove_trash=t
13641364
if test -z "$TEST_NO_CREATE_REPO"
13651365
then
1366-
test_create_repo "$TRASH_DIRECTORY"
1366+
git init "$TRASH_DIRECTORY" >&3 2>&4 ||
1367+
error "cannot run git init"
13671368
else
13681369
mkdir -p "$TRASH_DIRECTORY"
13691370
fi

0 commit comments

Comments
 (0)