package hugorepo import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "supermarket/internal/hugorepo/hugotest" ) func TestRepo(t *testing.T) { ctx := context.Background() clone := hugotest.Site(t) r := &Repo{Dir: filepath.Join(t.TempDir(), "sites", "o__site"), CloneURL: clone, Token: "secret-token", Branch: "main", Author: "Pat "} if err := r.Sync(ctx); err != nil { t.Fatal(err) } if err := r.Sync(ctx); err != nil { // a second time fetches t.Fatal(err) } for page, want := range map[string]string{ "/": "content/_index.md", "/pricing/": "content/pricing.md", "/blog/first-post/": "content/blog/first-post/index.md", "/company/": "content/about.md", } { if got, err := r.FindContent(page); err != nil || got != want { t.Errorf("%s: %q %v", page, got, err) } } if _, err := r.FindContent("/nope/"); err == nil { t.Error("found a file for a page that doesn't exist") } if err := r.NewBranch(ctx, "supermarket/seo-1"); err != nil { t.Fatal(err) } if err := r.SetFrontMatter("content/pricing.md", "title", `Pricing: "plans" from $5`); err != nil { t.Fatal(err) } if err := r.SetFrontMatter("content/pricing.md", "description", "New description"); err != nil { t.Fatal(err) } if err := r.SetFrontMatter("content/blog/first-post/index.md", "description", "A post"); err != nil { t.Fatal(err) } raw, _ := os.ReadFile(filepath.Join(r.Dir, "content/pricing.md")) if string(raw) != "---\ntitle: \"Pricing: \\\"plans\\\" from $5\"\ndescription: \"New description\"\ndraft: false\n---\nOur prices.\n" { t.Errorf("pricing.md:\n%s", raw) } raw, _ = os.ReadFile(filepath.Join(r.Dir, "content/blog/first-post/index.md")) if !strings.Contains(string(raw), "description = \"A post\"\n+++") { t.Errorf("toml:\n%s", raw) } if v := r.FrontMatterValue("content/pricing.md", "title"); v != `Pricing: "plans" from $5` { t.Errorf("read back: %q", v) } if HugoVersion(ctx) == "" { t.Skip("no hugo") } out, err := r.Build(ctx) if err != nil { t.Fatal(err) } defer os.RemoveAll(out) title, desc, err := BuiltHead(out, "/pricing/") if err != nil || title != `Pricing: "plans" from $5 ยท Site` || desc != "New description" { t.Errorf("built: %q %q %v", title, desc, err) } diff, _ := r.Diff(ctx) if !strings.Contains(diff, "+title:") { t.Errorf("diff: %s", diff) } if err := r.CommitAndPush(ctx, "supermarket/seo-1", "SEO: test"); err != nil { t.Fatal(err) } if out, err := exec.Command("git", "-C", strings.TrimPrefix(clone, "file://"), "log", "--format=%an <%ae>|%s", "supermarket/seo-1", "-1").CombinedOutput(); err != nil || strings.TrimSpace(string(out)) != "Pat |SEO: test" { t.Errorf("pushed: %s %v", out, err) } cfg, _ := os.ReadFile(filepath.Join(r.Dir, ".git", "config")) if strings.Contains(string(cfg), "secret-token") { t.Error("the token is stored in the working copy") } // Sync goes back to a clean main. if err := r.Sync(ctx); err != nil { t.Fatal(err) } if v := r.FrontMatterValue("content/pricing.md", "title"); v != "Pricing" { t.Errorf("after sync: %q", v) } }