forked from natefinch/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_test.go
83 lines (79 loc) · 2.21 KB
/
atomic_test.go
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
package atomic
import (
"bytes"
"os"
"testing"
)
func TestWriteFile(t *testing.T) {
file := "foo.txt"
content := bytes.NewBufferString("foo")
defer func() { _ = os.Remove(file) }()
if err := WriteFile(file, content); err != nil {
t.Errorf("Failed to write file: %q: %v", file, err)
}
fi, err := os.Stat(file)
if err != nil {
t.Errorf("Failed to stat file: %q: %v", file, err)
}
if fi.Mode() != 0600 {
t.Errorf("File mode not correct")
}
}
func TestWriteDefaultFileMode(t *testing.T) {
file := "bar.txt"
content := bytes.NewBufferString("bar")
defer func() { _ = os.Remove(file) }()
if err := WriteFile(file, content, DefaultFileMode(0644)); err != nil {
t.Errorf("Failed to write file: %q: %v", file, err)
}
fi, err := os.Stat(file)
if err != nil {
t.Errorf("Failed to stat file: %q: %v", file, err)
}
if fi.Mode() != 0644 {
t.Errorf("File mode not correct: %v", fi.Mode())
}
// check if file mode is preserved
if err := os.Chmod(file, 0600); err != nil {
t.Errorf("Failed to change file mode: %q: %v", file, err)
}
if err := WriteFile(file, content, DefaultFileMode(0644)); err != nil {
t.Errorf("Failed to write file: %q: %v", file, err)
}
fi, err = os.Stat(file)
if err != nil {
t.Errorf("Failed to stat file: %q: %v", file, err)
}
if fi.Mode() != 0600 {
t.Errorf("File mode not correct: %v", fi.Mode())
}
}
func TestWriteFileMode(t *testing.T) {
file := "baz.txt"
content := bytes.NewBufferString("baz")
defer func() { _ = os.Remove(file) }()
if err := WriteFile(file, content, FileMode(0644)); err != nil {
t.Errorf("Failed to write file: %q: %v", file, err)
}
fi, err := os.Stat(file)
if err != nil {
t.Errorf("Failed to stat file: %q: %v", file, err)
}
if fi.Mode() != 0644 {
t.Errorf("File mode not correct: %v", fi.Mode())
}
// ensure previous file mode is ingored
if err := os.Chmod(file, 0600); err != nil {
t.Errorf("Failed to change file mode: %q: %v", file, err)
}
if err := WriteFile(file, content, FileMode(0644)); err != nil {
t.Errorf("Failed to write file: %q: %v", file, err)
}
fi, err = os.Stat(file)
if err != nil {
t.Errorf("Failed to stat file: %q: %v", file, err)
}
if fi.Mode() != 0644 {
t.Errorf("File mode not correct: %v", fi.Mode())
}
}