oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs_test
2
3import (
4 "io/fs"
5 "os"
6 "path"
7 "regexp"
8 "strings"
9 "testing"
10
11 "amadan.net/rastrillo/fiesta/check/spacing"
12)
13
14// styleBlock is one <style> element's text in a template.
15var styleBlock = regexp.MustCompile(`(?s)<style[^>]*>(.*?)</style>`)
16
17// TestEveryStylesheetIsOnTheSpacingScale: Fiesta's spacing check (Paul,
18// 2026-11-22, "no ad-hoc spacing") over everything Docs styles its
19// pages with. The gate runs `go tool fiesta spacing` over brand/, the
20// stylesheets on disk, but most of Docs' CSS is in the <style> blocks
21// of its templates, which the tool does not read. Each block is checked
22// as a sheet of its own, its lines kept where they are in the template,
23// so a finding names the template's own line. Every margin, padding and
24// gap is a var(--rst-sp-N) or says why it is not, with a
25// /* spacing-allow: why */ beside it.
26func TestEveryStylesheetIsOnTheSpacingScale(t *testing.T) {
27 files := map[string][]byte{}
28 err := fs.WalkDir(os.DirFS("."), ".", func(name string, d fs.DirEntry, err error) error {
29 if err != nil {
30 return err
31 }
32 if d.IsDir() || !(strings.HasPrefix(name, "views/") || strings.HasPrefix(name, "brand/")) {
33 return nil
34 }
35 src, err := os.ReadFile(name)
36 if err != nil {
37 return err
38 }
39 switch path.Ext(name) {
40 case ".css":
41 files[name] = src
42 case ".html":
43 if css := styleSheets(string(src)); css != "" {
44 files[name] = []byte(css)
45 }
46 }
47 return nil
48 })
49 if err != nil {
50 t.Fatal(err)
51 }
52 if len(files) < 5 {
53 t.Fatalf("read %d stylesheets; the walk is not finding Docs' templates", len(files))
54 }
55 for _, f := range spacing.CheckFiles(files) {
56 t.Error(f)
57 }
58}
59
60// styleSheets is a template's <style> blocks with everything else
61// blanked, line for line, so the check's line numbers are the
62// template's.
63func styleSheets(src string) string {
64 var out strings.Builder
65 at, found := 0, false
66 for _, m := range styleBlock.FindAllStringSubmatchIndex(src, -1) {
67 out.WriteString(strings.Repeat("\n", strings.Count(src[at:m[2]], "\n")))
68 out.WriteString(src[m[2]:m[3]])
69 at, found = m[3], true
70 }
71 if !found {
72 return ""
73 }
74 return out.String()
75}
76
77// TestTheSpacingCheckReadsTemplates: a <style> block with a hand margin
78// is refused, on its own line, so the check above cannot pass by
79// reading nothing.
80func TestTheSpacingCheckReadsTemplates(t *testing.T) {
81 src := "{{define \"x\"}}\n<style>\n p {\n margin: 7px;\n }\n</style>\n<p>hi</p>{{end}}\n"
82 found := spacing.CheckFiles(map[string][]byte{"x.html": []byte(styleSheets(src))})
83 if len(found) != 1 || found[0].Line != 4 || found[0].Property != "margin" {
84 t.Fatalf("a 7px margin on line 4 of a template's <style>: got %v", found)
85 }
86}
87