diff options
| author | Shulhan <ms@kilabit.info> | 2023-05-14 17:06:21 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-05-14 17:13:56 +0700 |
| commit | fad7cd21134a4cac75f876bce866830c35042f8e (patch) | |
| tree | dd55dd5c6edf2b5bfb8f0bf7d6e1cb12b15712eb /watcher_test.go | |
| parent | c109a3dd026cf6fbdc147d5a2c17535569d8964d (diff) | |
| download | ciigo-fad7cd21134a4cac75f876bce866830c35042f8e.tar.xz | |
all: bring back support for Markdown
I use two remote repositories: GitHub and SourceHut.
GitHub support rendering README using asciidoc while SourceHut not.
This cause the repository that use README.adoc rendered as text in
SourceHut which make the repository page less readable.
Also, the pkg.go.dev now render README but only support Markdown.
Since we cannot control the SourceHut and go.dev, the only option is
to support converting Markdown in ciigo so I can write README using
Markdown and the rest of documentation using Asciidoc.
Diffstat (limited to 'watcher_test.go')
| -rw-r--r-- | watcher_test.go | 114 |
1 files changed, 109 insertions, 5 deletions
diff --git a/watcher_test.go b/watcher_test.go index 6b16223..d527309 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -64,14 +64,33 @@ func TestWatcher(t *testing.T) { t.Fatal(err) } + var tdata *test.Data + + tdata, err = test.LoadData(`testdata/watcher_test.txt`) + if err != nil { + t.Fatal(err) + } + t.Run(`createAdocFile`, testCreate) t.Run(`updateAdocFile`, testUpdate) t.Run(`deleteAdocFile`, testDelete) + + var pathFileMarkdown = filepath.Join(testWatcher.dir, `test.md`) + + t.Run(`testMarkdownCreate`, func(tt *testing.T) { + testMarkdownCreate(tt, tdata, pathFileMarkdown) + }) + t.Run(`testMarkdownUpdate`, func(tt *testing.T) { + testMarkdownUpdate(tt, tdata, pathFileMarkdown) + }) + t.Run(`testMarkdownDelete`, func(tt *testing.T) { + testMarkdownDelete(tt, pathFileMarkdown) + }) } func testCreate(t *testing.T) { var ( - got *fileMarkup + got *FileMarkup err error expBody string gotBody []byte @@ -114,7 +133,7 @@ func testUpdate(t *testing.T) { err error expBody string gotBody []byte - got *fileMarkup + got *FileMarkup ) _, err = testAdocFile.WriteString(`= Hello`) @@ -151,7 +170,7 @@ func testUpdate(t *testing.T) { func testDelete(t *testing.T) { var ( err error - got *fileMarkup + got *FileMarkup gotIsExist bool ) @@ -172,6 +191,91 @@ func testDelete(t *testing.T) { test.Assert(t, `adoc file deleted`, false, gotIsExist) } +func testMarkdownCreate(t *testing.T, tdata *test.Data, pathFileMarkdown string) { + var ( + body = tdata.Input[`create.md`] + + got *FileMarkup + err error + expBody string + gotBody []byte + ) + + // 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) + + err = os.WriteFile(pathFileMarkdown, body, 0600) + if err != nil { + t.Fatal(err) + } + + got = waitChanges() + + test.Assert(t, `New md file created`, pathFileMarkdown, got.path) + + gotBody, err = os.ReadFile(got.pathHtml) + if err != nil { + t.Fatal(err) + } + gotBody = removeFooter(gotBody) + + expBody = string(tdata.Output[`create.md.html`]) + test.Assert(t, `HTML body`, expBody, string(gotBody)) +} + +func testMarkdownUpdate(t *testing.T, tdata *test.Data, pathFileMarkdown string) { + var ( + body = tdata.Input[`update.md`] + + got *FileMarkup + err error + expBody string + gotBody []byte + ) + + // 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) + + err = os.WriteFile(pathFileMarkdown, body, 0600) + if err != nil { + t.Fatal(err) + } + + got = waitChanges() + + test.Assert(t, `changes path`, pathFileMarkdown, got.path) + + gotBody, err = os.ReadFile(got.pathHtml) + if err != nil { + t.Fatal(err) + } + gotBody = removeFooter(gotBody) + + expBody = string(tdata.Output[`update.md.html`]) + test.Assert(t, `HTML body`, expBody, string(gotBody)) +} + +func testMarkdownDelete(t *testing.T, pathFileMarkdown string) { + var ( + err error + got *FileMarkup + gotIsExist bool + ) + + err = os.Remove(pathFileMarkdown) + if err != nil { + t.Fatal(err) + } + + got = waitChanges() + test.Assert(t, `md file updated`, pathFileMarkdown, got.path) + + _, gotIsExist = testWatcher.fileMarkups[pathFileMarkdown] + test.Assert(t, `md file deleted`, false, gotIsExist) +} + // removeFooter remove the footer from generated HTML since its contains date // and time that changes during test. func removeFooter(in []byte) (out []byte) { @@ -186,13 +290,13 @@ func removeFooter(in []byte) (out []byte) { return out } -func waitChanges() (fmarkup *fileMarkup) { +func waitChanges() (fmarkup *FileMarkup) { var ( ok bool ) for { - fmarkup, ok = testWatcher.changes.Pop().(*fileMarkup) + fmarkup, ok = testWatcher.changes.Pop().(*FileMarkup) if ok { break } |
