Check that grpc_package statements have the right name (#27608)

* named-right

* Update check_package_name.py

* Automated change: Fix sanity tests

Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/27627/head
Craig Tiller 3 years ago committed by GitHub
parent 7d2f9c842c
commit 969f3d55ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      test/core/client_channel/resolvers/BUILD
  2. 2
      test/core/resource_quota/BUILD
  3. 2
      test/core/uri/BUILD
  4. 2
      test/cpp/ext/filters/census/BUILD
  5. 74
      tools/run_tests/sanity/check_package_name.py
  6. 1
      tools/run_tests/sanity/sanity_tests.yaml

@ -14,7 +14,7 @@
load("//bazel:grpc_build_system.bzl", "grpc_cc_binary", "grpc_cc_library", "grpc_cc_test", "grpc_package")
grpc_package(name = "test/core/client_channel_resolvers")
grpc_package(name = "test/core/client_channel/resolvers")
licenses(["notice"]) # Apache v2

@ -16,7 +16,7 @@ load("//bazel:grpc_build_system.bzl", "grpc_cc_test", "grpc_package")
licenses(["notice"])
grpc_package(name = "test/core/promise")
grpc_package(name = "test/core/resource_quota")
load("//test/core/util:grpc_fuzzer.bzl", "grpc_proto_fuzzer")

@ -14,7 +14,7 @@
load("//bazel:grpc_build_system.bzl", "grpc_cc_test", "grpc_package")
grpc_package(name = "test/core/client_channel")
grpc_package(name = "test/core/uri")
licenses(["notice"])

@ -17,7 +17,7 @@ load("//bazel:cc_grpc_library.bzl", "cc_grpc_library")
licenses(["notice"]) # Apache v2
grpc_package(name = "test/core/ext/census")
grpc_package(name = "test/cpp/ext/filters/census")
grpc_cc_test(
name = "grpc_opencensus_plugin_test",

@ -0,0 +1,74 @@
#!/usr/bin/env python3
# Copyright 2021 gRPC authors.
#
# 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.
import os
import sys
os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
# Allowance for overrides for specific files
EXPECTED_NAMES = {
'src/proto/grpc/channelz': 'channelz',
'src/proto/grpc/status': 'status',
'src/proto/grpc/testing': 'testing',
'src/proto/grpc/testing/duplicate': 'duplicate',
'src/proto/grpc/lb/v1': 'lb',
'src/proto/grpc/testing/xds': 'xds',
'src/proto/grpc/testing/xds/v3': 'xds_v3',
'src/proto/grpc/core': 'core',
'src/proto/grpc/http_over_grpc': 'http_over_grpc',
'src/proto/grpc/health/v1': 'health',
'src/proto/grpc/reflection/v1alpha': 'reflection',
}
errors = 0
for root, dirs, files in os.walk('.'):
if root.startswith('./'):
root = root[len('./'):]
# don't check third party
if root.startswith('third_party/'):
continue
# only check BUILD files
if 'BUILD' not in files:
continue
text = open('%s/BUILD' % root).read()
# find a grpc_package clause
pkg_start = text.find('grpc_package(')
if pkg_start == -1:
continue
# parse it, taking into account nested parens
pkg_end = pkg_start + len('grpc_package(')
level = 1
while level == 1:
if text[pkg_end] == ')':
level -= 1
elif text[pkg_end] == '(':
level += 1
pkg_end += 1
# it's a python statement, so evaluate it to pull out the name of the package
name = eval(text[pkg_start:pkg_end],
{'grpc_package': lambda name, **kwargs: name})
# the name should be the path within the source tree, excepting some special
# BUILD files (really we should normalize them too at some point)
# TODO(ctiller): normalize all package names
expected_name = EXPECTED_NAMES.get(root, root)
if name != expected_name:
print("%s/BUILD should define a grpc_package with name=%r, not %r" %
(root, expected_name, name))
errors += 1
if errors != 0:
sys.exit(1)

@ -18,6 +18,7 @@
- script: tools/run_tests/sanity/core_banned_functions.py
- script: tools/run_tests/sanity/core_untyped_structs.sh
- script: tools/run_tests/sanity/cpp_banned_constructs.sh
- script: tools/run_tests/sanity/check_package_name.py
- script: tools/buildgen/generate_projects.sh -j 3
cpu_cost: 3
- script: tools/distrib/check_copyright.py

Loading…
Cancel
Save