bloaty_merger_test.py 2.5 KB

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