unit_test_android_post_build.py 6.4 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. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import pathlib
  9. import platform
  10. import shutil
  11. from cmake.Tools.Platform.Android import android_post_build
  12. def test_copy_or_create_link(tmpdir):
  13. tmpdir.ensure('src/level1', dir=True)
  14. src_file_l1 = tmpdir.join('src/level1/file_l1')
  15. src_file_l1.write('file_l1')
  16. src_file_l2 = tmpdir.join('src/level1/file_l2')
  17. src_file_l2.write('file_l2')
  18. src_file_1 = tmpdir.join('src/file_1')
  19. src_file_1.write('file_1')
  20. src_level1 = tmpdir.join('src/level1')
  21. tmpdir.ensure('tgt', dir=True)
  22. tgt_level1 = tmpdir.join('tgt/level1')
  23. tgt_file_1 = tmpdir.join('tgt/file_1')
  24. android_post_build.create_link(pathlib.Path(src_file_1.realpath()),
  25. pathlib.Path(tgt_file_1.realpath()))
  26. android_post_build.create_link(pathlib.Path(src_level1.realpath()),
  27. pathlib.Path(tgt_level1.realpath()))
  28. assert pathlib.Path(tgt_level1.realpath()).exists()
  29. assert pathlib.Path(tgt_file_1.realpath()).exists()
  30. def test_safe_clear_folder(tmpdir):
  31. tmpdir.ensure('src/level1', dir=True)
  32. src_file1 = tmpdir.join('src/level1/file_l1')
  33. src_file1.write('file_l1')
  34. src_file2 = tmpdir.join('src/level1/file_l2')
  35. src_file2.write('file_l2')
  36. src_file3 = tmpdir.join('src/file_1')
  37. src_file3.write('file_1')
  38. src_level1 = tmpdir.join('src/level1')
  39. tmpdir.ensure('tgt', dir=True)
  40. tgt = tmpdir.join('tgt')
  41. tgt_level1 = tmpdir.join('tgt/level1')
  42. tgt_file1 = tmpdir.join('tgt/file_1')
  43. if platform.system() == "Windows":
  44. import _winapi
  45. _winapi.CreateJunction(str(src_level1.realpath()), str(tgt_level1.realpath()))
  46. else:
  47. pathlib.Path(src_level1.realpath()).symlink_to(tgt_level1.realpath())
  48. shutil.copy2(src_file3.realpath(), tgt_file1.realpath())
  49. target_path = pathlib.Path(tgt.realpath())
  50. android_post_build.safe_clear_folder(target_path)
  51. assert not pathlib.Path(tgt_level1.realpath()).exists()
  52. assert not pathlib.Path(tgt_file1.realpath()).exists()
  53. assert pathlib.Path(src_file1.realpath()).is_file()
  54. assert pathlib.Path(src_file2.realpath()).is_file()
  55. assert pathlib.Path(src_file3.realpath()).is_file()
  56. def test_copy_folder_with_linked_directories(tmpdir):
  57. tmpdir.ensure('src/level1', dir=True)
  58. src_path = tmpdir.join('src')
  59. src_file_l1 = tmpdir.join('src/level1/file_l1')
  60. src_file_l1.write('file_l1')
  61. src_file_l2 = tmpdir.join('src/level1/file_l2')
  62. src_file_l2.write('file_l2')
  63. src_file_1 = tmpdir.join('src/file_1')
  64. src_file_1.write('file_1')
  65. tmpdir.ensure('tgt', dir=True)
  66. tgt_path = tmpdir.join('tgt')
  67. android_post_build.synchronize_folders(pathlib.Path(src_path.realpath()),
  68. pathlib.Path(tgt_path.realpath()))
  69. tgt_level1 = tmpdir.join('tgt/level1')
  70. tgt_file_1 = tmpdir.join('tgt/file_1')
  71. assert pathlib.Path(tgt_level1.realpath()).exists()
  72. assert pathlib.Path(tgt_file_1.realpath()).exists()
  73. def test_apply_pak_layout_invalid_src_folder(tmpdir):
  74. tmpdir.ensure('src', dir=True)
  75. src_path = pathlib.Path(tmpdir.join('src').realpath())
  76. tmpdir.ensure('dst/android/app', dir=True)
  77. tgt_path = pathlib.Path(tmpdir.join('dst/android/app/assets').realpath())
  78. try:
  79. android_post_build.apply_pak_layout(project_root=src_path,
  80. asset_bundle_folder="Cache",
  81. target_layout_root=tgt_path)
  82. except android_post_build.AndroidPostBuildError as e:
  83. assert 'folder doesnt exist' in str(e)
  84. else:
  85. assert False, "Error expected"
  86. def test_apply_pak_layout_no_engine_pak_file(tmpdir):
  87. tmpdir.ensure('src/Cache', dir=True)
  88. src_path = pathlib.Path(tmpdir.join('src').realpath())
  89. tmpdir.ensure('dst/android/app', dir=True)
  90. tgt_path = pathlib.Path(tmpdir.join('dst/android/app/assets').realpath())
  91. try:
  92. android_post_build.apply_pak_layout(project_root=src_path,
  93. asset_bundle_folder="Cache",
  94. target_layout_root=tgt_path)
  95. except android_post_build.AndroidPostBuildError as e:
  96. assert 'engine_android.pak' in str(e)
  97. else:
  98. assert False, "Error expected"
  99. def test_apply_pak_layout_success(tmpdir):
  100. tmpdir.ensure('src/Cache', dir=True)
  101. src_path = pathlib.Path(tmpdir.join('src').realpath())
  102. test_engine_pak = tmpdir.join('src/Cache/engine_android.pak')
  103. test_engine_pak.write('engine')
  104. tmpdir.ensure('dst/android/app', dir=True)
  105. tgt_path = pathlib.Path(tmpdir.join('dst/android/app/assets').realpath())
  106. android_post_build.apply_pak_layout(project_root=src_path,
  107. asset_bundle_folder="Cache",
  108. target_layout_root=tgt_path)
  109. validate_engine_android_pak = tmpdir.join('dst/android/app/assets/engine_android.pak')
  110. assert pathlib.Path(validate_engine_android_pak.realpath()).exists()
  111. def test_apply_loose_layout_no_loose_assets(tmpdir):
  112. tmpdir.ensure('src/Cache/android', dir=True)
  113. src_path = pathlib.Path(tmpdir.join('src').realpath())
  114. tmpdir.ensure('dst/android/app', dir=True)
  115. tgt_path = pathlib.Path(tmpdir.join('dst/android/app/assets').realpath())
  116. try:
  117. android_post_build.apply_loose_layout(project_root=src_path,
  118. target_layout_root=tgt_path)
  119. except android_post_build.AndroidPostBuildError as e:
  120. assert 'Assets have not been built' in str(e)
  121. else:
  122. assert False, "Error expected"
  123. def test_apply_loose_layout_success(tmpdir):
  124. tmpdir.ensure('src/Cache/android', dir=True)
  125. src_path = pathlib.Path(tmpdir.join('src').realpath())
  126. test_engine_pak = tmpdir.join('src/Cache/android/engine.json')
  127. test_engine_pak.write('engine')
  128. tmpdir.ensure('dst/android/app', dir=True)
  129. tgt_path = pathlib.Path(tmpdir.join('dst/android/app/assets').realpath())
  130. android_post_build.apply_loose_layout(project_root=src_path,
  131. target_layout_root=tgt_path)
  132. validate_engine_android_pak = tmpdir.join('dst/android/app/assets/engine.json')
  133. assert pathlib.Path(validate_engine_android_pak.realpath()).exists()