diff options
| author | Shulhan <ms@kilabit.info> | 2021-02-21 18:58:31 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-02-21 18:58:31 +0700 |
| commit | e554256948d0f7b4b71dfa78014e2172f11fc1c6 (patch) | |
| tree | 93218f03ff20dfee0426b8013ef1d236bf3a4645 /watcher_test.go | |
| parent | 251865e6abbaf1f1688d91b0de71aa745ea3042c (diff) | |
| download | ciigo-e554256948d0f7b4b71dfa78014e2172f11fc1c6.tar.xz | |
all: implement Watch functionality
The Watch function, watch any changes on asciidoc files on directory
"dir" recursively and changes on the HTML template file.
If there is new or modified asciidoc files it will convert them into HTML
files using HTML template automatically.
If the HTML template file modified, it will re-convert all asciidoc files.
If the HTML template file deleted, it will replace them with internal,
default HTML template.
Diffstat (limited to 'watcher_test.go')
| -rw-r--r-- | watcher_test.go | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/watcher_test.go b/watcher_test.go new file mode 100644 index 0000000..043266a --- /dev/null +++ b/watcher_test.go @@ -0,0 +1,152 @@ +// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package ciigo + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/shuLhan/share/lib/test" +) + +var ( + testWatcher *watcher + testFileAdoc string + testAdocFile *os.File +) + +func TestWatcher(t *testing.T) { + testDir := "testdata/watcher" + + err := os.MkdirAll(testDir, 0700) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { + os.RemoveAll(testDir) + }) + + testWatcher, err = newWatcher(testDir, "testdata/html.tmpl") + if err != nil { + t.Fatal(err) + } + + err = testWatcher.start() + if err != nil { + t.Fatal(err) + } + + t.Run("createAdocFile", testCreate) + t.Run("updateAdocFile", testUpdate) + t.Run("deleteAdocFile", testDelete) +} + +func testCreate(t *testing.T) { + var ( + err error + ) + testFileAdoc = filepath.Join(testWatcher.dir, "index.adoc") + testAdocFile, err = os.Create(testFileAdoc) + if err != nil { + t.Fatal(err) + } + + got := waitChanges() + + test.Assert(t, "New adoc file created", testFileAdoc, got.path, true) + + expBody := ` +<div id="header"> +<div class="details"> +</div> +</div> +<div id="content"> +<div id="preamble"> +<div class="sectionbody"> +</div> +</div> +</div>` + gotBody := removeFooter(string(got.fhtml.Body)) + test.Assert(t, "HTML body", expBody, gotBody, true) +} + +func testUpdate(t *testing.T) { + _, err := testAdocFile.WriteString("= Hello") + if err != nil { + t.Fatal(err) + } + err = testAdocFile.Sync() + if err != nil { + t.Fatal(err) + } + + got := waitChanges() + test.Assert(t, "adoc file updated", testFileAdoc, got.path, true) + + expBody := ` +<div id="header"> +<h1>Hello</h1> +<div class="details"> +</div> +</div> +<div id="content"> +<div id="preamble"> +<div class="sectionbody"> +</div> +</div> +</div>` + gotBody := removeFooter(string(got.fhtml.Body)) + test.Assert(t, "HTML body", expBody, gotBody, true) +} + +func testDelete(t *testing.T) { + err := testAdocFile.Close() + if err != nil { + t.Fatal(err) + } + + err = os.Remove(testFileAdoc) + if err != nil { + t.Fatal(err) + } + + got := waitChanges() + test.Assert(t, "adoc file updated", testFileAdoc, got.path, true) + + _, gotIsExist := testWatcher.fileMarkups[testFileAdoc] + test.Assert(t, "adoc file deleted", false, gotIsExist, true) +} + +// +// removeFooter remove the footer from generated HTML. The footer is 4 lines +// at the bottom. +// +func removeFooter(in string) string { + lines := strings.Split(in, "\n") + n := len(lines) + if n > 5 { + lines = lines[:n-5] + } + return strings.Join(lines, "\n") +} + +func waitChanges() (fmarkup *fileMarkup) { + var ( + ok bool + ) + + for { + time.Sleep(5000) + fmarkup, ok = testWatcher.changes.Pop().(*fileMarkup) + if ok { + break + } + } + return fmarkup +} |
