You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
caddy-hugo2/doc_test.go

149 lines
3.0 KiB

package caddyhugo
import (
"io/ioutil"
"os"
"os/exec"
"path"
"sync"
"testing"
"time"
"git.stephensearles.com/stephen/acedoc"
)
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 TestEdits(t *testing.T) {
w := NewWorld(t)
defer w.Clean()
const title = "sometitle"
var contentPath = path.Join("content", title+".md")
w.CH.NewContent(title, "")
send := []acedoc.Delta{
acedoc.Insert(0, 0, "hello"),
acedoc.Insert(0, 5, " world"),
acedoc.Insert(0, 11, " world"),
}
var mtx sync.Mutex
received := []acedoc.Delta{}
doc, err := w.CH.client(contentPath)
if err != nil {
t.Fatal("error creating document client:", err)
}
doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error {
// receive some deltas...
mtx.Lock()
defer mtx.Unlock()
received = append(received, ds...)
return nil
}))
_, ok := w.CH.hasdocref(contentPath)
if !ok {
t.Fatal("expected there to be an established client")
}
doc.doc.Apply(send...)
<-time.After(5 * time.Second)
if len(received) != len(send) {
t.Errorf("expected %d deltas, received %d; expected: %v, received: %v", len(send), len(received), send, received)
}
}
func TestPagesInPagesOut(t *testing.T) {
w := NewWorld(t)
defer w.Clean()
// check there's no content at first
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))
}
titles := []string{
"test1",
"TEST 2!!",
}
found := map[string]bool{}
// create some known content
for i, title := range titles {
w.CH.NewContent(title, "")
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 != i {
t.Fatalf("expected %d page, but saw %d pages", i+1, len(c))
}
}
// make sure we get the content out that we just created
c, err = GetContent(w.BlogFolder, w.CH.HugoSites)
if err != nil {
t.Fatalf("couldn't get content from the test environment: %v", err)
}
for _, content := range c {
if content.Metadata == nil {
t.Errorf("didn't see metadata for %q", content.Filename)
}
found[content.Filename] = true
}
var missingSomething bool
for _, title := range titles {
adjusted := path.Join("content", docname(title)+".md")
if !found[adjusted] {
missingSomething = true
t.Errorf("expected to find title %q, but didn't see it", adjusted)
}
}
if missingSomething {
t.Logf("found titles: %v", found)
}
}