@ -161,6 +161,7 @@ nogo(
|
||||
"//build/linter/predeclared",
|
||||
"//build/linter/unconvert",
|
||||
"//build/linter/rowserrcheck",
|
||||
"//build/linter/toomanytests",
|
||||
] + staticcheck_analyzers(STATICHECK_ANALYZERS) +
|
||||
select({
|
||||
"//build:with_nogo": [
|
||||
|
||||
9
build/linter/toomanytests/BUILD.bazel
Normal file
9
build/linter/toomanytests/BUILD.bazel
Normal file
@ -0,0 +1,9 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "toomanytests",
|
||||
srcs = ["analyze.go"],
|
||||
importpath = "github.com/pingcap/tidb/build/linter/toomanytests",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = ["@org_golang_x_tools//go/analysis"],
|
||||
)
|
||||
47
build/linter/toomanytests/analyze.go
Normal file
47
build/linter/toomanytests/analyze.go
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright 2023 PingCAP, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package toomanytests
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
)
|
||||
|
||||
// Analyzer is the analyzer struct of toomanytests
|
||||
var Analyzer = &analysis.Analyzer{
|
||||
Name: "toomanytests",
|
||||
Doc: "too many tests in the package",
|
||||
Run: func(pass *analysis.Pass) (any, error) {
|
||||
cnt := 0
|
||||
for _, f := range pass.Files {
|
||||
for _, n := range f.Decls {
|
||||
funcDecl, ok := n.(*ast.FuncDecl)
|
||||
if ok {
|
||||
if strings.HasPrefix(funcDecl.Name.Name, "Test") && funcDecl.Recv == nil &&
|
||||
funcDecl.Name.Name != "TestMain" {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
}
|
||||
if cnt > 50 {
|
||||
pass.Reportf(f.Pos(), "%s: Too many test cases in one package", pass.Pkg.Name())
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
@ -585,6 +585,23 @@
|
||||
".*_generated\\.go$": "ignore generated code"
|
||||
}
|
||||
},
|
||||
"toomanytests": {
|
||||
"exclude_files": {
|
||||
"parser/parser.go": "parser/parser.go code",
|
||||
"external/": "no need to vet third party code",
|
||||
".*_generated\\.go$": "ignore generated code",
|
||||
"/cgo/": "ignore cgo code",
|
||||
"/rules_go_work-*": "ignore generated code",
|
||||
".*test_/testmain\\.go$": "ignore generated code",
|
||||
".*failpoint_binding__.go$": "ignore generated code",
|
||||
"util/chunk": "ignore util/chunk"
|
||||
},
|
||||
"only_files": {
|
||||
"disttask/": "disttask code",
|
||||
"timer/": "timer code",
|
||||
"util/": "util code"
|
||||
}
|
||||
},
|
||||
"unconvert": {
|
||||
"exclude_files": {
|
||||
"external/": "no need to vet third party code",
|
||||
|
||||
Reference in New Issue
Block a user