From 59304a33928badf1a2b1e2aa7510bfac58441c0e Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 9 Nov 2023 00:17:53 +0700 Subject: all: simplify test for Watcher by using TempDir By using TempDir we did not need to remove and create directory when the testing started. While at it, set the default DirWatcher delay to 100ms and decrease each "time.Sleep" duration on tests to make test finish faster. --- watcher_test.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/watcher_test.go b/watcher_test.go index 6136afc..7aa7d22 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -15,7 +15,7 @@ import ( func TestWatcher(t *testing.T) { var ( - testDir = `testdata/watcher` + testDir = t.TempDir() convertOpts = ConvertOptions{ Root: testDir, } @@ -24,20 +24,6 @@ func TestWatcher(t *testing.T) { err error ) - err = os.RemoveAll(testDir) - if err != nil { - t.Logf(err.Error()) - } - - err = os.MkdirAll(testDir, 0700) - if err != nil { - t.Fatal(err) - } - - t.Cleanup(func() { - os.RemoveAll(testDir) - }) - converter, err = NewConverter(``) if err != nil { t.Fatal(err) @@ -55,6 +41,8 @@ func TestWatcher(t *testing.T) { t.Fatal(err) } + testWatcher.watchDir.Delay = 100 * time.Millisecond + err = testWatcher.start() if err != nil { t.Fatal(err) @@ -105,7 +93,7 @@ func testAdocCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFi // Let the OS sync the file system before we create new file, // otherwise the modtime for fs.Root does not changes. - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) err = os.WriteFile(pathFile, expBody, 0600) if err != nil { @@ -138,7 +126,7 @@ func testAdocUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFi // Let the OS sync the file system before we create new file, // otherwise the modtime for fs.Root does not changes. - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) err = os.WriteFile(pathFile, expBody, 0600) if err != nil { @@ -192,7 +180,7 @@ func testMarkdownCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pa // Let the OS sync the file system before we create new file, // otherwise the modtime for fs.Root does not changes. - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) err = os.WriteFile(pathFileMarkdown, body, 0600) if err != nil { @@ -225,7 +213,7 @@ func testMarkdownUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pa // Let the OS sync the file system before we create new file, // otherwise the modtime for fs.Root does not changes. - time.Sleep(1 * time.Second) + time.Sleep(100 * time.Millisecond) err = os.WriteFile(pathFileMarkdown, body, 0600) if err != nil { @@ -253,6 +241,8 @@ func testMarkdownDelete(t *testing.T, testWatcher *watcher, pathFileMarkdown str gotIsExist bool ) + time.Sleep(100 * time.Millisecond) + err = os.Remove(pathFileMarkdown) if err != nil { t.Fatal(err) -- cgit v1.3