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