-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
84 lines (75 loc) · 1.5 KB
/
search_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
package main
import (
"os"
"os/exec"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestDetectGitRepo(t *testing.T) {
d, err := os.MkdirTemp(os.TempDir(), "test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(d)
cmd := exec.Command("git", "init")
cmd.Dir = d
err = cmd.Run()
if err != nil {
t.Error(err)
}
output := make(chan repo)
go func() {
_ = collectDirtyRepos(d, output)
defer close(output)
}()
select {
case result := <-output:
require.Equal(t, d, result.path)
case <-time.After(2 * time.Second):
t.Fatal("Test timed out")
}
}
func TestExcludedDirectory(t *testing.T) {
d, err := os.MkdirTemp(os.TempDir(), "test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(d)
err = os.MkdirAll(d+"/.hidden/test", 0750)
if err != nil {
t.Error(err)
}
cmd := exec.Command("git", "init")
cmd.Dir = d + "/.hidden/test"
err = cmd.Run()
if err != nil {
t.Error(err)
}
err = os.MkdirAll(d+"/node_modules/test", 0750)
if err != nil {
t.Error(err)
}
cmd = exec.Command("git", "init")
cmd.Dir = d + "/node_modules/test"
err = cmd.Run()
if err != nil {
t.Error(err)
}
output := make(chan repo)
go func() {
_ = collectDirtyRepos(d, output)
defer close(output)
}()
detected := 0
select {
case result := <-output:
// Closed channel returning the zero value of the underlying type, thus check non-emptyness..
if result.path != "" {
detected++
}
case <-time.After(2 * time.Second):
t.Fatal("Test timed out")
}
require.Equal(t, 0, detected)
}