OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import tempfile |
| 7 import unittest |
| 8 |
| 9 import extract_actions |
| 10 |
| 11 # Empty value to be inserted to |ACTIONS_MOCK|. |
| 12 NO_VALUE = '' |
| 13 |
| 14 ONE_OWNER = '<owner>name1@google.com</owner>\n' |
| 15 TWO_OWNERS = """ |
| 16 <owner>name1@google.com</owner>\n |
| 17 <owner>name2@google.com</owner>\n |
| 18 """ |
| 19 |
| 20 DESCRIPTION = '<description>Description.</description>\n' |
| 21 TWO_DESCRIPTIONS = """ |
| 22 <description>Description.</description>\n |
| 23 <description>Description2.</description>\n |
| 24 """ |
| 25 |
| 26 OBSOLETE = '<obsolete>Not used anymore. Replaced by action2.</obsolete>\n' |
| 27 TWO_OBSOLETE = '<obsolete>Obsolete1.</obsolete><obsolete>Obsolete2.</obsolete>' |
| 28 |
| 29 ACTIONS_XML = """ |
| 30 <actions> |
| 31 |
| 32 <action name="action1">\n{owners}{obsolete}{description} |
| 33 </action> |
| 34 |
| 35 </actions>""" |
| 36 |
| 37 NO_OWNER_EXPECTED_XML = ( |
| 38 '<actions>\n\n' |
| 39 '<action name="action1">\n' |
| 40 ' <owner>Please list the metric\'s owners.' |
| 41 ' Add more owner tags as needed.</owner>\n' |
| 42 ' <description>Description.</description>\n' |
| 43 '</action>\n\n' |
| 44 '</actions>\n' |
| 45 ) |
| 46 |
| 47 ONE_OWNER_EXPECTED_XML = ( |
| 48 '<actions>\n\n' |
| 49 '<action name="action1">\n' |
| 50 ' <owner>name1@google.com</owner>\n' |
| 51 ' <description>Description.</description>\n' |
| 52 '</action>\n\n' |
| 53 '</actions>\n' |
| 54 ) |
| 55 |
| 56 TWO_OWNERS_EXPECTED_XML = ( |
| 57 '<actions>\n\n' |
| 58 '<action name="action1">\n' |
| 59 ' <owner>name1@google.com</owner>\n' |
| 60 ' <owner>name2@google.com</owner>\n' |
| 61 ' <description>Description.</description>\n' |
| 62 '</action>\n\n' |
| 63 '</actions>\n' |
| 64 ) |
| 65 |
| 66 NO_DESCRIPTION_EXPECTED_XML = ( |
| 67 '<actions>\n\n' |
| 68 '<action name="action1">\n' |
| 69 ' <owner>name1@google.com</owner>\n' |
| 70 ' <owner>name2@google.com</owner>\n' |
| 71 ' <description>Please enter the description of the metric.</description>\n' |
| 72 '</action>\n\n' |
| 73 '</actions>\n' |
| 74 ) |
| 75 |
| 76 OBSOLETE_EXPECTED_XML = ( |
| 77 '<actions>\n\n' |
| 78 '<action name="action1">\n' |
| 79 ' <owner>name1@google.com</owner>\n' |
| 80 ' <owner>name2@google.com</owner>\n' |
| 81 ' <description>Description.</description>\n' |
| 82 ' <obsolete>Not used anymore. Replaced by action2.</obsolete>\n' |
| 83 '</action>\n\n' |
| 84 '</actions>\n' |
| 85 ) |
| 86 |
| 87 ADD_ACTION_EXPECTED_XML = ( |
| 88 '<actions>\n\n' |
| 89 '<action name="action1">\n' |
| 90 ' <owner>name1@google.com</owner>\n' |
| 91 ' <owner>name2@google.com</owner>\n' |
| 92 ' <description>Description.</description>\n' |
| 93 '</action>\n\n' |
| 94 '<action name="action2">\n' |
| 95 ' <owner>Please list the metric\'s owners.' |
| 96 ' Add more owner tags as needed.</owner>\n' |
| 97 ' <description>Please enter the description of the metric.</description>\n' |
| 98 '</action>\n\n' |
| 99 '</actions>\n' |
| 100 ) |
| 101 |
| 102 |
| 103 class ActionXmlTest(unittest.TestCase): |
| 104 |
| 105 def _assertActionResult(self, owner, description, obsolete, expected_result, |
| 106 new_action=None): |
| 107 current_xml = ACTIONS_XML.format(owners=owner, description=description, |
| 108 obsolete=obsolete) |
| 109 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 110 f.write(current_xml) |
| 111 file_name = f.name |
| 112 actions, actions_dict = extract_actions.ParseActionFile(file_name) |
| 113 if new_action: |
| 114 actions.add(new_action) |
| 115 pretty = extract_actions.PrettyPrint(actions, actions_dict) |
| 116 self.assertEqual(expected_result, pretty) |
| 117 |
| 118 def testNoOwner(self): |
| 119 self._assertActionResult(NO_VALUE, DESCRIPTION, NO_VALUE, |
| 120 NO_OWNER_EXPECTED_XML) |
| 121 |
| 122 def testOneOwnerOneDescription(self): |
| 123 self._assertActionResult(ONE_OWNER, DESCRIPTION, NO_VALUE, |
| 124 ONE_OWNER_EXPECTED_XML) |
| 125 |
| 126 def testTwoOwners(self): |
| 127 self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE, |
| 128 TWO_OWNERS_EXPECTED_XML) |
| 129 |
| 130 def testNoDescription(self): |
| 131 self._assertActionResult(TWO_OWNERS, NO_VALUE, NO_VALUE, |
| 132 NO_DESCRIPTION_EXPECTED_XML) |
| 133 |
| 134 def testTwoDescriptions(self): |
| 135 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=NO_VALUE, |
| 136 description=TWO_DESCRIPTIONS) |
| 137 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 138 f.write(current_xml) |
| 139 file_name = f.name |
| 140 with self.assertRaises(SystemExit) as cm: |
| 141 _, _ = extract_actions.ParseActionFile(file_name) |
| 142 self.assertEqual(cm.exception.code, 1) |
| 143 |
| 144 def testObsolete(self): |
| 145 self._assertActionResult(TWO_OWNERS, DESCRIPTION, OBSOLETE, |
| 146 OBSOLETE_EXPECTED_XML) |
| 147 |
| 148 def testTwoObsoletes(self): |
| 149 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=TWO_OBSOLETE, |
| 150 description=DESCRIPTION) |
| 151 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: |
| 152 f.write(current_xml) |
| 153 file_name = f.name |
| 154 with self.assertRaises(SystemExit) as cm: |
| 155 _, _ = extract_actions.ParseActionFile(file_name) |
| 156 self.assertEqual(cm.exception.code, 1) |
| 157 |
| 158 def testAddNewActions(self): |
| 159 self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE, |
| 160 ADD_ACTION_EXPECTED_XML, new_action='action2') |
| 161 |
| 162 |
| 163 if __name__ == '__main__': |
| 164 unittest.main() |
OLD | NEW |