blob: abcb1085fa76d18ffba9be3fc8299bce4e970a78 [file] [log] [blame]
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001// 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 xml
16
17import (
Jiyong Park5a8d1be2018-04-25 22:57:34 +090018 "io/ioutil"
19 "os"
20 "testing"
Colin Crossff6c33d2019-10-02 16:01:35 -070021
22 "android/soong/android"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070023 "android/soong/etc"
Jiyong Park5a8d1be2018-04-25 22:57:34 +090024)
25
Colin Crossff6c33d2019-10-02 16:01:35 -070026var buildDir string
27
28func setUp() {
29 var err error
30 buildDir, err = ioutil.TempDir("", "soong_xml_test")
31 if err != nil {
32 panic(err)
33 }
34}
35
36func tearDown() {
37 os.RemoveAll(buildDir)
38}
39
40func TestMain(m *testing.M) {
41 run := func() int {
42 setUp()
43 defer tearDown()
44
45 return m.Run()
46 }
47
48 os.Exit(run())
49}
50
Jiyong Park5a8d1be2018-04-25 22:57:34 +090051func testXml(t *testing.T, bp string) *android.TestContext {
Colin Cross98be1bb2019-12-13 20:41:13 -080052 fs := map[string][]byte{
53 "foo.xml": nil,
54 "foo.dtd": nil,
55 "bar.xml": nil,
56 "bar.xsd": nil,
57 "baz.xml": nil,
58 }
59 config := android.TestArchConfig(buildDir, nil, bp, fs)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090060 ctx := android.NewTestArchContext()
Jaewoong Jung4b79e982020-06-01 10:45:49 -070061 ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080062 ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -080063 ctx.Register(config)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090064 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
65 android.FailIfErrored(t, errs)
66 _, errs = ctx.PrepareBuildActions(config)
67 android.FailIfErrored(t, errs)
68
69 return ctx
70}
71
Jooyung Hana0171822019-07-22 15:48:36 +090072func assertEqual(t *testing.T, name, expected, actual string) {
73 t.Helper()
74 if expected != actual {
75 t.Errorf(name+" expected %q != got %q", expected, actual)
76 }
77}
78
Jiyong Park5a8d1be2018-04-25 22:57:34 +090079// Minimal test
80func TestPrebuiltEtcXml(t *testing.T) {
81 ctx := testXml(t, `
82 prebuilt_etc_xml {
83 name: "foo.xml",
84 src: "foo.xml",
85 schema: "foo.dtd",
86 }
87 prebuilt_etc_xml {
88 name: "bar.xml",
89 src: "bar.xml",
90 schema: "bar.xsd",
91 }
Jooyung Hana0171822019-07-22 15:48:36 +090092 prebuilt_etc_xml {
93 name: "baz.xml",
94 src: "baz.xml",
95 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +090096 `)
97
Jooyung Hana0171822019-07-22 15:48:36 +090098 for _, tc := range []struct {
99 rule, input, schemaType, schema string
100 }{
101 {rule: "xmllint-dtd", input: "foo.xml", schemaType: "dtd", schema: "foo.dtd"},
102 {rule: "xmllint-xsd", input: "bar.xml", schemaType: "xsd", schema: "bar.xsd"},
103 {rule: "xmllint-minimal", input: "baz.xml"},
104 } {
105 t.Run(tc.schemaType, func(t *testing.T) {
Colin Cross7113d202019-11-20 16:39:12 -0800106 rule := ctx.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
Jooyung Hana0171822019-07-22 15:48:36 +0900107 assertEqual(t, "input", tc.input, rule.Input.String())
108 if tc.schemaType != "" {
109 assertEqual(t, "schema", tc.schema, rule.Args[tc.schemaType])
110 }
111 })
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900112 }
Jooyung Hana0171822019-07-22 15:48:36 +0900113
Colin Cross7113d202019-11-20 16:39:12 -0800114 m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
Colin Crossff6c33d2019-10-02 16:01:35 -0700115 assertEqual(t, "installDir", buildDir+"/target/product/test_device/system/etc", m.InstallDirPath().String())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900116}