Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 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 | |
| 15 | package xml |
| 16 | |
| 17 | import ( |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "testing" |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame^] | 21 | |
| 22 | "android/soong/android" |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame^] | 25 | var buildDir string |
| 26 | |
| 27 | func setUp() { |
| 28 | var err error |
| 29 | buildDir, err = ioutil.TempDir("", "soong_xml_test") |
| 30 | if err != nil { |
| 31 | panic(err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func tearDown() { |
| 36 | os.RemoveAll(buildDir) |
| 37 | } |
| 38 | |
| 39 | func 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 Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 50 | func testXml(t *testing.T, bp string) *android.TestContext { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame^] | 51 | config := android.TestArchConfig(buildDir, nil) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 52 | 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 Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 62 | "baz.xml": nil, |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 63 | } |
| 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 Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 73 | func 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 Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 80 | // Minimal test |
| 81 | func 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 Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 93 | prebuilt_etc_xml { |
| 94 | name: "baz.xml", |
| 95 | src: "baz.xml", |
| 96 | } |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 97 | `) |
| 98 | |
Jooyung Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 99 | 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 Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 113 | } |
Jooyung Han | a017182 | 2019-07-22 15:48:36 +0900 | [diff] [blame] | 114 | |
| 115 | m := ctx.ModuleForTests("foo.xml", "android_arm64_armv8-a").Module().(*prebuiltEtcXml) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame^] | 116 | assertEqual(t, "installDir", buildDir+"/target/product/test_device/system/etc", m.InstallDirPath().String()) |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 117 | } |