file_util_test.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env vpython3
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests of file_util functions."""
  6. import os
  7. import shutil
  8. import unittest
  9. import file_util
  10. import test_runner_test
  11. class TestCoverageUtil(test_runner_test.TestCase):
  12. """Test cases for file_util.py"""
  13. def create_origin_profraw_file_if_not_exist(self):
  14. """Creates the profraw file in the correct udid data folder to move if it
  15. doesn't exist
  16. """
  17. if not os.path.exists(self.origin_profraw_file_path):
  18. with open(self.origin_profraw_file_path, 'w') as outfile:
  19. outfile.write("Some raw coverage data.\n")
  20. def setUp(self):
  21. super(TestCoverageUtil, self).setUp()
  22. self.test_folder = os.path.join(os.getcwd(), "file_util_test_data")
  23. self.simulators_folder = os.path.join(self.test_folder, "Devices")
  24. self.existing_udid = "existing-udid"
  25. self.existing_udid_folder = os.path.join(self.simulators_folder,
  26. "existing-udid")
  27. self.existing_udid_data_folder = os.path.join(self.simulators_folder,
  28. self.existing_udid, "data")
  29. if not os.path.exists(self.existing_udid_data_folder):
  30. os.makedirs(self.existing_udid_data_folder)
  31. self.profraw_file_name = "default.profraw"
  32. self.origin_profraw_file_path = os.path.join(self.existing_udid_data_folder,
  33. self.profraw_file_name)
  34. self.not_existing_udid = "not-existing-udid"
  35. self.not_existing_udid_data_folder = os.path.join(self.simulators_folder,
  36. self.not_existing_udid,
  37. "data")
  38. if os.path.exists(self.not_existing_udid_data_folder):
  39. shutil.rmtree(self.not_existing_udid_data_folder)
  40. self.output_folder = os.path.join(self.test_folder, "output")
  41. if not os.path.exists(self.output_folder):
  42. os.makedirs(self.output_folder)
  43. self.expected_profraw_output_path = os.path.join(self.output_folder,
  44. "profraw",
  45. self.profraw_file_name)
  46. self.mock(file_util, 'SIMULATORS_FOLDER', self.simulators_folder)
  47. def tearDown(self):
  48. shutil.rmtree(self.test_folder)
  49. def test_move_raw_coverage_data(self):
  50. """Tests if file_util can correctly move raw coverage data"""
  51. self.create_origin_profraw_file_if_not_exist()
  52. self.assertTrue(os.path.exists(self.origin_profraw_file_path))
  53. self.assertFalse(os.path.exists(self.expected_profraw_output_path))
  54. file_util.move_raw_coverage_data(self.existing_udid, self.output_folder)
  55. self.assertFalse(os.path.exists(self.origin_profraw_file_path))
  56. self.assertTrue(os.path.exists(self.expected_profraw_output_path))
  57. os.remove(self.expected_profraw_output_path)
  58. def test_move_raw_coverage_data_origin_not_exist(self):
  59. """Ensures that file_util won't break when raw coverage data folder or
  60. file doesn't exist
  61. """
  62. # Tests origin directory doesn't exist.
  63. file_util.move_raw_coverage_data(self.not_existing_udid, self.output_folder)
  64. self.assertFalse(os.path.exists(self.expected_profraw_output_path))
  65. # Tests profraw file doesn't exist.
  66. if os.path.exists(self.origin_profraw_file_path):
  67. os.remove(self.origin_profraw_file_path)
  68. self.assertFalse(os.path.exists(self.origin_profraw_file_path))
  69. self.assertFalse(os.path.exists(self.expected_profraw_output_path))
  70. file_util.move_raw_coverage_data(self.existing_udid, self.output_folder)
  71. self.assertFalse(os.path.exists(self.expected_profraw_output_path))
  72. if __name__ == '__main__':
  73. unittest.main()