Skip to content

Commit

Permalink
Add tests for stderr and file outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks committed Nov 30, 2021
1 parent a534f0a commit 2f423b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
50 changes: 49 additions & 1 deletion test/linters_test.go
Expand Up @@ -2,6 +2,8 @@ package test

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -100,7 +102,7 @@ func TestGciLocal(t *testing.T) {
func TestMultipleOutputs(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "gci", "gci.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number,json",
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number,json:stdout",
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
Expand All @@ -114,6 +116,52 @@ func TestMultipleOutputs(t *testing.T) {
ExpectOutputContains(`"Issues":[`)
}

func TestStderrOutput(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "gci", "gci.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number,json:stderr",
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
require.NoError(t, err)

testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue("testdata/gci/gci.go:7: File is not `gci`-ed").
ExpectOutputContains(`"Issues":[`)
}

func TestFileOutput(t *testing.T) {
f, err := os.CreateTemp("", "golangci_lint_test_result")
require.NoError(t, err)
f.Close()

resultPath := f.Name()
defer os.Remove(resultPath)

sourcePath := filepath.Join(testdataDir, "gci", "gci.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false",
fmt.Sprintf("--out-format=json:%s,line-number", resultPath),
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
require.NoError(t, err)

testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue("testdata/gci/gci.go:7: File is not `gci`-ed").
ExpectOutputNotContains(`"Issues":[`)

b, err := ioutil.ReadFile(resultPath)
require.NoError(t, err)
require.Contains(t, string(b), `"Issues":[`)
}

func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
f, err := os.CreateTemp("", "golangci_lint_test")
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions test/testshared/testshared.go
Expand Up @@ -76,6 +76,11 @@ func (r *RunResult) ExpectOutputContains(s string) *RunResult {
return r
}

func (r *RunResult) ExpectOutputNotContains(s string) *RunResult {
assert.NotContains(r.t, r.output, s, "exit code is %d", r.exitCode)
return r
}

func (r *RunResult) ExpectOutputEq(s string) *RunResult {
assert.Equal(r.t, s, r.output, "exit code is %d", r.exitCode)
return r
Expand Down

0 comments on commit 2f423b6

Please sign in to comment.