test_abstract_resource_locator.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Unit Tests for ly_test_tools._internal.managers.abstract_resource_locator
  6. """
  7. import os
  8. import unittest.mock as mock
  9. import pytest
  10. import ly_test_tools._internal.managers.abstract_resource_locator as abstract_resource_locator
  11. from ly_test_tools._internal.exceptions import LyTestToolsFrameworkException
  12. pytestmark = pytest.mark.SUITE_smoke
  13. mock_initial_path = "mock_initial_path"
  14. mock_engine_root = "mock_engine_root"
  15. mock_dev_path = "mock_dev_path"
  16. mock_build_directory = 'mock_build_directory'
  17. mock_project = 'mock_project'
  18. mock_manifest_json_file = {'projects': [mock_project]}
  19. mock_project_json_file = {'project_name': mock_project}
  20. mock_project_json = os.path.join(mock_project, 'project.json')
  21. class TestFindEngineRoot(object):
  22. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.abspath', mock.MagicMock())
  23. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.exists')
  24. def test_FindEngineRoot_InitialPathExists_ReturnsTuple(self, mock_path_exists):
  25. mock_path_exists.return_value = True
  26. engine_root = abstract_resource_locator._find_engine_root(mock_engine_root)
  27. assert engine_root == mock_engine_root
  28. mock_path_exists.assert_called_once()
  29. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.abspath')
  30. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.exists')
  31. def test_FindEngineRoot_InitialPathDoesntExist_RaisesOSError(self, mock_path_exists, mock_abspath):
  32. mock_path_exists.return_value = False
  33. mock_abspath.return_value = mock_engine_root
  34. with pytest.raises(LyTestToolsFrameworkException):
  35. abstract_resource_locator._find_engine_root(mock_initial_path)
  36. @mock.patch('builtins.open', mock.MagicMock())
  37. class TestFindProjectJson(object):
  38. @mock.patch('os.path.isfile', mock.MagicMock(return_value=True))
  39. @mock.patch('os.path.basename', mock.MagicMock(return_value=mock_project))
  40. @mock.patch('json.loads', mock.MagicMock(side_effect=[mock_manifest_json_file, mock_project_json_file]))
  41. def test_FindProjectJson_ManifestJson_ReturnsProjectJson(self):
  42. project = abstract_resource_locator._find_project_json(mock_engine_root, mock_project)
  43. assert project == mock_project_json
  44. @mock.patch('os.path.isfile', mock.MagicMock(side_effect=[False, True]))
  45. @mock.patch('json.loads', mock.MagicMock())
  46. def test_FindProjectJson_EngineRoot_ReturnsProjectJson(self):
  47. project = abstract_resource_locator._find_project_json(mock_engine_root, mock_project)
  48. assert project == os.path.join(mock_engine_root, mock_project_json)
  49. @mock.patch('os.path.isfile', mock.MagicMock(side_effect=[False, False]))
  50. @mock.patch('json.loads', mock.MagicMock())
  51. @mock.patch('ly_test_tools.environment.file_system.find_ancestor_file')
  52. def test_FindProjectJson_FindAncestorProject_ReturnsProjectJson(self, mock_find_ancestor):
  53. mock_find_ancestor_project = 'mock_ancestor_file//project'
  54. mock_find_ancestor.return_value = mock_find_ancestor_project
  55. project = abstract_resource_locator._find_project_json(mock_engine_root, mock_project)
  56. assert project == mock_find_ancestor_project
  57. assert mock_find_ancestor.called
  58. @mock.patch('os.path.isfile', mock.MagicMock(side_effect=[False, False]))
  59. @mock.patch('json.loads', mock.MagicMock())
  60. @mock.patch('ly_test_tools.environment.file_system.find_ancestor_file')
  61. def test_FindProjectJson_NoProjectReturned_RaisesOSError(self, mock_find_ancestor):
  62. mock_find_ancestor.return_value = None
  63. with pytest.raises(OSError):
  64. abstract_resource_locator._find_project_json(mock_engine_root, mock_project)
  65. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.abspath',
  66. mock.MagicMock(return_value=mock_initial_path))
  67. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator._find_engine_root',
  68. mock.MagicMock(return_value=mock_engine_root))
  69. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator._find_project_json',
  70. mock.MagicMock(return_value=os.path.join(mock_project, 'project.json')))
  71. class TestAbstractResourceLocator(object):
  72. def test_Init_HasEngineRoot_SetsAttrs(self):
  73. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  74. mock_build_directory, mock_project)
  75. assert mock_abstract_resource_locator._build_directory == mock_build_directory
  76. assert mock_abstract_resource_locator._engine_root == mock_engine_root
  77. assert mock_abstract_resource_locator._project == mock_project
  78. assert mock_abstract_resource_locator._project_json == os.path.join(mock_project, 'project.json')
  79. def test_EngineRoot_IsCalled_ReturnsEngineRoot(self):
  80. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  81. mock_build_directory, mock_project)
  82. assert mock_abstract_resource_locator.engine_root() == mock_engine_root
  83. def test_BuildDirectory_IsCalled_ReturnsBuildDirectoryPath(self):
  84. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  85. mock_build_directory, mock_project)
  86. assert mock_abstract_resource_locator.build_directory() == mock_build_directory
  87. def test_Project_IsCalled_ReturnsProjectDir(self):
  88. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  89. mock_build_directory, mock_project)
  90. assert mock_abstract_resource_locator.project() == os.path.dirname(mock_project_json)
  91. def test_AssetProcessor_IsCalled_ReturnsAssetProcessorPath(self):
  92. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  93. mock_build_directory, mock_project)
  94. expected_path = os.path.join(mock_abstract_resource_locator.build_directory(), 'AssetProcessor')
  95. assert mock_abstract_resource_locator.asset_processor() == expected_path
  96. def test_AssetProcessorBatch_IsCalled_ReturnsAssetProcessorBatchPath(self):
  97. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  98. mock_build_directory, mock_project)
  99. expected_path = os.path.join(mock_abstract_resource_locator.build_directory(), 'AssetProcessorBatch')
  100. assert mock_abstract_resource_locator.asset_processor_batch() == expected_path
  101. def test_Editor_IsCalled_ReturnsEditorPath(self):
  102. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  103. mock_build_directory, mock_project)
  104. expected_path = os.path.join(mock_abstract_resource_locator.build_directory(), 'Editor')
  105. assert mock_abstract_resource_locator.editor() == expected_path
  106. def test_Cache_IsCalled_ReturnsCachePath(self):
  107. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  108. mock_build_directory, mock_project)
  109. expected_path = os.path.join(mock_abstract_resource_locator.cache())
  110. assert mock_abstract_resource_locator.cache() == expected_path
  111. def test_AssetProcessorConfigFile_IsCalled_ReturnsAssetProcessorConfigFilePath(self):
  112. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  113. mock_build_directory, mock_project)
  114. expected_path = os.path.join(mock_abstract_resource_locator.engine_root(), 'Registry', 'AssetProcessorPlatformConfig.setreg')
  115. assert mock_abstract_resource_locator.asset_processor_config_file() == expected_path
  116. def test_AutoexecFile_IsCalled_ReturnsAutoexecFilePath(self):
  117. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  118. mock_build_directory, mock_project)
  119. expected_path = os.path.join(mock_abstract_resource_locator._project,
  120. 'autoexec.cfg')
  121. assert mock_abstract_resource_locator.autoexec_file() == expected_path
  122. def test_TestResults_IsCalled_TestResultsPath(self):
  123. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  124. mock_build_directory, mock_project)
  125. expected_path = os.path.join(mock_abstract_resource_locator.engine_root(), 'TestResults')
  126. assert mock_abstract_resource_locator.test_results() == expected_path
  127. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.expanduser')
  128. def test_DevicesFile_IsCalled_ReturnsDevicesFilePath(self, mock_expanduser):
  129. mock_expanded_path = 'C:/somepath/'
  130. mock_expanduser.return_value = mock_expanded_path
  131. expected_path = os.path.join(mock_expanded_path, 'ly_test_tools', 'devices.ini')
  132. assert abstract_resource_locator.AbstractResourceLocator.devices_file() == expected_path
  133. def test_PlatformCache_NotImplemented_RaisesNotImplementedError(self):
  134. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  135. mock_build_directory, mock_project)
  136. with pytest.raises(NotImplementedError):
  137. mock_abstract_resource_locator.platform_cache()
  138. def test_ProjectLog_NotImplemented_RaisesNotImplementedError(self):
  139. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  140. mock_build_directory, mock_project)
  141. with pytest.raises(NotImplementedError):
  142. mock_abstract_resource_locator.project_log()
  143. def test_ProjectScreenshots_NotImplemented_RaisesNotImplementedError(self):
  144. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  145. mock_build_directory, mock_project)
  146. with pytest.raises(NotImplementedError):
  147. mock_abstract_resource_locator.project_screenshots()
  148. def test_EditorLog_NotImplemented_RaisesNotImplementedError(self):
  149. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  150. mock_build_directory, mock_project)
  151. with pytest.raises(NotImplementedError):
  152. mock_abstract_resource_locator.editor_log()
  153. def test_CrashLog_NotImplemented_RaisesNotImplementedError(self):
  154. mock_abstract_resource_locator = abstract_resource_locator.AbstractResourceLocator(
  155. mock_build_directory, mock_project)
  156. with pytest.raises(NotImplementedError):
  157. mock_abstract_resource_locator.crash_log()