blob: 83680b9c2a4b8fc3392221080c8025bae34a6010 [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
Spandan Das16c2b8c2021-08-18 17:46:46 +000017# pylint: disable=import-error
Thiébaud Weksteen713db482021-02-10 14:03:27 +010018from pyfakefs import fake_filesystem_unittest
19
20import bloaty_merger
21import file_sections_pb2
22
23
24class BloatyMergerTestCase(fake_filesystem_unittest.TestCase):
Spandan Das16c2b8c2021-08-18 17:46:46 +000025 def setUp(self):
26 self.setUpPyfakefs()
Thiébaud Weksteen713db482021-02-10 14:03:27 +010027
Spandan Das16c2b8c2021-08-18 17:46:46 +000028 def test_parse_csv(self):
29 csv_content = "sections,vmsize,filesize\nsection1,2,3\n"
30 self.fs.create_file("file1.bloaty.csv", contents=csv_content)
31 pb = bloaty_merger.parse_csv("file1.bloaty.csv")
32 self.assertEqual(pb.path, "file1")
33 self.assertEqual(len(pb.sections), 1)
34 s = pb.sections[0]
35 self.assertEqual(s.name, "section1")
36 self.assertEqual(s.vm_size, 2)
37 self.assertEqual(s.file_size, 3)
Thiébaud Weksteen713db482021-02-10 14:03:27 +010038
Spandan Das16c2b8c2021-08-18 17:46:46 +000039 def test_missing_file(self):
40 with self.assertRaises(FileNotFoundError):
41 bloaty_merger.parse_csv("missing.bloaty.csv")
Thiébaud Weksteen713db482021-02-10 14:03:27 +010042
Spandan Das16c2b8c2021-08-18 17:46:46 +000043 def test_malformed_csv(self):
44 csv_content = "header1,heaVder2,header3\n4,5,6\n"
45 self.fs.create_file("file1.bloaty.csv", contents=csv_content)
46 with self.assertRaises(KeyError):
47 bloaty_merger.parse_csv("file1.bloaty.csv")
Thiébaud Weksteen713db482021-02-10 14:03:27 +010048
Spandan Das16c2b8c2021-08-18 17:46:46 +000049 def test_create_file_metrics(self):
50 file_list = "file1.bloaty.csv file2.bloaty.csv"
51 file1_content = "sections,vmsize,filesize\nsection1,2,3\nsection2,7,8"
52 file2_content = "sections,vmsize,filesize\nsection1,4,5\n"
Thiébaud Weksteen713db482021-02-10 14:03:27 +010053
Spandan Das16c2b8c2021-08-18 17:46:46 +000054 self.fs.create_file("files.lst", contents=file_list)
55 self.fs.create_file("file1.bloaty.csv", contents=file1_content)
56 self.fs.create_file("file2.bloaty.csv", contents=file2_content)
Thiébaud Weksteen713db482021-02-10 14:03:27 +010057
Spandan Das16c2b8c2021-08-18 17:46:46 +000058 bloaty_merger.create_file_size_metrics("files.lst", "output.pb.gz")
Thiébaud Weksteen713db482021-02-10 14:03:27 +010059
Spandan Das16c2b8c2021-08-18 17:46:46 +000060 metrics = file_sections_pb2.FileSizeMetrics()
61 with gzip.open("output.pb.gz", "rb") as output:
62 metrics.ParseFromString(output.read())
Thiébaud Weksteen713db482021-02-10 14:03:27 +010063
64
65if __name__ == '__main__':
Spandan Das16c2b8c2021-08-18 17:46:46 +000066 suite = unittest.TestLoader().loadTestsFromTestCase(BloatyMergerTestCase)
67 unittest.TextTestRunner(verbosity=2).run(suite)