adding basic test and organizing code a little bit

This commit is contained in:
2017-07-30 10:24:07 -07:00
parent a6ae6223eb
commit e1183ee316
7 changed files with 595 additions and 469 deletions
+61
View File
@@ -0,0 +1,61 @@
package caddyhugo
import (
"io/ioutil"
"os"
"os/exec"
"testing"
)
type World struct {
CH CaddyHugo
BlogFolder string
}
func (w World) Clean() {
if w.BlogFolder != "" {
os.RemoveAll(w.BlogFolder)
}
}
func NewWorld(t *testing.T) World {
dir, err := ioutil.TempDir("", "caddy-hugo2-test-")
if err != nil {
t.Fatalf("error initializing test environment: %v", err)
}
w := World{BlogFolder: dir}
cmd := exec.Command("hugo", "new", "site", dir)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("error initializing test site: %v\n\n%v", err, string(out))
}
w.CH.Setup(dir)
return w
}
func TestDoc(t *testing.T) {
w := NewWorld(t)
defer w.Clean()
c, err := GetContent(w.BlogFolder, w.CH.HugoSites)
if err != nil {
t.Fatalf("couldn't get content from a blank test environment: %v", err)
}
if len(c) != 0 {
t.Fatalf("expected a blank test environment, but saw %d pages", len(c))
}
w.CH.NewContent("test1", "")
c, err = GetContent(w.BlogFolder, w.CH.HugoSites)
if err != nil {
t.Fatalf("couldn't get content from the test environment: %v", err)
}
if len(c) != 1 {
t.Fatalf("expected 1 page, but saw %d pages", len(c))
}
}