Skip to content

Commit

Permalink
Fix load placement for files with docstring and copyright header (#1329)
Browse files Browse the repository at this point in the history
Also add missing setup calls for other tests.
  • Loading branch information
fmeum authored Jan 30, 2025
1 parent 1d76340 commit 635c122
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
8 changes: 4 additions & 4 deletions warn/warn_bazel_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func insertLoad(f *build.File, module string, symbols []string) *LinterReplaceme
i := 0
for i = range f.Stmt {
stmt := f.Stmt[i]
_, isComment := stmt.(*build.CommentBlock)
_, isString := stmt.(*build.StringExpr)
isDocString := isString && i == 0
if !isComment && !isDocString {
if _, isComment := stmt.(*build.CommentBlock); isComment {
continue
}
if _, isDocString := stmt.(*build.StringExpr); !isDocString {
// Insert a nil statement here
break
}
Expand Down
68 changes: 68 additions & 0 deletions warn/warn_bazel_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def macro():
}

func TestNativeShBinaryWarning(t *testing.T) {
defer setUpFileReader(nil)()

checkFindingsAndFix(t, "native-sh-binary", `
"""My file"""
Expand All @@ -799,6 +801,8 @@ sh_binary()
}

func TestNativeShLibraryWarning(t *testing.T) {
defer setUpFileReader(nil)()

checkFindingsAndFix(t, "native-sh-library", `
"""My file"""
Expand All @@ -824,6 +828,8 @@ sh_library()
}

func TestNativeShTestWarning(t *testing.T) {
defer setUpFileReader(nil)()

checkFindingsAndFix(t, "native-sh-test", `
"""My file"""
Expand All @@ -848,6 +854,68 @@ sh_test()
scopeBzl|scopeBuild)
}

func TestNativeWarningLoadPlacedAfterFileDocstringWithComment(t *testing.T) {
defer setUpFileReader(nil)()

checkFindingsAndFix(t, "native-sh-binary,native-java-binary", `
# Copyright 2020 Google LLC
#
# 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
#
# https://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.
"""My file"""
def macro():
native.sh_binary()
native.java_binary()
java_binary()
sh_binary()
`, `
# Copyright 2020 Google LLC
#
# 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
#
# https://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.
"""My file"""
load("@rules_java//java:java_binary.bzl", "java_binary")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
def macro():
sh_binary()
java_binary()
java_binary()
sh_binary()
`,
[]string{
fmt.Sprintf(`:18: Function "sh_binary" is not global anymore and needs to be loaded from "@rules_shell//shell:sh_binary.bzl".`),
fmt.Sprintf(`:19: Function "java_binary" is not global anymore and needs to be loaded from "@rules_java//java:java_binary.bzl".`),
fmt.Sprintf(`:21: Function "java_binary" is not global anymore and needs to be loaded from "@rules_java//java:java_binary.bzl".`),
fmt.Sprintf(`:22: Function "sh_binary" is not global anymore and needs to be loaded from "@rules_shell//shell:sh_binary.bzl".`),
},
scopeBzl|scopeBuild)
}

func TestKeywordParameters(t *testing.T) {
checkFindingsAndFix(t, "keyword-positional-params", `
foo(key = value)
Expand Down

0 comments on commit 635c122

Please sign in to comment.