LuaBuilderTests.cpp 12 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. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <Builders/LuaBuilder/LuaBuilderWorker.h>
  10. #include <AzTest/Utils.h>
  11. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  12. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/UserSettings/UserSettingsComponent.h>
  15. #include <AzCore/Utils/Utils.h>
  16. #include <AzToolsFramework/Application/ToolsApplication.h>
  17. #include <AzCore/IO/FileIO.h>
  18. #include <AzCore/IO/Path/Path.h>
  19. namespace UnitTest
  20. {
  21. using namespace LuaBuilder;
  22. using namespace AssetBuilderSDK;
  23. class LuaBuilderTests
  24. : public UnitTest::LeakDetectionFixture
  25. {
  26. void SetUp() override
  27. {
  28. AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
  29. auto projectPathKey =
  30. AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_path";
  31. AZ::IO::FixedMaxPath enginePath;
  32. registry->Get(enginePath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  33. registry->Set(projectPathKey, (enginePath / "AutomatedTesting").Native());
  34. AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*registry);
  35. AZ::ComponentApplication::StartupParameters startupParameters;
  36. startupParameters.m_loadSettingsRegistry = false;
  37. m_app.Start(m_descriptor, startupParameters);
  38. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  39. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  40. // in the unit tests.
  41. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  42. const AZStd::string engineRoot = AZ::Test::GetEngineRootPath();
  43. AZ::IO::FileIOBase::GetInstance()->SetAlias("@engroot@", engineRoot.c_str());
  44. AZ::IO::Path assetRoot(AZ::Utils::GetProjectPath());
  45. assetRoot /= "Cache";
  46. AZ::IO::FileIOBase::GetInstance()->SetAlias("@products@", assetRoot.c_str());
  47. // Set the @gemroot:<gem-name> alias for LmbrCentral gem
  48. AZ::Test::AddActiveGem("LmbrCentral", *registry, AZ::IO::FileIOBase::GetInstance());
  49. }
  50. void TearDown() override
  51. {
  52. m_app.Stop();
  53. }
  54. AzToolsFramework::ToolsApplication m_app;
  55. AZ::ComponentApplication::Descriptor m_descriptor;
  56. };
  57. TEST_F(LuaBuilderTests, ParseLuaScript_DependencyRegexStress_Success)
  58. {
  59. LuaBuilderWorker worker;
  60. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  61. AZ::IO::FixedMaxPath resolvedPath;
  62. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/dependencyRegexStress.lua");
  63. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  64. ASSERT_THAT(pathDependencies,
  65. testing::UnorderedElementsAre(
  66. ProductPathDependency("test0.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  67. ProductPathDependency("folder/test1.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  68. ProductPathDependency("folder/separated/test2.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  69. ProductPathDependency("folder/separated/test3.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  70. ProductPathDependency("folder/test4.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  71. ProductPathDependency("test5.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  72. ProductPathDependency("folder/test6.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  73. ProductPathDependency("test7.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  74. ProductPathDependency("folder/test8.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  75. ProductPathDependency("folder/test9.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  76. ProductPathDependency("folder/test10.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  77. ProductPathDependency("folder/test11.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  78. ProductPathDependency("folder/test12.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  79. ProductPathDependency("folder/test13.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  80. ProductPathDependency("folder/test14.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  81. ProductPathDependency("folder/test15.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  82. ProductPathDependency("folder/test16.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  83. ProductPathDependency("test17.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  84. ProductPathDependency("local_test0.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  85. ProductPathDependency("folder/local_test1.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  86. ProductPathDependency("folder/separated/local_test2.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  87. ProductPathDependency("folder/separated/local_test3.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  88. ProductPathDependency("folder/local_test4.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  89. ProductPathDependency("local_test5.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  90. ProductPathDependency("folder/local_test6.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  91. ProductPathDependency("local_test7.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  92. ProductPathDependency("folder/local_test8.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  93. ProductPathDependency("folder/local_test9.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  94. ProductPathDependency("folder/local_test10.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  95. ProductPathDependency("folder/local_test11.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  96. ProductPathDependency("folder/local_test12.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  97. ProductPathDependency("folder/local_test13.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  98. ProductPathDependency("folder/local_test14.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  99. ProductPathDependency("folder/local_test15.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  100. ProductPathDependency("folder/local_test16.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  101. ProductPathDependency("local_test17.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile)
  102. )
  103. );
  104. }
  105. TEST_F(LuaBuilderTests, ParseLuaScript_UsingRequire_ShouldFindDependencies)
  106. {
  107. LuaBuilderWorker worker;
  108. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  109. AZ::IO::FixedMaxPath resolvedPath;
  110. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/test1.lua");
  111. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  112. ASSERT_THAT(pathDependencies,
  113. testing::UnorderedElementsAre(
  114. ProductPathDependency("scripts/test2.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  115. ProductPathDependency("scripts/test3.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  116. ProductPathDependency("scripts/test4.luac", AssetBuilderSDK::ProductPathDependencyType::ProductFile))
  117. );
  118. }
  119. TEST_F(LuaBuilderTests, ParseLuaScript_UsingReloadScript_ShouldFindDependencies)
  120. {
  121. LuaBuilderWorker worker;
  122. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  123. AZ::IO::FixedMaxPath resolvedPath;
  124. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/test2.lua");
  125. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  126. ASSERT_THAT(pathDependencies,
  127. testing::UnorderedElementsAre(
  128. ProductPathDependency("some/path/test3.lua", AssetBuilderSDK::ProductPathDependencyType::ProductFile))
  129. );
  130. }
  131. TEST_F(LuaBuilderTests, ParseLuaScript_WithPathInAString_ShouldFindDependencies)
  132. {
  133. LuaBuilderWorker worker;
  134. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  135. AZ::IO::FixedMaxPath resolvedPath;
  136. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/test3_general_dependencies.lua");
  137. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  138. ASSERT_THAT(pathDependencies,
  139. testing::UnorderedElementsAre(
  140. ProductPathDependency("some/file/in/some/folder.cfg", AssetBuilderSDK::ProductPathDependencyType::ProductFile))
  141. );
  142. }
  143. TEST_F(LuaBuilderTests, ParseLuaScript_WithConsoleCommand_ShouldFindDependencies)
  144. {
  145. LuaBuilderWorker worker;
  146. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  147. AZ::IO::FixedMaxPath resolvedPath;
  148. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/test4_console_command.lua");
  149. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  150. ASSERT_THAT(pathDependencies,
  151. testing::UnorderedElementsAre(
  152. ProductPathDependency("somefile.cfg", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  153. ProductPathDependency("somefile/in/a/folder.cfg", AssetBuilderSDK::ProductPathDependencyType::ProductFile))
  154. );
  155. }
  156. void VerifyNoDependenciesGenerated(const AZStd::string& testFileUnresolvedPath)
  157. {
  158. LuaBuilderWorker worker;
  159. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  160. AZ::IO::FixedMaxPath resolvedPath;
  161. AZ::IO::FileIOBase::GetInstance()->ResolvePath(testFileUnresolvedPath.c_str());
  162. EXPECT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(resolvedPath.c_str()));
  163. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  164. EXPECT_TRUE(pathDependencies.empty());
  165. }
  166. TEST_F(LuaBuilderTests, ParseLuaScript_CommentedOutDependency_EntireLine_ShouldFindNoDependencies)
  167. {
  168. VerifyNoDependenciesGenerated("@gemroot:LmbrCentral@/Code/Tests/Lua/test5_whole_line_comment.lua");
  169. }
  170. TEST_F(LuaBuilderTests, ParseLuaScript_CommentedOutDependency_PartialLine_ShouldFindNoDependencies)
  171. {
  172. VerifyNoDependenciesGenerated("@gemroot:LmbrCentral@/Code/Tests/Lua/test6_partial_line_comment.lua");
  173. }
  174. TEST_F(LuaBuilderTests, ParseLuaScript_CommentedOutDependency_BlockComment_ShouldFindNoDependencies)
  175. {
  176. VerifyNoDependenciesGenerated("@gemroot:LmbrCentral@/Code/Tests/Lua/test7_block_comment.lua");
  177. }
  178. TEST_F(LuaBuilderTests, ParseLuaScript_CommentedOutDependency_NegatedBlockComment_ShouldFindDependencies)
  179. {
  180. LuaBuilderWorker worker;
  181. AssetBuilderSDK::ProductPathDependencySet pathDependencies;
  182. AZ::IO::FixedMaxPath resolvedPath;
  183. AZ::IO::FileIOBase::GetInstance()->ResolvePath(resolvedPath, "@gemroot:LmbrCentral@/Code/Tests/Lua/test8_negated_block_comment.lua");
  184. worker.ParseDependencies(resolvedPath.c_str(), pathDependencies);
  185. ASSERT_THAT(pathDependencies,
  186. testing::UnorderedElementsAre(
  187. ProductPathDependency("somefile.cfg", AssetBuilderSDK::ProductPathDependencyType::ProductFile),
  188. ProductPathDependency("somefile/in/a/folder.cfg", AssetBuilderSDK::ProductPathDependencyType::ProductFile))
  189. );
  190. }
  191. }