-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathfiles_test.go
More file actions
66 lines (56 loc) · 1.72 KB
/
files_test.go
File metadata and controls
66 lines (56 loc) · 1.72 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
package storage
import (
"os"
"path/filepath"
"testing"
)
func TestChmodSecretFileMode(t *testing.T) {
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "secret.txt")
if err := os.WriteFile(path, []byte("secret"), 0644); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
if err := Chmod(path, 0600); err != nil {
t.Fatalf("failed to chmod file: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("failed to stat file: %v", err)
}
if got := info.Mode().Perm(); got != 0600 {
t.Fatalf("unexpected mode: got %o want %o", got, os.FileMode(0600))
}
}
func TestChmodExecutableMode(t *testing.T) {
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "tool.sh")
if err := os.WriteFile(path, []byte("#!/bin/sh\necho ok\n"), 0644); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
if err := Chmod(path, 0755); err != nil {
t.Fatalf("failed to chmod file: %v", err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("failed to stat file: %v", err)
}
if got := info.Mode().Perm(); got != 0755 {
t.Fatalf("unexpected mode: got %o want %o", got, os.FileMode(0755))
}
}
func TestWriteFileAtomicallyCreatesMissingParentDirectory(t *testing.T) {
tmpDir := t.TempDir()
targetDir := filepath.Join(tmpDir, "nested", "config")
targetPath := filepath.Join(targetDir, ".lrc.toml")
content := []byte("api_key = \"test\"\n")
if err := WriteFileAtomically(targetPath, content, 0600); err != nil {
t.Fatalf("write atomically with missing parent: %v", err)
}
got, err := os.ReadFile(targetPath)
if err != nil {
t.Fatalf("read target file: %v", err)
}
if string(got) != string(content) {
t.Fatalf("unexpected file content: got %q want %q", string(got), string(content))
}
}