| Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 1 | // Copyright 2018 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 ( | 
 | 18 | 	"reflect" | 
 | 19 | 	"strings" | 
 | 20 | 	"testing" | 
 | 21 |  | 
 | 22 | 	"android/soong/android" | 
 | 23 | ) | 
 | 24 |  | 
 | 25 | func testGenruleContext(config android.Config) *android.TestContext { | 
 | 26 | 	ctx := android.NewTestArchContext(config) | 
 | 27 | 	ctx.RegisterModuleType("java_genrule", GenRuleFactory) | 
 | 28 | 	ctx.Register() | 
 | 29 |  | 
 | 30 | 	return ctx | 
 | 31 | } | 
 | 32 |  | 
 | 33 | func TestGenruleCmd(t *testing.T) { | 
 | 34 | 	fs := map[string][]byte{ | 
 | 35 | 		"tool": nil, | 
 | 36 | 		"foo":  nil, | 
 | 37 | 	} | 
 | 38 | 	bp := ` | 
 | 39 | 				java_genrule { | 
 | 40 | 					name: "gen", | 
 | 41 | 					tool_files: ["tool"], | 
 | 42 | 					cmd: "$(location tool) $(in) $(out)", | 
 | 43 | 					srcs: ["foo"], | 
 | 44 | 					out: ["out"], | 
 | 45 | 				} | 
 | 46 | 			` | 
 | 47 | 	config := android.TestArchConfig(t.TempDir(), nil, bp, fs) | 
 | 48 |  | 
 | 49 | 	ctx := testGenruleContext(config) | 
 | 50 |  | 
 | 51 | 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) | 
 | 52 | 	if errs == nil { | 
 | 53 | 		_, errs = ctx.PrepareBuildActions(config) | 
 | 54 | 	} | 
 | 55 | 	if errs != nil { | 
 | 56 | 		t.Fatal(errs) | 
 | 57 | 	} | 
 | 58 |  | 
 | 59 | 	gen := ctx.ModuleForTests("gen", "android_common").Output("out") | 
 | 60 | 	expected := []string{"foo"} | 
 | 61 | 	if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { | 
 | 62 | 		t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings()) | 
 | 63 | 	} | 
 | 64 | } | 
 | 65 |  | 
 | 66 | func TestJarGenrules(t *testing.T) { | 
 | 67 | 	ctx, _ := testJava(t, ` | 
 | 68 | 		java_library { | 
 | 69 | 			name: "foo", | 
 | 70 | 			srcs: ["a.java"], | 
 | 71 | 		} | 
 | 72 |  | 
 | 73 | 		java_genrule { | 
 | 74 | 			name: "jargen", | 
 | 75 | 			tool_files: ["b.java"], | 
 | 76 | 			cmd: "$(location b.java) $(in) $(out)", | 
 | 77 | 			out: ["jargen.jar"], | 
 | 78 | 			srcs: [":foo"], | 
 | 79 | 		} | 
 | 80 |  | 
 | 81 | 		java_library { | 
 | 82 | 			name: "bar", | 
 | 83 | 			static_libs: ["jargen"], | 
 | 84 | 			srcs: ["c.java"], | 
 | 85 | 		} | 
 | 86 |  | 
 | 87 | 		java_library { | 
 | 88 | 			name: "baz", | 
 | 89 | 			libs: ["jargen"], | 
 | 90 | 			srcs: ["c.java"], | 
 | 91 | 		} | 
 | 92 | 	`) | 
 | 93 |  | 
 | 94 | 	foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") | 
 | 95 | 	jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") | 
 | 96 | 	bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") | 
 | 97 | 	baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") | 
 | 98 | 	barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") | 
 | 99 |  | 
 | 100 | 	if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) { | 
 | 101 | 		t.Errorf("expected jargen inputs [%q], got %q", w, g) | 
 | 102 | 	} | 
 | 103 |  | 
 | 104 | 	if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { | 
 | 105 | 		t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) | 
 | 106 | 	} | 
 | 107 |  | 
 | 108 | 	if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { | 
 | 109 | 		t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) | 
 | 110 | 	} | 
 | 111 |  | 
 | 112 | 	if len(barCombined.Inputs) != 2 || | 
 | 113 | 		barCombined.Inputs[0].String() != bar.Output.String() || | 
 | 114 | 		barCombined.Inputs[1].String() != jargen.Output.String() { | 
 | 115 | 		t.Errorf("bar combined jar inputs %v is not [%q, %q]", | 
 | 116 | 			barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) | 
 | 117 | 	} | 
 | 118 | } |