blob: 0e3641d9cdfbf978baec25f504930ddea92d15a6 [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.
14import unittest
15
16from pyfakefs import fake_filesystem_unittest
17
18import bloaty_merger
19import file_sections_pb2
20
21
22class BloatyMergerTestCase(fake_filesystem_unittest.TestCase):
23 def setUp(self):
24 self.setUpPyfakefs()
25
26 def test_parse_csv(self):
27 csv_content = "sections,vmsize,filesize\nsection1,2,3\n"
28 self.fs.create_file("file1.bloaty.csv", contents=csv_content)
29 pb = bloaty_merger.parse_csv("file1.bloaty.csv")
30 self.assertEqual(pb.path, "file1")
31 self.assertEqual(len(pb.sections), 1)
32 s = pb.sections[0]
33 self.assertEqual(s.name, "section1")
34 self.assertEqual(s.vm_size, 2)
35 self.assertEqual(s.file_size, 3)
36
37 def test_missing_file(self):
38 with self.assertRaises(FileNotFoundError):
39 bloaty_merger.parse_csv("missing.bloaty.csv")
40
41 def test_malformed_csv(self):
42 csv_content = "header1,heaVder2,header3\n4,5,6\n"
43 self.fs.create_file("file1.bloaty.csv", contents=csv_content)
44 with self.assertRaises(KeyError):
45 bloaty_merger.parse_csv("file1.bloaty.csv")
46
47 def test_create_file_metrics(self):
48 file_list = "file1.bloaty.csv file2.bloaty.csv"
49 file1_content = "sections,vmsize,filesize\nsection1,2,3\nsection2,7,8"
50 file2_content = "sections,vmsize,filesize\nsection1,4,5\n"
51
52 self.fs.create_file("files.lst", contents=file_list)
53 self.fs.create_file("file1.bloaty.csv", contents=file1_content)
54 self.fs.create_file("file2.bloaty.csv", contents=file2_content)
55
56 bloaty_merger.create_file_size_metrics("files.lst", "output.pb")
57
58 metrics = file_sections_pb2.FileSizeMetrics()
59 with open("output.pb", "rb") as output:
60 metrics.ParseFromString(output.read())
61
62
63if __name__ == '__main__':
64 suite = unittest.TestLoader().loadTestsFromTestCase(BloatyMergerTestCase)
65 unittest.TextTestRunner(verbosity=2).run(suite)