xref: /aosp_15_r20/tools/treble/split/xml_diff_test.py (revision 105f628577ac4ba0e277a494fbb614ed8c12a994)
1*105f6285SAndroid Build Coastguard Worker# Copyright (C) 2020 The Android Open Source Project
2*105f6285SAndroid Build Coastguard Worker#
3*105f6285SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*105f6285SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*105f6285SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*105f6285SAndroid Build Coastguard Worker#
7*105f6285SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*105f6285SAndroid Build Coastguard Worker#
9*105f6285SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*105f6285SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*105f6285SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*105f6285SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*105f6285SAndroid Build Coastguard Worker# limitations under the License.
14*105f6285SAndroid Build Coastguard Worker"""Test XML diff."""
15*105f6285SAndroid Build Coastguard Workerimport unittest
16*105f6285SAndroid Build Coastguard Workerimport xml.etree.ElementTree as ET
17*105f6285SAndroid Build Coastguard Worker
18*105f6285SAndroid Build Coastguard Workerfrom treble.split import xml_diff
19*105f6285SAndroid Build Coastguard Worker
20*105f6285SAndroid Build Coastguard Worker
21*105f6285SAndroid Build Coastguard Workerclass XmlDiffTest(unittest.TestCase):
22*105f6285SAndroid Build Coastguard Worker
23*105f6285SAndroid Build Coastguard Worker  def test_attribute_changes(self):
24*105f6285SAndroid Build Coastguard Worker    e1 = ET.fromstring('<node attr1="hello" attr2="hello2" ignored="me"/>')
25*105f6285SAndroid Build Coastguard Worker    e2 = ET.fromstring('<node attr3="hello3" attr2="bye2"/>')
26*105f6285SAndroid Build Coastguard Worker    changes = xml_diff.attribute_changes(e1, e2, set(['ignored']))
27*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
28*105f6285SAndroid Build Coastguard Worker        xml_diff.ChangeMap(
29*105f6285SAndroid Build Coastguard Worker            added={'attr3': 'hello3'},
30*105f6285SAndroid Build Coastguard Worker            removed={'attr1': 'hello'},
31*105f6285SAndroid Build Coastguard Worker            modified={'attr2': xml_diff.Change('hello2', 'bye2')}), changes)
32*105f6285SAndroid Build Coastguard Worker
33*105f6285SAndroid Build Coastguard Worker  def test_compare_subelements(self):
34*105f6285SAndroid Build Coastguard Worker    p1 = ET.fromstring("""<parent>
35*105f6285SAndroid Build Coastguard Worker      <tag1 attr="newfile2" attrkey="notneeded" />
36*105f6285SAndroid Build Coastguard Worker      <tag1 attr="oldfile1" attrkey="dest1" />
37*105f6285SAndroid Build Coastguard Worker      <tag2 attr="oldfile2" attrkey="dest2" />
38*105f6285SAndroid Build Coastguard Worker    </parent>
39*105f6285SAndroid Build Coastguard Worker    """)
40*105f6285SAndroid Build Coastguard Worker    p2 = ET.fromstring("""<parent>
41*105f6285SAndroid Build Coastguard Worker      <tag1 attr="newfile1" attrkey="dest1" />
42*105f6285SAndroid Build Coastguard Worker      <tag2 attr="newfile2" attrkey="dest2" />
43*105f6285SAndroid Build Coastguard Worker      <tag2 attr="somefile" attrkey="addedfile" />
44*105f6285SAndroid Build Coastguard Worker    </parent>
45*105f6285SAndroid Build Coastguard Worker    """)
46*105f6285SAndroid Build Coastguard Worker
47*105f6285SAndroid Build Coastguard Worker    changes = xml_diff.compare_subelements(
48*105f6285SAndroid Build Coastguard Worker        tag='tag1',
49*105f6285SAndroid Build Coastguard Worker        p1=p1,
50*105f6285SAndroid Build Coastguard Worker        p2=p2,
51*105f6285SAndroid Build Coastguard Worker        ignored_attrs=set(),
52*105f6285SAndroid Build Coastguard Worker        key_fn=lambda x: x.get('attrkey'),
53*105f6285SAndroid Build Coastguard Worker        diff_fn=xml_diff.attribute_changes)
54*105f6285SAndroid Build Coastguard Worker    self.assertEqual(changes.added, {})
55*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
56*105f6285SAndroid Build Coastguard Worker        changes.removed,
57*105f6285SAndroid Build Coastguard Worker        {'notneeded': '<tag1 attr="newfile2" attrkey="notneeded" />'})
58*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
59*105f6285SAndroid Build Coastguard Worker        changes.modified, {
60*105f6285SAndroid Build Coastguard Worker            'dest1':
61*105f6285SAndroid Build Coastguard Worker                xml_diff.ChangeMap(
62*105f6285SAndroid Build Coastguard Worker                    modified={'attr': xml_diff.Change('oldfile1', 'newfile1')})
63*105f6285SAndroid Build Coastguard Worker        })
64