test: fixture workflow to make test functions concise
This commit is contained in:
parent
4e53f618e2
commit
df3f97310f
3
Makefile
3
Makefile
|
@ -2,3 +2,6 @@ default: test
|
|||
|
||||
test: *.go
|
||||
GOPATH=~/as/logyard go test -v
|
||||
|
||||
fmt:
|
||||
go fmt .
|
||||
|
|
2
tail.go
2
tail.go
|
@ -1,8 +1,8 @@
|
|||
package tail
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"launchpad.net/tomb"
|
||||
"log"
|
||||
|
|
75
tail_test.go
75
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)
|
||||
}
|
||||
}
|
||||
|
|
4
watch.go
4
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
|
||||
|
|
Loading…
Reference in New Issue