kotlin_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2017 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. "strconv"
  17. "strings"
  18. "testing"
  19. "android/soong/android"
  20. )
  21. func TestKotlin(t *testing.T) {
  22. ctx, _ := testJava(t, `
  23. java_library {
  24. name: "foo",
  25. srcs: ["a.java", "b.kt"],
  26. }
  27. java_library {
  28. name: "bar",
  29. srcs: ["b.kt"],
  30. libs: ["foo"],
  31. static_libs: ["baz"],
  32. }
  33. java_library {
  34. name: "baz",
  35. srcs: ["c.java"],
  36. }
  37. `)
  38. fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
  39. fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  40. fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
  41. if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
  42. fooKotlinc.Inputs[1].String() != "b.kt" {
  43. t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
  44. }
  45. if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
  46. t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
  47. }
  48. if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
  49. t.Errorf("foo classpath %v does not contain %q",
  50. fooJavac.Args["classpath"], fooKotlinc.Output.String())
  51. }
  52. if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
  53. t.Errorf("foo jar inputs %v does not contain %q",
  54. fooJar.Inputs.Strings(), fooKotlinc.Output.String())
  55. }
  56. fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
  57. bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
  58. barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
  59. if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
  60. t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
  61. }
  62. if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
  63. t.Errorf(`expected %q in bar implicits %v`,
  64. fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
  65. }
  66. if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
  67. t.Errorf(`expected %q in bar implicits %v`,
  68. bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
  69. }
  70. }
  71. func TestKapt(t *testing.T) {
  72. bp := `
  73. java_library {
  74. name: "foo",
  75. srcs: ["a.java", "b.kt"],
  76. plugins: ["bar", "baz"],
  77. errorprone: {
  78. extra_check_modules: ["my_check"],
  79. },
  80. }
  81. java_plugin {
  82. name: "bar",
  83. processor_class: "com.bar",
  84. srcs: ["b.java"],
  85. }
  86. java_plugin {
  87. name: "baz",
  88. processor_class: "com.baz",
  89. srcs: ["b.java"],
  90. }
  91. java_plugin {
  92. name: "my_check",
  93. srcs: ["b.java"],
  94. }
  95. `
  96. t.Run("", func(t *testing.T) {
  97. ctx, _ := testJava(t, bp)
  98. buildOS := ctx.Config().BuildOS.String()
  99. kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt")
  100. kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
  101. javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
  102. bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
  103. baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String()
  104. // Test that the kotlin and java sources are passed to kapt and kotlinc
  105. if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" {
  106. t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs)
  107. }
  108. if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {
  109. t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
  110. }
  111. // Test that only the java sources are passed to javac
  112. if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
  113. t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
  114. }
  115. // Test that the kapt srcjar is a dependency of kotlinc and javac rules
  116. if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) {
  117. t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings())
  118. }
  119. if !inList(kapt.Output.String(), javac.Implicits.Strings()) {
  120. t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings())
  121. }
  122. // Test that the kapt srcjar is extracted by the kotlinc and javac rules
  123. if kotlinc.Args["srcJars"] != kapt.Output.String() {
  124. t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
  125. }
  126. if javac.Args["srcJars"] != kapt.Output.String() {
  127. t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
  128. }
  129. // Test that the processors are passed to kapt
  130. expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
  131. " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
  132. if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
  133. t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
  134. }
  135. expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
  136. if kapt.Args["kaptProcessor"] != expectedProcessor {
  137. t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
  138. }
  139. // Test that the processors are not passed to javac
  140. if javac.Args["processorpath"] != "" {
  141. t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"])
  142. }
  143. if javac.Args["processor"] != "-proc:none" {
  144. t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
  145. }
  146. })
  147. t.Run("errorprone", func(t *testing.T) {
  148. env := map[string]string{
  149. "RUN_ERROR_PRONE": "true",
  150. }
  151. result := android.GroupFixturePreparers(
  152. PrepareForTestWithJavaDefaultModules,
  153. android.FixtureMergeEnv(env),
  154. ).RunTestWithBp(t, bp)
  155. buildOS := result.Config.BuildOS.String()
  156. kapt := result.ModuleForTests("foo", "android_common").Rule("kapt")
  157. javac := result.ModuleForTests("foo", "android_common").Description("javac")
  158. errorprone := result.ModuleForTests("foo", "android_common").Description("errorprone")
  159. bar := result.ModuleForTests("bar", buildOS+"_common").Description("javac").Output.String()
  160. baz := result.ModuleForTests("baz", buildOS+"_common").Description("javac").Output.String()
  161. myCheck := result.ModuleForTests("my_check", buildOS+"_common").Description("javac").Output.String()
  162. // Test that the errorprone plugins are not passed to kapt
  163. expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
  164. " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
  165. if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
  166. t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
  167. }
  168. expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
  169. if kapt.Args["kaptProcessor"] != expectedProcessor {
  170. t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
  171. }
  172. // Test that the errorprone plugins are not passed to javac
  173. if javac.Args["processorpath"] != "" {
  174. t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"])
  175. }
  176. if javac.Args["processor"] != "-proc:none" {
  177. t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
  178. }
  179. // Test that the errorprone plugins are passed to errorprone
  180. expectedProcessorPath = "-processorpath " + myCheck
  181. if errorprone.Args["processorpath"] != expectedProcessorPath {
  182. t.Errorf("expected processorpath %q, got %q", expectedProcessorPath, errorprone.Args["processorpath"])
  183. }
  184. if errorprone.Args["processor"] != "-proc:none" {
  185. t.Errorf("expected processor '-proc:none', got %q", errorprone.Args["processor"])
  186. }
  187. })
  188. }
  189. func TestKaptEncodeFlags(t *testing.T) {
  190. // Compares the kaptEncodeFlags against the results of the example implementation at
  191. // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding
  192. tests := []struct {
  193. in [][2]string
  194. out string
  195. }{
  196. {
  197. // empty input
  198. in: [][2]string{},
  199. out: "rO0ABXcEAAAAAA==",
  200. },
  201. {
  202. // common input
  203. in: [][2]string{
  204. {"-source", "1.8"},
  205. {"-target", "1.8"},
  206. },
  207. out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=",
  208. },
  209. {
  210. // input that serializes to a 255 byte block
  211. in: [][2]string{
  212. {"-source", "1.8"},
  213. {"-target", "1.8"},
  214. {"a", strings.Repeat("b", 218)},
  215. },
  216. out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi",
  217. },
  218. {
  219. // input that serializes to a 256 byte block
  220. in: [][2]string{
  221. {"-source", "1.8"},
  222. {"-target", "1.8"},
  223. {"a", strings.Repeat("b", 219)},
  224. },
  225. out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==",
  226. },
  227. {
  228. // input that serializes to a 257 byte block
  229. in: [][2]string{
  230. {"-source", "1.8"},
  231. {"-target", "1.8"},
  232. {"a", strings.Repeat("b", 220)},
  233. },
  234. out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=",
  235. },
  236. }
  237. for i, test := range tests {
  238. t.Run(strconv.Itoa(i), func(t *testing.T) {
  239. got := kaptEncodeFlags(test.in)
  240. if got != test.out {
  241. t.Errorf("\nwant %q\n got %q", test.out, got)
  242. }
  243. })
  244. }
  245. }