MCPcopy Create free account
hub / github.com/cli/cli / TestRootCreate

Function TestRootCreate

internal/safepaths/root_test.go:14–203  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestRootCreate(t *testing.T) {
15 var preservedMode os.FileMode
16
17 tests := []struct {
18 name string
19 path string
20 replace bool
21 setup func(t *testing.T, root, outside string)
22 wantErr string
23 verify func(t *testing.T, root, outside string)
24 }{
25 {
26 name: "creates nested file",
27 path: "nested/file.txt",
28 verify: func(t *testing.T, root, _ string) {
29 t.Helper()
30 content, err := os.ReadFile(filepath.Join(root, "nested", "file.txt"))
31 require.NoError(t, err)
32 assert.Equal(t, "new", string(content))
33 },
34 },
35 {
36 name: "rejects root directory as file",
37 path: ".",
38 wantErr: "root directory",
39 },
40 {
41 name: "rejects lexical escape",
42 path: "../file.txt",
43 wantErr: "not a local child path",
44 },
45 {
46 name: "rejects trailing separator",
47 path: "nested" + string(os.PathSeparator),
48 wantErr: "ends in a separator",
49 verify: func(t *testing.T, root, _ string) {
50 t.Helper()
51 _, err := os.Lstat(filepath.Join(root, "nested"))
52 require.True(t, os.IsNotExist(err))
53 assert.NoFileExists(t, filepath.Join(root, "nested", "nested"))
54 },
55 },
56 {
57 name: "rejects ancestor symlink",
58 path: "nested/file.txt",
59 setup: func(t *testing.T, root, outside string) {
60 t.Helper()
61 require.NoError(t, os.Symlink(outside, filepath.Join(root, "nested")))
62 },
63 wantErr: "symbolic link",
64 verify: func(t *testing.T, _, outside string) {
65 t.Helper()
66 assert.NoFileExists(t, filepath.Join(outside, "file.txt"))
67 },
68 },
69 {
70 name: "no clobber rejects dangling final symlink",
71 path: "file.txt",

Callers

nothing calls this directly

Calls 10

OpenRootFunction · 0.92
JoinMethod · 0.80
EqualMethod · 0.80
WriteFileMethod · 0.80
ModeMethod · 0.80
setupMethod · 0.80
HelperMethod · 0.65
RunMethod · 0.65
CloseMethod · 0.65
verifyMethod · 0.65

Tested by

no test coverage detected