blob: f2a440f9a4b3df9a8b9faed37b012b0c6b9c7830 [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 Crossff6c33d2019-10-02 16:01:35 -070051 config := android.TestArchConfig(buildDir, nil)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090052 ctx := android.NewTestArchContext()
53 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
54 ctx.RegisterModuleType("prebuilt_etc_xml", android.ModuleFactoryAdaptor(PrebuiltEtcXmlFactory))
55 ctx.Register()
56 mockFiles := map[string][]byte{
57 "Android.bp": []byte(bp),
58 "foo.xml": nil,
59 "foo.dtd": nil,
60 "bar.xml": nil,
61 "bar.xsd": nil,
Jooyung Hana0171822019-07-22 15:48:36 +090062 "baz.xml": nil,
Jiyong Park5a8d1be2018-04-25 22:57:34 +090063 }
64 ctx.MockFileSystem(mockFiles)
65 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
69
70 return ctx
71}
72
Jooyung Hana0171822019-07-22 15:48:36 +090073func assertEqual(t *testing.T, name, expected, actual string) {
74 t.Helper()
75 if expected != actual {
76 t.Errorf(name+" expected %q != got %q", expected, actual)
77 }
78}
79
Jiyong Park5a8d1be2018-04-25 22:57:34 +090080// Minimal test
81func TestPrebuiltEtcXml(t *testing.T) {
82 ctx := testXml(t, `
83 prebuilt_etc_xml {
84 name: "foo.xml",
85 src: "foo.xml",
86 schema: "foo.dtd",
87 }
88 prebuilt_etc_xml {
89 name: "bar.xml",
90 src: "bar.xml",
91 schema: "bar.xsd",
92 }
Jooyung Hana0171822019-07-22 15:48:36 +090093 prebuilt_etc_xml {
94 name: "baz.xml",
95 src: "baz.xml",
96 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +090097 `)
98
Jooyung Hana0171822019-07-22 15:48:36 +090099 for _, tc := range []struct {
100 rule, input, schemaType, schema string
101 }{
102 {rule: "xmllint-dtd", input: "foo.xml", schemaType: "dtd", schema: "foo.dtd"},
103 {rule: "xmllint-xsd", input: "bar.xml", schemaType: "xsd", schema: "bar.xsd"},
104 {rule: "xmllint-minimal", input: "baz.xml"},
105 } {
106 t.Run(tc.schemaType, func(t *testing.T) {
107 rule := ctx.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
108 assertEqual(t, "input", tc.input, rule.Input.String())
109 if tc.schemaType != "" {
110 assertEqual(t, "schema", tc.schema, rule.Args[tc.schemaType])
111 }
112 })
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900113 }
Jooyung Hana0171822019-07-22 15:48:36 +0900114
115 m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
Colin Crossff6c33d2019-10-02 16:01:35 -0700116 assertEqual(t, "installDir", buildDir+"/target/product/test_device/system/etc", m.InstallDirPath().String())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900117}