Skip to content

Commit

Permalink
add basic tests for hound
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Nov 11, 2015
1 parent 71907a5 commit 62993a8
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions hound_test.go
@@ -0,0 +1,93 @@
package main

import (
"github.com/ezekg/git-hound/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-diff/diff"
"testing"
"io/ioutil"
"os"
)

func TestDiffs(t *testing.T) {
rescueStdout := os.Stdout
var fileName string
var hunk *diff.Hunk

h := &Hound{}
c := []byte(`
warn:
- '(?i)user(name)?\W*[:=,]\W*.+$'
fail:
- '(?i)pass(word)?\W*[:=,]\W*.+$'
`)

if err := h.Parse(c); err != nil {
t.Fatalf("Should parse - %s", err)
}

// Should fail
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`)
if err := h.Sniff(fileName, hunk); err == nil {
t.Fatalf("Should fail - %s", err)
}

// Should warn
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`)
r, w, _ := os.Pipe()
os.Stdout = w

if err := h.Sniff(fileName, hunk); err != nil {
w.Close()
os.Stdout = rescueStdout
t.Fatalf("Should pass - %s", err)
}

w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

if len(out) <= 0 {
t.Fatalf("Should warn - %s", out)
}

// Should pass
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`)
if err := h.Sniff(fileName, hunk); err != nil {
t.Fatalf("Should pass - %s", err)
}

// Should only pay attention to added lines and pass
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`)
if err := h.Sniff(fileName, hunk); err != nil {
t.Fatalf("Should pass - %s", err)
}
}

func getDiff(diffContents string) (string, *diff.Hunk) {
fileDiff, _ := diff.ParseFileDiff([]byte(diffContents))
fileName := fileDiff.NewName
for _, hunk := range fileDiff.GetHunks() {
return fileName, hunk
}

return fileName, nil
}

0 comments on commit 62993a8

Please sign in to comment.