| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 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 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | import ( | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 18 | 	"strconv" | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 19 | 	"strings" | 
 | 20 | 	"testing" | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 21 |  | 
 | 22 | 	"android/soong/android" | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 23 | ) | 
 | 24 |  | 
 | 25 | func TestKotlin(t *testing.T) { | 
| Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 26 | 	ctx, _ := testJava(t, ` | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 27 | 		java_library { | 
 | 28 | 			name: "foo", | 
 | 29 | 			srcs: ["a.java", "b.kt"], | 
 | 30 | 		} | 
 | 31 |  | 
 | 32 | 		java_library { | 
 | 33 | 			name: "bar", | 
 | 34 | 			srcs: ["b.kt"], | 
 | 35 | 			libs: ["foo"], | 
 | 36 | 			static_libs: ["baz"], | 
 | 37 | 		} | 
 | 38 |  | 
 | 39 | 		java_library { | 
 | 40 | 			name: "baz", | 
 | 41 | 			srcs: ["c.java"], | 
 | 42 | 		} | 
 | 43 | 		`) | 
 | 44 |  | 
| Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 45 | 	kotlinStdlib := ctx.ModuleForTests("kotlin-stdlib", "android_common"). | 
 | 46 | 		Output("turbine-combined/kotlin-stdlib.jar").Output | 
| Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 47 | 	kotlinStdlibJdk7 := ctx.ModuleForTests("kotlin-stdlib-jdk7", "android_common"). | 
 | 48 | 		Output("turbine-combined/kotlin-stdlib-jdk7.jar").Output | 
 | 49 | 	kotlinStdlibJdk8 := ctx.ModuleForTests("kotlin-stdlib-jdk8", "android_common"). | 
 | 50 | 		Output("turbine-combined/kotlin-stdlib-jdk8.jar").Output | 
| Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 51 | 	kotlinAnnotations := ctx.ModuleForTests("kotlin-annotations", "android_common"). | 
 | 52 | 		Output("turbine-combined/kotlin-annotations.jar").Output | 
 | 53 |  | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 54 | 	fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") | 
 | 55 | 	fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") | 
 | 56 | 	fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 57 | 	fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") | 
 | 58 |  | 
 | 59 | 	fooKotlincClasses := fooKotlinc.Output | 
 | 60 | 	fooKotlincHeaderClasses := fooKotlinc.ImplicitOutput | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 61 |  | 
 | 62 | 	if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || | 
 | 63 | 		fooKotlinc.Inputs[1].String() != "b.kt" { | 
 | 64 | 		t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) | 
 | 65 | 	} | 
 | 66 |  | 
 | 67 | 	if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { | 
 | 68 | 		t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) | 
 | 69 | 	} | 
 | 70 |  | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 71 | 	if !strings.Contains(fooJavac.Args["classpath"], fooKotlincHeaderClasses.String()) { | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 72 | 		t.Errorf("foo classpath %v does not contain %q", | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 73 | 			fooJavac.Args["classpath"], fooKotlincHeaderClasses.String()) | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 74 | 	} | 
 | 75 |  | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 76 | 	if !inList(fooKotlincClasses.String(), fooJar.Inputs.Strings()) { | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 77 | 		t.Errorf("foo jar inputs %v does not contain %q", | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 78 | 			fooJar.Inputs.Strings(), fooKotlincClasses.String()) | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 79 | 	} | 
 | 80 |  | 
| Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 81 | 	if !inList(kotlinStdlib.String(), fooJar.Inputs.Strings()) { | 
 | 82 | 		t.Errorf("foo jar inputs %v does not contain %v", | 
 | 83 | 			fooJar.Inputs.Strings(), kotlinStdlib.String()) | 
 | 84 | 	} | 
 | 85 |  | 
| Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 86 | 	if !inList(kotlinStdlibJdk7.String(), fooJar.Inputs.Strings()) { | 
 | 87 | 		t.Errorf("foo jar inputs %v does not contain %v", | 
 | 88 | 			fooJar.Inputs.Strings(), kotlinStdlibJdk7.String()) | 
 | 89 | 	} | 
 | 90 |  | 
 | 91 | 	if !inList(kotlinStdlibJdk8.String(), fooJar.Inputs.Strings()) { | 
 | 92 | 		t.Errorf("foo jar inputs %v does not contain %v", | 
 | 93 | 			fooJar.Inputs.Strings(), kotlinStdlibJdk8.String()) | 
 | 94 | 	} | 
 | 95 |  | 
| Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 96 | 	if !inList(kotlinAnnotations.String(), fooJar.Inputs.Strings()) { | 
 | 97 | 		t.Errorf("foo jar inputs %v does not contain %v", | 
 | 98 | 			fooJar.Inputs.Strings(), kotlinAnnotations.String()) | 
 | 99 | 	} | 
 | 100 |  | 
| Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 101 | 	if !inList(fooKotlincHeaderClasses.String(), fooHeaderJar.Inputs.Strings()) { | 
 | 102 | 		t.Errorf("foo header jar inputs %v does not contain %q", | 
 | 103 | 			fooHeaderJar.Inputs.Strings(), fooKotlincHeaderClasses.String()) | 
 | 104 | 	} | 
 | 105 |  | 
| Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 106 | 	bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") | 
 | 107 | 	barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") | 
 | 108 |  | 
 | 109 | 	if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { | 
 | 110 | 		t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) | 
 | 111 | 	} | 
 | 112 |  | 
 | 113 | 	if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { | 
 | 114 | 		t.Errorf(`expected %q in bar implicits %v`, | 
 | 115 | 			fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) | 
 | 116 | 	} | 
 | 117 |  | 
 | 118 | 	if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { | 
 | 119 | 		t.Errorf(`expected %q in bar implicits %v`, | 
 | 120 | 			bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) | 
 | 121 | 	} | 
 | 122 | } | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 123 |  | 
 | 124 | func TestKapt(t *testing.T) { | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 125 | 	bp := ` | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 126 | 		java_library { | 
 | 127 | 			name: "foo", | 
 | 128 | 			srcs: ["a.java", "b.kt"], | 
| Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 129 | 			plugins: ["bar", "baz"], | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 130 | 			errorprone: { | 
 | 131 | 				extra_check_modules: ["my_check"], | 
 | 132 | 			}, | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 133 | 		} | 
 | 134 |  | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 135 | 		java_plugin { | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 136 | 			name: "bar", | 
| Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 137 | 			processor_class: "com.bar", | 
 | 138 | 			srcs: ["b.java"], | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 139 | 		} | 
| Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 140 |  | 
 | 141 | 		java_plugin { | 
 | 142 | 			name: "baz", | 
 | 143 | 			processor_class: "com.baz", | 
 | 144 | 			srcs: ["b.java"], | 
 | 145 | 		} | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 146 |  | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 147 | 		java_plugin { | 
 | 148 | 			name: "my_check", | 
 | 149 | 			srcs: ["b.java"], | 
 | 150 | 		} | 
 | 151 | 	` | 
 | 152 | 	t.Run("", func(t *testing.T) { | 
 | 153 | 		ctx, _ := testJava(t, bp) | 
| Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 154 |  | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 155 | 		buildOS := ctx.Config().BuildOS.String() | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 156 |  | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 157 | 		foo := ctx.ModuleForTests("foo", "android_common") | 
 | 158 | 		kaptStubs := foo.Rule("kapt") | 
 | 159 | 		turbineApt := foo.Description("turbine apt") | 
 | 160 | 		kotlinc := foo.Rule("kotlinc") | 
 | 161 | 		javac := foo.Rule("javac") | 
| Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 162 |  | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 163 | 		bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() | 
 | 164 | 		baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String() | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 165 |  | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 166 | 		// Test that the kotlin and java sources are passed to kapt and kotlinc | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 167 | 		if len(kaptStubs.Inputs) != 2 || kaptStubs.Inputs[0].String() != "a.java" || kaptStubs.Inputs[1].String() != "b.kt" { | 
 | 168 | 			t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kaptStubs.Inputs) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 169 | 		} | 
 | 170 | 		if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" { | 
 | 171 | 			t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) | 
 | 172 | 		} | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 173 |  | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 174 | 		// Test that only the java sources are passed to turbine-apt and javac | 
 | 175 | 		if len(turbineApt.Inputs) != 1 || turbineApt.Inputs[0].String() != "a.java" { | 
 | 176 | 			t.Errorf(`foo turbine apt inputs %v != ["a.java"]`, turbineApt.Inputs) | 
 | 177 | 		} | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 178 | 		if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { | 
 | 179 | 			t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) | 
 | 180 | 		} | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 181 |  | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 182 | 		// Test that the kapt stubs jar is a dependency of turbine-apt | 
 | 183 | 		if !inList(kaptStubs.Output.String(), turbineApt.Implicits.Strings()) { | 
 | 184 | 			t.Errorf("expected %q in turbine-apt implicits %v", kaptStubs.Output.String(), kotlinc.Implicits.Strings()) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 185 | 		} | 
| Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 186 |  | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 187 | 		// Test that the turbine-apt srcjar is a dependency of kotlinc and javac rules | 
| Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 188 | 		if !inList(turbineApt.Output.String(), kotlinc.Implicits.Strings()) { | 
 | 189 | 			t.Errorf("expected %q in kotlinc implicits %v", turbineApt.Output.String(), kotlinc.Implicits.Strings()) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 190 | 		} | 
| Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 191 | 		if !inList(turbineApt.Output.String(), javac.Implicits.Strings()) { | 
 | 192 | 			t.Errorf("expected %q in javac implicits %v", turbineApt.Output.String(), javac.Implicits.Strings()) | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 193 | 		} | 
 | 194 |  | 
 | 195 | 		// Test that the turbine-apt srcjar is extracted by the kotlinc and javac rules | 
| Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 196 | 		if kotlinc.Args["srcJars"] != turbineApt.Output.String() { | 
 | 197 | 			t.Errorf("expected %q in kotlinc srcjars %v", turbineApt.Output.String(), kotlinc.Args["srcJars"]) | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 198 | 		} | 
| Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 199 | 		if javac.Args["srcJars"] != turbineApt.Output.String() { | 
 | 200 | 			t.Errorf("expected %q in javac srcjars %v", turbineApt.Output.String(), kotlinc.Args["srcJars"]) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 201 | 		} | 
| Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 202 |  | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 203 | 		// Test that the processors are passed to kapt | 
 | 204 | 		expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + | 
 | 205 | 			" -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 206 | 		if kaptStubs.Args["kaptProcessorPath"] != expectedProcessorPath { | 
 | 207 | 			t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kaptStubs.Args["kaptProcessorPath"]) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 208 | 		} | 
 | 209 | 		expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" | 
| Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 210 | 		if kaptStubs.Args["kaptProcessor"] != expectedProcessor { | 
 | 211 | 			t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kaptStubs.Args["kaptProcessor"]) | 
 | 212 | 		} | 
 | 213 |  | 
 | 214 | 		// Test that the processors are passed to turbine-apt | 
 | 215 | 		expectedProcessorPath = "--processorpath " + bar + " " + baz | 
 | 216 | 		if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessorPath) { | 
 | 217 | 			t.Errorf("expected turbine-apt processorpath %q, got %q", expectedProcessorPath, turbineApt.Args["turbineFlags"]) | 
 | 218 | 		} | 
 | 219 | 		expectedProcessor = "--processors com.bar com.baz" | 
 | 220 | 		if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessor) { | 
 | 221 | 			t.Errorf("expected turbine-apt processor %q, got %q", expectedProcessor, turbineApt.Args["turbineFlags"]) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 222 | 		} | 
 | 223 |  | 
 | 224 | 		// Test that the processors are not passed to javac | 
 | 225 | 		if javac.Args["processorpath"] != "" { | 
 | 226 | 			t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) | 
 | 227 | 		} | 
 | 228 | 		if javac.Args["processor"] != "-proc:none" { | 
 | 229 | 			t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) | 
 | 230 | 		} | 
 | 231 | 	}) | 
 | 232 |  | 
 | 233 | 	t.Run("errorprone", func(t *testing.T) { | 
 | 234 | 		env := map[string]string{ | 
 | 235 | 			"RUN_ERROR_PRONE": "true", | 
 | 236 | 		} | 
| Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 237 |  | 
 | 238 | 		result := android.GroupFixturePreparers( | 
 | 239 | 			PrepareForTestWithJavaDefaultModules, | 
 | 240 | 			android.FixtureMergeEnv(env), | 
 | 241 | 		).RunTestWithBp(t, bp) | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 242 |  | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 243 | 		buildOS := result.Config.BuildOS.String() | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 244 |  | 
| Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 245 | 		kapt := result.ModuleForTests("foo", "android_common").Rule("kapt") | 
| Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 246 | 		javac := result.ModuleForTests("foo", "android_common").Description("javac") | 
 | 247 | 		errorprone := result.ModuleForTests("foo", "android_common").Description("errorprone") | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 248 |  | 
| Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 249 | 		bar := result.ModuleForTests("bar", buildOS+"_common").Description("javac").Output.String() | 
 | 250 | 		baz := result.ModuleForTests("baz", buildOS+"_common").Description("javac").Output.String() | 
 | 251 | 		myCheck := result.ModuleForTests("my_check", buildOS+"_common").Description("javac").Output.String() | 
| Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 252 |  | 
 | 253 | 		// Test that the errorprone plugins are not passed to kapt | 
 | 254 | 		expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + | 
 | 255 | 			" -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz | 
 | 256 | 		if kapt.Args["kaptProcessorPath"] != expectedProcessorPath { | 
 | 257 | 			t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"]) | 
 | 258 | 		} | 
 | 259 | 		expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" | 
 | 260 | 		if kapt.Args["kaptProcessor"] != expectedProcessor { | 
 | 261 | 			t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"]) | 
 | 262 | 		} | 
 | 263 |  | 
 | 264 | 		// Test that the errorprone plugins are not passed to javac | 
 | 265 | 		if javac.Args["processorpath"] != "" { | 
 | 266 | 			t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) | 
 | 267 | 		} | 
 | 268 | 		if javac.Args["processor"] != "-proc:none" { | 
 | 269 | 			t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) | 
 | 270 | 		} | 
 | 271 |  | 
 | 272 | 		// Test that the errorprone plugins are passed to errorprone | 
 | 273 | 		expectedProcessorPath = "-processorpath " + myCheck | 
 | 274 | 		if errorprone.Args["processorpath"] != expectedProcessorPath { | 
 | 275 | 			t.Errorf("expected processorpath %q, got %q", expectedProcessorPath, errorprone.Args["processorpath"]) | 
 | 276 | 		} | 
 | 277 | 		if errorprone.Args["processor"] != "-proc:none" { | 
 | 278 | 			t.Errorf("expected processor '-proc:none', got %q", errorprone.Args["processor"]) | 
 | 279 | 		} | 
 | 280 | 	}) | 
| Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 281 | } | 
 | 282 |  | 
 | 283 | func TestKaptEncodeFlags(t *testing.T) { | 
 | 284 | 	// Compares the kaptEncodeFlags against the results of the example implementation at | 
 | 285 | 	// https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding | 
 | 286 | 	tests := []struct { | 
 | 287 | 		in  [][2]string | 
 | 288 | 		out string | 
 | 289 | 	}{ | 
 | 290 | 		{ | 
 | 291 | 			// empty input | 
 | 292 | 			in:  [][2]string{}, | 
 | 293 | 			out: "rO0ABXcEAAAAAA==", | 
 | 294 | 		}, | 
 | 295 | 		{ | 
 | 296 | 			// common input | 
 | 297 | 			in: [][2]string{ | 
 | 298 | 				{"-source", "1.8"}, | 
 | 299 | 				{"-target", "1.8"}, | 
 | 300 | 			}, | 
 | 301 | 			out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=", | 
 | 302 | 		}, | 
 | 303 | 		{ | 
 | 304 | 			// input that serializes to a 255 byte block | 
 | 305 | 			in: [][2]string{ | 
 | 306 | 				{"-source", "1.8"}, | 
 | 307 | 				{"-target", "1.8"}, | 
 | 308 | 				{"a", strings.Repeat("b", 218)}, | 
 | 309 | 			}, | 
 | 310 | 			out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi", | 
 | 311 | 		}, | 
 | 312 | 		{ | 
 | 313 | 			// input that serializes to a 256 byte block | 
 | 314 | 			in: [][2]string{ | 
 | 315 | 				{"-source", "1.8"}, | 
 | 316 | 				{"-target", "1.8"}, | 
 | 317 | 				{"a", strings.Repeat("b", 219)}, | 
 | 318 | 			}, | 
 | 319 | 			out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==", | 
 | 320 | 		}, | 
 | 321 | 		{ | 
 | 322 | 			// input that serializes to a 257 byte block | 
 | 323 | 			in: [][2]string{ | 
 | 324 | 				{"-source", "1.8"}, | 
 | 325 | 				{"-target", "1.8"}, | 
 | 326 | 				{"a", strings.Repeat("b", 220)}, | 
 | 327 | 			}, | 
 | 328 | 			out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=", | 
 | 329 | 		}, | 
 | 330 | 	} | 
 | 331 |  | 
 | 332 | 	for i, test := range tests { | 
 | 333 | 		t.Run(strconv.Itoa(i), func(t *testing.T) { | 
 | 334 | 			got := kaptEncodeFlags(test.in) | 
 | 335 | 			if got != test.out { | 
 | 336 | 				t.Errorf("\nwant %q\n got %q", test.out, got) | 
 | 337 | 			} | 
 | 338 | 		}) | 
 | 339 | 	} | 
 | 340 | } | 
| Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 341 |  | 
 | 342 | func TestKotlinCompose(t *testing.T) { | 
 | 343 | 	result := android.GroupFixturePreparers( | 
 | 344 | 		PrepareForTestWithJavaDefaultModules, | 
 | 345 | 	).RunTestWithBp(t, ` | 
 | 346 | 		java_library { | 
 | 347 | 			name: "androidx.compose.runtime_runtime", | 
 | 348 | 		} | 
 | 349 |  | 
 | 350 | 		java_library_host { | 
 | 351 | 			name: "androidx.compose.compiler_compiler-hosted", | 
 | 352 | 		} | 
 | 353 |  | 
 | 354 | 		java_library { | 
 | 355 | 			name: "withcompose", | 
 | 356 | 			srcs: ["a.kt"], | 
| Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 357 | 			plugins: ["plugin"], | 
| Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 358 | 			static_libs: ["androidx.compose.runtime_runtime"], | 
 | 359 | 		} | 
 | 360 |  | 
 | 361 | 		java_library { | 
 | 362 | 			name: "nocompose", | 
 | 363 | 			srcs: ["a.kt"], | 
 | 364 | 		} | 
| Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 365 |  | 
 | 366 | 		java_plugin { | 
 | 367 | 			name: "plugin", | 
 | 368 | 		} | 
| Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 369 | 	`) | 
 | 370 |  | 
 | 371 | 	buildOS := result.Config.BuildOS.String() | 
 | 372 |  | 
 | 373 | 	composeCompiler := result.ModuleForTests("androidx.compose.compiler_compiler-hosted", buildOS+"_common").Rule("combineJar").Output | 
 | 374 | 	withCompose := result.ModuleForTests("withcompose", "android_common") | 
 | 375 | 	noCompose := result.ModuleForTests("nocompose", "android_common") | 
 | 376 |  | 
 | 377 | 	android.AssertStringListContains(t, "missing compose compiler dependency", | 
 | 378 | 		withCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String()) | 
 | 379 |  | 
 | 380 | 	android.AssertStringDoesContain(t, "missing compose compiler plugin", | 
 | 381 | 		withCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String()) | 
 | 382 |  | 
| Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 383 | 	android.AssertStringListContains(t, "missing kapt compose compiler dependency", | 
 | 384 | 		withCompose.Rule("kapt").Implicits.Strings(), composeCompiler.String()) | 
 | 385 |  | 
| Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 386 | 	android.AssertStringListDoesNotContain(t, "unexpected compose compiler dependency", | 
 | 387 | 		noCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String()) | 
 | 388 |  | 
 | 389 | 	android.AssertStringDoesNotContain(t, "unexpected compose compiler plugin", | 
 | 390 | 		noCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String()) | 
 | 391 | } |