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	"os"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/etc"
23)
24
25func TestMain(m *testing.M) {
26	os.Exit(m.Run())
27}
28
29func testXml(t *testing.T, bp string) *android.TestResult {
30	fs := android.MockFS{
31		"foo.xml": nil,
32		"foo.dtd": nil,
33		"bar.xml": nil,
34		"bar.xsd": nil,
35		"baz.xml": nil,
36	}
37
38	return android.GroupFixturePreparers(
39		android.PrepareForTestWithArchMutator,
40		etc.PrepareForTestWithPrebuiltEtc,
41		PreparerForTestWithXmlBuildComponents,
42		fs.AddToFixture(),
43		android.FixtureWithRootAndroidBp(bp),
44	).RunTest(t)
45}
46
47// Minimal test
48func TestPrebuiltEtcXml(t *testing.T) {
49	result := testXml(t, `
50		prebuilt_etc_xml {
51			name: "foo.xml",
52			src: "foo.xml",
53			schema: "foo.dtd",
54		}
55		prebuilt_etc_xml {
56			name: "bar.xml",
57			src: "bar.xml",
58			schema: "bar.xsd",
59		}
60		prebuilt_etc_xml {
61			name: "baz.xml",
62			src: "baz.xml",
63		}
64	`)
65
66	for _, tc := range []struct {
67		rule, input, schemaType, schema string
68	}{
69		{rule: "xmllint-dtd", input: "foo.xml", schemaType: "dtd", schema: "foo.dtd"},
70		{rule: "xmllint-xsd", input: "bar.xml", schemaType: "xsd", schema: "bar.xsd"},
71		{rule: "xmllint-minimal", input: "baz.xml"},
72	} {
73		t.Run(tc.schemaType, func(t *testing.T) {
74			rule := result.ModuleForTests(tc.input, "android_arm64_armv8-a").Rule(tc.rule)
75			android.AssertStringEquals(t, "input", tc.input, rule.Input.String())
76			if tc.schemaType != "" {
77				android.AssertStringEquals(t, "schema", tc.schema, rule.Args[tc.schemaType])
78			}
79		})
80	}
81
82	m := result.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml)
83	android.AssertPathRelativeToTopEquals(t, "installDir", "out/soong/target/product/test_device/system/etc", m.InstallDirPath())
84}
85