Skip to content

Commit

Permalink
fix tests and potential race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Nov 23, 2015
1 parent 09d7e6d commit 7d91f14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
5 changes: 2 additions & 3 deletions hound.go
Expand Up @@ -51,6 +51,8 @@ func (h *Hound) Parse(data []byte) error {
// Sniff matches the passed git-diff hunk against all regexp patterns that
// were parsed from the local configuration.
func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, warnc chan string, failc chan error, donec chan bool) {
defer func() { donec <- true }()

r1, _ := regexp.Compile(`^\w+\/`)
fileName = r1.ReplaceAllString(fileName, "")
if _, ok := h.MatchPatterns(h.Skips, []byte(fileName)); ok {
Expand All @@ -75,11 +77,8 @@ func (h *Hound) Sniff(fileName string, hunk *diff.Hunk, warnc chan string, failc
"Failure: pattern `%s` match found for `%s` starting at line %d in %s\n",
pattern, line, hunk.NewStartLine, fileName))
failc <- errors.New(msg)
return
}
}

donec <- true
}

// Match matches a byte array against a regexp pattern and returns a bool.
Expand Down
31 changes: 20 additions & 11 deletions hound_test.go
Expand Up @@ -6,13 +6,6 @@ import (
)

func TestDiffs(t *testing.T) {
var fileName string
var hunk *diff.Hunk

warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)

hound := &Hound{}
config := []byte(`
warn:
Expand All @@ -27,12 +20,16 @@ fail:

// Should fail
{
fileName, hunk = getDiff(`diff --git a/test1.go b/test1.go
fileName, hunk := getDiff(`diff --git a/test1.go b/test1.go
index 000000..000000 000000
--- a/test1.go
+++ b/test1.go
@@ -1,2 +3,4 @@
+Password: something-secret`)
warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)

go hound.Sniff(fileName, hunk, warnc, failc, donec)

select {
Expand All @@ -47,12 +44,16 @@ index 000000..000000 000000

// Should pass but output warning
{
fileName, hunk = getDiff(`diff --git a/test2.go b/test2.go
fileName, hunk := getDiff(`diff --git a/test2.go b/test2.go
index 000000..000000 000000
--- a/test2.go
+++ b/test2.go
@@ -1,2 +3,4 @@
+Username: something-secret`)
warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)

go hound.Sniff(fileName, hunk, warnc, failc, donec)

select {
Expand All @@ -67,12 +68,16 @@ index 000000..000000 000000

// Should pass
{
fileName, hunk = getDiff(`diff --git a/test3.go b/test3.go
fileName, hunk := getDiff(`diff --git a/test3.go b/test3.go
index 000000..000000 000000
--- a/test3.go
+++ b/test3.go
@@ -1,2 +3,4 @@
+Something that is okay to commit`)
warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)

go hound.Sniff(fileName, hunk, warnc, failc, donec)

select {
Expand All @@ -87,12 +92,16 @@ index 000000..000000 000000

// Should only pay attention to added lines and pass
{
fileName, hunk = getDiff(`diff --git a/test4.go b/test4.go
fileName, hunk := getDiff(`diff --git a/test4.go b/test4.go
index 000000..000000 000000
--- a/test4.go
+++ b/test4.go
@@ -1,2 +3,4 @@
-Password: something-secret`)
warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)

go hound.Sniff(fileName, hunk, warnc, failc, donec)

select {
Expand Down
9 changes: 8 additions & 1 deletion main.go
Expand Up @@ -37,6 +37,8 @@ func main() {
}

hunkCount := 0
errCount := 0

warnc := make(chan string)
failc := make(chan error)
donec := make(chan bool)
Expand All @@ -57,11 +59,16 @@ func main() {
fmt.Print(msg)
case err := <-failc:
fmt.Print(err)
os.Exit(1)
errCount++
case <-donec:
c++
}
}

if errCount > 0 {
fmt.Printf("%d failures detected. Please fix them before you can commit.\n", errCount)
os.Exit(1)
}
}

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

0 comments on commit 7d91f14

Please sign in to comment.