test: fixture workflow to make test functions concise

This commit is contained in:
Sridhar Ratnakumar 2012-10-13 11:44:47 -07:00
parent 4e53f618e2
commit df3f97310f
4 changed files with 51 additions and 33 deletions

View File

@ -2,3 +2,6 @@ default: test
test: *.go
GOPATH=~/as/logyard go test -v
fmt:
go fmt .

View File

@ -1,8 +1,8 @@
package tail
import (
"fmt"
"bufio"
"fmt"
"io"
"launchpad.net/tomb"
"log"

View File

@ -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)
}
}