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