diff --git a/build/BUILD.bazel b/build/BUILD.bazel index fed2e1c7f4..a6171ef57a 100644 --- a/build/BUILD.bazel +++ b/build/BUILD.bazel @@ -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": [ diff --git a/build/linter/toomanytests/BUILD.bazel b/build/linter/toomanytests/BUILD.bazel new file mode 100644 index 0000000000..026c41b68a --- /dev/null +++ b/build/linter/toomanytests/BUILD.bazel @@ -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"], +) diff --git a/build/linter/toomanytests/analyze.go b/build/linter/toomanytests/analyze.go new file mode 100644 index 0000000000..e6530eeb28 --- /dev/null +++ b/build/linter/toomanytests/analyze.go @@ -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 + }, +} diff --git a/build/nogo_config.json b/build/nogo_config.json index 8594b8319e..916053d73a 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -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",