linter: add too much test checker (#45434)

ref pingcap/tidb#44940
This commit is contained in:
Weizhen Wang
2023-07-18 19:06:46 +08:00
committed by GitHub
parent 49bdaaf283
commit dfa03cd4e1
4 changed files with 74 additions and 0 deletions

View File

@ -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": [

View 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"],
)

View 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
},
}

View File

@ -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",