aboutsummaryrefslogtreecommitdiff
path: root/watcher_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-09 00:17:53 +0700
committerShulhan <ms@kilabit.info>2023-11-09 20:27:23 +0700
commit59304a33928badf1a2b1e2aa7510bfac58441c0e (patch)
tree69195db3dc09dad2dde8518a5038779d9e3ada01 /watcher_test.go
parent80c4242daca3ae106dcf1fccba37b6bf73598558 (diff)
downloadciigo-59304a33928badf1a2b1e2aa7510bfac58441c0e.tar.xz
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.
Diffstat (limited to 'watcher_test.go')
-rw-r--r--watcher_test.go28
1 files 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)