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

Function TestOpenFileRejectsTrailingSeparator

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

Source from the content-addressed store, hash-verified

254}
255
256func TestOpenFileRejectsTrailingSeparator(t *testing.T) {
257 tests := []struct {
258 name string
259 path func(root string) string
260 }{
261 {
262 name: "relative path",
263 path: func(string) string {
264 return "output" + string(os.PathSeparator)
265 },
266 },
267 {
268 name: "absolute path",
269 path: func(root string) string {
270 return filepath.Join(root, "output") + string(os.PathSeparator)
271 },
272 },
273 }
274
275 for _, tt := range tests {
276 t.Run(tt.name, func(t *testing.T) {
277 root := t.TempDir()
278 t.Chdir(root)
279 path := tt.path(root)
280
281 file, err := safepaths.OpenFile(path, 0o644, 0o755, false)
282 if file != nil {
283 _ = file.Close()
284 }
285 require.ErrorContains(t, err, "ends in a separator")
286 _, statErr := os.Lstat(filepath.Join(root, "output"))
287 require.True(t, os.IsNotExist(statErr))
288 assert.NoFileExists(t, filepath.Join(root, "output", "output"))
289 })
290 }
291}
292
293func TestOpenFileAllowsTrustedParentSymlinks(t *testing.T) {
294 tests := []struct {

Callers

nothing calls this directly

Calls 5

OpenFileFunction · 0.92
JoinMethod · 0.80
pathMethod · 0.80
RunMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected