xref: /aosp_15_r20/build/soong/bloaty/bloaty_merger_test.py (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker# Copyright 2021 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker#
3*333d2b36SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker#
7*333d2b36SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker#
9*333d2b36SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker# limitations under the License.
14*333d2b36SAndroid Build Coastguard Workerimport gzip
15*333d2b36SAndroid Build Coastguard Workerimport unittest
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Worker# pylint: disable=import-error
18*333d2b36SAndroid Build Coastguard Workerfrom pyfakefs import fake_filesystem_unittest
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Workerimport bloaty_merger
21*333d2b36SAndroid Build Coastguard Workerimport file_sections_pb2
22*333d2b36SAndroid Build Coastguard Worker
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Workerclass BloatyMergerTestCase(fake_filesystem_unittest.TestCase):
25*333d2b36SAndroid Build Coastguard Worker    def setUp(self):
26*333d2b36SAndroid Build Coastguard Worker        self.setUpPyfakefs()
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Worker    def test_parse_csv(self):
29*333d2b36SAndroid Build Coastguard Worker        csv_content = "sections,vmsize,filesize\nsection1,2,3\n"
30*333d2b36SAndroid Build Coastguard Worker        self.fs.create_file("file1.bloaty.csv", contents=csv_content)
31*333d2b36SAndroid Build Coastguard Worker        pb = bloaty_merger.parse_csv("file1.bloaty.csv")
32*333d2b36SAndroid Build Coastguard Worker        self.assertEqual(pb.path, "file1")
33*333d2b36SAndroid Build Coastguard Worker        self.assertEqual(len(pb.sections), 1)
34*333d2b36SAndroid Build Coastguard Worker        s = pb.sections[0]
35*333d2b36SAndroid Build Coastguard Worker        self.assertEqual(s.name, "section1")
36*333d2b36SAndroid Build Coastguard Worker        self.assertEqual(s.vm_size, 2)
37*333d2b36SAndroid Build Coastguard Worker        self.assertEqual(s.file_size, 3)
38*333d2b36SAndroid Build Coastguard Worker
39*333d2b36SAndroid Build Coastguard Worker    def test_missing_file(self):
40*333d2b36SAndroid Build Coastguard Worker        with self.assertRaises(FileNotFoundError):
41*333d2b36SAndroid Build Coastguard Worker            bloaty_merger.parse_csv("missing.bloaty.csv")
42*333d2b36SAndroid Build Coastguard Worker
43*333d2b36SAndroid Build Coastguard Worker    def test_malformed_csv(self):
44*333d2b36SAndroid Build Coastguard Worker        csv_content = "header1,heaVder2,header3\n4,5,6\n"
45*333d2b36SAndroid Build Coastguard Worker        self.fs.create_file("file1.bloaty.csv", contents=csv_content)
46*333d2b36SAndroid Build Coastguard Worker        with self.assertRaises(KeyError):
47*333d2b36SAndroid Build Coastguard Worker            bloaty_merger.parse_csv("file1.bloaty.csv")
48*333d2b36SAndroid Build Coastguard Worker
49*333d2b36SAndroid Build Coastguard Worker    def test_create_file_metrics(self):
50*333d2b36SAndroid Build Coastguard Worker        file_list = "file1.bloaty.csv file2.bloaty.csv"
51*333d2b36SAndroid Build Coastguard Worker        file1_content = "sections,vmsize,filesize\nsection1,2,3\nsection2,7,8"
52*333d2b36SAndroid Build Coastguard Worker        file2_content = "sections,vmsize,filesize\nsection1,4,5\n"
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Worker        self.fs.create_file("files.lst", contents=file_list)
55*333d2b36SAndroid Build Coastguard Worker        self.fs.create_file("file1.bloaty.csv", contents=file1_content)
56*333d2b36SAndroid Build Coastguard Worker        self.fs.create_file("file2.bloaty.csv", contents=file2_content)
57*333d2b36SAndroid Build Coastguard Worker
58*333d2b36SAndroid Build Coastguard Worker        bloaty_merger.create_file_size_metrics("files.lst", "output.pb.gz")
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Worker        metrics = file_sections_pb2.FileSizeMetrics()
61*333d2b36SAndroid Build Coastguard Worker        with gzip.open("output.pb.gz", "rb") as output:
62*333d2b36SAndroid Build Coastguard Worker            metrics.ParseFromString(output.read())
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Workerif __name__ == '__main__':
66*333d2b36SAndroid Build Coastguard Worker    suite = unittest.TestLoader().loadTestsFromTestCase(BloatyMergerTestCase)
67*333d2b36SAndroid Build Coastguard Worker    unittest.TextTestRunner(verbosity=2).run(suite)
68