github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/github_test.go (about) 1 //nolint:dupl 2 package printers 3 4 import ( 5 "bytes" 6 "go/token" 7 "runtime" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/vanstinator/golangci-lint/pkg/result" 14 ) 15 16 func TestGithub_Print(t *testing.T) { 17 issues := []result.Issue{ 18 { 19 FromLinter: "linter-a", 20 Severity: "warning", 21 Text: "some issue", 22 Pos: token.Position{ 23 Filename: "path/to/filea.go", 24 Offset: 2, 25 Line: 10, 26 Column: 4, 27 }, 28 }, 29 { 30 FromLinter: "linter-b", 31 Severity: "error", 32 Text: "another issue", 33 SourceLines: []string{ 34 "func foo() {", 35 "\tfmt.Println(\"bar\")", 36 "}", 37 }, 38 Pos: token.Position{ 39 Filename: "path/to/fileb.go", 40 Offset: 5, 41 Line: 300, 42 Column: 9, 43 }, 44 }, 45 } 46 47 buf := new(bytes.Buffer) 48 printer := NewGithub(buf) 49 50 err := printer.Print(issues) 51 require.NoError(t, err) 52 53 expected := `::warning file=path/to/filea.go,line=10,col=4::some issue (linter-a) 54 ::error file=path/to/fileb.go,line=300,col=9::another issue (linter-b) 55 ` 56 57 assert.Equal(t, expected, buf.String()) 58 } 59 60 func TestFormatGithubIssue(t *testing.T) { 61 sampleIssue := result.Issue{ 62 FromLinter: "sample-linter", 63 Text: "some issue", 64 Pos: token.Position{ 65 Filename: "path/to/file.go", 66 Offset: 2, 67 Line: 10, 68 Column: 4, 69 }, 70 } 71 require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 72 73 sampleIssue.Pos.Column = 0 74 require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 75 } 76 77 func TestFormatGithubIssueWindows(t *testing.T) { 78 if runtime.GOOS != "windows" { 79 t.Skip("Skipping test on non Windows") 80 } 81 82 sampleIssue := result.Issue{ 83 FromLinter: "sample-linter", 84 Text: "some issue", 85 Pos: token.Position{ 86 Filename: "path\\to\\file.go", 87 Offset: 2, 88 Line: 10, 89 Column: 4, 90 }, 91 } 92 require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 93 94 sampleIssue.Pos.Column = 0 95 require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) 96 }