-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
111 lines (94 loc) · 2.33 KB
/
Copy pathcontext_test.go
File metadata and controls
111 lines (94 loc) · 2.33 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//go:build unix
package fs_test
import (
"context"
"fmt"
"log"
"testing"
"lesiw.io/fs"
"lesiw.io/fs/osfs"
)
func TestContextModesIndependent(t *testing.T) {
ctx := t.Context()
// Set both modes
ctx = fs.WithDirMode(ctx, 0700)
ctx = fs.WithFileMode(ctx, 0600)
// Verify both are preserved independently
dirMode := fs.DirMode(ctx)
fileMode := fs.FileMode(ctx)
if dirMode != 0700 {
t.Errorf("DirMode(ctx) = %04o, want 0700", dirMode)
}
if fileMode != 0600 {
t.Errorf("FileMode(ctx) = %04o, want 0600", fileMode)
}
}
func ExampleWithFileMode() {
fsys, ctx := osfs.NewTemp(), context.Background()
defer fs.Close(fsys)
ctx = fs.WithFileMode(ctx, 0600)
err := fs.WriteFile(ctx, fsys, "private.txt", []byte("secret"))
if err != nil {
log.Fatal(err)
}
info, err := fs.Stat(ctx, fsys, "private.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Mode: %04o\n", info.Mode().Perm())
// Output:
// Mode: 0600
}
func ExampleWithDirMode() {
fsys, ctx := osfs.NewTemp(), context.Background()
defer fs.Close(fsys)
ctx = fs.WithDirMode(ctx, 0700)
err := fs.MkdirAll(ctx, fsys, "private/data")
if err != nil {
log.Fatal(err)
}
info, err := fs.Stat(ctx, fsys, "private")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Mode: %04o\n", info.Mode().Perm())
// Output:
// Mode: 0700
}
func ExampleFileMode() {
ctx := context.Background()
ctx = fs.WithFileMode(ctx, 0600)
mode := fs.FileMode(ctx)
fmt.Printf("Mode: %04o\n", mode)
// Output:
// Mode: 0600
}
func ExampleDirMode() {
ctx := context.Background()
ctx = fs.WithDirMode(ctx, 0700)
mode := fs.DirMode(ctx)
fmt.Printf("Mode: %04o\n", mode)
// Output:
// Mode: 0700
}
func TestWithoutWorkDir(t *testing.T) {
ctx := fs.WithWorkDir(t.Context(), "/some/dir")
ctx = fs.WithoutWorkDir(ctx)
if got := fs.WorkDir(ctx); got != "" {
t.Errorf("WorkDir() = %q, want %q", got, "")
}
}
func TestWithWorkDirEmptyIsNoop(t *testing.T) {
ctx := fs.WithWorkDir(t.Context(), "/some/dir")
ctx = fs.WithWorkDir(ctx, "")
if got, want := fs.WorkDir(ctx), "/some/dir"; got != want {
t.Errorf("WorkDir() = %q, want %q", got, want)
}
}
func TestWithWorkDirRelativeComposes(t *testing.T) {
ctx := fs.WithWorkDir(t.Context(), "/some/dir")
ctx = fs.WithWorkDir(ctx, "sub")
if got, want := fs.WorkDir(ctx), "/some/dir/sub"; got != want {
t.Errorf("WorkDir() = %q, want %q", got, want)
}
}