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 xml
16
17import (
18	"io/ioutil"
19	"os"
20	"testing"
21
22	"android/soong/android"
23	"android/soong/etc"
24)
25
26var 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
51func testXml(t *testing.T, bp string) *android.TestContext {
52	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)
60	ctx := android.NewTestArchContext()
61	ctx.RegisterModuleType("prebuilt_etc", etc.PrebuiltEtcFactory)
62	ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory)
63	ctx.Register(config)
64	_, 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
72func 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
79// 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		}
92		prebuilt_etc_xml {
93			name: "baz.xml",
94			src: "baz.xml",
95		}
96	`)
97
98	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) {
106			rule := ctx.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
107			assertEqual(t, "input", tc.input, rule.Input.String())
108			if tc.schemaType != "" {
109				assertEqual(t, "schema", tc.schema, rule.Args[tc.schemaType])
110			}
111		})
112	}
113
114	m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
115	assertEqual(t, "installDir", buildDir+"/target/product/test_device/system/etc", m.InstallDirPath().String())
116}
117