-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
58 lines (48 loc) · 1.21 KB
/
main_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
package main
import (
"io/ioutil"
"os"
"os/exec"
"testing"
"time"
)
func TestFileWatcher(t *testing.T) {
// Create a temporary directory
tempDir, err := ioutil.TempDir("", "watcher_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
// Create a temporary file in the directory
tempFile, err := ioutil.TempFile(tempDir, "testfile")
if err != nil {
t.Fatal(err)
}
tempFile.Close()
// Define the action to be executed
action := "echo 'file changed'"
// Run the main function in a separate goroutine
go func() {
os.Args = []string{"cmd", "-source", tempDir, "-action", action}
main()
}()
// Wait for the watcher to start
time.Sleep(1 * time.Second)
// Modify the temporary file
err = ioutil.WriteFile(tempFile.Name(), []byte("new content"), 0644)
if err != nil {
t.Fatal(err)
}
// Wait for the debounce timer and action execution
time.Sleep(1 * time.Second)
// Check if the action was executed by verifying the output
cmd := exec.Command("sh", "-c", "echo 'file changed'")
output, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
expectedOutput := "file changed\n"
if string(output) != expectedOutput {
t.Fatalf("expected %q, got %q", expectedOutput, string(output))
}
}