blob: ae3e9fe3c77b7442b7e75b1bf8710ca4c22e4ede [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 (
18 "android/soong/android"
19 "io/ioutil"
20 "os"
21 "testing"
22)
23
24func testXml(t *testing.T, bp string) *android.TestContext {
25 config, buildDir := setup(t)
26 defer teardown(buildDir)
27 ctx := android.NewTestArchContext()
28 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
29 ctx.RegisterModuleType("prebuilt_etc_xml", android.ModuleFactoryAdaptor(PrebuiltEtcXmlFactory))
30 ctx.Register()
31 mockFiles := map[string][]byte{
32 "Android.bp": []byte(bp),
33 "foo.xml": nil,
34 "foo.dtd": nil,
35 "bar.xml": nil,
36 "bar.xsd": nil,
Jooyung Hana0171822019-07-22 15:48:36 +090037 "baz.xml": nil,
Jiyong Park5a8d1be2018-04-25 22:57:34 +090038 }
39 ctx.MockFileSystem(mockFiles)
40 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
41 android.FailIfErrored(t, errs)
42 _, errs = ctx.PrepareBuildActions(config)
43 android.FailIfErrored(t, errs)
44
45 return ctx
46}
47
48func setup(t *testing.T) (config android.Config, buildDir string) {
49 buildDir, err := ioutil.TempDir("", "soong_xml_test")
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 config = android.TestArchConfig(buildDir, nil)
55
56 return
57}
58
59func teardown(buildDir string) {
60 os.RemoveAll(buildDir)
61}
62
Jooyung Hana0171822019-07-22 15:48:36 +090063func assertEqual(t *testing.T, name, expected, actual string) {
64 t.Helper()
65 if expected != actual {
66 t.Errorf(name+" expected %q != got %q", expected, actual)
67 }
68}
69
Jiyong Park5a8d1be2018-04-25 22:57:34 +090070// Minimal test
71func TestPrebuiltEtcXml(t *testing.T) {
72 ctx := testXml(t, `
73 prebuilt_etc_xml {
74 name: "foo.xml",
75 src: "foo.xml",
76 schema: "foo.dtd",
77 }
78 prebuilt_etc_xml {
79 name: "bar.xml",
80 src: "bar.xml",
81 schema: "bar.xsd",
82 }
Jooyung Hana0171822019-07-22 15:48:36 +090083 prebuilt_etc_xml {
84 name: "baz.xml",
85 src: "baz.xml",
86 }
Jiyong Park5a8d1be2018-04-25 22:57:34 +090087 `)
88
Jooyung Hana0171822019-07-22 15:48:36 +090089 for _, tc := range []struct {
90 rule, input, schemaType, schema string
91 }{
92 {rule: "xmllint-dtd", input: "foo.xml", schemaType: "dtd", schema: "foo.dtd"},
93 {rule: "xmllint-xsd", input: "bar.xml", schemaType: "xsd", schema: "bar.xsd"},
94 {rule: "xmllint-minimal", input: "baz.xml"},
95 } {
96 t.Run(tc.schemaType, func(t *testing.T) {
97 rule := ctx.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
98 assertEqual(t, "input", tc.input, rule.Input.String())
99 if tc.schemaType != "" {
100 assertEqual(t, "schema", tc.schema, rule.Args[tc.schemaType])
101 }
102 })
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900103 }
Jooyung Hana0171822019-07-22 15:48:36 +0900104
105 m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
106 assertEqual(t, "installDir", "target/product/test_device/system/etc", m.InstallDirPath().RelPathString())
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900107}