bloaty_merger_test.py 2.3 KB

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