github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/printers/github_test.go (about) 1 package printers 2 3 import ( 4 "bytes" 5 "context" 6 "go/token" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/golangci/golangci-lint/pkg/result" 13 ) 14 15 func TestGithub_Print(t *testing.T) { 16 issues := []result.Issue{ 17 { 18 FromLinter: "linter-a", 19 Severity: "warning", 20 Text: "some issue", 21 Pos: token.Position{ 22 Filename: "path/to/filea.go", 23 Offset: 2, 24 Line: 10, 25 Column: 4, 26 }, 27 }, 28 { 29 FromLinter: "linter-b", 30 Severity: "error", 31 Text: "another issue", 32 SourceLines: []string{ 33 "func foo() {", 34 "\tfmt.Println(\"bar\")", 35 "}", 36 }, 37 Pos: token.Position{ 38 Filename: "path/to/fileb.go", 39 Offset: 5, 40 Line: 300, 41 Column: 9, 42 }, 43 }, 44 } 45 46 buf := new(bytes.Buffer) 47 printer := NewGithub(buf) 48 49 err := printer.Print(context.Background(), issues) 50 require.NoError(t, err) 51 52 expected := `::warning file=path/to/filea.go,line=10,col=4::some issue (linter-a) 53 ::error file=path/to/fileb.go,line=300,col=9::another issue (linter-b) 54 ` 55 56 assert.Equal(t, expected, buf.String()) 57 } 58 59 func TestFormatGithubIssue(t *testing.T) { 60 sampleIssue := result.Issue{ 61 FromLinter: "sample-linter", 62 Text: "some issue", 63 Pos: token.Position{ 64 Filename: "path/to/file.go", 65 Offset: 2, 66 Line: 10, 67 Column: 4, 68 }, 69 } 70 require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 71 72 sampleIssue.Pos.Column = 0 73 require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 74 }