xref: /aosp_15_r20/tools/treble/split/manifest_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 manifest 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 manifest_diff
19*105f6285SAndroid Build Coastguard Worker
20*105f6285SAndroid Build Coastguard Worker
21*105f6285SAndroid Build Coastguard Workerclass ManifestDiffTest(unittest.TestCase):
22*105f6285SAndroid Build Coastguard Worker
23*105f6285SAndroid Build Coastguard Worker  def _assertEqualCanonical(self, change1, change2):
24*105f6285SAndroid Build Coastguard Worker    def _canonicalize(change):
25*105f6285SAndroid Build Coastguard Worker      return {
26*105f6285SAndroid Build Coastguard Worker          identifier : ' '.join(sorted(value.split(' ')))
27*105f6285SAndroid Build Coastguard Worker          for identifier, value in change.items()
28*105f6285SAndroid Build Coastguard Worker      }
29*105f6285SAndroid Build Coastguard Worker    return self.assertEqual(_canonicalize(change1), _canonicalize(change2))
30*105f6285SAndroid Build Coastguard Worker
31*105f6285SAndroid Build Coastguard Worker  def test_project_changes(self):
32*105f6285SAndroid Build Coastguard Worker    p1 = ET.fromstring("""<project attr1="hello">
33*105f6285SAndroid Build Coastguard Worker      <linkfile src="newfile2" dest="notneeded" />
34*105f6285SAndroid Build Coastguard Worker      <linkfile src="oldfile1" dest="dest1" />
35*105f6285SAndroid Build Coastguard Worker      <copyfile src="oldfile2" dest="dest2" />
36*105f6285SAndroid Build Coastguard Worker    </project>
37*105f6285SAndroid Build Coastguard Worker    """)
38*105f6285SAndroid Build Coastguard Worker    p2 = ET.fromstring("""<project>
39*105f6285SAndroid Build Coastguard Worker      <linkfile src="newfile1" dest="dest1" />
40*105f6285SAndroid Build Coastguard Worker      <copyfile src="newfile2" dest="dest2" />
41*105f6285SAndroid Build Coastguard Worker      <copyfile src="somefile" dest="addedfile" />
42*105f6285SAndroid Build Coastguard Worker    </project>
43*105f6285SAndroid Build Coastguard Worker    """)
44*105f6285SAndroid Build Coastguard Worker    changes = manifest_diff.project_changes(p1, p2, set())
45*105f6285SAndroid Build Coastguard Worker    self.assertEqual(changes.linkfiles.added, {})
46*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(
47*105f6285SAndroid Build Coastguard Worker        changes.linkfiles.removed,
48*105f6285SAndroid Build Coastguard Worker        {'notneeded': '<linkfile src="newfile2" dest="notneeded" />'})
49*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
50*105f6285SAndroid Build Coastguard Worker        changes.linkfiles.modified, {
51*105f6285SAndroid Build Coastguard Worker            'dest1':
52*105f6285SAndroid Build Coastguard Worker                manifest_diff.ChangeMap(modified={
53*105f6285SAndroid Build Coastguard Worker                    'src': manifest_diff.Change('oldfile1', 'newfile1')
54*105f6285SAndroid Build Coastguard Worker                })
55*105f6285SAndroid Build Coastguard Worker        })
56*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(
57*105f6285SAndroid Build Coastguard Worker        changes.copyfiles.added,
58*105f6285SAndroid Build Coastguard Worker        {'addedfile': '<copyfile src="somefile" dest="addedfile" />'})
59*105f6285SAndroid Build Coastguard Worker    self.assertEqual(changes.copyfiles.removed, {})
60*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
61*105f6285SAndroid Build Coastguard Worker        changes.copyfiles.modified, {
62*105f6285SAndroid Build Coastguard Worker            'dest2':
63*105f6285SAndroid Build Coastguard Worker                manifest_diff.ChangeMap(modified={
64*105f6285SAndroid Build Coastguard Worker                    'src': manifest_diff.Change('oldfile2', 'newfile2')
65*105f6285SAndroid Build Coastguard Worker                })
66*105f6285SAndroid Build Coastguard Worker        })
67*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
68*105f6285SAndroid Build Coastguard Worker        changes.attributes,
69*105f6285SAndroid Build Coastguard Worker        manifest_diff.ChangeMap(
70*105f6285SAndroid Build Coastguard Worker            added={}, removed={'attr1': 'hello'}, modified={}))
71*105f6285SAndroid Build Coastguard Worker
72*105f6285SAndroid Build Coastguard Worker  def test_project_changes_same(self):
73*105f6285SAndroid Build Coastguard Worker    p1 = ET.fromstring("""<project attr1="hello">
74*105f6285SAndroid Build Coastguard Worker      <linkfile src="newfile2" dest="notneeded" />
75*105f6285SAndroid Build Coastguard Worker      <linkfile src="oldfile1" dest="dest1" />
76*105f6285SAndroid Build Coastguard Worker      <copyfile src="oldfile2" dest="dest2" />
77*105f6285SAndroid Build Coastguard Worker    </project>
78*105f6285SAndroid Build Coastguard Worker    """)
79*105f6285SAndroid Build Coastguard Worker    changes = manifest_diff.project_changes(p1, p1, set())
80*105f6285SAndroid Build Coastguard Worker    self.assertFalse(changes)
81*105f6285SAndroid Build Coastguard Worker
82*105f6285SAndroid Build Coastguard Worker  def test_compare_single_node_elements(self):
83*105f6285SAndroid Build Coastguard Worker    m1 = ET.fromstring("""<manifest>
84*105f6285SAndroid Build Coastguard Worker      <default revision='dev' remote='aosp' />
85*105f6285SAndroid Build Coastguard Worker      <repo-hooks />
86*105f6285SAndroid Build Coastguard Worker    </manifest>
87*105f6285SAndroid Build Coastguard Worker    """)
88*105f6285SAndroid Build Coastguard Worker    m2 = ET.fromstring("""<manifest>
89*105f6285SAndroid Build Coastguard Worker      <default revision='release' />
90*105f6285SAndroid Build Coastguard Worker    </manifest>
91*105f6285SAndroid Build Coastguard Worker    """)
92*105f6285SAndroid Build Coastguard Worker    changes = manifest_diff.compare_single_node_elements(m1, m2, set())
93*105f6285SAndroid Build Coastguard Worker    self.assertEqual(changes.added, {})
94*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(changes.removed, {'repo-hooks': '<repo-hooks />'})
95*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
96*105f6285SAndroid Build Coastguard Worker        changes.modified, {
97*105f6285SAndroid Build Coastguard Worker            'default':
98*105f6285SAndroid Build Coastguard Worker                manifest_diff.ChangeMap(
99*105f6285SAndroid Build Coastguard Worker                    added={},
100*105f6285SAndroid Build Coastguard Worker                    removed={'remote': 'aosp'},
101*105f6285SAndroid Build Coastguard Worker                    modified={
102*105f6285SAndroid Build Coastguard Worker                        'revision': manifest_diff.Change('dev', 'release')
103*105f6285SAndroid Build Coastguard Worker                    })
104*105f6285SAndroid Build Coastguard Worker        })
105*105f6285SAndroid Build Coastguard Worker
106*105f6285SAndroid Build Coastguard Worker  def test_compare_remote_elements(self):
107*105f6285SAndroid Build Coastguard Worker    m1 = ET.fromstring("""<manifest>
108*105f6285SAndroid Build Coastguard Worker      <remote revision="dev" name="aosp" fetch="https://aosp-source.com" />
109*105f6285SAndroid Build Coastguard Worker      <remote name="android" fetch="https://android-source.com" attr="test" />
110*105f6285SAndroid Build Coastguard Worker      <repo-hooks />
111*105f6285SAndroid Build Coastguard Worker    </manifest>
112*105f6285SAndroid Build Coastguard Worker    """)
113*105f6285SAndroid Build Coastguard Worker    m2 = ET.fromstring("""<manifest>
114*105f6285SAndroid Build Coastguard Worker      <remote revision="dev" name="android" fetch="https://android-source.com"
115*105f6285SAndroid Build Coastguard Worker        attr="test2"/>
116*105f6285SAndroid Build Coastguard Worker    </manifest>
117*105f6285SAndroid Build Coastguard Worker    """)
118*105f6285SAndroid Build Coastguard Worker    changes = manifest_diff.compare_remote_elements(m1, m2, set())
119*105f6285SAndroid Build Coastguard Worker    self.assertEqual(changes.added, {})
120*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(
121*105f6285SAndroid Build Coastguard Worker        changes.removed, {
122*105f6285SAndroid Build Coastguard Worker            'aosp':
123*105f6285SAndroid Build Coastguard Worker                '<remote revision="dev" name="aosp" fetch="https://aosp-source.com" />'
124*105f6285SAndroid Build Coastguard Worker        })
125*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
126*105f6285SAndroid Build Coastguard Worker        changes.modified, {
127*105f6285SAndroid Build Coastguard Worker            'android':
128*105f6285SAndroid Build Coastguard Worker                manifest_diff.ChangeMap(
129*105f6285SAndroid Build Coastguard Worker                    added={'revision': 'dev'},
130*105f6285SAndroid Build Coastguard Worker                    removed={},
131*105f6285SAndroid Build Coastguard Worker                    modified={'attr': manifest_diff.Change('test', 'test2')})
132*105f6285SAndroid Build Coastguard Worker        })
133*105f6285SAndroid Build Coastguard Worker
134*105f6285SAndroid Build Coastguard Worker  def test_compare_project_elements(self):
135*105f6285SAndroid Build Coastguard Worker    m1 = ET.fromstring("""<manifest>
136*105f6285SAndroid Build Coastguard Worker      <project name="platform/project1" path="system/project1" />
137*105f6285SAndroid Build Coastguard Worker      <project name="platform/project2" path="system/project2" />
138*105f6285SAndroid Build Coastguard Worker      <project name="platform/project3" path="system/project3" />
139*105f6285SAndroid Build Coastguard Worker    </manifest>""")
140*105f6285SAndroid Build Coastguard Worker    m2 = ET.fromstring("""<manifest>
141*105f6285SAndroid Build Coastguard Worker      <project name="platform/project1" path="system/project1" />
142*105f6285SAndroid Build Coastguard Worker      <project name="system/project2" />
143*105f6285SAndroid Build Coastguard Worker      <project name="platform/project4" path="system/project4" />
144*105f6285SAndroid Build Coastguard Worker    </manifest>""")
145*105f6285SAndroid Build Coastguard Worker    changes = manifest_diff.compare_project_elements(m1, m2, set())
146*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(
147*105f6285SAndroid Build Coastguard Worker        changes.added, {
148*105f6285SAndroid Build Coastguard Worker            'system/project4':
149*105f6285SAndroid Build Coastguard Worker                '<project name="platform/project4" path="system/project4" />'
150*105f6285SAndroid Build Coastguard Worker        })
151*105f6285SAndroid Build Coastguard Worker    self._assertEqualCanonical(
152*105f6285SAndroid Build Coastguard Worker        changes.removed, {
153*105f6285SAndroid Build Coastguard Worker            'system/project3':
154*105f6285SAndroid Build Coastguard Worker                '<project name="platform/project3" path="system/project3" />'
155*105f6285SAndroid Build Coastguard Worker        })
156*105f6285SAndroid Build Coastguard Worker    self.assertEqual(
157*105f6285SAndroid Build Coastguard Worker        changes.modified, {
158*105f6285SAndroid Build Coastguard Worker            'system/project2':
159*105f6285SAndroid Build Coastguard Worker                manifest_diff.ProjectChanges(
160*105f6285SAndroid Build Coastguard Worker                    attributes=manifest_diff.ChangeMap(
161*105f6285SAndroid Build Coastguard Worker                        added={},
162*105f6285SAndroid Build Coastguard Worker                        removed={},
163*105f6285SAndroid Build Coastguard Worker                        modified={
164*105f6285SAndroid Build Coastguard Worker                            'name':
165*105f6285SAndroid Build Coastguard Worker                                manifest_diff.Change('platform/project2',
166*105f6285SAndroid Build Coastguard Worker                                                     'system/project2')
167*105f6285SAndroid Build Coastguard Worker                        }))
168*105f6285SAndroid Build Coastguard Worker        })
169