-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatcher_test.go
More file actions
86 lines (77 loc) · 2.11 KB
/
Copy pathwatcher_test.go
File metadata and controls
86 lines (77 loc) · 2.11 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
85
86
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestDirWatcher(t *testing.T) {
dw := newDirWatcher("")
if dw != nil {
t.Fatal("newDirWatcher with empty directory arg returned non-nil dirWatcher")
}
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Failed to create a temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
dw = newDirWatcher(tempDir)
a, r, err := dw.check()
if err != nil {
t.Fatalf("Failed to check temporary directory: %s", err)
}
if len(a) != 0 {
t.Fatalf("Expected 0 added files in temporary directory, got %d", len(a))
}
if len(r) != 0 {
t.Fatalf("Expected 0 removed files in temporary directory, got %d", len(r))
}
f, err := os.Create(filepath.Join(tempDir, "test-file"))
if err != nil {
t.Fatalf("Failed to create temporary file: %s", err)
}
a, r, err = dw.check()
if err != nil {
t.Fatalf("Failed to check temporary directory: %s", err)
}
if len(a) != 1 {
t.Fatalf("Expected 1 added files in temporary directory, got %d", len(a))
}
if a[0] != f.Name() {
t.Fatalf("Expected added file to be %s, got %s", f.Name(), a[0])
}
if len(r) != 0 {
t.Fatalf("Expected 0 removed files in temporary directory, got %d", len(r))
}
err = os.Remove(f.Name())
if err != nil {
t.Fatalf("Failed to remove test file: %s", err)
}
a, r, err = dw.check()
if err != nil {
t.Fatalf("Failed to check temporary directory: %s", err)
}
if len(a) != 0 {
t.Fatalf("Expected 0 added files in temporary directory, got %d", len(a))
}
if len(r) != 1 {
t.Fatalf("Expected 1 removed files in temporary directory, got %d", len(r))
}
if r[0] != f.Name() {
t.Fatalf("Expected removed file to be %s, got %s", f.Name(), r[0])
}
_, err = ioutil.TempDir(tempDir, "")
if err != nil {
t.Fatalf("Failed to create a temporary directory: %s", err)
}
a, r, err = dw.check()
if err != nil {
t.Fatalf("Failed to check temporary directory: %s", err)
}
if len(a) != 0 {
t.Fatalf("Expected 0 added files in temporary directory, got %d", len(a))
}
if len(r) != 0 {
t.Fatalf("Expected 0 removed files in temporary directory, got %d", len(r))
}
}