blob: f8ec82356eea51d0adac08552ed3276f73e1d1c7 [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"
Jiyong Park5a8d1be2018-04-25 22:57:34 +090023)
24
Colin Crossff6c33d2019-10-02 16:01:35 -070025var buildDir string
26
27func setUp() {
28 var err error
29 buildDir, err = ioutil.TempDir("", "soong_xml_test")
30 if err != nil {
31 panic(err)
32 }
33}
34
35func tearDown() {
36 os.RemoveAll(buildDir)
37}
38
39func TestMain(m *testing.M) {
40 run := func() int {
41 setUp()
42 defer tearDown()
43
44 return m.Run()
45 }
46
47 os.Exit(run())
48}
49
Jiyong Park5a8d1be2018-04-25 22:57:34 +090050func testXml(t *testing.T, bp string) *android.TestContext {
Colin Cross98be1bb2019-12-13 20:41:13 -080051 fs := map[string][]byte{
52 "foo.xml": nil,
53 "foo.dtd": nil,
54 "bar.xml": nil,
55 "bar.xsd": nil,
56 "baz.xml": nil,
57 }
58 config := android.TestArchConfig(buildDir, nil, bp, fs)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090059 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080060 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
61 ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -080062 ctx.Register(config)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090063 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
64 android.FailIfErrored(t, errs)
65 _, errs = ctx.PrepareBuildActions(config)
66 android.FailIfErrored(t, errs)
67
68 return ctx
69}
70
Jooyung Hana0171822019-07-22 15:48:36 +090071func assertEqual(t *testing.T, name, expected, actual string) {
72 t.Helper()
73 if expected != actual {
74 t.Errorf(name+" expected %q != got %q", expected, actual)
75 }
76}
77
Jiyong Park5a8d1be2018-04-25 22:57:34 +090078// Minimal test
79func TestPrebuiltEtcXml(t *testing.T) {
80 ctx := testXml(t, `
81 prebuilt_etc_xml {
82 name: "foo.xml",
83 src: "foo.xml",
84 schema: "foo.dtd",
85 }
86 prebuilt_etc_xml {
87 name: "bar.xml",
88 src: "bar.xml",
89 schema: "bar.xsd",
90 }
Jooyung Hana0171822019-07-22 15:48:36 +090091 prebuilt_etc_xml {
92 name: "baz.xml",
93 src: "baz.xml",
94 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +090095 `)
96
Jooyung Hana0171822019-07-22 15:48:36 +090097 for _, tc := range []struct {
98 rule, input, schemaType, schema string
99 }{
100 {rule: "xmllint-dtd", input: "foo.xml", schemaType: "dtd", schema: "foo.dtd"},
101 {rule: "xmllint-xsd", input: "bar.xml", schemaType: "xsd", schema: "bar.xsd"},
102 {rule: "xmllint-minimal", input: "baz.xml"},
103 } {
104 t.Run(tc.schemaType, func(t *testing.T) {
Colin Cross7113d202019-11-20 16:39:12 -0800105 rule := ctx.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
Jooyung Hana0171822019-07-22 15:48:36 +0900106 assertEqual(t, "input", tc.input, rule.Input.String())
107 if tc.schemaType != "" {
108 assertEqual(t, "schema", tc.schema, rule.Args[tc.schemaType])
109 }
110 })
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900111 }
Jooyung Hana0171822019-07-22 15:48:36 +0900112
Colin Cross7113d202019-11-20 16:39:12 -0800113 m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
Colin Crossff6c33d2019-10-02 16:01:35 -0700114 assertEqual(t, "installDir", buildDir+"/target/product/test_device/system/etc", m.InstallDirPath().String())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900115}