Repository navigation
Expand file tree
/
Copy pathsafedir_test.go
More file actions
70 lines (64 loc) · 2.25 KB
/
Copy pathsafedir_test.go
File metadata and controls
70 lines (64 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gitconfig
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSafeDirectory tests git's safe.directory matching, which Discover
// applies to a repository the user does not own. The directories exist: a
// listed path that cannot be resolved is skipped. Values are written with
// '/', as a backslash is an escape in a configuration file.
func TestSafeDirectory(t *testing.T) {
root, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)
for _, d := range []string{"a/b", "ab"} {
require.NoError(t, os.MkdirAll(filepath.Join(root, d), 0o755))
}
s := filepath.ToSlash(root)
a := filepath.Join(root, "a")
check := func(t *testing.T, config, dir string, want bool) {
t.Helper()
f, err := Parse("global", []byte("[safe]\n"+config))
require.NoError(t, err)
assert.Equal(t, want, safeDirectory(f.Config(), dir, ""))
}
for _, tc := range []struct {
name string
config string
dir string
want bool
}{
{"not listed", "", a, false},
{"listed", "directory = " + s + "/a\n", a, true},
{"other directory listed", "directory = " + s + "/a\n", filepath.Join(a, "b"), false},
{"no prefix match", "directory = " + s + "/a\n", filepath.Join(root, "ab"), false},
{"star", "directory = *\n", a, true},
{"trailing star", "directory = " + s + "/*\n", filepath.Join(a, "b"), true},
{"trailing star, the directory itself", "directory = " + s + "/*\n", root, false},
{"empty value resets", "directory = *\ndirectory =\n", a, false},
{"listed after a reset", "directory =\ndirectory = " + s + "/a\n", a, true},
} {
t.Run(tc.name, func(t *testing.T) { check(t, tc.config, tc.dir, tc.want) })
}
t.Run("relative directory", func(t *testing.T) {
// Checked against git 2.56: "../*" trusts nothing, as "../repo"
// does not. A relative dir is taken from the working directory.
t.Chdir(a)
for _, tc := range []struct {
entry string
dir string
want bool
}{
{s + "/a", ".", true},
{s + "/a/b", "b", true},
{s + "/ab", "../ab", true},
{s + "/*", "../ab", true},
{"../*", "../ab", false},
{"../ab", "../ab", false},
} {
t.Run(tc.entry+" "+tc.dir, func(t *testing.T) { check(t, "directory = "+tc.entry+"\n", tc.dir, tc.want) })
}
})
}