unit_test_common.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import configparser
  9. import hashlib
  10. import json
  11. import os
  12. import pathlib
  13. import pytest
  14. import re
  15. from . import common
  16. @pytest.mark.parametrize(
  17. "engine_json_content, expected_success", [
  18. pytest.param({'fake': 'foo'}, True, id="TestSuccess"),
  19. pytest.param(None, False, id="TestFail")
  20. ])
  21. def test_determine_engine_root(tmpdir, engine_json_content, expected_success):
  22. test_folder_heirarchy = 'dev/foo1/foo2/foo3/'
  23. tmpdir.ensure(test_folder_heirarchy)
  24. if engine_json_content:
  25. fake_engine_json_content = json.dumps(engine_json_content,
  26. sort_keys=True,
  27. separators=(',', ': '),
  28. indent=4)
  29. engine_json_file = tmpdir.join('dev/engine.json')
  30. engine_json_file.write(fake_engine_json_content)
  31. expected_path = str(tmpdir.join('dev/').realpath())
  32. else:
  33. expected_path = None
  34. starting_path = str(tmpdir.join(test_folder_heirarchy).realpath())
  35. result = common.determine_engine_root(starting_path)
  36. if expected_path:
  37. assert os.path.normcase(result) == os.path.normcase(expected_path)
  38. else:
  39. assert result is None
  40. TEST_AP_CONFIG_1 = """
  41. [Platforms]
  42. ;pc=enabled
  43. ;ios=enabled
  44. """
  45. TEST_AP_CONFIG_2 = """
  46. [Platforms]
  47. ;pc=enabled
  48. ios=enabled
  49. """
  50. TEST_AP_CONFIG_3 = """
  51. [Platforms]
  52. pc=disabled
  53. ios=enabled
  54. """
  55. @pytest.mark.parametrize(
  56. "contents, check_asset_type, expected_result", [
  57. pytest.param(TEST_AP_CONFIG_1, 'ios', False, id='IosDisabled'),
  58. pytest.param(TEST_AP_CONFIG_2, 'ios', True, id='IosEnabled'),
  59. pytest.param(TEST_AP_CONFIG_3, 'ios', True, id='IosEnabled'),
  60. pytest.param(TEST_AP_CONFIG_1, 'pc', True, id='PCEnabled'),
  61. pytest.param(TEST_AP_CONFIG_3, 'pc', False, id='PCDisabled'),
  62. ]
  63. )
  64. def test_validate_ap_config_asset_type_enabled(tmpdir, contents, check_asset_type, expected_result):
  65. test_dev_root = 'dev'
  66. tmpdir.ensure('{}/AssetProcessorPlatformConfig.ini'.format(test_dev_root))
  67. ap_config_file = tmpdir.join('{}/AssetProcessorPlatformConfig.ini'.format(test_dev_root))
  68. ap_config_file.write(contents)
  69. ap_config_file_path = str(tmpdir.join(test_dev_root).realpath())
  70. result = common.validate_ap_config_asset_type_enabled(ap_config_file_path, check_asset_type)
  71. assert expected_result == result
  72. @pytest.mark.parametrize(
  73. "filename, file_mtime, file_size, contents, deep_check", [
  74. pytest.param('alpha.txt', 1000, 1000, "Alpha Alpha Alpha", False, id="TestShallow"),
  75. pytest.param('alpha.txt', 1000, 1000, "Alpha Alpha Alpha", True, id="TestDeepMatch"),
  76. pytest.param('beta.txt', 1000, 1000, "Beta Beta Beta", False, id="TestShallowMatch2"),
  77. pytest.param('beta.txt', 1001, 1000, "Beta Beta Beta", False, id="TestShallowMatch3"),
  78. pytest.param('ceti.txt', 2000, 2000, "Ceti Ceti Ceti", True, id="TestDeepMatch3"),
  79. ]
  80. )
  81. def test_file_fingerprint_success(tmpdir, filename, file_mtime, file_size, contents, deep_check):
  82. backup_stat = os.stat
  83. tmpdir.ensure(filename)
  84. ap_config_file = tmpdir.join(filename)
  85. ap_config_file.write(contents)
  86. full_path = str(tmpdir.join(filename).realpath())
  87. try:
  88. class MockStatResult(object):
  89. def __init__(self):
  90. self.st_mtime = file_mtime
  91. self.st_size = file_size
  92. def _mock_stat(path):
  93. assert path == full_path
  94. return MockStatResult()
  95. os.stat = _mock_stat
  96. expected_hasher = hashlib.md5()
  97. expected_hasher.update(str(file_mtime).encode('UTF-8'))
  98. expected_hasher.update(str(file_size).encode('UTF-8'))
  99. # If doing a deep check, also include the contents
  100. if deep_check:
  101. expected_hasher.update(contents.encode('UTF-8'))
  102. expected_result = expected_hasher.hexdigest()
  103. result = common.file_fingerprint(full_path, deep_check)
  104. assert result == expected_result
  105. finally:
  106. os.stat = backup_stat
  107. def test_load_template_file_success(tmpdir):
  108. tmpdir.ensure('test_template.txt.in')
  109. test_template_content = """
  110. ### Copyright will be removed from template
  111. [test]
  112. subjectA = ${subject_A_value}
  113. subjectB = ${subject_B_value}
  114. """
  115. expected_a = 'foo'
  116. expected_b = 'bar'
  117. test_template_file = tmpdir / 'test_template.txt.in'
  118. test_template_file.write_text(test_template_content, encoding='ascii')
  119. test_template_env = {
  120. 'subject_A_value': expected_a,
  121. 'subject_B_value': expected_b
  122. }
  123. result = common.load_template_file(pathlib.Path(str(test_template_file.realpath())), test_template_env)
  124. assert '###' not in result
  125. validator = configparser.ConfigParser()
  126. validator.read_string(result)
  127. validate_subjA = validator.get('test', 'subjectA')
  128. validate_subjB = validator.get('test', 'subjectB')
  129. assert validate_subjA == expected_a
  130. assert validate_subjB == expected_b
  131. TEST_GAME_PROJECT_JSON_FORMAT = """
  132. {{
  133. "project_name": "{project_name}",
  134. "product_name": "{project_name}",
  135. "executable_name": "{project_name}.GameLauncher",
  136. "modules" : [],
  137. "project_id": "{{4F3363D3-4A7C-47A6-B464-B21524771358}}"
  138. }}
  139. """
  140. def test_verify_game_project_and_dev_root_success(tmpdir):
  141. dev_root = 'dev'
  142. game_name = 'MyFoo'
  143. game_folder = 'myfoo'
  144. game_project_json = TEST_GAME_PROJECT_JSON_FORMAT.format(project_name=game_name)
  145. tmpdir.ensure(f'{dev_root}/{game_folder}/project.json')
  146. project_json_path = tmpdir / dev_root / game_folder / 'project.json'
  147. project_json_path.write_text(game_project_json, encoding='ascii')
  148. result_game_name, _ = common.verify_game_project_and_dev_root(game_folder,
  149. str(tmpdir.join(dev_root).realpath()))
  150. assert result_game_name == game_name
  151. def test_platform_last_settings_success(tmpdir):
  152. tmpdir.ensure('platform.list')
  153. test_build_dir = "c:/test/path"
  154. test_projects_str = "ProjA;ProjB"
  155. test_projects = test_projects_str.split(';')
  156. test_asset_deploy_mode = "LOOSE"
  157. test_asset_deploy_type = "pc"
  158. test_platform = 'foo'
  159. last_settings_content = f"""
  160. # Auto Generated from last cmake project generation (2020-07-24T12:10:47)
  161. [settings]
  162. platform={test_platform}
  163. game_projects={test_projects_str}
  164. asset_deploy_mode={test_asset_deploy_mode}
  165. asset_deploy_type={test_asset_deploy_type}
  166. """
  167. test_last_file = tmpdir / 'platform.settings'
  168. test_last_file.write_text(last_settings_content, encoding='ascii')
  169. result = common.PlatformSettings(tmpdir.realpath())
  170. assert result.projects == test_projects
  171. assert result.asset_deploy_mode == test_asset_deploy_mode
  172. assert result.asset_deploy_type == test_asset_deploy_type
  173. def test_cmake_dependency_success(tmpdir):
  174. test_module = 'FooBar'
  175. tmpdir.ensure(f'Registry/cmake_dependencies.{test_module.lower()}.setreg')
  176. test_setreg_path = tmpdir / 'Registry' / f'cmake_dependencies.{test_module.lower()}.setreg'
  177. test_module_1 = "Gem.Maestro.Editor.3b9a978ed6f742a1acb99f74379a342c.v0.1.0.dll"
  178. test_module_2 = "Gem.TextureAtlas.5a149b6b3c964064bd4970f0e92f72e2.v0.1.0.dll"
  179. test_retreg_content = f"""
  180. {{
  181. "Amazon":
  182. {{
  183. "Gems":
  184. {{
  185. "Maestro.Editor":
  186. {{
  187. "Module":"{test_module_1}",
  188. "SourcePaths":["Gems/Maestro"]
  189. }},
  190. "TextureAtlas":
  191. {{
  192. "Module":"{test_module_2}",
  193. "SourcePaths":["Gems/TextureAtlas"]
  194. }}
  195. }}
  196. }}
  197. }}
  198. """
  199. test_setreg_path.write_text(test_retreg_content, encoding='ascii')
  200. result = common.get_cmake_dependency_modules(tmpdir, test_module, 'Gems')
  201. assert result
  202. assert test_module_1 in result
  203. assert test_module_2 in result