Skip to content

Commit 7e31453

Browse files
pyokagangitster
authored andcommitted
t0302: test credential-store support for XDG_CONFIG_HOME
t0302 now tests git-credential-store's support for the XDG user-specific configuration file $XDG_CONFIG_HOME/git/credentials. Specifically: * Ensure that the XDG file is strictly opt-in. It should not be created by git at all times if it does not exist. * Conversely, if the XDG file exists, ~/.git-credentials should not be created at all times. * If both the XDG file and ~/.git-credentials exists, then both files should be used for credential lookups. However, credentials should only be written to ~/.git-credentials. * Credentials must be erased from both files. * $XDG_CONFIG_HOME can be a custom directory set by the user as per the XDG base directory specification. Test that git-credential-store respects that, but defaults to "~/.config/git/credentials" if it does not exist or is empty. Helped-by: Matthieu Moy <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Paul Tan <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 44b2289 commit 7e31453

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

t/t0302-credential-store.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,118 @@ test_description='credential-store tests'
66

77
helper_test store
88

9+
test_expect_success 'when xdg file does not exist, xdg file not created' '
10+
test_path_is_missing "$HOME/.config/git/credentials" &&
11+
test -s "$HOME/.git-credentials"
12+
'
13+
14+
test_expect_success 'setup xdg file' '
15+
rm -f "$HOME/.git-credentials" &&
16+
mkdir -p "$HOME/.config/git" &&
17+
>"$HOME/.config/git/credentials"
18+
'
19+
20+
helper_test store
21+
22+
test_expect_success 'when xdg file exists, home file not created' '
23+
test -s "$HOME/.config/git/credentials" &&
24+
test_path_is_missing "$HOME/.git-credentials"
25+
'
26+
27+
test_expect_success 'setup custom xdg file' '
28+
rm -f "$HOME/.git-credentials" &&
29+
rm -f "$HOME/.config/git/credentials" &&
30+
mkdir -p "$HOME/xdg/git" &&
31+
>"$HOME/xdg/git/credentials"
32+
'
33+
34+
XDG_CONFIG_HOME="$HOME/xdg"
35+
export XDG_CONFIG_HOME
36+
helper_test store
37+
unset XDG_CONFIG_HOME
38+
39+
test_expect_success 'if custom xdg file exists, home and xdg files not created' '
40+
test_when_finished "rm -f $HOME/xdg/git/credentials" &&
41+
test -s "$HOME/xdg/git/credentials" &&
42+
test_path_is_missing "$HOME/.git-credentials" &&
43+
test_path_is_missing "$HOME/.config/git/credentials"
44+
'
45+
46+
test_expect_success 'get: use home file if both home and xdg files have matches' '
47+
echo "https://home-user:[email protected]" >"$HOME/.git-credentials" &&
48+
mkdir -p "$HOME/.config/git" &&
49+
echo "https://xdg-user:[email protected]" >"$HOME/.config/git/credentials" &&
50+
check fill store <<-\EOF
51+
protocol=https
52+
host=example.com
53+
--
54+
protocol=https
55+
host=example.com
56+
username=home-user
57+
password=home-pass
58+
--
59+
EOF
60+
'
61+
62+
test_expect_success 'get: use xdg file if home file has no matches' '
63+
>"$HOME/.git-credentials" &&
64+
mkdir -p "$HOME/.config/git" &&
65+
echo "https://xdg-user:[email protected]" >"$HOME/.config/git/credentials" &&
66+
check fill store <<-\EOF
67+
protocol=https
68+
host=example.com
69+
--
70+
protocol=https
71+
host=example.com
72+
username=xdg-user
73+
password=xdg-pass
74+
--
75+
EOF
76+
'
77+
78+
test_expect_success 'get: use xdg file if home file is unreadable' '
79+
echo "https://home-user:[email protected]" >"$HOME/.git-credentials" &&
80+
chmod -r "$HOME/.git-credentials" &&
81+
mkdir -p "$HOME/.config/git" &&
82+
echo "https://xdg-user:[email protected]" >"$HOME/.config/git/credentials" &&
83+
check fill store <<-\EOF
84+
protocol=https
85+
host=example.com
86+
--
87+
protocol=https
88+
host=example.com
89+
username=xdg-user
90+
password=xdg-pass
91+
--
92+
EOF
93+
'
94+
95+
test_expect_success 'store: if both xdg and home files exist, only store in home file' '
96+
>"$HOME/.git-credentials" &&
97+
mkdir -p "$HOME/.config/git" &&
98+
>"$HOME/.config/git/credentials" &&
99+
check approve store <<-\EOF &&
100+
protocol=https
101+
host=example.com
102+
username=store-user
103+
password=store-pass
104+
EOF
105+
echo "https://store-user:[email protected]" >expected &&
106+
test_cmp expected "$HOME/.git-credentials" &&
107+
test_must_be_empty "$HOME/.config/git/credentials"
108+
'
109+
110+
111+
test_expect_success 'erase: erase matching credentials from both xdg and home files' '
112+
echo "https://home-user:[email protected]" >"$HOME/.git-credentials" &&
113+
mkdir -p "$HOME/.config/git" &&
114+
echo "https://xdg-user:[email protected]" >"$HOME/.config/git/credentials" &&
115+
check reject store <<-\EOF &&
116+
protocol=https
117+
host=example.com
118+
EOF
119+
test_must_be_empty "$HOME/.git-credentials" &&
120+
test_must_be_empty "$HOME/.config/git/credentials"
121+
'
122+
9123
test_done

0 commit comments

Comments
 (0)