| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/fiesta/check/spacing" |
| 11 | ) |
| 12 | |
| 13 | // vendoredSheets are the stylesheets under static/ that are somebody |
| 14 | // else's to space: rastrillo's (tokens.css, shell.css; rastrillo |
| 15 | // doctor), estilo's (theme.css and busy.css by scripts/writeestilo, |
| 16 | // brand/brand.css by estilo's brand/distribute.py). make spacing skips |
| 17 | // the same list. |
| 18 | var vendoredSheets = map[string]bool{ |
| 19 | "tokens.css": true, "shell.css": true, "theme.css": true, "busy.css": true, "brand/brand.css": true, |
| 20 | } |
| 21 | |
| 22 | // styleBlock is one <style> element's text in a template. |
| 23 | var styleBlock = regexp.MustCompile(`(?s)<style[^>]*>(.*?)</style>`) |
| 24 | |
| 25 | // TestEveryStylesheetIsOnTheSpacingScale: Fiesta's spacing check (Paul, |
| 26 | // 2026-11-22, "no ad-hoc spacing") over everything Slides styles its |
| 27 | // pages with. make spacing runs `go tool fiesta spacing` over static/, |
| 28 | // which does not read the <style> blocks in the templates (the viewer's, |
| 29 | // the projector's, the presenter's, the remote's); this reads both, |
| 30 | // together, so a custom property declared in a stylesheet and used in |
| 31 | // a template resolves. Each block is checked with its lines kept where |
| 32 | // they are in the template, so a finding names the template's own line. |
| 33 | func TestEveryStylesheetIsOnTheSpacingScale(t *testing.T) { |
| 34 | files := map[string][]byte{} |
| 35 | static := filepath.Join("..", "slides", "static") |
| 36 | err := filepath.WalkDir(static, func(name string, d os.DirEntry, err error) error { |
| 37 | if err != nil || d.IsDir() || filepath.Ext(name) != ".css" { |
| 38 | return err |
| 39 | } |
| 40 | rel, _ := filepath.Rel(static, name) |
| 41 | if vendoredSheets[filepath.ToSlash(rel)] { |
| 42 | return nil |
| 43 | } |
| 44 | src, err := os.ReadFile(name) |
| 45 | files["static/"+filepath.ToSlash(rel)] = src |
| 46 | return err |
| 47 | }) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | templates, err := filepath.Glob(filepath.Join("..", "slides", "templates", "*.html")) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | blocks := 0 |
| 56 | for _, name := range templates { |
| 57 | src, err := os.ReadFile(name) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if css := styleSheets(string(src)); css != "" { |
| 62 | files["templates/"+filepath.Base(name)] = []byte(css) |
| 63 | blocks++ |
| 64 | } |
| 65 | } |
| 66 | if blocks < 5 { |
| 67 | t.Fatalf("read %d templates with a <style>; the walk is not finding Slides' templates", blocks) |
| 68 | } |
| 69 | for _, f := range spacing.CheckFiles(files) { |
| 70 | t.Error(f) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // styleSheets is a template's <style> blocks with everything else |
| 75 | // blanked, line for line, so the check's line numbers are the |
| 76 | // template's. |
| 77 | func styleSheets(src string) string { |
| 78 | var out strings.Builder |
| 79 | at, found := 0, false |
| 80 | for _, m := range styleBlock.FindAllStringSubmatchIndex(src, -1) { |
| 81 | out.WriteString(strings.Repeat("\n", strings.Count(src[at:m[2]], "\n"))) |
| 82 | out.WriteString(src[m[2]:m[3]]) |
| 83 | at, found = m[3], true |
| 84 | } |
| 85 | if !found { |
| 86 | return "" |
| 87 | } |
| 88 | return out.String() |
| 89 | } |
| 90 | |
| 91 | // TestTheSpacingCheckReadsTemplates: a <style> block with a hand margin |
| 92 | // is refused, on its own line, so the check above cannot pass by |
| 93 | // reading nothing. |
| 94 | func TestTheSpacingCheckReadsTemplates(t *testing.T) { |
| 95 | src := "{{define \"x\"}}\n<style>\n p {\n margin: 7px;\n }\n</style>\n<p>hi</p>{{end}}\n" |
| 96 | found := spacing.CheckFiles(map[string][]byte{"x.html": []byte(styleSheets(src))}) |
| 97 | if len(found) != 1 || found[0].Line != 4 || found[0].Property != "margin" { |
| 98 | t.Fatalf("a 7px margin on line 4 of a template's <style>: got %v", found) |
| 99 | } |
| 100 | } |
| 101 | |