2022-08-20 18:53:45 +02:00
package test
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
2022-08-21 13:30:34 +02:00
"github.com/golangci/golangci-lint/pkg/exitcodes"
2022-08-20 18:53:45 +02:00
"github.com/golangci/golangci-lint/test/testshared"
)
2024-03-19 17:38:59 +02:00
//nolint:misspell // misspelling is intentional
2022-08-21 13:30:34 +02:00
const expectedJSONOutput = ` { "Issues":[ { "FromLinter":"misspell","Text":" ` + "`" + ` occured ` + "`" + ` is a misspelling of ` + "`" + ` occurred ` + "`" + ` ","Severity":"","SourceLines":["\t// comment with incorrect spelling: occured // want \" ` + "`" + ` occured ` + "`" + ` is a misspelling of ` + "`" + ` occurred ` + "`" + ` \""],"Replacement": { "NeedOnlyDelete":false,"NewLines":null,"Inline": { "StartCol":37,"Length":7,"NewString":"occurred"}},"Pos": { "Filename":"testdata/misspell.go","Offset":0,"Line":6,"Column":38},"ExpectNoLint":false,"ExpectedNoLintLinter":""}] `
2022-08-20 18:53:45 +02:00
2022-08-21 13:30:34 +02:00
func TestOutput_lineNumber ( t * testing . T ) {
2022-08-20 18:53:45 +02:00
sourcePath := filepath . Join ( testdataDir , "misspell.go" )
testshared . NewRunnerBuilder ( t ) .
WithArgs (
"--disable-all" ,
"--print-issued-lines=false" ,
"--print-linter-name=false" ,
2022-08-21 13:30:34 +02:00
"--out-format=line-number" ,
2022-08-20 18:53:45 +02:00
) .
WithDirectives ( sourcePath ) .
WithTargetPath ( sourcePath ) .
Runner ( ) .
Install ( ) .
Run ( ) .
2024-03-19 17:38:59 +02:00
//nolint:misspell // misspelling is intentional
2022-08-21 13:30:34 +02:00
ExpectHasIssue ( "testdata/misspell.go:6:38: `occured` is a misspelling of `occurred`" )
}
func TestOutput_Stderr ( t * testing . T ) {
sourcePath := filepath . Join ( testdataDir , "misspell.go" )
testshared . NewRunnerBuilder ( t ) .
WithArgs (
"--disable-all" ,
"--print-issued-lines=false" ,
"--print-linter-name=false" ,
"--out-format=json:stderr" ,
) .
WithDirectives ( sourcePath ) .
WithTargetPath ( sourcePath ) .
Runner ( ) .
Install ( ) .
Run ( ) .
2022-08-24 22:10:51 +02:00
ExpectHasIssue ( testshared . NormalizeFilePathInJSON ( expectedJSONOutput ) )
2022-08-20 18:53:45 +02:00
}
func TestOutput_File ( t * testing . T ) {
2022-08-24 22:10:51 +02:00
resultPath := filepath . Join ( t . TempDir ( ) , "golangci_lint_test_result" )
2022-08-20 18:53:45 +02:00
sourcePath := filepath . Join ( testdataDir , "misspell.go" )
testshared . NewRunnerBuilder ( t ) .
WithArgs (
"--disable-all" ,
"--print-issued-lines=false" ,
"--print-linter-name=false" ,
2022-08-21 13:30:34 +02:00
fmt . Sprintf ( "--out-format=json:%s" , resultPath ) ,
2022-08-20 18:53:45 +02:00
) .
WithDirectives ( sourcePath ) .
WithTargetPath ( sourcePath ) .
Runner ( ) .
Install ( ) .
Run ( ) .
2022-08-21 13:30:34 +02:00
ExpectExitCode ( exitcodes . IssuesFound )
2022-08-20 18:53:45 +02:00
b , err := os . ReadFile ( resultPath )
require . NoError ( t , err )
2022-08-24 22:10:51 +02:00
require . Contains ( t , string ( b ) , testshared . NormalizeFilePathInJSON ( expectedJSONOutput ) )
2022-08-20 18:53:45 +02:00
}
func TestOutput_Multiple ( t * testing . T ) {
sourcePath := filepath . Join ( testdataDir , "misspell.go" )
testshared . NewRunnerBuilder ( t ) .
WithArgs (
"--disable-all" ,
"--print-issued-lines=false" ,
"--print-linter-name=false" ,
"--out-format=line-number,json:stdout" ,
) .
WithDirectives ( sourcePath ) .
WithTargetPath ( sourcePath ) .
Runner ( ) .
Install ( ) .
Run ( ) .
2024-03-19 17:38:59 +02:00
//nolint:misspell // misspelling is intentional
2022-08-20 18:53:45 +02:00
ExpectHasIssue ( "testdata/misspell.go:6:38: `occured` is a misspelling of `occurred`" ) .
2022-08-24 22:10:51 +02:00
ExpectOutputContains ( testshared . NormalizeFilePathInJSON ( expectedJSONOutput ) )
2022-08-20 18:53:45 +02:00
}