Skip to content

Commit 9838a57

Browse files
authored
dev: remove old TODO and remove assert import alias on require. (#1838)
1 parent 7a612da commit 9838a57

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

test/fix_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"path/filepath"
88
"testing"
99

10-
assert "github.com/stretchr/testify/require"
10+
"github.com/stretchr/testify/require"
1111
yaml "gopkg.in/yaml.v2"
1212

1313
"github.com/golangci/golangci-lint/test/testshared"
@@ -16,8 +16,8 @@ import (
1616
func TestFix(t *testing.T) {
1717
findSources := func(pathPatterns ...string) []string {
1818
sources, err := filepath.Glob(filepath.Join(pathPatterns...))
19-
assert.NoError(t, err)
20-
assert.NotEmpty(t, sources)
19+
require.NoError(t, err)
20+
require.NotEmpty(t, sources)
2121
return sources
2222
}
2323

@@ -34,7 +34,7 @@ func TestFix(t *testing.T) {
3434

3535
fixDir := filepath.Join(testdataDir, "fix")
3636
err := exec.Command("cp", "-R", fixDir, tmpDir).Run()
37-
assert.NoError(t, err)
37+
require.NoError(t, err)
3838

3939
inputs := findSources(tmpDir, "in", "*.go")
4040
for _, input := range inputs {
@@ -51,16 +51,16 @@ func TestFix(t *testing.T) {
5151
args = append(args, rc.args...)
5252

5353
cfg, err := yaml.Marshal(rc.config)
54-
assert.NoError(t, err)
54+
require.NoError(t, err)
5555

5656
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...)
5757
output, err := ioutil.ReadFile(input)
58-
assert.NoError(t, err)
58+
require.NoError(t, err)
5959

6060
expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
61-
assert.NoError(t, err)
61+
require.NoError(t, err)
6262

63-
assert.Equal(t, string(expectedOutput), string(output))
63+
require.Equal(t, string(expectedOutput), string(output))
6464
})
6565
}
6666
}

test/linters_test.go

+30-31
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"strings"
1010
"testing"
1111

12-
assert "github.com/stretchr/testify/require"
13-
yaml "gopkg.in/yaml.v2"
12+
"github.com/stretchr/testify/require"
13+
"gopkg.in/yaml.v2"
1414

15+
"github.com/golangci/golangci-lint/pkg/exitcodes"
1516
"github.com/golangci/golangci-lint/test/testshared"
1617
)
1718

@@ -21,29 +22,27 @@ func runGoErrchk(c *exec.Cmd, defaultExpectedLinter string, files []string, t *t
2122
// and thus the linter exits with exit code 0. So perform the additional
2223
// assertions only if the error is non-nil.
2324
if err != nil {
24-
_, ok := err.(*exec.ExitError)
25-
assert.True(t, ok, err)
25+
var exitErr *exec.ExitError
26+
require.ErrorAs(t, err, &exitErr)
27+
require.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode())
2628
}
2729

28-
// TODO: uncomment after deprecating go1.11
29-
// assert.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode())
30-
3130
fullshort := make([]string, 0, len(files)*2)
3231
for _, f := range files {
3332
fullshort = append(fullshort, f, filepath.Base(f))
3433
}
3534

3635
err = errorCheck(string(output), false, defaultExpectedLinter, fullshort...)
37-
assert.NoError(t, err)
36+
require.NoError(t, err)
3837
}
3938

