From df3f97310fdf69f3514ae89a912e165cf56d9613 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 13 Oct 2012 11:44:47 -0700 Subject: [PATCH] test: fixture workflow to make test functions concise --- Makefile | 3 +++ tail.go | 2 +- tail_test.go | 75 +++++++++++++++++++++++++++++++--------------------- watch.go | 4 +-- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index f5cc885..82328bb 100644 --- a/Makefile +++ b/Makefile @@ -2,3 +2,6 @@ default: test test: *.go GOPATH=~/as/logyard go test -v + +fmt: + go fmt . diff --git a/tail.go b/tail.go index 6ca0a66..c807d16 100644 --- a/tail.go +++ b/tail.go @@ -1,8 +1,8 @@ package tail import ( - "fmt" "bufio" + "fmt" "io" "launchpad.net/tomb" "log" diff --git a/tail_test.go b/tail_test.go index b69af5a..a23307b 100644 --- a/tail_test.go +++ b/tail_test.go @@ -1,11 +1,11 @@ package tail import ( + _ "fmt" + "io/ioutil" + "os" "testing" "time" - "os" - "io/ioutil" - _ "fmt" ) func init() { @@ -35,55 +35,70 @@ func TestMissingFile(t *testing.T) { } func TestLocationMinusOne(t *testing.T) { - fix := NewFixture("simple") - err := ioutil.WriteFile(fix.path + "/test.txt", []byte("hello\nworld\n"), 0777) - if err != nil { - t.Error(err) - } - tail, err := TailFile(fix.path + "/test.txt", Config{Follow: true, Location: -1}) - if err != nil { - t.Error(err) - } - go CompareTailWithLines(t, tail, []string{"hello", "world"}) + fix := NewFixture("simple", t) + fix.CreateFile("test.txt", "hello\nworld\n") + tail := fix.StartTail("test.txt", Config{Follow: true, Location: -1}) + go fix.VerifyTail(tail, []string{"hello", "world"}) - // delete after 1 second, to give tail sufficient time to read all lines. - <-time.After(1 * time.Second) - err = os.Remove(fix.path + "/test.txt") - if err != nil { - t.Error(err) - } + // Delete after a reasonable delay, to give tail sufficient time + // to read all lines. + <-time.After(250 * time.Millisecond) + fix.RemoveFile("test.txt") tail.Stop() } type Fixture struct { Name string path string + t *testing.T } -func NewFixture(name string) Fixture { - fix := Fixture{name, ".test/" + name} - os.MkdirAll(fix.path, os.ModeTemporary | 0700) +func NewFixture(name string, t *testing.T) Fixture { + fix := Fixture{name, ".test/" + name, t} + err := os.MkdirAll(fix.path, os.ModeTemporary|0700) + if err != nil { + t.Fatal(err) + } return fix } -func CompareTailWithLines(t *testing.T, tail *Tail, lines []string) { +func (fix Fixture) CreateFile(name string, contents string) { + err := ioutil.WriteFile(fix.path+"/"+name, []byte(contents), 0777) + if err != nil { + fix.t.Fatal(err) + } +} + +func (fix Fixture) RemoveFile(name string) { + err := os.Remove(fix.path + "/" + name) + if err != nil { + fix.t.Fatal(err) + } +} + +func (fix Fixture) StartTail(name string, config Config) *Tail { + tail, err := TailFile(fix.path+"/"+name, config) + if err != nil { + fix.t.Fatal(err) + } + return tail +} + +func (fix Fixture) VerifyTail(tail *Tail, lines []string) { for _, line := range lines { tailedLine, ok := <-tail.Lines if !ok { - t.Error("insufficient lines from tail") - return + fix.t.Fatal("insufficient lines from tail") } if tailedLine == nil { - t.Errorf("tail.Lines returned nil; not possible") - return + fix.t.Fatalf("tail.Lines returned nil; not possible") } if tailedLine.Text != line { - t.Errorf("mismatch; %s != %s", tailedLine.Text, line) - return + fix.t.Fatalf("mismatch; %s != %s", tailedLine.Text, line) } } line, ok := <-tail.Lines if ok { - t.Errorf("more content from tail: %s", line.Text) + fix.t.Fatalf("more content from tail: %s", line.Text) } } diff --git a/watch.go b/watch.go index dea369d..4e6f30e 100644 --- a/watch.go +++ b/watch.go @@ -121,11 +121,11 @@ func (fw *PollingFileWatcher) ChangeEvents() chan bool { if err != nil { if os.IsNotExist(err) { // below goroutine (every2Seconds) will catch up eventually and stop us. - continue + continue } panic(err) } - + modTime := fi.ModTime() if modTime != prevModTime { prevModTime = modTime