Download
41 lines 1.6 KiBGoModified 2026-10-18 raw
1234567891011121314151617181920212223242526272829303132333435363738394041
package render_test

import (
	"os"
	"path/filepath"
	"testing"
)

// The site's sitemap indexes one sitemap per listed repository, which
// names every page of the default branch: files and directories, but not
// symlinks or submodules, which have no page, nor other branches, which
// ask not to be indexed. robots.txt points crawlers at it.
func TestSitemap(t *testing.T) {
	e := newEnv(t, "proj", "repos:\n  proj:\n    branches: [dev]\n")
	e.seed()
	if err := os.Symlink("foo", filepath.Join(e.work, "link")); err != nil {
		t.Fatal(err)
	}
	e.commit("link")
	e.git(e.work, "branch", "dev")
	e.git(e.work, "push", "-q", "origin", "main", "dev")
	e.render()

	const base = "https://" + testHost
	mustContain(t, "sitemap.xml", e.read("sitemap.xml"), "<sitemapindex", "<loc>"+base+"/sitemap/proj.xml</loc>", "<lastmod>")
	mustNotContain(t, "sitemap.xml", e.read("sitemap.xml"), "proj@dev")
	repo := e.read("sitemap/proj.xml")
	mustContain(t, "proj.xml", repo, `<?xml version="1.0"`, "<urlset",
		"<loc>"+base+"/code/proj</loc>", "<loc>"+base+"/code/proj/src</loc>",
		"<loc>"+base+"/code/proj/src/main.c</loc>", "<loc>"+base+"/code/proj/d/index</loc>", "<lastmod>")
	mustNotContain(t, "proj.xml", repo, "/code/proj/link<", "proj@dev")
	e.mustNotExist("sitemap/proj@dev.xml")
	mustContain(t, "robots.txt", e.read("robots.txt"), "\nSitemap: "+base+"/sitemap.xml\n")

	// A push that adds a file adds its page.
	e.write("src/new.c", "int x;\n")
	e.commit("add")
	e.push(false)
	e.render()
	mustContain(t, "proj.xml after push", e.read("sitemap/proj.xml"), "<loc>"+base+"/code/proj/src/new.c</loc>")
}