| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package presentscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "html/templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/" |
| 11 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 12 | "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 13 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 14 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 15 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 16 | ) |
| 17 | |
| 18 | func TestTestdatascip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/TestTestdata().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 19 | tmpl := templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.Mustscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/Must().(Templatescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Template().().Parsescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/Template#Parse().(testTmplscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/testTmpl.)) |
| 20 | filesP, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Glob().("testdata/*.p") |
| 21 | if err != nil { |
| 22 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 23 | } |
| 24 | filesMD, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Glob().("testdata/*.md") |
| 25 | if err != nil { |
| 26 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 27 | } |
| 28 | files := append(filesP, filesMD...) |
| 29 | for _, file := range files { |
| 30 | name := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(file) |
| 31 | if name == "README" { |
| 32 | continue |
| 33 | } |
| 34 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 35 | data, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(file) |
| 36 | if err != nil { |
| 37 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: %v", file, err) |
| 38 | } |
| 39 | marker := []byte("\n---\n") |
| 40 | i := bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Indexscip-go gomod github.com/golang/go/src go1.25.0 bytes/Index().(data, marker) |
| 41 | if i < 0 { |
| 42 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: cannot find --- marker in input", file) |
| 43 | } |
| 44 | input, html := data[:i+1], data[i+len(marker):] |
| 45 | doc, err := Parsescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Parse().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewReader().(input), name, 0) |
| 46 | if err != nil { |
| 47 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: %v", file, err) |
| 48 | } |
| 49 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 50 | if err := doc.Renderscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Doc#Render().(&buf, tmpl); err != nil { |
| 51 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: %v", file, err) |
| 52 | } |
| 53 | if !bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.25.0 bytes/Equal().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), html) { |
| 54 | diffText, err := diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/diff().("present-test-", "want", html, "have", buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().()) |
| 55 | if err != nil { |
| 56 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: diff: %v", file, err) |
| 57 | } |
| 58 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: incorrect html:\n%s", file, diffText) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/diff().(prefix string, name1 string, b1 []byte, name2 string, b2 []byte) ([]byte, error) { |
| 65 | f1, err := writeTempFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/writeTempFile().(prefix, b1) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(f1) |
| 70 | |
| 71 | f2, err := writeTempFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/writeTempFile().(prefix, b2) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(f2) |
| 76 | |
| 77 | cmd := "diff" |
| 78 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. == "plan9" { |
| 79 | cmd = "/bin/ape/diff" |
| 80 | } |
| 81 | |
| 82 | data, err := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(cmd, "-u", f1, f2).Outputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Output().() |
| 83 | if len(data) > 0 { |
| 84 | // diff exits with a non-zero status when the files don't match. |
| 85 | // Ignore that failure as long as we get output. |
| 86 | err = nil |
| 87 | } else if exit, ok := err.(*execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#); ok && len(exit.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Stderr.) > 0 { |
| 88 | err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w\nstderr:\n%s)", err, exit.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Stderr.) |
| 89 | } |
| 90 | |
| 91 | data = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Replacescip-go gomod github.com/golang/go/src go1.25.0 bytes/Replace().(data, []byte(f1), []byte(name1), -1) |
| 92 | data = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Replacescip-go gomod github.com/golang/go/src go1.25.0 bytes/Replace().(data, []byte(f2), []byte(name2), -1) |
| 93 | |
| 94 | return data, err |
| 95 | } |
| 96 | |
| 97 | func writeTempFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/writeTempFile().(prefix string, data []byte) (string, error) { |
| 98 | file, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.CreateTempscip-go gomod github.com/golang/go/src go1.25.0 os/CreateTemp().("", prefix) |
| 99 | if err != nil { |
| 100 | return "", err |
| 101 | } |
| 102 | _, err = file.Writescip-go gomod github.com/golang/go/src go1.25.0 os/File#Write().(data) |
| 103 | if err1 := file.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().(); err == nil { |
| 104 | err = err1 |
| 105 | } |
| 106 | if err != nil { |
| 107 | osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(file.Namescip-go gomod github.com/golang/go/src go1.25.0 os/File#Name().()) |
| 108 | return "", err |
| 109 | } |
| 110 | return file.Namescip-go gomod github.com/golang/go/src go1.25.0 os/File#Name().(), nil |
| 111 | } |
| 112 | |
| 113 | var testTmplscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/testTmpl. = ` |
| 114 | {{define "root" -}} |
| 115 | <h1>{{.Title}}</h1> |
| 116 | {{with .Subtitle}}<h2>{{.}}</h2> |
| 117 | {{end -}} |
| 118 | {{range .Authors}}<author> |
| 119 | {{range .Elem}}{{elem $.Template .}}{{end}}</author> |
| 120 | {{end -}} |
| 121 | {{range .Sections}}<section>{{elem $.Template .}}</section> |
| 122 | {{end -}} |
| 123 | {{end}} |
| 124 | |
| 125 | {{define "newline"}}{{/* No automatic line break. Paragraphs are free-form. */}} |
| 126 | {{end}} |
| 127 | |
| 128 | {{define "section"}} |
| 129 | {{if .Title}}<h2 id="TOC_{{.FormattedNumber}}">{{.Title}}</h2> |
| 130 | {{end -}} |
| 131 | {{range .Elem}}{{elem $.Template .}}{{end}} |
| 132 | {{- end}} |
| 133 | |
| 134 | {{define "list" -}} |
| 135 | <ul> |
| 136 | {{range .Bullet -}} |
| 137 | <li>{{style .}}</li> |
| 138 | {{end -}} |
| 139 | </ul> |
| 140 | {{end}} |
| 141 | |
| 142 | {{define "text" -}} |
| 143 | {{if .Pre -}} |
| 144 | <pre>{{range .Lines}}{{.}}{{end}}</pre> |
| 145 | {{else -}} |
| 146 | <p>{{range $i, $l := .Lines}}{{if $i}}{{template "newline"}}{{end}}{{style $l}}{{end}}</p> |
| 147 | {{end -}} |
| 148 | {{end}} |
| 149 | |
| 150 | {{define "code" -}} |
| 151 | {{if .Play -}} |
| 152 | <div class="playground">{{.Text}}</div> |
| 153 | {{else -}} |
| 154 | <div class="code">{{.Text}}</div> |
| 155 | {{end -}} |
| 156 | {{end}} |
| 157 | |
| 158 | {{define "image" -}} |
| 159 | <img src="{{.URL}}"{{with .Height}} height="{{.}}"{{end}}{{with .Width}} width="{{.}}"{{end}} alt=""> |
| 160 | {{end}} |
| 161 | |
| 162 | {{define "caption" -}} |
| 163 | <figcaption>{{style .Text}}</figcaption> |
| 164 | {{end}} |
| 165 | |
| 166 | {{define "iframe" -}} |
| 167 | <iframe src="{{.URL}}"{{with .Height}} height="{{.}}"{{end}}{{with .Width}} width="{{.}}"{{end}}></iframe> |
| 168 | {{end}} |
| 169 | |
| 170 | {{define "link" -}} |
| 171 | <p class="link"><a href="{{.URL}}">{{style .Label}}</a></p> |
| 172 | {{end}} |
| 173 | |
| 174 | {{define "html" -}}{{.HTML}}{{end}} |
| 175 | ` |