golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/logparser/parse_test.go (about) 1 // Copyright 2022 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 logparser 6 7 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "golang.org/x/build/internal/diff" 16 ) 17 18 func Test(t *testing.T) { 19 // testdata/x.log is a build log, and 20 // testdata/x.fail is fmtFails(Parse(log)). 21 // Check that we get the same result as in x.fail. 22 files, _ := filepath.Glob("testdata/*.log") 23 if len(files) == 0 { 24 t.Fatalf("no testdata") 25 } 26 27 for _, file := range files { 28 t.Run(filepath.Base(file), func(t *testing.T) { 29 data, err := os.ReadFile(file) 30 if err != nil { 31 t.Fatal(err) 32 } 33 want, err := os.ReadFile(strings.TrimSuffix(file, ".log") + ".fail") 34 if err != nil { 35 t.Fatal(err) 36 } 37 have := fmtFails(Parse(string(data))) 38 if !bytes.Equal(have, want) { 39 t.Errorf("mismatch:\n%s", diff.Diff("want", want, "have", have)) 40 } 41 }) 42 } 43 } 44 45 func fmtFails(fails []*Fail) []byte { 46 var b bytes.Buffer 47 for i, f := range fails { 48 if i > 0 { 49 fmt.Fprintf(&b, "---\n") 50 } 51 fmt.Fprintf(&b, "Section: %q\nPkg: %q\nTest: %q\nMode: %q\n", f.Section, f.Pkg, f.Test, f.Mode) 52 fmt.Fprintf(&b, "Snippet:\n%s", indent(f.Snippet)) 53 fmt.Fprintf(&b, "Output:\n%s", indent(f.Output)) 54 } 55 return b.Bytes() 56 } 57 58 // indent indents s with a leading tab on every line. 59 func indent(s string) string { 60 s = strings.TrimRight(s, "\n") 61 if s == "" { 62 return "" 63 } 64 s = "\t" + strings.ReplaceAll(s, "\n", "\n\t") + "\n" 65 s = strings.ReplaceAll(s, "\t\n", "\n") 66 return s 67 }