lint_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2021 Google Inc. 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. package java
  15. import (
  16. "strings"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func TestJavaLint(t *testing.T) {
  21. ctx, _ := testJavaWithFS(t, `
  22. java_library {
  23. name: "foo",
  24. srcs: [
  25. "a.java",
  26. "b.java",
  27. "c.java",
  28. ],
  29. min_sdk_version: "29",
  30. sdk_version: "system_current",
  31. }
  32. `, map[string][]byte{
  33. "lint-baseline.xml": nil,
  34. })
  35. foo := ctx.ModuleForTests("foo", "android_common")
  36. sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
  37. if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
  38. t.Error("did not pass --baseline flag")
  39. }
  40. }
  41. func TestJavaLintWithoutBaseline(t *testing.T) {
  42. ctx, _ := testJavaWithFS(t, `
  43. java_library {
  44. name: "foo",
  45. srcs: [
  46. "a.java",
  47. "b.java",
  48. "c.java",
  49. ],
  50. min_sdk_version: "29",
  51. sdk_version: "system_current",
  52. }
  53. `, map[string][]byte{})
  54. foo := ctx.ModuleForTests("foo", "android_common")
  55. sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
  56. if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
  57. t.Error("passed --baseline flag for non existent file")
  58. }
  59. }
  60. func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
  61. android.GroupFixturePreparers(
  62. PrepareForTestWithJavaDefaultModules,
  63. android.PrepareForTestDisallowNonExistentPaths,
  64. ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})).
  65. RunTestWithBp(t, `
  66. java_library {
  67. name: "foo",
  68. srcs: [
  69. ],
  70. min_sdk_version: "29",
  71. sdk_version: "system_current",
  72. lint: {
  73. baseline_filename: "mybaseline.xml",
  74. },
  75. }
  76. `)
  77. }
  78. func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
  79. ctx, _ := testJavaWithFS(t, `
  80. java_library {
  81. name: "foo",
  82. srcs: [
  83. "a.java",
  84. "b.java",
  85. "c.java",
  86. ],
  87. min_sdk_version: "29",
  88. sdk_version: "system_current",
  89. lint: {
  90. error_checks: ["SomeCheck"],
  91. baseline_filename: "mybaseline.xml",
  92. },
  93. }
  94. `, map[string][]byte{
  95. "mybaseline.xml": nil,
  96. })
  97. foo := ctx.ModuleForTests("foo", "android_common")
  98. sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
  99. if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
  100. t.Error("did not use the correct file for baseline")
  101. }
  102. if !strings.Contains(*sboxProto.Commands[0].Command, "--warning_check NewApi") {
  103. // TODO(b/268261262): Change this to check for --error_check
  104. t.Error("should check NewApi warnings")
  105. }
  106. if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check SomeCheck") {
  107. t.Error("should combine NewApi errors with SomeCheck errors")
  108. }
  109. }
  110. func TestJavaLintBypassUpdatableChecks(t *testing.T) {
  111. testCases := []struct {
  112. name string
  113. bp string
  114. error string
  115. }{
  116. {
  117. name: "warning_checks",
  118. bp: `
  119. java_library {
  120. name: "foo",
  121. srcs: [
  122. "a.java",
  123. ],
  124. min_sdk_version: "29",
  125. sdk_version: "current",
  126. lint: {
  127. warning_checks: ["NewApi"],
  128. },
  129. }
  130. `,
  131. error: "lint.warning_checks: Can't treat \\[NewApi\\] checks as warnings if min_sdk_version is different from sdk_version.",
  132. },
  133. {
  134. name: "disable_checks",
  135. bp: `
  136. java_library {
  137. name: "foo",
  138. srcs: [
  139. "a.java",
  140. ],
  141. min_sdk_version: "29",
  142. sdk_version: "current",
  143. lint: {
  144. disabled_checks: ["NewApi"],
  145. },
  146. }
  147. `,
  148. error: "lint.disabled_checks: Can't disable \\[NewApi\\] checks if min_sdk_version is different from sdk_version.",
  149. },
  150. }
  151. for _, testCase := range testCases {
  152. t.Run(testCase.name, func(t *testing.T) {
  153. errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.error)
  154. android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules).
  155. ExtendWithErrorHandler(errorHandler).
  156. RunTestWithBp(t, testCase.bp)
  157. })
  158. }
  159. }
  160. // TODO(b/193460475): Re-enable this test
  161. //func TestJavaLintStrictUpdatabilityLinting(t *testing.T) {
  162. // bp := `
  163. // java_library {
  164. // name: "foo",
  165. // srcs: [
  166. // "a.java",
  167. // ],
  168. // static_libs: ["bar"],
  169. // min_sdk_version: "29",
  170. // sdk_version: "current",
  171. // lint: {
  172. // strict_updatability_linting: true,
  173. // },
  174. // }
  175. //
  176. // java_library {
  177. // name: "bar",
  178. // srcs: [
  179. // "a.java",
  180. // ],
  181. // min_sdk_version: "29",
  182. // sdk_version: "current",
  183. // }
  184. // `
  185. // fs := android.MockFS{
  186. // "lint-baseline.xml": nil,
  187. // }
  188. //
  189. // result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, fs.AddToFixture()).
  190. // RunTestWithBp(t, bp)
  191. //
  192. // foo := result.ModuleForTests("foo", "android_common")
  193. // sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
  194. // if !strings.Contains(*sboxProto.Commands[0].Command,
  195. // "--baseline lint-baseline.xml --disallowed_issues NewApi") {
  196. // t.Error("did not restrict baselining NewApi")
  197. // }
  198. //
  199. // bar := result.ModuleForTests("bar", "android_common")
  200. // sboxProto = android.RuleBuilderSboxProtoForTests(t, bar.Output("lint.sbox.textproto"))
  201. // if !strings.Contains(*sboxProto.Commands[0].Command,
  202. // "--baseline lint-baseline.xml --disallowed_issues NewApi") {
  203. // t.Error("did not restrict baselining NewApi")
  204. // }
  205. //}
  206. func TestJavaLintDatabaseSelectionFull(t *testing.T) {
  207. testCases := []struct {
  208. sdk_version string
  209. expected_file string
  210. }{
  211. {
  212. "current",
  213. "api_versions_public.xml",
  214. }, {
  215. "core_platform",
  216. "api_versions_public.xml",
  217. }, {
  218. "system_current",
  219. "api_versions_system.xml",
  220. }, {
  221. "module_current",
  222. "api_versions_module_lib.xml",
  223. }, {
  224. "system_server_current",
  225. "api_versions_system_server.xml",
  226. }, {
  227. "S",
  228. "api_versions_public.xml",
  229. }, {
  230. "30",
  231. "api_versions_public.xml",
  232. }, {
  233. "10000",
  234. "api_versions_public.xml",
  235. },
  236. }
  237. bp := `
  238. java_library {
  239. name: "foo",
  240. srcs: [
  241. "a.java",
  242. ],
  243. min_sdk_version: "29",
  244. sdk_version: "XXX",
  245. lint: {
  246. strict_updatability_linting: true,
  247. },
  248. }
  249. `
  250. for _, testCase := range testCases {
  251. thisBp := strings.Replace(bp, "XXX", testCase.sdk_version, 1)
  252. result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, FixtureWithPrebuiltApis(map[string][]string{
  253. "30": {"foo"},
  254. "10000": {"foo"},
  255. })).
  256. RunTestWithBp(t, thisBp)
  257. foo := result.ModuleForTests("foo", "android_common")
  258. sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
  259. if !strings.Contains(*sboxProto.Commands[0].Command, "/"+testCase.expected_file) {
  260. t.Error("did not use full api database for case", testCase)
  261. }
  262. }
  263. }