-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
102 lines (81 loc) · 2 KB
/
Copy pathexample_test.go
File metadata and controls
102 lines (81 loc) · 2 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
package fsx_test
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/slashdevops/fsx"
)
// ExampleExpandPath demonstrates expanding a user-facing path.
func ExampleExpandPath() {
expanded := fsx.ExpandPath("~/config.yaml")
fmt.Println(filepath.IsAbs(expanded))
// Output:
// true
}
// ExampleIsWithin demonstrates a containment check before deleting a file.
func ExampleIsWithin() {
base := filepath.Join(os.TempDir(), "workspace")
target := filepath.Join(base, "page.md")
fmt.Println(fsx.IsWithin(base, target))
fmt.Println(fsx.IsWithin(base, filepath.Join(base, "..", "escape.md")))
// Output:
// true
// false
}
// ExampleHasExtension demonstrates extension matching.
func ExampleHasExtension() {
fmt.Println(fsx.HasExtension("config.YAML", "yaml", "json"))
fmt.Println(fsx.HasExtension("README", "md"))
// Output:
// true
// false
}
// ExampleWriteFileAtomic demonstrates an atomic file replacement.
func ExampleWriteFileAtomic() {
dir, err := os.MkdirTemp("", "fsx-example-*")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
log.Fatal(err)
}
}()
path := filepath.Join(dir, "config.txt")
if err := fsx.WriteFileAtomic(path, []byte("ready"), 0o600); err != nil {
log.Fatal(err)
}
data, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
// Output:
// ready
}
func ExampleIsWritable() {
dir, err := os.MkdirTemp("", "fsx-example-*")
if err != nil {
fmt.Println("error:", err)
return
}
defer os.RemoveAll(dir)
writable := filepath.Join(dir, "writable.yaml")
if err := os.WriteFile(writable, []byte("a: 1\n"), 0o644); err != nil {
fmt.Println("error:", err)
return
}
readOnly := filepath.Join(dir, "readonly.yaml")
if err := os.WriteFile(readOnly, []byte("a: 1\n"), 0o400); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(fsx.IsWritable(writable))
fmt.Println(fsx.IsWritable(readOnly))
fmt.Println(fsx.IsReadable(readOnly))
// Output:
// true
// false
// true
}