github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/test/output_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/chenfeining/golangci-lint/pkg/exitcodes"
    12  	"github.com/chenfeining/golangci-lint/test/testshared"
    13  )
    14  
    15  //nolint:misspell,lll
    16  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":""}]`
    17  
    18  func TestOutput_lineNumber(t *testing.T) {
    19  	sourcePath := filepath.Join(testdataDir, "misspell.go")
    20  
    21  	testshared.NewRunnerBuilder(t).
    22  		WithArgs(
    23  			"--disable-all",
    24  			"--print-issued-lines=false",
    25  			"--print-linter-name=false",
    26  			"--out-format=line-number",
    27  		).
    28  		WithDirectives(sourcePath).
    29  		WithTargetPath(sourcePath).
    30  		Runner().
    31  		Install().
    32  		Run().
    33  		//nolint:misspell
    34  		ExpectHasIssue("testdata/misspell.go:6:38: `occured` is a misspelling of `occurred`")
    35  }
    36  
    37  func TestOutput_Stderr(t *testing.T) {
    38  	sourcePath := filepath.Join(testdataDir, "misspell.go")
    39  
    40  	testshared.NewRunnerBuilder(t).
    41  		WithArgs(
    42  			"--disable-all",
    43  			"--print-issued-lines=false",
    44  			"--print-linter-name=false",
    45  			"--out-format=json:stderr",
    46  		).
    47  		WithDirectives(sourcePath).
    48  		WithTargetPath(sourcePath).
    49  		Runner().
    50  		Install().
    51  		Run().
    52  		ExpectHasIssue(testshared.NormalizeFilePathInJSON(expectedJSONOutput))
    53  }
    54  
    55  func TestOutput_File(t *testing.T) {
    56  	resultPath := filepath.Join(t.TempDir(), "golangci_lint_test_result")
    57  
    58  	sourcePath := filepath.Join(testdataDir, "misspell.go")
    59  
    60  	testshared.NewRunnerBuilder(t).
    61  		WithArgs(
    62  			"--disable-all",
    63  			"--print-issued-lines=false",
    64  			"--print-linter-name=false",
    65  			fmt.Sprintf("--out-format=json:%s", resultPath),
    66  		).
    67  		WithDirectives(sourcePath).
    68  		WithTargetPath(sourcePath).
    69  		Runner().
    70  		Install().
    71  		Run().
    72  		ExpectExitCode(exitcodes.IssuesFound)
    73  
    74  	b, err := os.ReadFile(resultPath)
    75  	require.NoError(t, err)
    76  	require.Contains(t, string(b), testshared.NormalizeFilePathInJSON(expectedJSONOutput))
    77  }
    78  
    79  func TestOutput_Multiple(t *testing.T) {
    80  	sourcePath := filepath.Join(testdataDir, "misspell.go")
    81  
    82  	testshared.NewRunnerBuilder(t).
    83  		WithArgs(
    84  			"--disable-all",
    85  			"--print-issued-lines=false",
    86  			"--print-linter-name=false",
    87  			"--out-format=line-number,json:stdout",
    88  		).
    89  		WithDirectives(sourcePath).
    90  		WithTargetPath(sourcePath).
    91  		Runner().
    92  		Install().
    93  		Run().
    94  		//nolint:misspell
    95  		ExpectHasIssue("testdata/misspell.go:6:38: `occured` is a misspelling of `occurred`").
    96  		ExpectOutputContains(testshared.NormalizeFilePathInJSON(expectedJSONOutput))
    97  }