Skip to content

Instantly share code, notes, and snippets.

@chinmay185
Last active April 15, 2017 08:14
Show Gist options
  • Save chinmay185/15e0a81deee73571ce9b74d65d1da7eb to your computer and use it in GitHub Desktop.
Save chinmay185/15e0a81deee73571ce9b74d65d1da7eb to your computer and use it in GitHub Desktop.
package grep
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
type testProcessor struct {
files []string
}
func (t *testProcessor) Process(name string) {
t.files = append(t.files, name)
}
func TestWalker(t *testing.T) {
t.Run("test walk", func(t *testing.T) {
parent, err := setupDirs()
defer removeDirs(parent)
if err != nil {
t.Errorf("cannot complete setup %+v", err)
return
}
dummy := &testProcessor{}
w := makeWalker(".go", parent)
// when
w.Walk(dummy)
// then
if len(dummy.files) != 2 {
t.Errorf("expected 2 found %d\n", len(dummy.files))
}
})
}
func TestGrepper(t *testing.T) {
t.Run("test Process", func(t *testing.T) {
path, err := setupFile()
defer removeFile(path)
if err != nil {
t.Errorf("cannot complete setup %+v", err)
return
}
var wg sync.WaitGroup
results := make(chan []string)
g := makeGrepper(results, &wg, "test")
// when
g.Process(path)
// then
t.Run("test results", func(t *testing.T) {
select {
case res := <-results:
if len(res) != 0 {
t.Errorf("results should have been empty but got %+v", res)
}
case <-time.After(1 * time.Second):
t.Error("timeout, couldn't read from results channel in 1 second")
}
})
t.Run("test waitgroup", func(t *testing.T) {
done := make(chan bool)
go func(d chan bool) {
wg.Wait()
d <- true
}(done)
select {
case <-done:
case <-time.After(1 * time.Second):
t.Error("timeout, wait group didn't complete in 1 second")
}
})
})
t.Run("test search", func(t *testing.T) {
var wg sync.WaitGroup
g := makeGrepper(make(chan []string), &wg, "test")
matchingLine1 := "line1 test"
matchingLine2 := "testline2"
nonMatchingLine := "something else"
reader := strings.NewReader(strings.Join([]string{matchingLine1, nonMatchingLine, matchingLine2}, "\n"))
prefix := "/prefix"
// when
results := g.search(reader, prefix)
// then
expectedResults := []string{prefix + ":" + matchingLine1, prefix + ":" + matchingLine2}
if len(expectedResults) != len(results) {
t.Errorf("expected length %d but found %d", len(expectedResults), len(results))
return
}
for i := range expectedResults {
if expectedResults[i] != results[i] {
t.Errorf("expected %s but got %s", expectedResults[i], results[i])
}
}
})
}
func setupFile() (string, error) {
path := filepath.Join(".", "__test_file__.go")
if _, err := os.Create(path); err != nil {
return "", err
}
return path, nil
}
func removeFile(path string) {
os.Remove(path)
}
func setupDirs() (string, error) {
parent := filepath.Join(".", "__test__")
dir1 := filepath.Join(parent, "dir1")
dir2 := filepath.Join(dir1, "dir2")
os.MkdirAll(dir2, 0755)
if _, err := os.Create(filepath.Join(dir1, "file1.go")); err != nil {
return "", err
}
if _, err := os.Create(filepath.Join(dir2, "file2.go")); err != nil {
return "", err
}
return parent, nil
}
func removeDirs(parent string) {
os.RemoveAll(parent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment