1
16
17 package warn
18
19 import "testing"
20
21 func TestConstantGlob(t *testing.T) {
22 checkFindings(t, "constant-glob", `
23 cc_library(srcs = glob(["foo.cc"]))
24 cc_library(srcs = glob(["*.cc"]))
25 cc_library(srcs =
26 ["constant"] + glob([
27 "*.cc",
28 "test.cpp",
29 ])
30 )`,
31 []string{`:1: Glob pattern "foo.cc" has no wildcard`,
32 `:6: Glob pattern "test.cpp" has no wildcard`},
33 scopeBuild|scopeBzl|scopeWorkspace)
34 }
35
36 func TestNativeInBuildFiles(t *testing.T) {
37 checkFindingsAndFix(t, "native-build", `
38 native.package("foo")
39
40 native.cc_library(name = "lib")
41 `, `
42 package("foo")
43
44 cc_library(name = "lib")
45 `, []string{
46 `:1: The "native" module shouldn't be used in BUILD files, its members are available as global symbols.`,
47 `:3: The "native" module shouldn't be used in BUILD files, its members are available as global symbols.`,
48 }, scopeBuild)
49 }
50
51 func TestNativePackage(t *testing.T) {
52 checkFindings(t, "native-package", `
53 native.package("foo")
54
55 native.cc_library(name = "lib")
56 `, []string{
57 `:1: "native.package()" shouldn't be used in .bzl files.`,
58 }, scopeBzl)
59 }
60
61 func TestDuplicatedName(t *testing.T) {
62 checkFindings(t, "duplicated-name", `
63 cc_library(name = "x")
64 cc_library(name = "y")
65 py_library(name = "x")
66 py_library(name = "z")
67 php_library(name = "x")`,
68 []string{
69 `:3: A rule with name "x" was already found on line 1`,
70 `:5: A rule with name "x" was already found on line 1`,
71 }, scopeBuild|scopeWorkspace)
72
73 checkFindings(t, "duplicated-name", `
74 exports_files(["foo.txt"])
75 [macro(name = "bar_%s" % i) for i in ii]
76 `,
77 []string{},
78 scopeBuild|scopeWorkspace)
79 }
80
81 func TestPositionalArguments(t *testing.T) {
82 checkFindings(t, "positional-args", `
83 my_macro(foo = "bar")
84 my_macro("foo", "bar")
85 my_macro(foo = bar(x))
86 [my_macro(foo) for foo in bar]`,
87 []string{
88 ":2: All calls to rules or macros should pass arguments by keyword (arg_name=value) syntax.",
89 ":4: All calls to rules or macros should pass arguments by keyword (arg_name=value) syntax.",
90 },
91 scopeBuild)
92
93 checkFindings(t, "positional-args", `
94 register_toolchains(
95 "//foo",
96 "//bar",
97 )`,
98 []string{},
99 scopeBuild)
100 }
101
102 func TestKwargsInBuildFilesWarning(t *testing.T) {
103 checkFindings(t, "build-args-kwargs", `
104 cc_library(
105 name = "foo",
106 *args,
107 **kwargs,
108 )
109
110 foo(*bar(**kgs))`,
111 []string{
112 ":3: *args are not allowed in BUILD files.",
113 ":4: **kwargs are not allowed in BUILD files.",
114 ":7: *args are not allowed in BUILD files.",
115 ":7: **kwargs are not allowed in BUILD files.",
116 },
117 scopeBuild)
118
119 checkFindings(t, "build-args-kwargs", `
120 cc_library(
121 name = "foo",
122 -args,
123 )
124
125 foo(not bar(-kgs))`,
126 []string{},
127 scopeBuild)
128 }
129
130 func TestPrintWarning(t *testing.T) {
131 checkFindings(t, "print", `
132 foo()
133
134 print("foo")
135
136 def f(x):
137 print(x)
138
139 g(x) or print("not g")
140 `,
141 []string{
142 `:3: "print()" is a debug function and shouldn't be submitted.`,
143 `:6: "print()" is a debug function and shouldn't be submitted.`,
144 `:8: "print()" is a debug function and shouldn't be submitted.`,
145 },
146 scopeBazel)
147 }
148
View as plain text