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