Skip to content

Commit e7029c7

Browse files
committed
add a test for error messages
1 parent b93cdb4 commit e7029c7

9 files changed

Lines changed: 94 additions & 5 deletions

File tree

gazelle/python/generate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func getMappedKind(c *config.Config, kind string) string {
5858

5959
// kindMatches returns whether r matches the canonical Python rule kind `expected`, respecting `# gazelle:map_kind` and
6060
// `# gazelle:alias_kind` directives in the config.Config c.
61-
func kindMatches(r *rule.Rule, expected string, c *config.Config) bool {
61+
func kindMatches(c *config.Config, r *rule.Rule, expected string) bool {
6262
kind := r.Kind()
6363
return kind == getMappedKind(c, expected) || c.AliasMap[kind] == expected
6464
}
@@ -320,7 +320,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
320320
}
321321
generateEmptyLibrary := false
322322
for _, r := range args.File.Rules {
323-
if r.Name() == pyLibraryTargetName && kindMatches(r, pyLibraryKind, args.Config) {
323+
if r.Name() == pyLibraryTargetName && kindMatches(args.Config, r, pyLibraryKind) {
324324
generateEmptyLibrary = true
325325
}
326326
}
@@ -579,7 +579,7 @@ func (py *Python) getRulesWithInvalidSrcs(args language.GenerateArgs, validFiles
579579
return strings.HasPrefix(src, "@") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, ":")
580580
}
581581
for _, existingRule := range args.File.Rules {
582-
if !kindMatches(existingRule, pyBinaryKind, args.Config) {
582+
if !kindMatches(args.Config, existingRule, pyBinaryKind) {
583583
continue
584584
}
585585
var hasValidSrcs bool
@@ -660,7 +660,7 @@ func ensureNoCollision(c *config.Config, file *rule.File, targetName, kind strin
660660
return nil
661661
}
662662
for _, t := range file.Rules {
663-
if t.Name() == targetName && !kindMatches(t, kind, c) {
663+
if t.Name() == targetName && !kindMatches(c, t, kind) {
664664
return fmt.Errorf("a target of kind %q with the same name already exists", t.Kind())
665665
}
666666
}
@@ -683,7 +683,7 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
683683
pyProtoRulesForProto := map[string]string{}
684684
if args.File != nil {
685685
for _, r := range args.File.Rules {
686-
if kindMatches(r, pyProtoLibraryKind, args.Config) {
686+
if kindMatches(args.Config, r, pyProtoLibraryKind) {
687687
pyProtoRules[r.Name()] = false
688688

689689
protos := r.AttrStrings("deps")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# gazelle:map_kind py_library my_lib :mytest.bzl
2+
# gazelle:map_kind py_binary my_bin :mytest.bzl
3+
# gazelle:map_kind py_test my_test :mytest.bzl
4+
5+
py_library(name = "naming_convention_mapped_fail")
6+
7+
py_binary(name = "naming_convention_mapped_fail_bin")
8+
9+
py_test(name = "naming_convention_mapped_fail_test")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# gazelle:map_kind py_library my_lib :mytest.bzl
2+
# gazelle:map_kind py_binary my_bin :mytest.bzl
3+
# gazelle:map_kind py_test my_test :mytest.bzl
4+
5+
py_library(name = "naming_convention_mapped_fail")
6+
7+
py_binary(name = "naming_convention_mapped_fail_bin")
8+
9+
py_test(name = "naming_convention_mapped_fail_test")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Naming convention fail with map_kind
2+
3+
This test case asserts that collision error messages reference the mapped rule
4+
kind over than the canonical Python kind.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# For test purposes only.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# For test purposes only.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# For test purposes only.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
expect:
17+
exit_code: 1
18+
stderr: |
19+
gazelle: ERROR: failed to generate target "//:naming_convention_mapped_fail" of kind "my_lib": a target of kind "py_library" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention.
20+
gazelle: ERROR: failed to generate target "//:naming_convention_mapped_fail_bin" of kind "my_bin": a target of kind "py_binary" with the same name already exists. Use the '# gazelle:python_binary_naming_convention' directive to change the naming convention.
21+
gazelle: ERROR: failed to generate target "//:naming_convention_mapped_fail_test" of kind "my_test": a target of kind "py_test" with the same name already exists. Use the '# gazelle:python_test_naming_convention' directive to change the naming convention.

0 commit comments

Comments
 (0)