commit 76fd1aee668fb1e4dc0de3a7d0b9191a291ac0a0
parent 4d46076dbb639d70ce464fa96c2e1ceae8a32067
Author: gearsix <gearsix@tuta.io>
Date: Mon, 14 Mar 2022 12:07:00 +0000
tidyup: moved all none Test* functions in _test files to pagr_test.go
Since pagr.go doesn't have any functions that require testing I decided
to put all shared setup code in it.
Diffstat:
M | copy_test.go | | | 27 | ++++++++++++++------------- |
M | page_test.go | | | 130 | ++----------------------------------------------------------------------------- |
A | pagr_test.go | | | 171 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | sitemap_test.go | | | 11 | +++-------- |
M | template_test.go | | | 38 | +------------------------------------- |
5 files changed, 191 insertions(+), 186 deletions(-)
diff --git a/copy_test.go b/copy_test.go
@@ -2,33 +2,34 @@ package main
import (
"os"
+ "path/filepath"
"testing"
)
func TestCopyFile(test *testing.T) {
test.Parallel()
- var err error
tdir := test.TempDir()
- src := tdir + "/src"
- dst := tdir + "/dst"
- data := []byte("data")
- if err = os.WriteFile(src, data, 0666); err != nil {
+ src := filepath.Join(tdir, "/src")
+ srcData := []byte("data")
+ dst := filepath.Join(tdir, "/dst")
+
+ if err := os.WriteFile(src, srcData, 0666); err != nil {
test.Error("setup failed, could not write", tdir+"/src")
}
- if err = CopyFile(src, dst); err != nil {
+ if err := CopyFile(src, dst); err != nil {
test.Fatal("CopyFile failed", err)
}
- if _, err = os.Stat(dst); err != nil {
+ if _, err := os.Stat(dst); err != nil {
test.Fatalf("could not stat '%s'", dst)
}
- var buf []byte
- if buf, err = os.ReadFile(dst); err != nil {
+
+ if buf, err := os.ReadFile(dst); err != nil {
test.Errorf("could not read '%s'", dst)
- } else if len(buf) < len(data) {
- test.Fatalf("not all data (%s) copied to '%s' (%s)", data, dst, buf)
- } else if string(buf) != string(data) {
- test.Fatalf("copied data (%s) does not match source (%s)", buf, data)
+ } else if len(buf) < len(srcData) {
+ test.Fatalf("not all srcData (%s) copied to '%s' (%s)", srcData, dst, buf)
+ } else if string(buf) != string(srcData) {
+ test.Fatalf("copied srcData (%s) does not match source (%s)", buf, srcData)
}
}
diff --git a/page_test.go b/page_test.go
@@ -1,7 +1,6 @@
package main
import (
- "fmt"
"notabug.org/gearsix/suti"
"os"
"path/filepath"
@@ -9,134 +8,10 @@ import (
"time"
)
-// helper functions
-func validateContents(t *testing.T, pages []Page, e error) {
- if len(pages) != len(contents) {
- t.Fatalf("invalid number of pages returned (%d should be %d)",
- len(pages), len(contents))
- }
-
- var last, pt time.Time
- for i, p := range pages {
- if len(p.Slug) == 0 && p.Slug != filepath.Base(p.Path) {
- t.Errorf("empty Slug for page: '%s'. Should be '%s'", p.Slug, filepath.Base(p.Path))
- }
- if len(p.Path) == 0 {
- t.Error("empty Path for page:", p)
- }
- // TODO test p.Nav here
- if _, ok := p.Meta["page"]; !ok || len(p.Meta) == 0 {
- t.Errorf("missing page Meta key for page: '%s'", p.Path)
- }
- if _, ok := p.Meta["default"]; !ok || len(p.Meta) == 0 {
- t.Error("empty default Meta key for page:", p.Path)
- }
- if len(p.Contents) == 0 {
- t.Error("empty Contents for page:", p.Path)
- }
- if len(p.Assets) == 0 {
- t.Error("empty Assets for page:", p.Path)
- }
- if pt, e = time.Parse(timefmt, p.Updated); e != nil {
- t.Fatal(e)
- }
-
- if i == 0 {
- last = pt
- } else if pt.Before(last) {
- for _, pp := range pages {
- t.Log(pp.Updated)
- }
- t.Error("Contents Pages returned in wrong order")
- }
- }
-}
-
-var contents = map[string]string{
- ".txt": `p1
-p2
-
- pre1
- pre2
-p3
-
-p4
-`,
- ".html": `<p>p1<br>
-p2</p>
-<pre>pre1
-pre2
-</pre>
-<p>p3</p>
-<p>p4</p>`,
- ".md": `p1
-p2
-
- pre1
- pre2
-
-p3
-`,
- ".gfm": `p1
-p2
-
- pre1
- pre2
-
-p3`,
- ".cm": `p1
-p2
-
- pre1
- pre2
-
-p3`,
-}
-
-var asset = []byte{ // 5x5 black png
- 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00,
- 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
- 0x00, 0x05, 0x01, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xa1, 0xb4, 0xa6,
- 0x00, 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xa5, 0x67, 0xb9, 0xcf, 0x00, 0x00, 0x00, 0x02,
- 0x74, 0x52, 0x4e, 0x53, 0xff, 0x00, 0xe5, 0xb7, 0x30, 0x4a, 0x00,
- 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc4,
- 0x00, 0x00, 0x0e, 0xc4, 0x01, 0x95, 0x2b, 0x0e, 0x1b, 0x00, 0x00,
- 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x08, 0x99, 0x63, 0x60, 0x66,
- 0x60, 0x66, 0x60, 0x00, 0x62, 0x76, 0x00, 0x00, 0x4a, 0x00, 0x11,
- 0x3a, 0x34, 0x8c, 0xad, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
- 0x44, 0xae, 0x42, 0x60, 0x82,
-}
-
-func createProjectContents(dir string) (err error) {
- writef := func(path, data string) {
- if err == nil {
- err = os.WriteFile(path, []byte(data), 0644)
- }
- }
-
- for l, lang := range contentExts {
- if l == 0 {
- writef(fmt.Sprintf("%s/defaults.json", dir), "{ \"default\": \"data\" }")
- } else if l > 1 {
- dir, err = os.MkdirTemp(dir, "page")
- }
- writef(fmt.Sprintf("%s/.page.toml", dir), "page = \"data\"")
- writef(fmt.Sprintf("%s/body%d%s", dir, l, lang), contents[lang])
- writef(fmt.Sprintf("%s/asset.png", dir), string(asset))
-
- if err != nil {
- break
- }
- }
-
- return
-}
-
func TestLoadPagesDir(test *testing.T) {
var err error
tdir := test.TempDir()
- if err = createProjectContents(tdir); err != nil {
+ if err = createTestContents(tdir); err != nil {
test.Errorf("failed to create test content: %s", err)
}
@@ -145,7 +20,7 @@ func TestLoadPagesDir(test *testing.T) {
test.Fatalf("LoadPagesDir failed: %s", err)
}
- validateContents(test, p, err)
+ validateTestPages(test, p, err)
}
func TestMergeMeta(test *testing.T) {
@@ -277,7 +152,6 @@ func TestCopyAssets(test *testing.T) {
func TestBuild(test *testing.T) {
test.Parallel()
- // setup
var err error
tdir := test.TempDir()
p := NewPage("/test", time.Now())
diff --git a/pagr_test.go b/pagr_test.go
@@ -0,0 +1,171 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "time"
+ "path/filepath"
+ "testing"
+)
+
+var templates = map[string]string{ // [ext]template
+ "tmpl": "{{.Contents}}",
+ "hmpl": "{{.Contents}}",
+ "mst": "{{Contents}}",
+}
+
+func createTestTemplates(dir string) (err error) {
+ writef := func(path, data string) {
+ if err == nil {
+ err = os.WriteFile(path, []byte(data), 0644)
+ }
+ }
+
+ for ext, data := range templates {
+ writef(fmt.Sprintf("%s/root.%s", dir, ext), data)
+ writef(fmt.Sprintf("%s/root.ignore.%s", dir, ext), data)
+ writef(fmt.Sprintf("%s/root.%s.ignore", dir, ext), data)
+
+ pdir := filepath.Join(dir, ext)
+ err = os.Mkdir(pdir, 0755)
+ writef(fmt.Sprintf("%s/partial.%s", pdir, ext), data)
+ writef(fmt.Sprintf("%s/partial.ignore.%s", pdir, ext), data)
+ writef(fmt.Sprintf("%s/partial.%s.ignore", pdir, ext), data)
+
+ if err != nil {
+ break
+ }
+ }
+
+ return err
+}
+
+// file contents used in "contents" files
+const contentsTxt = `p1
+p2
+
+ pre1
+ pre2
+p3
+
+p4
+`
+const contentsHtml = `<p>p1<br>
+p2</p>
+<pre>pre1
+pre2
+</pre>
+<p>p3</p>
+<p>p4</p>`
+const contentsMd = `p1
+p2
+
+ pre1
+ pre2
+
+p3
+`
+const contentsGfm = `p1
+p2
+
+ pre1
+ pre2
+
+p3`
+const contentsCm =`p1
+p2
+
+ pre1
+ pre2
+
+p3`
+var contents = map[string]string{
+ ".txt": contentsTxt,
+ ".html": contentsHtml,
+ ".md": contentsMd,
+ ".gfm": contentsGfm,
+ ".cm": contentsCm,
+}
+
+var asset = []byte{ // 5x5 black png image
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00,
+ 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
+ 0x00, 0x05, 0x01, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xa1, 0xb4, 0xa6,
+ 0x00, 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0xa5, 0x67, 0xb9, 0xcf, 0x00, 0x00, 0x00, 0x02,
+ 0x74, 0x52, 0x4e, 0x53, 0xff, 0x00, 0xe5, 0xb7, 0x30, 0x4a, 0x00,
+ 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc4,
+ 0x00, 0x00, 0x0e, 0xc4, 0x01, 0x95, 0x2b, 0x0e, 0x1b, 0x00, 0x00,
+ 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x08, 0x99, 0x63, 0x60, 0x66,
+ 0x60, 0x66, 0x60, 0x00, 0x62, 0x76, 0x00, 0x00, 0x4a, 0x00, 0x11,
+ 0x3a, 0x34, 0x8c, 0xad, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
+ 0x44, 0xae, 0x42, 0x60, 0x82,
+}
+
+func createTestContents(dir string) (err error) {
+ writef := func(path, data string) {
+ if err == nil {
+ err = os.WriteFile(path, []byte(data), 0644)
+ }
+ }
+
+ for l, lang := range contentExts {
+ if l == 0 {
+ writef(fmt.Sprintf("%s/defaults.json", dir), "{ \"default\": \"data\" }")
+ } else if l > 1 {
+ dir, err = os.MkdirTemp(dir, "page")
+ }
+ writef(fmt.Sprintf("%s/.page.toml", dir), "page = \"data\"")
+ writef(fmt.Sprintf("%s/body%d%s", dir, l, lang), contents[lang])
+ writef(fmt.Sprintf("%s/asset.png", dir), string(asset))
+
+ if err != nil {
+ break
+ }
+ }
+
+ return
+}
+
+func validateTestPages(t *testing.T, pages []Page, e error) {
+ if len(pages) != len(contents) {
+ t.Fatalf("invalid number of pages returned (%d should be %d)",
+ len(pages), len(contents))
+ }
+
+ var last, pt time.Time
+ for i, p := range pages {
+ if len(p.Slug) == 0 && p.Slug != filepath.Base(p.Path) {
+ t.Errorf("bad Slug for page: '%s' (%s) - should be '%s'",
+ p.Slug, p.Path, filepath.Base(p.Path))
+ }
+ if len(p.Path) == 0 {
+ t.Error("empty Path for page:", p)
+ }
+ // TODO test p.Nav here
+ if _, ok := p.Meta["page"]; !ok || len(p.Meta) == 0 {
+ t.Errorf("missing page Meta key for page: '%s'", p.Path)
+ }
+ if _, ok := p.Meta["default"]; !ok || len(p.Meta) == 0 {
+ t.Error("empty default Meta key for page:", p.Path)
+ }
+ if len(p.Contents) == 0 {
+ t.Error("empty Contents for page:", p.Path)
+ }
+ if len(p.Assets) == 0 {
+ t.Error("empty Assets for page:", p.Path)
+ }
+ if pt, e = time.Parse(timefmt, p.Updated); e != nil {
+ t.Fatal(e)
+ }
+
+ if i == 0 {
+ last = pt
+ } else if pt.Before(last) {
+ for _, pp := range pages {
+ t.Logf("%s - %s", pp.Path, pp.Updated)
+ }
+ t.Error("Contents Pages returned in wrong order")
+ }
+ }
+}
diff --git a/sitemap_test.go b/sitemap_test.go
@@ -9,15 +9,9 @@ func TestBuildCrumbs(test *testing.T) {
func TestBuildSitemap(test *testing.T) {
var err error
- /*
- writef := func(path, data string) {
- if err == nil {
- err = os.WriteFile(path, []byte(data), 0644)
- }
- }
- */
+
tdir := test.TempDir()
- if err = createProjectContents(tdir); err != nil {
+ if err = createTestContents(tdir); err != nil {
test.Errorf("failed to create test content: %s", err)
}
@@ -25,6 +19,7 @@ func TestBuildSitemap(test *testing.T) {
if p, err = LoadPagesDir(tdir); err != nil {
test.Errorf("LoadPagesDir failed: %s", err)
}
+
p = BuildSitemap(p)
// TODO validate p
}
diff --git a/template_test.go b/template_test.go
@@ -1,9 +1,6 @@
package main
import (
- "fmt"
- "os"
- "path/filepath"
"testing"
)
@@ -13,7 +10,7 @@ func TestLoadTemplateDir(t *testing.T) {
tdir := t.TempDir()
var err error
- if err = createProjectTemplates(tdir); err != nil {
+ if err = createTestTemplates(tdir); err != nil {
t.Errorf("failed to create test templates: %s", err)
}
@@ -26,36 +23,3 @@ func TestLoadTemplateDir(t *testing.T) {
len(tmpls), len(templates))
}
}
-
-var templates = map[string]string{ // [ext]template
- "tmpl": "{{.Contents}}",
- "hmpl": "{{.Contents}}",
- "mst": "{{Contents}}",
-}
-
-func createProjectTemplates(dir string) error {
- var err error
- writef := func(path, data string) {
- if err == nil {
- err = os.WriteFile(path, []byte(data), 0644)
- }
- }
-
- for ext, data := range templates {
- writef(fmt.Sprintf("%s/root.%s", dir, ext), data)
- writef(fmt.Sprintf("%s/root.ignore.%s", dir, ext), data)
- writef(fmt.Sprintf("%s/root.%s.ignore", dir, ext), data)
-
- pdir := filepath.Join(dir, ext)
- err = os.Mkdir(pdir, 0755)
- writef(fmt.Sprintf("%s/partial.%s", pdir, ext), data)
- writef(fmt.Sprintf("%s/partial.ignore.%s", pdir, ext), data)
- writef(fmt.Sprintf("%s/partial.%s.ignore", pdir, ext), data)
-
- if err != nil {
- break
- }
- }
-
- return err
-}