diff options
| author | Shulhan <ms@kilabit.info> | 2023-05-14 18:06:01 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-05-14 18:06:01 +0700 |
| commit | d6553f938300943c37d576da81ea7531139f3311 (patch) | |
| tree | 8d47cf9a079fdc06c966288f54878139e86fe9a9 | |
| parent | 3df554eb34b449dd5fad0239eb738beebe1debbb (diff) | |
| download | ciigo-d6553f938300943c37d576da81ea7531139f3311.tar.xz | |
all: rewrite watcher tests using lib/test.Data
Instead of using another template, use the embedded template when
generating HTML.
In this way we can see how the generated HTML looks like.
| -rw-r--r-- | .gitignore | 8 | ||||
| -rw-r--r-- | ciigo_test.go | 10 | ||||
| -rw-r--r-- | template_index_html.go | 16 | ||||
| -rw-r--r-- | testdata/html.tmpl | 5 | ||||
| -rw-r--r-- | testdata/watcher_test.txt | 151 | ||||
| -rw-r--r-- | watcher.go | 13 | ||||
| -rw-r--r-- | watcher_test.go | 143 |
7 files changed, 235 insertions, 111 deletions
@@ -1,8 +1,10 @@ ## SPDX-FileCopyrightText: 2019 Shulhan <ms@kilabit.info> ## SPDX-License-Identifier: GPL-3.0-or-later +*.html +/_bin /ciigo /ciigo-example -/_bin -*.html -cover.out +/testdata/watcher/test.adoc +/testdata/watcher/test.md cover.html +cover.out diff --git a/ciigo_test.go b/ciigo_test.go index 1efb6f4..02eb9a2 100644 --- a/ciigo_test.go +++ b/ciigo_test.go @@ -4,6 +4,8 @@ package ciigo import ( + "html/template" + "os" "regexp" "sort" "testing" @@ -11,6 +13,14 @@ import ( "github.com/shuLhan/share/lib/test" ) +func TestMain(m *testing.M) { + var templateCss = template.CSS(`body{}`) + + _embeddedCSS = &templateCss + + os.Exit(m.Run()) +} + func TestListFileMarkups(t *testing.T) { type testCase struct { excRegex string diff --git a/template_index_html.go b/template_index_html.go index 5dad59b..8b43083 100644 --- a/template_index_html.go +++ b/template_index_html.go @@ -9,16 +9,18 @@ const templateIndexHTML = `<!DOCTYPE html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="theme-color" content="#375EAB"> - {{range $k, $v := .Metadata -}} +{{- range $k, $v := .Metadata}} <meta name="{{$k}}" content="{{$v}}"> - {{end -}} +{{- end}} <title>{{.Title}}</title> +{{- if .EmbeddedCSS}} <style> - {{- .EmbeddedCSS -}} + {{.EmbeddedCSS}} </style> - {{- range .Styles}} +{{- end}} +{{- range .Styles}} <link rel="stylesheet" href="{{.}}"> - {{- end}} +{{- end}} </head> <body> <div class="topbar"> @@ -33,13 +35,11 @@ const templateIndexHTML = `<!DOCTYPE html> </div> </div> </div> - <div class="page"> <div class="container"> - {{- .Body -}} +{{.Body}} </div> </div> - <div class="footer"> Powered by <a href="https://git.sr.ht/~shulhan/ciigo" diff --git a/testdata/html.tmpl b/testdata/html.tmpl deleted file mode 100644 index 967be5e..0000000 --- a/testdata/html.tmpl +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE> -<html> -<head><title>{{.Title}}</title></head> -<body>{{.Body}}</body> -</html> diff --git a/testdata/watcher_test.txt b/testdata/watcher_test.txt index a1937b9..dc21a06 100644 --- a/testdata/watcher_test.txt +++ b/testdata/watcher_test.txt @@ -1,3 +1,85 @@ +>>> create.adoc +# watch create + +<<< create.adoc.html +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="theme-color" content="#375EAB"> + <meta name="generator" content="asciidoctor-go 0.4.1"> + <title>watch create</title> + <style> + body{} + </style> + </head> + <body> + <div class="topbar"> + <div class="container"> + <div class="top-heading"> + <a href="/">watch create</a> + </div> + <div class="menu"> + <form class="item" action="/_internal/search"> + <input type="text" name="q" placeholder="Search" /> + </form> + </div> + </div> + </div> + <div class="page"> + <div class="container"> +<div id="header"> +<h1>watch create</h1> +</div> +<div id="content"> +</div> +<div id="footer"> + +>>> update.adoc +# watch updated +:stylesheet: /path/to/style.css +:keywords: ciigo,asciidoc + +Hello, body. + +<<< update.adoc.html +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="theme-color" content="#375EAB"> + <meta name="generator" content="asciidoctor-go 0.4.1"> + <meta name="keywords" content="ciigo,asciidoc"> + <title>watch updated</title> + <link rel="stylesheet" href="/path/to/style.css"> + </head> + <body> + <div class="topbar"> + <div class="container"> + <div class="top-heading"> + <a href="/">watch updated</a> + </div> + <div class="menu"> + <form class="item" action="/_internal/search"> + <input type="text" name="q" placeholder="Search" /> + </form> + </div> + </div> + </div> + <div class="page"> + <div class="container"> +<div id="header"> +<h1>watch updated</h1> +</div> +<div id="content"> +<div class="paragraph"> +<p>Hello, body.</p> +</div> +</div> +<div id="footer"> + >>> create.md --- Title: a title @@ -5,12 +87,37 @@ Title: a title # watch create <<< create.md.html -<!DOCTYPE> +<!DOCTYPE html> <html> -<head><title>a title</title></head> -<body><h1>watch create</h1> -</body> -</html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="theme-color" content="#375EAB"> + <title>a title</title> + <style> + body{} + </style> + </head> + <body> + <div class="topbar"> + <div class="container"> + <div class="top-heading"> + <a href="/">a title</a> + </div> + <div class="menu"> + <form class="item" action="/_internal/search"> + <input type="text" name="q" placeholder="Search" /> + </form> + </div> + </div> + </div> + <div class="page"> + <div class="container"> +<h1>watch create</h1> + + </div> + </div> + <div class="footer"> >>> update.md --- @@ -21,9 +128,33 @@ keywords: ciigo,markdown # watch updated <<< update.md.html -<!DOCTYPE> +<!DOCTYPE html> <html> -<head><title>A new title</title></head> -<body><h1>watch updated</h1> -</body> -</html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="theme-color" content="#375EAB"> + <meta name="keywords" content="ciigo,markdown"> + <title>A new title</title> + <link rel="stylesheet" href="/path/to/style.css"> + </head> + <body> + <div class="topbar"> + <div class="container"> + <div class="top-heading"> + <a href="/">A new title</a> + </div> + <div class="menu"> + <form class="item" action="/_internal/search"> + <input type="text" name="q" placeholder="Search" /> + </form> + </div> + </div> + </div> + <div class="page"> + <div class="container"> +<h1>watch updated</h1> + + </div> + </div> + <div class="footer"> @@ -197,3 +197,16 @@ func (w *watcher) watchHtmlTemplate() { w.converter.convertFileMarkups(w.fileMarkups, true) } } + +// waitChanges wait for changes on file markup and return it. +func (w *watcher) waitChanges() (fmarkup *FileMarkup) { + var ok bool + + for { + fmarkup, ok = w.changes.Pop().(*FileMarkup) + if ok { + break + } + } + return fmarkup +} diff --git a/watcher_test.go b/watcher_test.go index d527309..331e2c5 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -13,12 +13,6 @@ import ( "github.com/shuLhan/share/lib/test" ) -var ( - testWatcher *watcher - testFileAdoc string - testAdocFile *os.File -) - func TestWatcher(t *testing.T) { var ( testDir = `testdata/watcher` @@ -44,7 +38,7 @@ func TestWatcher(t *testing.T) { os.RemoveAll(testDir) }) - converter, err = NewConverter(`testdata/html.tmpl`) + converter, err = NewConverter(``) if err != nil { t.Fatal(err) } @@ -54,6 +48,8 @@ func TestWatcher(t *testing.T) { t.Fatal(err) } + var testWatcher *watcher + testWatcher, err = newWatcher(converter, &convertOpts) if err != nil { t.Fatal(err) @@ -71,127 +67,118 @@ func TestWatcher(t *testing.T) { t.Fatal(err) } - t.Run(`createAdocFile`, testCreate) - t.Run(`updateAdocFile`, testUpdate) - t.Run(`deleteAdocFile`, testDelete) + var pathFileMarkup = filepath.Join(testWatcher.dir, `test.adoc`) - var pathFileMarkdown = filepath.Join(testWatcher.dir, `test.md`) + t.Run(`testAdocCreate`, func(tt *testing.T) { + testAdocCreate(tt, testWatcher, tdata, pathFileMarkup) + }) + t.Run(`testAdocUpdate`, func(tt *testing.T) { + testAdocUpdate(tt, testWatcher, tdata, pathFileMarkup) + }) + t.Run(`testAdocDelete`, func(tt *testing.T) { + testAdocDelete(tt, testWatcher, pathFileMarkup) + }) + + pathFileMarkup = filepath.Join(testWatcher.dir, `test.md`) t.Run(`testMarkdownCreate`, func(tt *testing.T) { - testMarkdownCreate(tt, tdata, pathFileMarkdown) + testMarkdownCreate(tt, testWatcher, tdata, pathFileMarkup) }) t.Run(`testMarkdownUpdate`, func(tt *testing.T) { - testMarkdownUpdate(tt, tdata, pathFileMarkdown) + testMarkdownUpdate(tt, testWatcher, tdata, pathFileMarkup) }) t.Run(`testMarkdownDelete`, func(tt *testing.T) { - testMarkdownDelete(tt, pathFileMarkdown) + testMarkdownDelete(tt, testWatcher, pathFileMarkup) }) } -func testCreate(t *testing.T) { +func testAdocCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFile string) { var ( + expBody []byte = tdata.Input[`create.adoc`] + got *FileMarkup err error - expBody string gotBody []byte ) - testFileAdoc = filepath.Join(testWatcher.dir, `index.adoc`) - // 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) - testAdocFile, err = os.Create(testFileAdoc) + err = os.WriteFile(pathFile, expBody, 0600) if err != nil { t.Fatal(err) } - got = waitChanges() - - test.Assert(t, `New adoc file created`, testFileAdoc, got.path) + got = testWatcher.waitChanges() - expBody = `<!DOCTYPE> -<html> -<head><title></title></head> -<body><div id="header"> -</div> -<div id="content"> -</div>` + test.Assert(t, `New adoc file created`, pathFile, got.path) gotBody, err = os.ReadFile(got.pathHtml) if err != nil { t.Fatal(err) } - gotBody = removeFooter(gotBody) - test.Assert(t, `HTML body`, expBody, string(gotBody)) + gotBody = removeFooter(gotBody, 15) + expBody = tdata.Output[`create.adoc.html`] + + test.Assert(t, `HTML body`, string(expBody), string(gotBody)) } -func testUpdate(t *testing.T) { +func testAdocUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFile string) { var ( + expBody = tdata.Input[`update.adoc`] + + got *FileMarkup err error - expBody string gotBody []byte - got *FileMarkup ) - _, err = testAdocFile.WriteString(`= Hello`) - if err != nil { - t.Fatal(err) - } - err = testAdocFile.Sync() + // 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(pathFile, expBody, 0600) if err != nil { t.Fatal(err) } - got = waitChanges() - test.Assert(t, `adoc file updated`, testFileAdoc, got.path) + got = testWatcher.waitChanges() - expBody = `<!DOCTYPE> -<html> -<head><title>Hello</title></head> -<body><div id="header"> -<h1>Hello</h1> -</div> -<div id="content"> -</div>` + test.Assert(t, `adoc file updated`, pathFile, got.path) gotBody, err = os.ReadFile(got.pathHtml) if err != nil { t.Fatal(err) } - gotBody = removeFooter(gotBody) + gotBody = removeFooter(gotBody, 15) + expBody = tdata.Output[`update.adoc.html`] - test.Assert(t, `HTML body`, expBody, string(gotBody)) + test.Assert(t, `HTML body`, string(expBody), string(gotBody)) } -func testDelete(t *testing.T) { +func testAdocDelete(t *testing.T, testWatcher *watcher, pathFile string) { var ( err error got *FileMarkup gotIsExist bool ) - err = testAdocFile.Close() + err = os.Remove(pathFile) if err != nil { t.Fatal(err) } - err = os.Remove(testFileAdoc) - if err != nil { - t.Fatal(err) - } + got = testWatcher.waitChanges() - got = waitChanges() - test.Assert(t, `adoc file updated`, testFileAdoc, got.path) + test.Assert(t, `adoc file updated`, pathFile, got.path) - _, gotIsExist = testWatcher.fileMarkups[testFileAdoc] + _, gotIsExist = testWatcher.fileMarkups[pathFile] test.Assert(t, `adoc file deleted`, false, gotIsExist) } -func testMarkdownCreate(t *testing.T, tdata *test.Data, pathFileMarkdown string) { +func testMarkdownCreate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFileMarkdown string) { var ( body = tdata.Input[`create.md`] @@ -210,7 +197,7 @@ func testMarkdownCreate(t *testing.T, tdata *test.Data, pathFileMarkdown string) t.Fatal(err) } - got = waitChanges() + got = testWatcher.waitChanges() test.Assert(t, `New md file created`, pathFileMarkdown, got.path) @@ -218,13 +205,13 @@ func testMarkdownCreate(t *testing.T, tdata *test.Data, pathFileMarkdown string) if err != nil { t.Fatal(err) } - gotBody = removeFooter(gotBody) + gotBody = removeFooter(gotBody, 8) 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) { +func testMarkdownUpdate(t *testing.T, testWatcher *watcher, tdata *test.Data, pathFileMarkdown string) { var ( body = tdata.Input[`update.md`] @@ -243,7 +230,7 @@ func testMarkdownUpdate(t *testing.T, tdata *test.Data, pathFileMarkdown string) t.Fatal(err) } - got = waitChanges() + got = testWatcher.waitChanges() test.Assert(t, `changes path`, pathFileMarkdown, got.path) @@ -251,13 +238,13 @@ func testMarkdownUpdate(t *testing.T, tdata *test.Data, pathFileMarkdown string) if err != nil { t.Fatal(err) } - gotBody = removeFooter(gotBody) + gotBody = removeFooter(gotBody, 8) expBody = string(tdata.Output[`update.md.html`]) test.Assert(t, `HTML body`, expBody, string(gotBody)) } -func testMarkdownDelete(t *testing.T, pathFileMarkdown string) { +func testMarkdownDelete(t *testing.T, testWatcher *watcher, pathFileMarkdown string) { var ( err error got *FileMarkup @@ -269,7 +256,7 @@ func testMarkdownDelete(t *testing.T, pathFileMarkdown string) { t.Fatal(err) } - got = waitChanges() + got = testWatcher.waitChanges() test.Assert(t, `md file updated`, pathFileMarkdown, got.path) _, gotIsExist = testWatcher.fileMarkups[pathFileMarkdown] @@ -278,28 +265,14 @@ func testMarkdownDelete(t *testing.T, pathFileMarkdown string) { // removeFooter remove the footer from generated HTML since its contains date // and time that changes during test. -func removeFooter(in []byte) (out []byte) { +func removeFooter(in []byte, nlast int) (out []byte) { var ( lines = bytes.Split(in, []byte("\n")) n = len(lines) ) - if n > 7 { - lines = lines[:n-7] + if n > nlast { + lines = lines[:n-nlast] } out = bytes.Join(lines, []byte("\n")) return out } - -func waitChanges() (fmarkup *FileMarkup) { - var ( - ok bool - ) - - for { - fmarkup, ok = testWatcher.changes.Pop().(*FileMarkup) - if ok { - break - } - } - return fmarkup -} |
