TestSuite_Utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import os
  7. import pytest
  8. import sys
  9. import ly_test_tools.environment.file_system as fs
  10. from .utils.FileManagement import FileManagement as fm
  11. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../automatedtesting_shared')
  12. from base import TestAutomationBase
  13. @pytest.mark.parametrize("launcher_platform", ['windows_editor'])
  14. @pytest.mark.parametrize("project", ["AutomatedTesting"])
  15. class TestUtils(TestAutomationBase):
  16. def test_UtilTest_Tracer_PicksErrorsAndWarnings(self, request, workspace, launcher_platform, editor):
  17. from .utils import UtilTest_Tracer_PicksErrorsAndWarnings as testcase_module
  18. self._run_test(request, workspace, editor, testcase_module, [], [])
  19. def test_FileManagement_FindingFiles(self, workspace, launcher_platform):
  20. """
  21. Tests the functionality of "searching for files" with FileManagement._find_files()
  22. :param workspace: ly_test_tools workspace fixture
  23. :return: None
  24. """
  25. # Set up known files and paths
  26. search_dir = os.path.join(workspace.paths.project(), "Levels", "Utils", "Managed_Files")
  27. find_me_path = search_dir
  28. first_also_find_me_path = os.path.join(search_dir, "some_folder")
  29. second_also_find_me_path = os.path.join(search_dir, "some_folder", "another_folder", "one_more_folder")
  30. find_me_too_path = os.path.join(search_dir, "some_folder", "another_folder")
  31. # Test for expected failures
  32. call_failed = False
  33. try:
  34. # Should throw a warning from finding too many copies of "AlsoFindMe.txt"
  35. fm._find_files(["FindMe.txt", "AlsoFindMe.txt", "FindMeToo.txt"], search_dir, search_subdirs=True)
  36. except RuntimeWarning:
  37. call_failed = True
  38. assert call_failed, "_find_files() DID NOT fail when finding multiple files"
  39. call_failed = False
  40. try:
  41. # Should throw a warning from not finding "DNE.txt"
  42. fm._find_files(["FindMe.txt", "DNE.txt"], search_dir, search_subdirs=True)
  43. except RuntimeWarning:
  44. call_failed = True
  45. assert call_failed, "_find_files() DID NOT fail when looking for a non-existent file"
  46. # Expected successful usages
  47. found_me = fm._find_files(["FindMe.txt"], search_dir)
  48. also_found_me_1 = fm._find_files(["AlsoFindMe.txt"], os.path.join(search_dir, "some_folder"))
  49. also_found_me_2 = fm._find_files(
  50. ["AlsoFindMe.txt"], os.path.join(search_dir, "some_folder", "another_folder", "one_more_folder")
  51. )
  52. find_a_few = fm._find_files(["FindMe.txt", "FindMeToo.txt"], search_dir, search_subdirs=True)
  53. # Test results
  54. assert (
  55. found_me["FindMe.txt"] == find_me_path
  56. ), "FindMe.txt path was not as expected. Expected: {} Found: {} ".format(find_me_path, found_me["FindMe.txt"])
  57. assert (
  58. also_found_me_1["AlsoFindMe.txt"] == first_also_find_me_path
  59. ), "First AlsoFindMe.txt path was not as expected. Expected: {} Found: {} ".format(
  60. first_also_find_me_path, also_found_me_1["AlsoFindMe.txt"]
  61. )
  62. assert (
  63. also_found_me_2["AlsoFindMe.txt"] == second_also_find_me_path
  64. ), "Second AlsoFindMe.txt path was not as expected. Expected: {} Found: {} ".format(
  65. second_also_find_me_path, also_found_me_2["AlsoFindMe.txt"]
  66. )
  67. for file_name, file_path in find_a_few.items():
  68. assert file_name == "FindMe.txt" or file_name == "FindMeToo.txt", "{} was found on accident".format(
  69. file_name
  70. )
  71. assert (
  72. "FindMe.txt" in find_a_few and find_a_few["FindMe.txt"] is not None
  73. ), "Finding multiple files did not find 'FindMe.txt'"
  74. assert (
  75. find_a_few["FindMe.txt"] == find_me_path
  76. ), "FindMe.txt path was not as expected. Expected: {} Found: {} ".format(find_me_path, found_me["FindMe.txt"])
  77. assert (
  78. "FindMeToo.txt" in find_a_few and find_a_few["FindMeToo.txt"] is not None
  79. ), "Finding multiple files did not find 'FindMeToo.txt'"
  80. assert (
  81. find_a_few["FindMeToo.txt"] == find_me_too_path
  82. ), "FindMeToo.txt path was not as expected. Expected: {} Found: {} ".format(
  83. find_me_too_path, found_me["FindMeToo.txt"]
  84. )
  85. def test_FileManagement_FileBackup(self, workspace, launcher_platform):
  86. """
  87. Tests the functionality of the file back up system via the FileManagement class
  88. :param workspace: ly_test_tools workspace fixture
  89. :return: None
  90. """
  91. def get_contents(file_path):
  92. # type: (str) -> [str]
  93. """
  94. Simply gets the contents of a file specified by the [file_path]
  95. :param file_path: file to get contents of
  96. :return: a list of strings
  97. """
  98. lines = []
  99. with open(file_path, "r") as file:
  100. for line in file:
  101. lines.append(line)
  102. return lines
  103. # Set up known paths and file names
  104. target_folder = os.path.join(workspace.paths.project(), "Levels", "Utils", "Managed_Files")
  105. target_file_path = os.path.join(target_folder, "FindMe.txt")
  106. backup_folder = fm.backup_folder_path
  107. # Make sure the target file exists
  108. assert os.path.exists(target_file_path), "Target file: {} does not exist".format(target_file_path)
  109. original_contents = get_contents(target_file_path)
  110. fm._backup_file("FindMe.txt", target_folder)
  111. # Get newly-created backup file full path
  112. file_map = fm._load_file_map()
  113. assert target_file_path in file_map.keys(), "File path {} was not added to FileManagement file_map"
  114. backup_file_path = os.path.join(backup_folder, file_map[target_file_path])
  115. # Ensure back up worked as expected
  116. assert os.path.exists(target_file_path), "Original file got destroyed: {}".format(target_file_path)
  117. for i, line in enumerate(get_contents(target_file_path)):
  118. assert (
  119. original_contents[i] == line
  120. ), "Original file ({}) changed after being backed up. Line[{}]: expected: {}, found: {}".format(
  121. target_file_path, i, original_contents[i], line
  122. )
  123. for i, line in enumerate(get_contents(backup_file_path)):
  124. assert (
  125. original_contents[i] == line
  126. ), "Backed up file ({}) different from original. Line[{}]: expected: {}, found: {}".format(
  127. backup_file_path, i, original_contents[i], line
  128. )
  129. # Delete up back up file and clear entry in file map
  130. fs.delete([backup_file_path], True, False)
  131. del file_map[target_file_path]
  132. fm._save_file_map(file_map)
  133. def test_FileManagement_FileRestoration(self, workspace, launcher_platform):
  134. """
  135. Tests the restore file system via the FileManagement class
  136. :param workspace: ly_test_tools workspace fixture
  137. :return: None
  138. """
  139. def get_contents(file_path):
  140. # type: (str) -> [str]
  141. """
  142. Simply gets the contents of a file specified by the [file_path]
  143. :param file_path: file to get contents of
  144. :return: a list of strings
  145. """
  146. lines = []
  147. with open(file_path, "r") as file:
  148. for line in file:
  149. lines.append(line)
  150. return lines
  151. # Set know files and paths
  152. target_folder = os.path.join(workspace.paths.project(), "Levels", "Utils", "Managed_Files")
  153. target_file_path = os.path.join(target_folder, "FindMe.txt")
  154. backup_folder = fm.backup_folder_path
  155. assert os.path.exists(target_file_path), "Target file: {} does not exist".format(target_file_path)
  156. # original file back up
  157. original_contents = get_contents(target_file_path)
  158. fm._backup_file("FindMe.txt", target_folder)
  159. # Get newly-created backup file full path
  160. file_map = fm._load_file_map()
  161. assert target_file_path in file_map.keys(), "File path {} was not added to FileManagement file_map".format(
  162. target_file_path
  163. )
  164. backup_file_path = os.path.join(backup_folder, file_map[target_file_path])
  165. assert os.path.exists(backup_file_path), "Back up file {} was not created".format(backup_file_path)
  166. # Makes sure back up is exactly the same as original
  167. for i, line in enumerate(get_contents(backup_file_path)):
  168. assert (
  169. original_contents[i] == line
  170. ), "Backed up file ({}) different than original. Line[{}]: expected: {}, found: {}".format(
  171. backup_file_path, i, original_contents[i], line
  172. )
  173. # Change original file
  174. fs.unlock_file(target_file_path)
  175. text_to_write = "Some new text to write into the target file!"
  176. with open(target_file_path, "w") as target_file:
  177. target_file.write(text_to_write)
  178. fs.lock_file(target_file_path)
  179. # Make sure original actually got changed
  180. changed_lines = get_contents(target_file_path)
  181. assert changed_lines[0] == text_to_write, "The file {} did not get changed while writing in test".format(
  182. target_file_path
  183. )
  184. assert changed_lines[0] != original_contents[0], "The file {} did not get changed while writing in test".format(
  185. target_file_path
  186. )
  187. # Restore the file!
  188. fm._restore_file("FindMe.txt", target_folder)
  189. # Ensure backup file was deleted after restoration
  190. assert not os.path.exists(backup_file_path), "Backup file {} WAS NOT deleted on restoration".format(
  191. backup_file_path
  192. )
  193. # Ensure saved file map was updated
  194. file_map = fm._load_file_map()
  195. assert (
  196. target_file_path not in file_map.keys()
  197. ), "File path {} was not cleared from file map after restoration".format(target_file_path)
  198. # Ensure restore brought original file back to it's default state
  199. restored_lines = get_contents(target_file_path)
  200. assert restored_lines[0] != text_to_write, "File restore for {} did not work".format(target_file_path)
  201. for restored, original, i in zip(restored_lines, original_contents, range(len(original_contents))):
  202. assert (
  203. restored == original
  204. ), "Restored file varies from original. Line [{}]: original: {}, restored: {}".format(i, original, restored)
  205. # Clean up back up folder and file
  206. fs.delete([backup_file_path], True, False)