diff --git a/.gitignore b/.gitignore index f9d65e0..4f41710 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ test_data/*.log .idea .swo .swp -.code \ No newline at end of file +.code +release \ No newline at end of file diff --git a/Makefile b/Makefile index 5d21347..595d1e4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ rectail : cmd/rectail/args.go cmd/rectail/rectail.go go test -v && go install ./cmd/rectail && echo 'installed to $$GOPATH/bin/rectail' && \ - echo "to use, run 'rectail --help'" \ No newline at end of file + echo "to use, run 'rectail --help'" + +test : + go test -v \ No newline at end of file diff --git a/rectail_test.go b/rectail_test.go index b47ac8a..fa05ad9 100644 --- a/rectail_test.go +++ b/rectail_test.go @@ -153,3 +153,66 @@ func TestFirstRead(t *testing.T) { } } } + +func TestFirstReadForSmallOffset(t *testing.T) { + logger := log.New(logOutput, "", 0) + + var ( + fileUpdate rectail.FileUpdate + expectedLines = []string{ + "џѕѓ2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 џѕѓ2 bar2", + "џѕѓ3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 џѕѓ3 bar3", + "џѕѓ4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 џѕѓ4 bar4", + } + textToWrite = `џѕѓ bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar џѕѓ bar +џѕѓ1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 foo1 bar1 џѕѓ1 bar1 +џѕѓ2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 foo2 bar2 џѕѓ2 bar2 +џѕѓ3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 foo3 bar3 џѕѓ3 bar3 +џѕѓ4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 foo4 bar4 џѕѓ4 bar4` + fileUpdates = make(chan rectail.FileUpdate) + updates = make(chan string) + err error + f *os.File + ) + f, err = os.Create("test_data/test.log") + if err != nil { + t.Fatal(err) + } + if _, err = f.WriteString(textToWrite); err != nil { + t.Fatal(err) + } + + rt, err := rectail.NewRecTail( + []string{"test_data"}, + []string{"\\.log"}, + updates, + 600, + 400, + logger, + ) + if err != nil { + t.Fatalf("could not initialize rectail: %v", err) + } + rectailCtx, rectailCtxCancel := context.WithCancel(context.Background()) + defer rectailCtxCancel() + + go func() { + if err = rt.Watch(rectailCtx, fileUpdates); err != nil { + logger.Fatalln(err) + } + }() + go func() { + for update := range updates { + logger.Println(update) + } + }() + fileUpdate = <-fileUpdates + if len(fileUpdate.Lines) != len(expectedLines) { + t.Errorf("expected %d lines with update; got: %d", len(expectedLines), len(fileUpdate.Lines)) + } + for lineIndex := range fileUpdate.Lines { + if fileUpdate.Lines[lineIndex] != expectedLines[lineIndex] { + t.Errorf("expected: %s; got: %s", expectedLines[lineIndex], fileUpdate.Lines[lineIndex]) + } + } +} diff --git a/release.sh b/release.sh index 87ec98e..fa18e77 100644 --- a/release.sh +++ b/release.sh @@ -1,13 +1,14 @@ #!/bin/bash -tag=$(git describe --tags) +releasePath="release" +mkdir -p releasePath -mkdir -p release/$tag +GOOS=windows GOARCH=386 go build -o $releasePath/rectail.exe ./cmd/rectail +GOOS=windows GOARCH=amd64 go build -o $releasePath/rectail64.exe ./cmd/rectail -GOOS=windows GOARCH=386 go build -o release/$tag/rectail.exe ./cmd/rectail -GOOS=windows GOARCH=amd64 go build -o release/$tag/rectail64.exe ./cmd/rectail +GOOS=linux GOARCH=386 go build -o $releasePath/rectail_linux ./cmd/rectail +GOOS=linux GOARCH=amd64 go build -o $releasePath/rectail64_linux ./cmd/rectail -GOOS=linux GOARCH=386 go build -o release/$tag/rectail_linux ./cmd/rectail -GOOS=linux GOARCH=amd64 go build -o release/$tag/rectail64_linux ./cmd/rectail +GOOS=darwin GOARCH=amd64 go build -o $releasePath/rectail_osx ./cmd/rectail -GOOS=darwin GOARCH=amd64 go build -o release/$tag/rectail_osx ./cmd/rectail \ No newline at end of file +echo "generated" \ No newline at end of file