-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
69 lines (56 loc) · 1.37 KB
/
example_test.go
File metadata and controls
69 lines (56 loc) · 1.37 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
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
}