Skip to content

Commit e47363e

Browse files
derrickstoleegitster
authored andcommitted
t0033: add tests for safe.directory
It is difficult to change the ownership on a directory in our test suite, so insert a new GIT_TEST_ASSUME_DIFFERENT_OWNER environment variable to trick Git into thinking we are in a differently-owned directory. This allows us to test that the config is parsed correctly. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb95038 commit e47363e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

setup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,8 @@ static int ensure_valid_ownership(const char *path)
10531053
{
10541054
struct safe_directory_data data = { .path = path };
10551055

1056-
if (is_path_owned_by_current_user(path))
1056+
if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
1057+
is_path_owned_by_current_user(path))
10571058
return 1;
10581059

10591060
read_very_early_config(safe_directory_cb, &data);

t/t0033-safe-directory.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
test_description='verify safe.directory checks'
4+
5+
. ./test-lib.sh
6+
7+
GIT_TEST_ASSUME_DIFFERENT_OWNER=1
8+
export GIT_TEST_ASSUME_DIFFERENT_OWNER
9+
10+
expect_rejected_dir () {
11+
test_must_fail git status 2>err &&
12+
grep "safe.directory" err
13+
}
14+
15+
test_expect_success 'safe.directory is not set' '
16+
expect_rejected_dir
17+
'
18+
19+
test_expect_success 'safe.directory does not match' '
20+
git config --global safe.directory bogus &&
21+
expect_rejected_dir
22+
'
23+
24+
test_expect_success 'safe.directory matches' '
25+
git config --global --add safe.directory "$(pwd)" &&
26+
git status
27+
'
28+
29+
test_expect_success 'safe.directory matches, but is reset' '
30+
git config --global --add safe.directory "" &&
31+
expect_rejected_dir
32+
'
33+
34+
test_done

0 commit comments

Comments
 (0)