forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_test.go
159 lines (131 loc) · 4.17 KB
/
fs_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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package fs_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/influxdata/influxdb/v2/pkg/fs"
)
func TestRenameFileWithReplacement(t *testing.T) {
// sample data for loading into files
sampleData1 := "this is some data"
sampleData2 := "we got some more data"
t.Run("exists", func(t *testing.T) {
oldpath := MustCreateTempFile(t, sampleData1)
newpath := MustCreateTempFile(t, sampleData2)
defer MustRemoveAll(oldpath)
defer MustRemoveAll(newpath)
oldContents := MustReadAllFile(oldpath)
newContents := MustReadAllFile(newpath)
if got, exp := oldContents, sampleData1; got != exp {
t.Fatalf("got contents %q, expected %q", got, exp)
} else if got, exp := newContents, sampleData2; got != exp {
t.Fatalf("got contents %q, expected %q", got, exp)
}
if err := fs.RenameFileWithReplacement(oldpath, newpath); err != nil {
t.Fatalf("ReplaceFileIfExists returned an error: %s", err)
}
if err := fs.SyncDir(filepath.Dir(oldpath)); err != nil {
panic(err)
}
// Contents of newpath will now be equivalent to oldpath' contents.
newContents = MustReadAllFile(newpath)
if newContents != oldContents {
t.Fatalf("contents for files differ: %q versus %q", newContents, oldContents)
}
// oldpath will be removed.
if MustFileExists(oldpath) {
t.Fatalf("file %q still exists, but it shouldn't", oldpath)
}
})
t.Run("not exists", func(t *testing.T) {
oldpath := MustCreateTempFile(t, sampleData1)
defer MustRemoveAll(oldpath)
oldContents := MustReadAllFile(oldpath)
if got, exp := oldContents, sampleData1; got != exp {
t.Fatalf("got contents %q, expected %q", got, exp)
}
root := filepath.Dir(oldpath)
newpath := filepath.Join(root, "foo")
if err := fs.RenameFileWithReplacement(oldpath, newpath); err != nil {
t.Fatalf("ReplaceFileIfExists returned an error: %s", err)
}
if err := fs.SyncDir(filepath.Dir(oldpath)); err != nil {
panic(err)
}
// Contents of newpath will now be equivalent to oldpath's contents.
newContents := MustReadAllFile(newpath)
if newContents != oldContents {
t.Fatalf("contents for files differ: %q versus %q", newContents, oldContents)
}
// oldpath will be removed.
if MustFileExists(oldpath) {
t.Fatalf("file %q still exists, but it shouldn't", oldpath)
}
})
}
func TestCreateFileWithReplacement(t *testing.T) {
path := MustCreateTempFile(t, "sample data")
defer MustRemoveAll(path)
// should return an error if we CreateFile to the same path
_, err := fs.CreateFile(path)
if err == nil {
t.Fatalf("CreateFile did not return an error")
}
// contents of the file should be intact
contents := MustReadAllFile(path)
if got, exp := contents, "sample data"; got != exp {
t.Fatalf("got contents %q, expected %q", got, exp)
}
// running CreateFileWithReplacement on path should not return an error
if _, err = fs.CreateFileWithReplacement(path); err != nil {
t.Fatalf("CreateFileWithReplacement returned err: %v", err)
}
// the file at path should now be empty
contents = MustReadAllFile(path)
if contents != "" {
t.Fatalf("expected file to be empty but got: %v", contents)
}
}
// CreateTempFileOrFail creates a temporary file returning the path to the file.
func MustCreateTempFile(t testing.TB, data string) string {
t.Helper()
f, err := ioutil.TempFile("", "fs-test")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
} else if _, err := f.WriteString(data); err != nil {
t.Fatal(err)
} else if err := f.Close(); err != nil {
t.Fatal(err)
}
return f.Name()
}
func MustRemoveAll(path string) {
if err := os.RemoveAll(path); err != nil {
panic(err)
}
}
// MustFileExists determines if a file exists, panicking if any error
// (other than one associated with the file not existing) is returned.
func MustFileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
} else if os.IsNotExist(err) {
return false
}
panic(err)
}
// MustReadAllFile reads the contents of path, panicking if there is an error.
func MustReadAllFile(path string) string {
fd, err := os.Open(path)
if err != nil {
panic(err)
}
defer fd.Close()
data, err := ioutil.ReadAll(fd)
if err != nil {
panic(err)
}
return string(data)
}