From 7d91f1464e2c635c9fc3df73d85cc14009f4d336 Mon Sep 17 00:00:00 2001 From: ezekg Date: Mon, 23 Nov 2015 11:22:23 -0600 Subject: [PATCH] fix tests and potential race condition --- hound.go | 5 ++--- hound_test.go | 31 ++++++++++++++++++++----------- main.go | 9 ++++++++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/hound.go b/hound.go index b7484f4..babc93a 100644 --- a/hound.go +++ b/hound.go @@ -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 { @@ -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. diff --git a/hound_test.go b/hound_test.go index e2d2715..efafc70 100644 --- a/hound_test.go +++ b/hound_test.go @@ -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: @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/main.go b/main.go index fb79a51..6c7bc7b 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,8 @@ func main() { } hunkCount := 0 + errCount := 0 + warnc := make(chan string) failc := make(chan error) donec := make(chan bool) @@ -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()...)