Add tparallel linter ()

This commit is contained in:
Melvin 2020-10-02 13:00:46 -07:00 committed by GitHub
parent c88841dac4
commit 926e76dcf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 130 additions and 8 deletions

View file

@ -252,11 +252,55 @@ func TestExtractRunContextFromComments(t *testing.T) {
assert.Equal(t, []string{"-Egoimports"}, rc.args)
}
func TestGolintConsumesXTestFiles(t *testing.T) {
dir := getTestDataDir("withxtest")
const expIssue = "`if` block ends with a `return` statement, so drop this `else` and outdent its block"
func TestTparallel(t *testing.T) {
t.Run("should fail on missing top-level Parallel()", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "missing_toplevel_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
args = append(args, rc.args...)
r := testshared.NewLintRunner(t)
r.Run("--no-config", "--disable-all", "-Egolint", dir).ExpectHasIssue(expIssue)
r.Run("--no-config", "--disable-all", "-Egolint", filepath.Join(dir, "p_test.go")).ExpectHasIssue(expIssue)
cfg, err := yaml.Marshal(rc.config)
assert.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue(
"testdata/tparallel/missing_toplevel_test.go:7:6: TestTopLevel should call t.Parallel on the top level as well as its subtests\n",
)
})
t.Run("should fail on missing subtest Parallel()", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "missing_subtest_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config)
assert.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).
ExpectHasIssue(
"testdata/tparallel/missing_subtest_test.go:7:6: TestSubtests's subtests should call t.Parallel\n",
)
})
t.Run("should pass on parallel test with no subtests", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "happy_path_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
sourcePath,
}
rc := extractRunContextFromComments(t, sourcePath)
args = append(args, rc.args...)
cfg, err := yaml.Marshal(rc.config)
assert.NoError(t, err)
testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...).ExpectNoIssues()
})
}