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 <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 13, 2022 at 15:32 UTC e47363e5a8bdf5144059d664c45c0975243ef05b
2 files changed +36 -1
setup.c
+2 -1
@@ -1053,7 +1053,8 @@ static int ensure_valid_ownership(const char *path)
1053 {
1054 struct safe_directory_data data = { .path = path };
1055
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))
1058 return 1;
1059
1060 read_very_early_config(safe_directory_cb, &data);
t/t0033-safe-directory.sh new
+34
@@ -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