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

Function TestResolveWorktreeTargetPathSafety

pkg/cmd/pr/shared/worktree_test.go:108–172  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

106}
107
108func TestResolveWorktreeTargetPathSafety(t *testing.T) {
109 base := t.TempDir()
110
111 existingDir := filepath.Join(base, "dir")
112 require.NoError(t, os.Mkdir(existingDir, 0o755))
113
114 nonEmptyDir := filepath.Join(base, "non-empty-dir")
115 require.NoError(t, os.Mkdir(nonEmptyDir, 0o755))
116 require.NoError(t, os.WriteFile(filepath.Join(nonEmptyDir, "file"), []byte("x"), 0o644))
117
118 regularFile := filepath.Join(base, "file")
119 require.NoError(t, os.WriteFile(regularFile, []byte("x"), 0o644))
120
121 symlink := filepath.Join(base, "link")
122 require.NoError(t, os.Symlink(existingDir, symlink))
123
124 tests := []struct {
125 name string
126 path string
127 wantErr string
128 }{
129 {
130 name: "non-existent path is allowed",
131 path: filepath.Join(base, "does-not-exist"),
132 },
133 {
134 name: "existing directory is allowed",
135 path: existingDir,
136 },
137 {
138 name: "existing non-empty directory is rejected",
139 path: nonEmptyDir,
140 wantErr: "--worktree path must be empty",
141 },
142 {
143 name: "leaf symlink is rejected",
144 path: symlink,
145 wantErr: "--worktree path must not be a symlink",
146 },
147 {
148 name: "existing non-directory is rejected",
149 path: regularFile,
150 wantErr: "--worktree path must be a directory",
151 },
152 }
153
154 for _, tt := range tests {
155 t.Run(tt.name, func(t *testing.T) {
156 cs, teardown := run.Stub()
157 defer teardown(t)
158 if tt.wantErr == "" || tt.path == nonEmptyDir {
159 cs.Register(`git rev-parse --path-format=absolute --show-toplevel --git-common-dir`, 128, "")
160 }
161
162 client := &git.Client{GitPath: "/some/path/git"}
163 _, err := ResolveWorktreeTarget(client, tt.path)
164 if tt.wantErr == "" {
165 require.NoError(t, err)

Callers

nothing calls this directly

Calls 7

StubFunction · 0.92
ResolveWorktreeTargetFunction · 0.85
JoinMethod · 0.80
ContainsMethod · 0.80
RunMethod · 0.65
RegisterMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected