From 47fc35acc0fe84e25d03da8f27f54ac7ec2ed870 Mon Sep 17 00:00:00 2001 From: Tw Date: Sat, 3 Jun 2017 22:28:08 +0800 Subject: [PATCH 1/3] markdown: fix fake tests Signed-off-by: Tw --- caddyhttp/markdown/markdown_test.go | 112 +++++------------- caddyhttp/markdown/setup.go | 9 +- caddyhttp/markdown/setup_test.go | 5 +- caddyhttp/markdown/testdata/blog/test.md | 14 +++ .../markdown/testdata/docflags/template.txt | 2 + caddyhttp/markdown/testdata/docflags/test.md | 4 + caddyhttp/markdown/testdata/header.html | 1 + caddyhttp/markdown/testdata/log/test.md | 14 +++ caddyhttp/markdown/testdata/markdown_tpl.html | 11 ++ caddyhttp/markdown/testdata/og/first.md | 5 + .../markdown/testdata/tpl_with_include.html | 11 ++ 11 files changed, 98 insertions(+), 90 deletions(-) create mode 100644 caddyhttp/markdown/testdata/blog/test.md create mode 100644 caddyhttp/markdown/testdata/docflags/template.txt create mode 100644 caddyhttp/markdown/testdata/docflags/test.md create mode 100644 caddyhttp/markdown/testdata/header.html create mode 100644 caddyhttp/markdown/testdata/log/test.md create mode 100644 caddyhttp/markdown/testdata/markdown_tpl.html create mode 100644 caddyhttp/markdown/testdata/og/first.md create mode 100644 caddyhttp/markdown/testdata/tpl_with_include.html diff --git a/caddyhttp/markdown/markdown_test.go b/caddyhttp/markdown/markdown_test.go index 4da39a7f..658da51e 100644 --- a/caddyhttp/markdown/markdown_test.go +++ b/caddyhttp/markdown/markdown_test.go @@ -1,16 +1,12 @@ package markdown import ( - "bufio" "io/ioutil" "net/http" "net/http/httptest" - "os" "path/filepath" - "strings" "testing" "text/template" - "time" "github.com/mholt/caddy/caddyhttp/httpserver" "github.com/russross/blackfriday" @@ -79,19 +75,23 @@ func TestMarkdown(t *testing.T) { }), } - req, err := http.NewRequest("GET", "/blog/test.md", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) + get := func(url string) string { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + t.Fatalf("Could not create HTTP request: %v", err) + } + rec := httptest.NewRecorder() + code, err := md.ServeHTTP(rec, req) + if err != nil { + t.Fatal(err) + } + if code != http.StatusOK { + t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, code) + } + return rec.Body.String() } - rec := httptest.NewRecorder() - - md.ServeHTTP(rec, req) - if rec.Code != http.StatusOK { - t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code) - } - - respBody := rec.Body.String() + respBody := get("/blog/test.md") expectedBody := ` @@ -99,7 +99,6 @@ func TestMarkdown(t *testing.T) {

Header for: Markdown test 1

- Welcome to A Caddy website!

Welcome on the blog

@@ -113,46 +112,22 @@ Welcome to A Caddy website! ` - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) - } - - req, err = http.NewRequest("GET", "/docflags/test.md", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - rec = httptest.NewRecorder() - - md.ServeHTTP(rec, req) - if rec.Code != http.StatusOK { - t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code) - } - respBody = rec.Body.String() + respBody = get("/docflags/test.md") expectedBody = `Doc.var_string hello -Doc.var_bool -DocFlags.var_string -DocFlags.var_bool true` +Doc.var_bool true +` - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } - req, err = http.NewRequest("GET", "/log/test.md", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - rec = httptest.NewRecorder() - - md.ServeHTTP(rec, req) - if rec.Code != http.StatusOK { - t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code) - } - respBody = rec.Body.String() + respBody = get("/log/test.md") expectedBody = ` Markdown test 2 + @@ -171,26 +146,11 @@ DocFlags.var_bool true` ` - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } - req, err = http.NewRequest("GET", "/og/first.md", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - rec = httptest.NewRecorder() - currenttime := time.Now().Local().Add(-time.Second) - _ = os.Chtimes("testdata/og/first.md", currenttime, currenttime) - currenttime = time.Now().Local() - _ = os.Chtimes("testdata/og_static/og/first.md/index.html", currenttime, currenttime) - time.Sleep(time.Millisecond * 200) - - md.ServeHTTP(rec, req) - if rec.Code != http.StatusOK { - t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code) - } - respBody = rec.Body.String() + respBody = get("/og/first.md") expectedBody = ` @@ -198,32 +158,18 @@ DocFlags.var_bool true`

Header for: first_post

- Welcome to title!

Test h1

-` + +` - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } } -func equalStrings(s1, s2 string) bool { - s1 = strings.TrimSpace(s1) - s2 = strings.TrimSpace(s2) - in := bufio.NewScanner(strings.NewReader(s1)) - for in.Scan() { - txt := strings.TrimSpace(in.Text()) - if !strings.HasPrefix(strings.TrimSpace(s2), txt) { - return false - } - s2 = strings.Replace(s2, txt, "", 1) - } - return true -} - func setDefaultTemplate(filename string) *template.Template { buf, err := ioutil.ReadFile(filename) if err != nil { diff --git a/caddyhttp/markdown/setup.go b/caddyhttp/markdown/setup.go index 2424abf6..3117062c 100644 --- a/caddyhttp/markdown/setup.go +++ b/caddyhttp/markdown/setup.go @@ -115,15 +115,17 @@ func loadParams(c *caddy.Controller, mdc *Config) error { fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[0])) if err := SetTemplate(mdc.Template, "", fpath); err != nil { - c.Errf("default template parse error: %v", err) + return c.Errf("default template parse error: %v", err) } + return nil case 2: fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[1])) if err := SetTemplate(mdc.Template, tArgs[0], fpath); err != nil { - c.Errf("template parse error: %v", err) + return c.Errf("template parse error: %v", err) } + return nil } case "templatedir": @@ -132,11 +134,12 @@ func loadParams(c *caddy.Controller, mdc *Config) error { } _, err := mdc.Template.ParseGlob(c.Val()) if err != nil { - c.Errf("template load error: %v", err) + return c.Errf("template load error: %v", err) } if c.NextArg() { return c.ArgErr() } + return nil default: return c.Err("Expected valid markdown configuration property") diff --git a/caddyhttp/markdown/setup_test.go b/caddyhttp/markdown/setup_test.go index a86f95e5..745337b6 100644 --- a/caddyhttp/markdown/setup_test.go +++ b/caddyhttp/markdown/setup_test.go @@ -71,12 +71,9 @@ func TestMarkdownParse(t *testing.T) { Extensions: map[string]struct{}{ ".md": {}, }, - Template: GetDefaultTemplate(), + Template: setDefaultTemplate("./testdata/tpl_with_include.html"), }}}, } - // Setup the extra template - tmpl := tests[1].expectedMarkdownConfig[0].Template - SetTemplate(tmpl, "", "./testdata/tpl_with_include.html") for i, test := range tests { c := caddy.NewTestController("http", test.inputMarkdownConfig) diff --git a/caddyhttp/markdown/testdata/blog/test.md b/caddyhttp/markdown/testdata/blog/test.md new file mode 100644 index 00000000..93f07a49 --- /dev/null +++ b/caddyhttp/markdown/testdata/blog/test.md @@ -0,0 +1,14 @@ +--- +title: Markdown test 1 +sitename: A Caddy website +--- + +## Welcome on the blog + +Body + +``` go +func getTrue() bool { + return true +} +``` diff --git a/caddyhttp/markdown/testdata/docflags/template.txt b/caddyhttp/markdown/testdata/docflags/template.txt new file mode 100644 index 00000000..2b001388 --- /dev/null +++ b/caddyhttp/markdown/testdata/docflags/template.txt @@ -0,0 +1,2 @@ +Doc.var_string {{.Doc.var_string}} +Doc.var_bool {{.Doc.var_bool}} diff --git a/caddyhttp/markdown/testdata/docflags/test.md b/caddyhttp/markdown/testdata/docflags/test.md new file mode 100644 index 00000000..64ca7f78 --- /dev/null +++ b/caddyhttp/markdown/testdata/docflags/test.md @@ -0,0 +1,4 @@ +--- +var_string: hello +var_bool: true +--- diff --git a/caddyhttp/markdown/testdata/header.html b/caddyhttp/markdown/testdata/header.html new file mode 100644 index 00000000..cfbdc75b --- /dev/null +++ b/caddyhttp/markdown/testdata/header.html @@ -0,0 +1 @@ +

Header for: {{.Doc.title}}

\ No newline at end of file diff --git a/caddyhttp/markdown/testdata/log/test.md b/caddyhttp/markdown/testdata/log/test.md new file mode 100644 index 00000000..476ab301 --- /dev/null +++ b/caddyhttp/markdown/testdata/log/test.md @@ -0,0 +1,14 @@ +--- +title: Markdown test 2 +sitename: A Caddy website +--- + +## Welcome on the blog + +Body + +``` go +func getTrue() bool { + return true +} +``` diff --git a/caddyhttp/markdown/testdata/markdown_tpl.html b/caddyhttp/markdown/testdata/markdown_tpl.html new file mode 100644 index 00000000..7c697850 --- /dev/null +++ b/caddyhttp/markdown/testdata/markdown_tpl.html @@ -0,0 +1,11 @@ + + + +{{.Doc.title}} + + +{{.Include "header.html"}} +Welcome to {{.Doc.sitename}}! +{{.Doc.body}} + + diff --git a/caddyhttp/markdown/testdata/og/first.md b/caddyhttp/markdown/testdata/og/first.md new file mode 100644 index 00000000..4d7a4251 --- /dev/null +++ b/caddyhttp/markdown/testdata/og/first.md @@ -0,0 +1,5 @@ +--- +title: first_post +sitename: title +--- +# Test h1 diff --git a/caddyhttp/markdown/testdata/tpl_with_include.html b/caddyhttp/markdown/testdata/tpl_with_include.html new file mode 100644 index 00000000..68cc986c --- /dev/null +++ b/caddyhttp/markdown/testdata/tpl_with_include.html @@ -0,0 +1,11 @@ + + + + {{.Doc.title}} + + + Welcome to {{.Doc.sitename}}! +

+ {{.Doc.body}} + + From 3390862918f5ed0d147d7f45de6e4f29b4ef8f53 Mon Sep 17 00:00:00 2001 From: Tw Date: Sun, 25 Jun 2017 19:31:12 +0800 Subject: [PATCH 2/3] markdown: reload template on each request Signed-off-by: Tw --- caddyhttp/markdown/markdown.go | 3 ++ caddyhttp/markdown/markdown_test.go | 69 ++++++++++++++++++++++++++++- caddyhttp/markdown/setup.go | 22 ++++++--- caddyhttp/markdown/setup_test.go | 15 +++++-- caddyhttp/markdown/template.go | 12 ++++- 5 files changed, 111 insertions(+), 10 deletions(-) diff --git a/caddyhttp/markdown/markdown.go b/caddyhttp/markdown/markdown.go index 27e60b03..12c2ec61 100644 --- a/caddyhttp/markdown/markdown.go +++ b/caddyhttp/markdown/markdown.go @@ -53,6 +53,9 @@ type Config struct { // Template(s) to render with Template *template.Template + + // a pair of template's name and its underlying file path + TemplateFiles map[string]string } // ServeHTTP implements the http.Handler interface. diff --git a/caddyhttp/markdown/markdown_test.go b/caddyhttp/markdown/markdown_test.go index 658da51e..94827d80 100644 --- a/caddyhttp/markdown/markdown_test.go +++ b/caddyhttp/markdown/markdown_test.go @@ -4,10 +4,12 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "os" "path/filepath" "testing" "text/template" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" "github.com/russross/blackfriday" ) @@ -127,7 +129,7 @@ Doc.var_bool true Markdown test 2 - + @@ -178,3 +180,68 @@ func setDefaultTemplate(filename string) *template.Template { return template.Must(GetDefaultTemplate().Parse(string(buf))) } + +func TestTemplateReload(t *testing.T) { + const ( + templateFile = "testdata/test.html" + targetFile = "testdata/hello.md" + ) + c := caddy.NewTestController("http", `markdown { + template `+templateFile+` + }`) + + err := ioutil.WriteFile(templateFile, []byte("hello {{.Doc.body}}"), 0644) + if err != nil { + t.Fatal(err) + } + err = ioutil.WriteFile(targetFile, []byte("caddy"), 0644) + if err != nil { + t.Fatal(err) + } + defer func() { + os.Remove(templateFile) + os.Remove(targetFile) + }() + + config, err := markdownParse(c) + if err != nil { + t.Fatal(err) + } + md := Markdown{ + Root: "./testdata", + FileSys: http.Dir("./testdata"), + Configs: config, + Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + t.Fatalf("Next shouldn't be called") + return 0, nil + }), + } + + req := httptest.NewRequest("GET", "/hello.md", nil) + get := func() string { + rec := httptest.NewRecorder() + code, err := md.ServeHTTP(rec, req) + if err != nil { + t.Fatal(err) + } + if code != http.StatusOK { + t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, code) + } + return rec.Body.String() + } + + if expect, got := "hello

caddy

\n", get(); expect != got { + t.Fatalf("Expected body:\n%q\nbut got:\n%q", expect, got) + } + + // update template + err = ioutil.WriteFile(templateFile, []byte("hi {{.Doc.body}}"), 0644) + if err != nil { + t.Fatal(err) + } + + if expect, got := "hi

caddy

\n", get(); expect != got { + t.Fatalf("Expected body:\n%q\nbut got:\n%q", expect, got) + } + +} diff --git a/caddyhttp/markdown/setup.go b/caddyhttp/markdown/setup.go index 3117062c..8792f85b 100644 --- a/caddyhttp/markdown/setup.go +++ b/caddyhttp/markdown/setup.go @@ -44,10 +44,11 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) { for c.Next() { md := &Config{ - Renderer: blackfriday.HtmlRenderer(0, "", ""), - Extensions: make(map[string]struct{}), - Template: GetDefaultTemplate(), - IndexFiles: []string{}, + Renderer: blackfriday.HtmlRenderer(0, "", ""), + Extensions: make(map[string]struct{}), + Template: GetDefaultTemplate(), + IndexFiles: []string{}, + TemplateFiles: make(map[string]string), } // Get the path scope @@ -118,6 +119,7 @@ func loadParams(c *caddy.Controller, mdc *Config) error { return c.Errf("default template parse error: %v", err) } + mdc.TemplateFiles[""] = fpath return nil case 2: fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[1])) @@ -126,13 +128,16 @@ func loadParams(c *caddy.Controller, mdc *Config) error { return c.Errf("template parse error: %v", err) } + mdc.TemplateFiles[tArgs[0]] = fpath return nil } case "templatedir": if !c.NextArg() { return c.ArgErr() } - _, err := mdc.Template.ParseGlob(c.Val()) + + pattern := c.Val() + _, err := mdc.Template.ParseGlob(pattern) if err != nil { return c.Errf("template load error: %v", err) } @@ -140,6 +145,13 @@ func loadParams(c *caddy.Controller, mdc *Config) error { return c.ArgErr() } + paths, err := filepath.Glob(pattern) + if err != nil { + return c.Errf("glob %q failed: %v", pattern, err) + } + for _, path := range paths { + mdc.TemplateFiles[filepath.Base(path)] = path + } return nil default: return c.Err("Expected valid markdown configuration property") diff --git a/caddyhttp/markdown/setup_test.go b/caddyhttp/markdown/setup_test.go index 745337b6..596ad84d 100644 --- a/caddyhttp/markdown/setup_test.go +++ b/caddyhttp/markdown/setup_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "reflect" "testing" "text/template" @@ -59,9 +60,10 @@ func TestMarkdownParse(t *testing.T) { ".md": {}, ".txt": {}, }, - Styles: []string{"/resources/css/blog.css"}, - Scripts: []string{"/resources/js/blog.js"}, - Template: GetDefaultTemplate(), + Styles: []string{"/resources/css/blog.css"}, + Scripts: []string{"/resources/js/blog.js"}, + Template: GetDefaultTemplate(), + TemplateFiles: make(map[string]string), }}}, {`markdown /blog { ext .md @@ -72,6 +74,9 @@ func TestMarkdownParse(t *testing.T) { ".md": {}, }, Template: setDefaultTemplate("./testdata/tpl_with_include.html"), + TemplateFiles: map[string]string{ + "": "testdata/tpl_with_include.html", + }, }}}, } @@ -107,6 +112,10 @@ func TestMarkdownParse(t *testing.T) { if ok, tx, ty := equalTemplates(actualMarkdownConfig.Template, test.expectedMarkdownConfig[j].Template); !ok { t.Errorf("Test %d the %dth Markdown Config Templates did not match, expected %s to be %s", i, j, tx, ty) } + if expect, got := test.expectedMarkdownConfig[j].TemplateFiles, actualMarkdownConfig.TemplateFiles; !reflect.DeepEqual(expect, got) { + t.Errorf("Test %d the %d Markdown config TemplateFiles did not match, expect %v, but got %v", i, j, expect, got) + } + } } } diff --git a/caddyhttp/markdown/template.go b/caddyhttp/markdown/template.go index 639f06fe..b21e5984 100644 --- a/caddyhttp/markdown/template.go +++ b/caddyhttp/markdown/template.go @@ -38,8 +38,18 @@ func execTemplate(c *Config, mdata metadata.Metadata, meta map[string]string, fi Files: files, } + templateName := mdata.Template + // reload template on every request for now + // TODO: cache templates by a general plugin + if templateFile, ok := c.TemplateFiles[templateName]; ok { + err := SetTemplate(c.Template, templateName, templateFile) + if err != nil { + return nil, err + } + } + b := new(bytes.Buffer) - if err := c.Template.ExecuteTemplate(b, mdata.Template, mdData); err != nil { + if err := c.Template.ExecuteTemplate(b, templateName, mdData); err != nil { return nil, err } From 655e61ab32a72831e897a46cf8763577fa719612 Mon Sep 17 00:00:00 2001 From: Tw Date: Wed, 28 Jun 2017 09:28:16 +0800 Subject: [PATCH 3/3] markdown: fix ineffectual assignment CI issue Signed-off-by: Tw --- caddyhttp/markdown/markdown_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/caddyhttp/markdown/markdown_test.go b/caddyhttp/markdown/markdown_test.go index 94827d80..e8279da9 100644 --- a/caddyhttp/markdown/markdown_test.go +++ b/caddyhttp/markdown/markdown_test.go @@ -81,14 +81,17 @@ func TestMarkdown(t *testing.T) { req, err := http.NewRequest("GET", url, nil) if err != nil { t.Fatalf("Could not create HTTP request: %v", err) + return "" } rec := httptest.NewRecorder() code, err := md.ServeHTTP(rec, req) if err != nil { t.Fatal(err) + return "" } if code != http.StatusOK { t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, code) + return "" } return rec.Body.String() } @@ -223,9 +226,11 @@ func TestTemplateReload(t *testing.T) { code, err := md.ServeHTTP(rec, req) if err != nil { t.Fatal(err) + return "" } if code != http.StatusOK { t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, code) + return "" } return rec.Body.String() }