xref: /aosp_15_r20/tools/asuite/aidegen/idea/xml_gen_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2020, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for XMLGenerator."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport os
20*c2e18aaaSAndroid Build Coastguard Workerimport shutil
21*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
22*c2e18aaaSAndroid Build Coastguard Workerimport unittest
23*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom xml.etree import ElementTree
26*c2e18aaaSAndroid Build Coastguard Worker
27*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
28*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.idea import xml_gen
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Workerclass XMLGenUnittests(unittest.TestCase):
32*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for XMLGenerator class."""
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Worker    _TEST_DIR = None
35*c2e18aaaSAndroid Build Coastguard Worker    _XML_NAME = 'test.xml'
36*c2e18aaaSAndroid Build Coastguard Worker    _DEFAULT_XML = """\
37*c2e18aaaSAndroid Build Coastguard Worker<?xml version="1.0" encoding="UTF-8"?>
38*c2e18aaaSAndroid Build Coastguard Worker<project version="4"></project>
39*c2e18aaaSAndroid Build Coastguard Worker"""
40*c2e18aaaSAndroid Build Coastguard Worker    _IGNORE_GIT_XML = """\
41*c2e18aaaSAndroid Build Coastguard Worker<?xml version="1.0" encoding="UTF-8"?>
42*c2e18aaaSAndroid Build Coastguard Worker<project version="4">
43*c2e18aaaSAndroid Build Coastguard Worker  <component name="VcsManagerConfiguration">
44*c2e18aaaSAndroid Build Coastguard Worker    <ignored-roots><path value="/b" /></ignored-roots>
45*c2e18aaaSAndroid Build Coastguard Worker  </component>
46*c2e18aaaSAndroid Build Coastguard Worker</project>
47*c2e18aaaSAndroid Build Coastguard Worker"""
48*c2e18aaaSAndroid Build Coastguard Worker
49*c2e18aaaSAndroid Build Coastguard Worker    def setUp(self):
50*c2e18aaaSAndroid Build Coastguard Worker        """Prepare the testdata related path."""
51*c2e18aaaSAndroid Build Coastguard Worker        XMLGenUnittests._TEST_DIR = tempfile.mkdtemp()
52*c2e18aaaSAndroid Build Coastguard Worker        self.xml = xml_gen.XMLGenerator(self._TEST_DIR, self._XML_NAME)
53*c2e18aaaSAndroid Build Coastguard Worker        common_util.file_generate(self.xml.xml_path, self._DEFAULT_XML)
54*c2e18aaaSAndroid Build Coastguard Worker        self.xml.parse()
55*c2e18aaaSAndroid Build Coastguard Worker
56*c2e18aaaSAndroid Build Coastguard Worker    def tearDown(self):
57*c2e18aaaSAndroid Build Coastguard Worker        """Clear the testdata related path."""
58*c2e18aaaSAndroid Build Coastguard Worker        shutil.rmtree(self._TEST_DIR)
59*c2e18aaaSAndroid Build Coastguard Worker
60*c2e18aaaSAndroid Build Coastguard Worker    def test_find_elements_by_name(self):
61*c2e18aaaSAndroid Build Coastguard Worker        """Test find_elements_by_name."""
62*c2e18aaaSAndroid Build Coastguard Worker        node = self.xml.xml_obj.getroot()
63*c2e18aaaSAndroid Build Coastguard Worker        ElementTree.SubElement(node, 'a', attrib={'name': 'b'})
64*c2e18aaaSAndroid Build Coastguard Worker        elements = self.xml.find_elements_by_name('a', 'b')
65*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(len(elements), 1)
66*c2e18aaaSAndroid Build Coastguard Worker
67*c2e18aaaSAndroid Build Coastguard Worker    def test_append_node(self):
68*c2e18aaaSAndroid Build Coastguard Worker        """Test append_node."""
69*c2e18aaaSAndroid Build Coastguard Worker        node = self.xml.xml_obj.getroot()
70*c2e18aaaSAndroid Build Coastguard Worker        self.xml.append_node(node, '<a />')
71*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(len(node.findall('a')), 1)
72*c2e18aaaSAndroid Build Coastguard Worker
73*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'to_pretty_xml')
74*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
75*c2e18aaaSAndroid Build Coastguard Worker    def test_create_xml(self, mock_file_gen, mock_pretty_xml):
76*c2e18aaaSAndroid Build Coastguard Worker        """Test create_xml."""
77*c2e18aaaSAndroid Build Coastguard Worker        self.xml.create_xml()
78*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_gen.called)
79*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_pretty_xml.called)
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
82*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'get_android_root_dir')
83*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(xml_gen, 'XMLGenerator')
84*c2e18aaaSAndroid Build Coastguard Worker    def test_gen_vcs_xml(self, mock_xml_gen, mock_root_dir, mock_file_gen):
85*c2e18aaaSAndroid Build Coastguard Worker        """Test gen_vcs_xml."""
86*c2e18aaaSAndroid Build Coastguard Worker        mock_gen_xml = mock.Mock()
87*c2e18aaaSAndroid Build Coastguard Worker        mock_xml_gen.return_value = mock_gen_xml
88*c2e18aaaSAndroid Build Coastguard Worker        mock_xml_gen.xml_obj = None
89*c2e18aaaSAndroid Build Coastguard Worker        mock_root_dir.return_value = self._TEST_DIR
90*c2e18aaaSAndroid Build Coastguard Worker        xml_gen.gen_vcs_xml(self._TEST_DIR, [])
91*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_file_gen.called)
92*c2e18aaaSAndroid Build Coastguard Worker        mock_root_dir.return_value = '/a'
93*c2e18aaaSAndroid Build Coastguard Worker        xml_gen.gen_vcs_xml(self._TEST_DIR, ['/a'])
94*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_gen.called)
95*c2e18aaaSAndroid Build Coastguard Worker
96*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(os.path, 'exists')
97*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
98*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(xml_gen.XMLGenerator, 'create_xml')
99*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(xml_gen, 'XMLGenerator')
100*c2e18aaaSAndroid Build Coastguard Worker    def test_write_ignore_git_dirs_file(self, mock_xml_gen, mock_create_xml,
101*c2e18aaaSAndroid Build Coastguard Worker                                        mock_file_gen, mock_exists):
102*c2e18aaaSAndroid Build Coastguard Worker        """Test write_ignore_git_dirs_file."""
103*c2e18aaaSAndroid Build Coastguard Worker        mock_gen_xml = mock.Mock()
104*c2e18aaaSAndroid Build Coastguard Worker        mock_xml_gen.return_value = mock_gen_xml
105*c2e18aaaSAndroid Build Coastguard Worker        mock_gen_xml.xml_obj = False
106*c2e18aaaSAndroid Build Coastguard Worker        mock_exists.return_value = False
107*c2e18aaaSAndroid Build Coastguard Worker        xml_gen.write_ignore_git_dirs_file(self._TEST_DIR, ['/a'])
108*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_gen.called)
109*c2e18aaaSAndroid Build Coastguard Worker        mock_exists.return_value = True
110*c2e18aaaSAndroid Build Coastguard Worker        mock_xml_gen.return_value = self.xml
111*c2e18aaaSAndroid Build Coastguard Worker        xml_gen.write_ignore_git_dirs_file(self._TEST_DIR, ['/a'])
112*c2e18aaaSAndroid Build Coastguard Worker        ignore_root = self.xml.xml_obj.find('component').find('ignored-roots')
113*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(ignore_root.find('path').attrib['value'], '/a')
114*c2e18aaaSAndroid Build Coastguard Worker        common_util.file_generate(os.path.join(self._TEST_DIR, 'workspace.xml'),
115*c2e18aaaSAndroid Build Coastguard Worker                                  self._IGNORE_GIT_XML)
116*c2e18aaaSAndroid Build Coastguard Worker        self.xml = xml_gen.XMLGenerator(self._TEST_DIR, 'workspace.xml')
117*c2e18aaaSAndroid Build Coastguard Worker        mock_xml_gen.return_value = self.xml
118*c2e18aaaSAndroid Build Coastguard Worker        xml_gen.write_ignore_git_dirs_file(self._TEST_DIR, ['/a/b'])
119*c2e18aaaSAndroid Build Coastguard Worker        ignore_root = self.xml.xml_obj.find('component').find('ignored-roots')
120*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(ignore_root.find('path').attrib['value'], '/a/b')
121*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_create_xml.called)
122*c2e18aaaSAndroid Build Coastguard Worker
123*c2e18aaaSAndroid Build Coastguard Worker
124*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
125*c2e18aaaSAndroid Build Coastguard Worker    unittest.main()
126