Skip to content

Commit 7d91f14

Browse files
committed
fix tests and potential race condition
1 parent 09d7e6d commit 7d91f14

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

hound.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func (h *Hound) Parse(data []byte) error {
5151
// Sniff matches the passed git-diff hunk against all regexp patterns that
5252
// were parsed from the local configuration.
5353
func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, warnc chan string, failc chan error, donec chan bool) {
54+
defer func() { donec <- true }()
55+
5456
r1, _ := regexp.Compile(`^\w+\/`)
5557
fileName = r1.ReplaceAllString(fileName, "")
5658
if _, ok := h.MatchPatterns(h.Skips, []byte(fileName)); ok {
@@ -75,11 +77,8 @@ func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, warnc chan string, failc
7577
"Failure: pattern `%s` match found for `%s` starting at line %d in %s\n",
7678
pattern, line, hunk.NewStartLine, fileName))
7779
failc <- errors.New(msg)
78-
return
7980
}
8081
}
81-
82-
donec <- true
8382
}
8483

8584
// Match matches a byte array against a regexp pattern and returns a bool.

hound_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import (
66
)
77

88
func TestDiffs(t *testing.T) {
9-
var fileName string
10-
var hunk *diff.Hunk
11-
12-
warnc := make(chan string)
13-
failc := make(chan error)
14-
donec := make(chan bool)
15-
169
hound := &Hound{}
1710
config := []byte(`
1811
warn:
@@ -27,12 +20,16 @@ fail:
2720

2821
// Should fail
2922
{
30-
fileName, hunk = getDiff(`diff --git a/test1.go b/test1.go
23+
fileName, hunk := getDiff(`diff --git a/test1.go b/test1.go
3124
index 000000..000000 000000
3225
--- a/test1.go
3326
+++ b/test1.go
3427
@@ -1,2 +3,4 @@
3528
+Password: something-secret`)
29+
warnc := make(chan string)
30+
failc := make(chan error)
31+
donec := make(chan bool)
32+
3633
go hound.Sniff(fileName, hunk, warnc, failc, donec)
3734

3835
select {
@@ -47,12 +44,16 @@ index 000000..000000 000000
4744

4845
// Should pass but output warning
4946
{
50-
fileName, hunk = getDiff(`diff --git a/test2.go b/test2.go
47+
fileName, hunk := getDiff(`diff --git a/test2.go b/test2.go
5148
index 000000..000000 000000
5249
--- a/test2.go
5350
+++ b/test2.go
5451
@@ -1,2 +3,4 @@
5552
+Username: something-secret`)
53+
warnc := make(chan string)
54+
failc := make(chan error)
55+
donec := make(chan bool)
56+
5657
go hound.Sniff(fileName, hunk, warnc, failc, donec)
5758

5859
select {
@@ -67,12 +68,16 @@ index 000000..000000 000000
6768

6869
// Should pass
6970
{
70-
fileName, hunk = getDiff(`diff --git a/test3.go b/test3.go
71+
fileName, hunk := getDiff(`diff --git a/test3.go b/test3.go
7172
index 000000..000000 000000
7273
--- a/test3.go
7374
+++ b/test3.go
7475
@@ -1,2 +3,4 @@
7576
+Something that is okay to commit`)
77+
warnc := make(chan string)
78+
failc := make(chan error)
79+
donec := make(chan bool)
80+
7681
go hound.Sniff(fileName, hunk, warnc, failc, donec)
7782

7883
select {
@@ -87,12 +92,16 @@ index 000000..000000 000000
8792

8893
// Should only pay attention to added lines and pass
8994
{
90-
fileName, hunk = getDiff(`diff --git a/test4.go b/test4.go
95+
fileName, hunk := getDiff(`diff --git a/test4.go b/test4.go
9196
index 000000..000000 000000
9297
--- a/test4.go
9398
+++ b/test4.go
9499
@@ -1,2 +3,4 @@
95100
-Password: something-secret`)
101+
warnc := make(chan string)
102+
failc := make(chan error)
103+
donec := make(chan bool)
104+
96105
go hound.Sniff(fileName, hunk, warnc, failc, donec)
97106

98107
select {

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func main() {
3737
}
3838

3939
hunkCount := 0
40+
errCount := 0
41+
4042
warnc := make(chan string)
4143
failc := make(chan error)
4244
donec := make(chan bool)
@@ -57,11 +59,16 @@ func main() {
5759
fmt.Print(msg)
5860
case err := <-failc:
5961
fmt.Print(err)
60-
os.Exit(1)
62+
errCount++
6163
case <-donec:
6264
c++
6365
}
6466
}
67+
68+
if errCount > 0 {
69+
fmt.Printf("%d failures detected. Please fix them before you can commit.\n", errCount)
70+
os.Exit(1)
71+
}
6572
}
6673

6774
out, code := git.Exec(flag.Args()...)

0 commit comments

Comments
 (0)