blob: 832bca9c0b968ef1f88e0124bd0ea1bd491fd6cb [file] [log] [blame]
Mathew Inwood3ff4fa22019-12-17 09:44:11 +00001#!/usr/bin/env python
2#
3# Copyright (C) 2019 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"""Unit tests for process_compat_config.py."""
18
19import difflib
20import io
Mathew Inwood1c03ad92020-01-09 15:47:54 +000021from StringIO import StringIO
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000022import unittest
23import xml.dom.minidom
Mathew Inwood1c03ad92020-01-09 15:47:54 +000024from inspect import currentframe, getframeinfo
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000025
26import process_compat_config
27
Mathew Inwood1c03ad92020-01-09 15:47:54 +000028def here():
29 f = currentframe().f_back
30 return "%s:%d" % (getframeinfo(f).filename, f.f_lineno)
31
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000032class ProcessCompatConfigTest(unittest.TestCase):
33
34 def setUp(self):
Mathew Inwood1c03ad92020-01-09 15:47:54 +000035 self.merger = process_compat_config.ConfigMerger(detect_conflicts = True)
36 self.stderr = StringIO()
37 self.merger.write_errors_to = self.stderr
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000038 self.xml = io.BytesIO()
39
40 def assert_same_xml(self, got, expected):
41 got = xml.dom.minidom.parseString(got).toprettyxml()
42 expected = xml.dom.minidom.parseString(expected).toprettyxml()
43 diffs = [diff for diff in difflib.ndiff(got.split('\n'), expected.split('\n')) if not diff.startswith(" ")]
44 self.assertEqual("", "\n".join(diffs), msg="Got unexpected diffs in XML")
45
46 def test_no_config_to_merge(self):
47 self.merger.write(self.xml)
48 self.assert_same_xml(self.xml.getvalue(), "<config />")
49
50 def test_merge_one_file(self):
Mathew Inwood1c03ad92020-01-09 15:47:54 +000051 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000052 self.merger.write(self.xml)
53 self.assert_same_xml(self.xml.getvalue(), '<config><compat-change id="1234" name="TEST_CHANGE" /></config>')
54
55 def test_merge_two_files(self):
Mathew Inwood1c03ad92020-01-09 15:47:54 +000056 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
57 self.merger.merge(io.BytesIO(b'<config><compat-change id="1235" name="TEST_CHANGE2" /></config>'), here())
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000058 self.merger.write(self.xml)
59 self.assert_same_xml(self.xml.getvalue(),
60 '<config><compat-change id="1234" name="TEST_CHANGE" /><compat-change id="1235" name="TEST_CHANGE2" /></config>')
61
62 def test_merge_two_files_metadata(self):
63 self.merger.merge(io.BytesIO(
Mathew Inwood1c03ad92020-01-09 15:47:54 +000064 b'<config><compat-change id="1234" name="TEST_CHANGE"><meta-data definedIn="some.Class" sourcePosition="some.java:1" />'
65 b'</compat-change></config>'), here())
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000066 self.merger.merge(io.BytesIO(
Mathew Inwood1c03ad92020-01-09 15:47:54 +000067 b'<config><compat-change id="1235" name="TEST_CHANGE2"><meta-data definedIn="other.Class" sourcePosition="other.java:2" />'
68 b'</compat-change></config>'), here())
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000069 self.merger.write(self.xml)
70 self.assert_same_xml(self.xml.getvalue(), b'<config>'
Mathew Inwood1c03ad92020-01-09 15:47:54 +000071 b'<compat-change id="1234" name="TEST_CHANGE"><meta-data definedIn="some.Class" sourcePosition="some.java:1" /></compat-change>'
72 b'<compat-change id="1235" name="TEST_CHANGE2"><meta-data definedIn="other.Class" sourcePosition="other.java:2" /></compat-change>'
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000073 b'</config>')
74
75 def test_write_device_config_metadata_stripped(self):
76 self.merger.merge(io.BytesIO(
Mathew Inwood1c03ad92020-01-09 15:47:54 +000077 b'<config><compat-change id="1234" name="TEST_CHANGE"><meta-data definedIn="some.Class" sourcePosition="file.java:1"/>'
78 b'</compat-change></config>'), here())
Mathew Inwood3ff4fa22019-12-17 09:44:11 +000079 self.merger.write_device_config(self.xml)
80 self.assert_same_xml(self.xml.getvalue(), b'<config>'
81 b'<compat-change id="1234" name="TEST_CHANGE" />'
82 b'</config>')
83
Mathew Inwood1c03ad92020-01-09 15:47:54 +000084 def test_merge_two_files_duplicate_id(self):
85 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
86 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE2" /></config>'), here())
87 self.assertIn(r'ERROR: Duplicate definitions for compat change with ID 1234', self.stderr.getvalue())
88 with self.assertRaisesRegexp(Exception, ' 1 .*error'):
89 self.merger.write(self.xml)
90
91 def test_merge_two_files_duplicate_name(self):
92 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
93 self.merger.merge(io.BytesIO(b'<config><compat-change id="1235" name="TEST_CHANGE" /></config>'), here())
94 self.assertIn(r'ERROR: Duplicate definitions for compat change with name TEST_CHANGE', self.stderr.getvalue())
95 with self.assertRaisesRegexp(Exception, ' 1 .*error'):
96 self.merger.write(self.xml)
97
98 def test_merge_two_files_duplicate_id_allow_duplicates(self):
99 self.merger = process_compat_config.ConfigMerger(detect_conflicts = False)
100 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
101 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE2" /></config>'), here())
102 self.merger.write(self.xml)
103
104 def test_merge_two_files_duplicate_name_allow_duplicates(self):
105 self.merger = process_compat_config.ConfigMerger(detect_conflicts = False)
106 self.merger.merge(io.BytesIO(b'<config><compat-change id="1234" name="TEST_CHANGE" /></config>'), here())
107 self.merger.merge(io.BytesIO(b'<config><compat-change id="1235" name="TEST_CHANGE" /></config>'), here())
108 self.merger.write(self.xml)
Mathew Inwood3ff4fa22019-12-17 09:44:11 +0000109
110if __name__ == '__main__':
111 unittest.main(verbosity=2)