4039
func testSourcesFromDir(t *testing.T, dir string) {
4140
t.Log(filepath.Join(dir, "*.go"))
4241

4342
findSources := func(pathPatterns ...string) []string {
4443
sources, err := filepath.Glob(filepath.Join(pathPatterns...))
45-
assert.NoError(t, err)
46-
assert.NotEmpty(t, sources)
44+
require.NoError(t, err)
45+
require.NotEmpty(t, sources)
4746
return sources
4847
}
4948
sources := findSources(dir, "*.go")
@@ -77,7 +76,7 @@ func TestGoimportsLocal(t *testing.T) {
7776
args = append(args, rc.args...)
7877

7978
cfg, err := yaml.Marshal(rc.config)
80-
assert.NoError(t, err)
79+
require.NoError(t, err)
8180

8281
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
8382
ExpectHasIssue("testdata/goimports/goimports.go:8: File is not `goimports`-ed")
@@ -93,27 +92,27 @@ func TestGciLocal(t *testing.T) {
9392
args = append(args, rc.args...)
9493

9594
cfg, err := yaml.Marshal(rc.config)
96-
assert.NoError(t, err)
95+
require.NoError(t, err)
9796

9897
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
9998
ExpectHasIssue("testdata/gci/gci.go:7: File is not `gci`-ed")
10099
}
101100

102101
func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
103102
f, err := ioutil.TempFile("", "golangci_lint_test")
104-
assert.NoError(t, err)
103+
require.NoError(t, err)
105104

106105
cfgPath = f.Name() + ".yml"
107106
err = os.Rename(f.Name(), cfgPath)
108-
assert.NoError(t, err)
107+
require.NoError(t, err)
109108

110109
err = yaml.NewEncoder(f).Encode(cfg)
111-
assert.NoError(t, err)
110+
require.NoError(t, err)
112111

113112
return cfgPath, func() {
114-
assert.NoError(t, f.Close())
113+
require.NoError(t, f.Close())
115114
if os.Getenv("GL_KEEP_TEMP_FILES") != "1" {
116-
assert.NoError(t, os.Remove(cfgPath))
115+
require.NoError(t, os.Remove(cfgPath))
117116
}
118117
}
119118
}
@@ -168,10 +167,10 @@ type runContext struct {
168167

169168
func buildConfigFromShortRepr(t *testing.T, repr string, config map[string]interface{}) {
170169
kv := strings.Split(repr, "=")
171-
assert.Len(t, kv, 2)
170+
require.Len(t, kv, 2)
172171

173172
keyParts := strings.Split(kv[0], ".")
174-
assert.True(t, len(keyParts) >= 2, len(keyParts))
173+
require.True(t, len(keyParts) >= 2, len(keyParts))
175174

176175
lastObj := config
177176
for _, k := range keyParts[:len(keyParts)-1] {
@@ -197,7 +196,7 @@ func skipMultilineComment(scanner *bufio.Scanner) {
197196

198197
func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext {
199198
f, err := os.Open(sourcePath)
200-
assert.NoError(t, err)
199+
require.NoError(t, err)
201200
defer f.Close()
202201

203202
rc := &runContext{}
@@ -218,16 +217,16 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
218217

219218
line = strings.TrimLeft(strings.TrimPrefix(line, "//"), " ")
220219
if strings.HasPrefix(line, "args: ") {
221-
assert.Nil(t, rc.args)
220+
require.Nil(t, rc.args)
222221
args := strings.TrimPrefix(line, "args: ")
223-
assert.NotEmpty(t, args)
222+
require.NotEmpty(t, args)
224223
rc.args = strings.Split(args, " ")
225224
continue
226225
}
227226

228227
if strings.HasPrefix(line, "config: ") {
229228
repr := strings.TrimPrefix(line, "config: ")
230-
assert.NotEmpty(t, repr)
229+
require.NotEmpty(t, repr)
231230
if rc.config == nil {
232231
rc.config = map[string]interface{}{}
233232
}
@@ -237,27 +236,27 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
237236

238237
if strings.HasPrefix(line, "config_path: ") {
239238
configPath := strings.TrimPrefix(line, "config_path: ")
240-
assert.NotEmpty(t, configPath)
239+
require.NotEmpty(t, configPath)
241240
rc.configPath = configPath
242241
continue
243242
}
244243

245244
if strings.HasPrefix(line, "expected_linter: ") {
246245
expectedLinter := strings.TrimPrefix(line, "expected_linter: ")
247-
assert.NotEmpty(t, expectedLinter)
246+
require.NotEmpty(t, expectedLinter)
248247
rc.expectedLinter = expectedLinter
249248
continue
250249
}
251250

252-
assert.Fail(t, "invalid prefix of comment line %s", line)
251+
require.Fail(t, "invalid prefix of comment line %s", line)
253252
}
254253

255254
// guess the expected linter if none is specified
256255
if rc.expectedLinter == "" {
257256
for _, arg := range rc.args {
258257
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
259258
if rc.expectedLinter != "" {
260-
assert.Fail(t, "could not infer expected linter for errors because multiple linters are enabled. Please use the `expected_linter: ` directive in your test to indicate the linter-under-test.") //nolint:lll
259+
require.Fail(t, "could not infer expected linter for errors because multiple linters are enabled. Please use the `expected_linter: ` directive in your test to indicate the linter-under-test.") //nolint:lll
261260
break
262261
}
263262
rc.expectedLinter = arg[2:]
@@ -270,7 +269,7 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
270269

271270
func TestExtractRunContextFromComments(t *testing.T) {
272271
rc := extractRunContextFromComments(t, filepath.Join(testdataDir, "goimports", "goimports.go"))
273-
assert.Equal(t, []string{"-Egoimports"}, rc.args)
272+
require.Equal(t, []string{"-Egoimports"}, rc.args)
274273
}
275274

276275
func TestTparallel(t *testing.T) {
@@ -284,7 +283,7 @@ func TestTparallel(t *testing.T) {
284283
args = append(args, rc.args...)
285284

286285
cfg, err := yaml.Marshal(rc.config)
287-
assert.NoError(t, err)
286+
require.NoError(t, err)
288287

289288
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
290289
ExpectHasIssue(
@@ -302,7 +301,7 @@ func TestTparallel(t *testing.T) {
302301
args = append(args, rc.args...)
303302

304303
cfg, err := yaml.Marshal(rc.config)
305-
assert.NoError(t, err)
304+
require.NoError(t, err)
306305

307306
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
308307
ExpectHasIssue(
@@ -320,7 +319,7 @@ func TestTparallel(t *testing.T) {
320319
args = append(args, rc.args...)
321320

322321
cfg, err := yaml.Marshal(rc.config)
323-
assert.NoError(t, err)
322+
require.NoError(t, err)
324323

325324
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).ExpectNoIssues()
326325
})

0 commit comments

Comments
 (0)