package web
import (
"os"
"path/filepath"
"strings"
"testing"
)
// No source file carries merge conflict markers.
//
// gofmt, go vet and go build ALL passed with conflict markers sitting
// in web.go, because the markers were inside a Go raw string holding
// template markup. The compiler never sees a stray "<<<<<<< current"
// there; it is just text in a string, and the page then serves it to
// a reader. The whole gate was blind to a botched merge in any
// template, which is most of this package.
//
// Checked as TEXT, per line, rather than by parsing: the point is
// precisely that the parser cannot see these.
func TestNoSourceFileCarriesConflictMarkers(t *testing.T) {
// The markers, spelled so this file is not itself a false positive:
// each is built at runtime rather than written out.
markers := []string{
strings.Repeat("<", 7) + " ",
strings.Repeat("|", 7) + " ",
strings.Repeat(">", 7) + " ",
strings.Repeat("=", 7),
}
root := ".."
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".md") {
return nil
}
if strings.HasSuffix(path, "conflict_test.go") {
return nil
}
b, rerr := os.ReadFile(path)
if rerr != nil {
return rerr
}
for n, line := range strings.Split(string(b), "\n") {
for _, m := range markers {
// A marker only counts at the start of a line, which is
// where a merge writes it. "=======" also underlines a
// heading in Markdown, so the bare rule is accepted
// there and only the three-way markers are refused.
if !strings.HasPrefix(line, m) {
continue
}
if m == strings.Repeat("=", 7) && strings.HasSuffix(path, ".md") {
continue
}
if m == strings.Repeat("=", 7) && strings.TrimRight(line, "=") != "" {
continue
}
t.Errorf("FAIL: %s:%d begins with a merge conflict marker. The build cannot "+
"see this when it lands inside a raw string, and the page will serve it.",
path, n+1)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}