-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfs_test.go
More file actions
84 lines (67 loc) · 2.02 KB
/
fs_test.go
File metadata and controls
84 lines (67 loc) · 2.02 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package sys
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
const testFilename string = "sys-fs-tests"
var expected = []byte("temporary file's content")
func TestRealFS(t *testing.T) {
fs := RealFS()
if _, err := fs.ReadFile("/this-file-does-not-exist"); err == nil {
t.Error("expected to receive error when real file does not exist")
}
tmpfile, err := ioutil.TempFile("", testFilename)
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(expected); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
actual, err := fs.ReadFile(tmpfile.Name())
if err != nil {
t.Errorf("expected error to be nil, got '%v'", err)
}
if string(actual) != string(expected) {
t.Errorf("expected '%s', got '%s'", string(expected), string(actual))
}
if err := fs.RemoveAll(tmpfile.Name()); err != nil {
t.Errorf("Could not remove %s (%v)", tmpfile.Name(), err)
}
if _, err := os.Stat(tmpfile.Name()); err == nil {
t.Errorf("%s was not removed from the real filesystem!", tmpfile.Name())
}
}
func TestFakeFS(t *testing.T) {
fs := NewFakeFS()
if _, err := fs.ReadFile(testFilename); err == nil {
t.Error("expected to receive error when fake file does not exist")
}
fs.Files[testFilename] = expected
actual, err := fs.ReadFile(testFilename)
if err != nil {
t.Errorf("expected error to be nil, got '%v'", err)
}
if string(actual) != string(expected) {
t.Errorf("expected '%s', got '%s'", string(expected), string(actual))
}
if err := fs.RemoveAll(testFilename); err != nil {
t.Errorf("Could not remove %s (%v)", testFilename, err)
}
if _, ok := fs.Files[testFilename]; ok {
t.Errorf("%s was not removed from the fake filesystem!", testFilename)
}
err = fs.RemoveAll(testFilename)
if err == nil {
t.Errorf("Expected removing %s a second time would fail", testFilename)
}
expectedError := fmt.Sprintf("Fake file %s not found", testFilename)
if err.Error() != expectedError {
t.Errorf("expected '%s', got '%s'", expectedError, err.Error())
}
}