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

Function TestFileCreationModes

internal/safepaths/root_mode_unix_test.go:17–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestFileCreationModes(t *testing.T) {
18 tests := []struct {
19 name string
20 filePerm os.FileMode
21 dirPerm os.FileMode
22 umask int
23 replace bool
24 wantFilePerm os.FileMode
25 wantDirPerm os.FileMode
26 }{
27 {
28 name: "separate file and directory modes",
29 filePerm: 0o600,
30 dirPerm: 0o700,
31 wantFilePerm: 0o600,
32 wantDirPerm: 0o700,
33 },
34 {
35 name: "creation respects umask",
36 filePerm: 0o666,
37 dirPerm: 0o777,
38 umask: 0o027,
39 wantFilePerm: 0o640,
40 wantDirPerm: 0o750,
41 },
42 {
43 name: "replacement preserves existing modes",
44 filePerm: 0o666,
45 dirPerm: 0o777,
46 replace: true,
47 wantFilePerm: 0o640,
48 wantDirPerm: 0o750,
49 },
50 }
51
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 // Umask is process-wide, so these tests must stay sequential.
55 oldUmask := syscall.Umask(tt.umask)
56 t.Cleanup(func() { syscall.Umask(oldUmask) })
57
58 for _, operation := range []string{"OpenFile", "WriteFile", "Root.Create", "Root.WriteFile", "Root.CopyFile"} {
59 t.Run(operation, func(t *testing.T) {
60 rootDir := t.TempDir()
61 name := filepath.Join("one", "two", "file.txt")
62 path := filepath.Join(rootDir, name)
63 if tt.replace {
64 require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o750))
65 require.NoError(t, os.WriteFile(path, []byte("old"), 0o640))
66 }
67
68 root, err := safepaths.OpenRoot(rootDir)
69 require.NoError(t, err)
70 t.Cleanup(func() { require.NoError(t, root.Close()) })
71
72 var file *os.File
73 switch operation {
74 case "OpenFile":

Callers

nothing calls this directly

Calls 12

OpenRootFunction · 0.92
OpenFileFunction · 0.92
WriteFileFunction · 0.92
JoinMethod · 0.80
MkdirAllMethod · 0.80
WriteFileMethod · 0.80
CopyFileMethod · 0.80
EqualMethod · 0.80
ModeMethod · 0.80
RunMethod · 0.65
CloseMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected