test_workspace.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 workspace module
  6. """
  7. import unittest.mock as mock
  8. import pytest
  9. import unittest
  10. import ly_test_tools._internal.managers.workspace
  11. pytestmark = pytest.mark.SUITE_smoke
  12. mock_initial_path = "mock_initial_path"
  13. mock_engine_root = "mock_engine_root"
  14. mock_dev_path = "mock_dev_path"
  15. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator.os.path.abspath',
  16. mock.MagicMock(return_value=mock_initial_path))
  17. @mock.patch('ly_test_tools._internal.managers.abstract_resource_locator._find_engine_root',
  18. mock.MagicMock(return_value=(mock_engine_root, mock_dev_path)))
  19. class MockedWorkspaceManager(ly_test_tools._internal.managers.workspace.AbstractWorkspaceManager):
  20. def __init__(
  21. self,
  22. resource_locator=mock.MagicMock(),
  23. project=mock.MagicMock(),
  24. tmp_path=mock.MagicMock(),
  25. output_path=mock.MagicMock()
  26. ):
  27. super(MockedWorkspaceManager, self).__init__(
  28. resource_locator=resource_locator,
  29. project=project,
  30. tmp_path=tmp_path,
  31. output_path=output_path
  32. )
  33. def setup(self):
  34. super(MockedWorkspaceManager, self).setup()
  35. def run_setup_assistant(self):
  36. pass
  37. class TestWorkspaceManager:
  38. def test_Init_NoInheritanceAbstractWorkspaceManager_RaisesTypeError(self):
  39. mock_resource_locator = mock.MagicMock()
  40. with pytest.raises(TypeError):
  41. ly_test_tools._internal.managers.workspace.AbstractWorkspaceManager(resource_locator=mock_resource_locator)
  42. @mock.patch('ly_test_tools._internal.managers.artifact_manager.NullArtifactManager.__init__')
  43. @mock.patch('tempfile.mkdtemp')
  44. def test_Init_TmpPathIsNone_TmpPathIsCreated(self, under_test, mock_null_artifact_manager):
  45. mock_null_artifact_manager.return_value = None
  46. mock_workspace = MockedWorkspaceManager(tmp_path=None)
  47. under_test.assert_called_once()
  48. @mock.patch('os.path.exists', mock.MagicMock())
  49. @mock.patch('tempfile.mkdtemp', mock.MagicMock())
  50. def test_Init_LogsPathIsNone_LogsPathIsSetToDefault(self):
  51. dummy_path = 'mockTestResults'
  52. mock_resource_locator = mock.MagicMock()
  53. mock_resource_locator.test_results.return_value = dummy_path
  54. mock_workspace = MockedWorkspaceManager(resource_locator=mock_resource_locator, output_path=None)
  55. assert mock_workspace.output_path.startswith(dummy_path)
  56. class TestSetup(unittest.TestCase):
  57. def setUp(self):
  58. self.mock_workspace = MockedWorkspaceManager()
  59. @mock.patch('os.makedirs')
  60. @mock.patch('os.path.exists')
  61. def test_Setup_TmpPathExists_CallsMakeDirs(self, mock_path_exists, mock_makedirs):
  62. self.mock_workspace.tmp_path = 'mock_tmp_path'
  63. mock_path_exists.side_effect = [False, True, True] # ArtifactManager.__init__() calls os.path.exists()
  64. self.mock_workspace._custom_output_path = True
  65. self.mock_workspace.setup()
  66. mock_makedirs.assert_called_once_with(self.mock_workspace.tmp_path, exist_ok=True)
  67. @mock.patch('os.makedirs')
  68. @mock.patch('os.path.exists')
  69. def test_Setup_TmpPathNotExists_NoCallsMakeDirs(self, mock_path_exists, mock_makedirs):
  70. mock_path_exists.return_value = True
  71. self.mock_workspace.tmp_path = None
  72. self.mock_workspace._custom_output_path = True
  73. self.mock_workspace.setup()
  74. assert not mock_makedirs.called
  75. @mock.patch('os.makedirs')
  76. @mock.patch('os.path.exists')
  77. def test_Setup_LogPathExists_NoCallsMakeDirs(self, mock_path_exists, mock_makedirs):
  78. mock_path_exists.return_value = True
  79. self.mock_workspace.tmp_path = None
  80. self.mock_workspace.setup()
  81. mock_makedirs.assert_not_called()
  82. @mock.patch('os.makedirs')
  83. @mock.patch('os.path.exists')
  84. def test_Setup_LogPathNotExists_CallsMakeDirs(self, mock_path_exists, mock_makedirs):
  85. mock_path_exists.return_value = False
  86. self.mock_workspace.tmp_path = None
  87. self.mock_workspace.output_path = 'mock_output_path'
  88. self.mock_workspace.setup()
  89. mock_makedirs.assert_called_with(self.mock_workspace.output_path, exist_ok=True)
  90. assert mock_makedirs.call_count == 2 # ArtifactManager.__init__() calls os.path.exists()
  91. class TestTeardown(unittest.TestCase):
  92. def setUp(self):
  93. self.mock_workspace = MockedWorkspaceManager()
  94. @mock.patch('os.chdir', mock.MagicMock())
  95. @mock.patch('ly_test_tools.environment.file_system.delete')
  96. def test_Teardown_TmpPathNotNone_DeletesTmpPath(self, mock_delete):
  97. self.mock_workspace.tmp_path = 'tmp_path'
  98. self.mock_workspace.teardown()
  99. mock_delete.assert_called_once_with([self.mock_workspace.tmp_path], True, True)
  100. @mock.patch('ly_test_tools.environment.file_system.delete', mock.MagicMock())
  101. @mock.patch('os.chdir')
  102. def test_Teardown_CwdModified_RevertsCwdToDefault(self, mock_chdir):
  103. self.mock_workspace.tmp_path = 'tmp_path'
  104. self.mock_workspace.teardown()
  105. mock_chdir.assert_called_once_with(self.mock_workspace._original_cwd)
  106. class TestClearCache(unittest.TestCase):
  107. def setUp(self):
  108. self.mock_workspace = MockedWorkspaceManager()
  109. @mock.patch('ly_test_tools.environment.file_system.delete')
  110. @mock.patch('os.path.exists')
  111. def test_ClearCache_CacheExists_CacheIsDeleted(self, mock_path_exists, mock_delete):
  112. mock_path_exists.return_value = True
  113. self.mock_workspace.clear_cache()
  114. mock_delete.assert_called_once_with([self.mock_workspace.paths.cache()], True, True)
  115. @mock.patch('ly_test_tools.environment.file_system.delete')
  116. @mock.patch('os.path.exists')
  117. def test_ClearCache_CacheNotExists_CacheNotDeleted(self, mock_path_exists, mock_delete):
  118. mock_path_exists.return_value = False
  119. self.mock_workspace.clear_cache()
  120. mock_delete.assert_not_called()
  121. class TestClearBin(unittest.TestCase):
  122. def setUp(self):
  123. self.mock_workspace = MockedWorkspaceManager()
  124. @mock.patch('ly_test_tools.environment.file_system.delete')
  125. @mock.patch('os.path.exists')
  126. def test_ClearBin_BinExists_BinIsDeleted(self, mock_path_exists, mock_delete):
  127. mock_path_exists.return_value = True
  128. self.mock_workspace.clear_bin()
  129. mock_delete.assert_called_once_with([self.mock_workspace.paths.build_directory()], True, True)
  130. @mock.patch('ly_test_tools.environment.file_system.delete')
  131. @mock.patch('os.path.exists')
  132. def test_ClearBin_BinNotExists_BinNotDeleted(self, mock_path_exists, mock_delete):
  133. mock_path_exists.return_value = False
  134. self.mock_workspace.clear_bin()
  135. mock_delete.assert_not_called()