github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/text_test.go (about) 1 package printers 2 3 import ( 4 "bytes" 5 "go/token" 6 "testing" 7 8 "github.com/fatih/color" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/vanstinator/golangci-lint/pkg/logutils" 13 "github.com/vanstinator/golangci-lint/pkg/result" 14 ) 15 16 func TestText_Print(t *testing.T) { 17 // force color globally 18 backup := color.NoColor 19 t.Cleanup(func() { 20 color.NoColor = backup 21 }) 22 color.NoColor = false 23 24 issues := []result.Issue{ 25 { 26 FromLinter: "linter-a", 27 Severity: "warning", 28 Text: "some issue", 29 Pos: token.Position{ 30 Filename: "path/to/filea.go", 31 Offset: 2, 32 Line: 10, 33 Column: 4, 34 }, 35 }, 36 { 37 FromLinter: "linter-b", 38 Severity: "error", 39 Text: "another issue", 40 SourceLines: []string{ 41 "func foo() {", 42 "\tfmt.Println(\"bar\")", 43 "}", 44 }, 45 Pos: token.Position{ 46 Filename: "path/to/fileb.go", 47 Offset: 5, 48 Line: 300, 49 Column: 9, 50 }, 51 }, 52 } 53 54 testCases := []struct { 55 desc string 56 printIssuedLine bool 57 printLinterName bool 58 useColors bool 59 expected string 60 }{ 61 { 62 desc: "printIssuedLine and printLinterName", 63 printIssuedLine: true, 64 printLinterName: true, 65 useColors: false, 66 expected: `path/to/filea.go:10:4: some issue (linter-a) 67 path/to/fileb.go:300:9: another issue (linter-b) 68 func foo() { 69 fmt.Println("bar") 70 } 71 `, 72 }, 73 { 74 desc: "printLinterName only", 75 printIssuedLine: false, 76 printLinterName: true, 77 useColors: false, 78 expected: `path/to/filea.go:10:4: some issue (linter-a) 79 path/to/fileb.go:300:9: another issue (linter-b) 80 `, 81 }, 82 { 83 desc: "printIssuedLine only", 84 printIssuedLine: true, 85 printLinterName: false, 86 useColors: false, 87 expected: `path/to/filea.go:10:4: some issue 88 path/to/fileb.go:300:9: another issue 89 func foo() { 90 fmt.Println("bar") 91 } 92 `, 93 }, 94 { 95 desc: "enable all options", 96 printIssuedLine: true, 97 printLinterName: true, 98 useColors: true, 99 //nolint:lll // color characters must be in a simple string. 100 expected: "\x1b[1mpath/to/filea.go:10\x1b[22m:4: \x1b[31msome issue\x1b[0m (linter-a)\n\x1b[1mpath/to/fileb.go:300\x1b[22m:9: \x1b[31manother issue\x1b[0m (linter-b)\nfunc foo() {\n\tfmt.Println(\"bar\")\n}\n", 101 }, 102 { 103 desc: "disable all options", 104 printIssuedLine: false, 105 printLinterName: false, 106 useColors: false, 107 expected: `path/to/filea.go:10:4: some issue 108 path/to/fileb.go:300:9: another issue 109 `, 110 }, 111 } 112 113 for _, test := range testCases { 114 test := test 115 t.Run(test.desc, func(t *testing.T) { 116 t.Parallel() 117 118 buf := new(bytes.Buffer) 119 120 printer := NewText(test.printIssuedLine, test.useColors, test.printLinterName, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf) 121 122 err := printer.Print(issues) 123 require.NoError(t, err) 124 125 assert.Equal(t, test.expected, buf.String()) 126 }) 127 } 128 }