Download
68 lines 2.6 KiBGoModified 2026-10-28 raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
package render_test

import (
	"bytes"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"code.bruce-hill.com/repo-renderer/internal/mirror"
)

// Check reports on the writer it is given. Directories a first render
// would create, and a repository naming a section the config hasn't or
// with a tag that can't be shown, are warnings only.
func TestCheckWarnings(t *testing.T) {
	e := newEnv(t, "proj", "sections:\n  - name: tools\n    title: Tools\n")
	e.place("proj", "missing", "ok", " spaced")
	var out bytes.Buffer
	if err := e.site.Check(&out, "repo-render.yaml"); err != nil {
		t.Fatalf("Check: %v\n%s", err, out.String())
	}
	mustContain(t, "report", out.String(), "git: ", "config: repo-render.yaml\n",
		"warning: "+e.site.Lay.Htdocs+" does not exist yet",
		`warning: proj's section is "missing", which no section in the config is named, so it is in none`,
		`warning: proj's tag " spaced" is not usable`, "\nok\n")
	mustNotContain(t, "report", out.String(), `tag "ok"`, "warning: "+e.site.Lay.GitBase+" ")
}

// A lexer that resolves to plain text, and git failing, are problems.
func TestCheckProblems(t *testing.T) {
	e := newEnv(t, "proj", "lexers:\n  plaintext:\n    globs: [\"*.txt\"]\n  go:\n    globs: [\"*.go\"]\n")
	var out bytes.Buffer
	err := e.site.Check(&out, "repo-render.yaml")
	if err == nil || !strings.Contains(err.Error(), `lexer "plaintext" resolves to plain text`) {
		t.Errorf("Check = %v, want the plaintext lexer reported", err)
	}
	mustContain(t, "report", out.String(), "lexer go: ok")
	mustNotContain(t, "report", out.String(), "\nok\n")

	// git cannot start in a git_dir that is gone.
	e = newEnv(t, "proj", "")
	if err := os.RemoveAll(e.site.Lay.GitBase); err != nil {
		t.Fatal(err)
	}
	out.Reset()
	err = e.site.Check(&out, "repo-render.yaml")
	if err == nil || !strings.Contains(err.Error(), "git cannot run inside "+e.site.Lay.GitBase) {
		t.Errorf("Check without git_dir = %v", err)
	}
	mustNotContain(t, "report", out.String(), "\nok\n")
}

// Check warns of a mirror push that failed last time.
func TestCheckMirrors(t *testing.T) {
	e := newEnv(t, "proj", "")
	dir := e.site.Lay.GitDir("proj")
	e.git("", "-C", dir, "config", "--add", "repo-render.mirror", "git@codeberg.org:me/proj.git")
	if err := os.WriteFile(filepath.Join(dir, mirror.FailedFile), []byte("mirror to git@codeberg.org:me/proj.git failed: no key\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	var out bytes.Buffer
	if err := e.site.Check(&out, "repo-render.yaml"); err != nil {
		t.Fatalf("Check: %v\n%s", err, out.String())
	}
	mustContain(t, "report", out.String(),
		"warning: proj: the last mirror push failed: mirror to git@codeberg.org:me/proj.git failed: no key\n", "\nok\n")
}