Skip to content

Commit

Permalink
Use temp directory for tests, instead of relative (#29)
Browse files Browse the repository at this point in the history
* Use temp directory for tests, instead of relative

Since the Go module cache is read-only by default, modifications in the
module directory cause failures for dependents running `go test all`.

* Make test dir cleanup Go 1.14-friendly

* Make test dir cleanup Go 1.11-friendly

Co-authored-by: nxadm <nxadm@users.noreply.github.com>
  • Loading branch information
ches and nxadm authored Nov 2, 2021
1 parent da54e9f commit 6327674
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.idea/
.test/
examples/_*
examples/_*
78 changes: 46 additions & 32 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ import (
"github.com/nxadm/tail/watch"
)

func init() {
// Clear the temporary test directory
err := os.RemoveAll(".test")
if err != nil {
panic(err)
}
}

func TestTailFile(t *testing.T) {
t.SkipNow()
}
Expand Down Expand Up @@ -75,7 +67,8 @@ func TestMustExist(t *testing.T) {
}

func TestWaitsForFileToExist(t *testing.T) {
tailTest := NewTailTest("waits-for-file-to-exist", t)
tailTest, cleanup := NewTailTest("waits-for-file-to-exist", t)
defer cleanup()
tail := tailTest.StartTail("test.txt", Config{})
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)

Expand All @@ -85,7 +78,8 @@ func TestWaitsForFileToExist(t *testing.T) {
}

func TestWaitsForFileToExistRelativePath(t *testing.T) {
tailTest := NewTailTest("waits-for-file-to-exist-relative", t)
tailTest, cleanup := NewTailTest("waits-for-file-to-exist-relative", t)
defer cleanup()

oldWD, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -120,7 +114,8 @@ func TestStop(t *testing.T) {
}

func TestStopNonEmptyFile(t *testing.T) {
tailTest := NewTailTest("maxlinesize", t)
tailTest, cleanup := NewTailTest("maxlinesize", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nthere\nworld\n")
tail := tailTest.StartTail("test.txt", Config{})
tail.Stop()
Expand All @@ -129,7 +124,8 @@ func TestStopNonEmptyFile(t *testing.T) {
}

func TestStopAtEOF(t *testing.T) {
tailTest := NewTailTest("maxlinesize", t)
tailTest, cleanup := NewTailTest("maxlinesize", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nthere\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})

Expand Down Expand Up @@ -158,7 +154,8 @@ func TestMaxLineSizeNoFollow(t *testing.T) {
}

func TestOver4096ByteLine(t *testing.T) {
tailTest := NewTailTest("Over4096ByteLine", t)
tailTest, cleanup := NewTailTest("Over4096ByteLine", t)
defer cleanup()
testString := strings.Repeat("a", 4097)
tailTest.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})
Expand All @@ -172,7 +169,8 @@ func TestOver4096ByteLine(t *testing.T) {
}

func TestOver4096ByteLineWithSetMaxLineSize(t *testing.T) {
tailTest := NewTailTest("Over4096ByteLineMaxLineSize", t)
tailTest, cleanup := NewTailTest("Over4096ByteLineMaxLineSize", t)
defer cleanup()
testString := strings.Repeat("a", 4097)
tailTest.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil, MaxLineSize: 4097})
Expand All @@ -187,7 +185,8 @@ func TestOver4096ByteLineWithSetMaxLineSize(t *testing.T) {

func TestReOpenWithCursor(t *testing.T) {
delay := 300 * time.Millisecond // account for POLL_DURATION
tailTest := NewTailTest("reopen-cursor", t)
tailTest, cleanup := NewTailTest("reopen-cursor", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail(
"test.txt",
Expand Down Expand Up @@ -220,7 +219,8 @@ func TestReOpenWithCursor(t *testing.T) {
}

func TestLocationFull(t *testing.T) {
tailTest := NewTailTest("location-full", t)
tailTest, cleanup := NewTailTest("location-full", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
Expand All @@ -233,7 +233,8 @@ func TestLocationFull(t *testing.T) {
}

func TestLocationFullDontFollow(t *testing.T) {
tailTest := NewTailTest("location-full-dontfollow", t)
tailTest, cleanup := NewTailTest("location-full-dontfollow", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: false, Location: nil})
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
Expand All @@ -247,7 +248,8 @@ func TestLocationFullDontFollow(t *testing.T) {
}

func TestLocationEnd(t *testing.T) {
tailTest := NewTailTest("location-end", t)
tailTest, cleanup := NewTailTest("location-end", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{0, io.SeekEnd}})
go tailTest.VerifyTailOutput(tail, []string{"more", "data"}, false)
Expand All @@ -264,7 +266,8 @@ func TestLocationEnd(t *testing.T) {

func TestLocationMiddle(t *testing.T) {
// Test reading from middle.
tailTest := NewTailTest("location-middle", t)
tailTest, cleanup := NewTailTest("location-middle", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{-6, io.SeekEnd}})
go tailTest.VerifyTailOutput(tail, []string{"world", "more", "data"}, false)
Expand Down Expand Up @@ -302,7 +305,8 @@ func TestReSeekPolling(t *testing.T) {
}

func TestReSeekWithCursor(t *testing.T) {
tailTest := NewTailTest("reseek-cursor", t)
tailTest, cleanup := NewTailTest("reseek-cursor", t)
defer cleanup()
tailTest.CreateFile("test.txt", "a really long string goes here\nhello\nworld\n")
tail := tailTest.StartTail(
"test.txt",
Expand All @@ -327,7 +331,8 @@ func TestReSeekWithCursor(t *testing.T) {
}

func TestRateLimiting(t *testing.T) {
tailTest := NewTailTest("rate-limiting", t)
tailTest, cleanup := NewTailTest("rate-limiting", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\nagain\nextra\n")
config := Config{
Follow: true,
Expand Down Expand Up @@ -355,7 +360,8 @@ func TestRateLimiting(t *testing.T) {
}

func TestTell(t *testing.T) {
tailTest := NewTailTest("tell-position", t)
tailTest, cleanup := NewTailTest("tell-position", t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\nagain\nmore\n")
config := Config{
Follow: false,
Expand Down Expand Up @@ -394,7 +400,8 @@ func TestTell(t *testing.T) {
}

func TestBlockUntilExists(t *testing.T) {
tailTest := NewTailTest("block-until-file-exists", t)
tailTest, cleanup := NewTailTest("block-until-file-exists", t)
defer cleanup()
config := Config{
Follow: true,
}
Expand All @@ -416,7 +423,8 @@ func TestBlockUntilExists(t *testing.T) {
}

func maxLineSize(t *testing.T, follow bool, fileContent string, expected []string) {
tailTest := NewTailTest("maxlinesize", t)
tailTest, cleanup := NewTailTest("maxlinesize", t)
defer cleanup()
tailTest.CreateFile("test.txt", fileContent)
tail := tailTest.StartTail("test.txt", Config{Follow: follow, Location: nil, MaxLineSize: 3})
go tailTest.VerifyTailOutput(tail, expected, false)
Expand All @@ -438,7 +446,8 @@ func reOpen(t *testing.T, poll bool) {
name = "reopen-inotify"
delay = 100 * time.Millisecond
}
tailTest := NewTailTest(name, t)
tailTest, cleanup := NewTailTest(name, t)
defer cleanup()
tailTest.CreateFile("test.txt", "hello\nworld\n")
tail := tailTest.StartTail(
"test.txt",
Expand Down Expand Up @@ -479,7 +488,8 @@ func reOpen(t *testing.T, poll bool) {
}

func TestInotify_WaitForCreateThenMove(t *testing.T) {
tailTest := NewTailTest("wait-for-create-then-reopen", t)
tailTest, cleanup := NewTailTest("wait-for-create-then-reopen", t)
defer cleanup()
os.Remove(tailTest.path + "/test.txt") // Make sure the file does NOT exist.

tail := tailTest.StartTail(
Expand Down Expand Up @@ -512,7 +522,8 @@ func reSeek(t *testing.T, poll bool) {
} else {
name = "reseek-inotify"
}
tailTest := NewTailTest(name, t)
tailTest, cleanup := NewTailTest(name, t)
defer cleanup()
tailTest.CreateFile("test.txt", "a really long string goes here\nhello\nworld\n")
tail := tailTest.StartTail(
"test.txt",
Expand Down Expand Up @@ -545,14 +556,17 @@ type TailTest struct {
*testing.T
}

func NewTailTest(name string, t *testing.T) TailTest {
tt := TailTest{name, ".test/" + name, make(chan struct{}), t}
err := os.MkdirAll(tt.path, os.ModeTemporary|0700)
func NewTailTest(name string, t *testing.T) (TailTest, func()) {
testdir, err := ioutil.TempDir("", "tail-test-" + name)
if err != nil {
tt.Fatal(err)
t.Fatal(err)
}

return tt
return TailTest{name, testdir, make(chan struct{}), t}, func() {
if err := os.RemoveAll(testdir); err != nil {
t.Logf("failed to remove test directory: %v", testdir)
}
}
}

func (t TailTest) CreateFile(name string, contents string) {
Expand Down

0 comments on commit 6327674

Please sign in to comment.