forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem_helpers_test.go
90 lines (73 loc) · 2.59 KB
/
filesystem_helpers_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
package filecount
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestMTime(t *testing.T) {
//this is the time our foo file should have
mtime := time.Date(2015, time.December, 14, 18, 25, 5, 0, time.UTC)
fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata/foo")
require.Nil(t, err)
require.Equal(t, mtime, fileInfo.ModTime())
}
func TestSize(t *testing.T) {
//this is the time our foo file should have
size := int64(4096)
fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata")
require.Nil(t, err)
require.Equal(t, size, fileInfo.Size())
}
func TestIsDir(t *testing.T) {
//this is the time our foo file should have
dir := true
fs := getTestFileSystem()
fileInfo, err := fs.Stat("/testdata")
require.Nil(t, err)
require.Equal(t, dir, fileInfo.IsDir())
}
func TestRealFS(t *testing.T) {
//test that the default (non-test) empty FS causes expected behaviour
var fs fileSystem = osFS{}
//the following file exists on disk - and not in our fake fs
fileInfo, err := fs.Stat(getTestdataDir() + "/qux")
require.Nil(t, err)
require.Equal(t, false, fileInfo.IsDir())
require.Equal(t, int64(446), fileInfo.Size())
// now swap out real, for fake filesystem
fs = getTestFileSystem()
// now, the same test as above will return an error as the file doesn't exist in our fake fs
expectedError := "Stat " + getTestdataDir() + "/qux: No such file or directory"
fileInfo, err = fs.Stat(getTestdataDir() + "/qux")
require.Equal(t, expectedError, err.Error())
// and verify that what we DO expect to find, we do
fileInfo, err = fs.Stat("/testdata/foo")
require.Nil(t, err)
}
func getTestFileSystem() fakeFileSystem {
/*
create our desired "filesystem" object, complete with an internal map allowing our funcs to return meta data as requested
type FileInfo interface {
Name() string // base name of the file
Size() int64 // length in bytes of file
Mode() FileMode // file mode bits
ModTime() time.Time // modification time
IsDir() bool // returns bool indicating if a Dir or not
Sys() interface{} // underlying data source. always nil (in this case)
}
*/
mtime := time.Date(2015, time.December, 14, 18, 25, 5, 0, time.UTC)
// set file permisions
var fmask uint32 = 0666
var dmask uint32 = 0666
// set directory bit
dmask |= (1 << uint(32-1))
fileList := map[string]fakeFileInfo{
"/testdata": {name: "testdata", size: int64(4096), filemode: uint32(dmask), modtime: mtime, isdir: true},
"/testdata/foo": {name: "foo", filemode: uint32(fmask), modtime: mtime},
}
fs := fakeFileSystem{files: fileList}
return fs
}