PlatformConfigurationUnitTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <native/utilities/PlatformConfiguration.h>
  9. #include <native/unittests/UnitTestUtils.h>
  10. #include <native/unittests/AssetProcessorUnitTests.h>
  11. #include <native/tests/MockAssetDatabaseRequestsHandler.h>
  12. using namespace UnitTestUtils;
  13. using namespace AssetProcessor;
  14. class PlatformConfigurationTests
  15. : public UnitTest::AssetProcessorUnitTestBase
  16. {
  17. public:
  18. void SetUp() override
  19. {
  20. UnitTest::AssetProcessorUnitTestBase::SetUp();
  21. m_assetRootPath = QDir(m_assetDatabaseRequestsHandler->GetAssetRootDir().c_str());
  22. for (const AssetBuilderSDK::PlatformInfo& enabledPlatform : m_enabledPlatforms)
  23. {
  24. m_config.EnablePlatform(enabledPlatform, true);
  25. }
  26. m_config.EnablePlatform({ "fandago",{ "console" } }, false);
  27. AZStd::vector<AssetBuilderSDK::PlatformInfo> platforms;
  28. m_config.PopulatePlatformsForScanFolder(platforms);
  29. // PATH DisplayName PortKey root recurse platforms
  30. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder3"), "", "sf3", false, false, platforms)); // subfolder 3 is expected to override subfolder2
  31. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder2"), "", "sf4", false, true, platforms)); // subfolder 2 is expected to override subfolder1
  32. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder1"), "", "sf1", false, true, platforms)); // subfolder1 is expected to override root
  33. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder4"), "", "sf4", false, true, platforms)); // subfolder4 is expected to override subfolder5
  34. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder5"), "", "sf5", false, true, platforms)); // subfolder5 is expected to override subfolder6
  35. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder6"), "", "sf6", false, true, platforms)); // subfolder6 is expected to override subfolder7
  36. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder7"), "", "sf7", false, true, platforms)); // subfolder7 is expected to override subfolder8
  37. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("subfolder8/x"), "", "sf8", false, true, platforms)); // subfolder8 is expected to override root
  38. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.absolutePath(), "temp", "temp", true, false, platforms)); // add the root
  39. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("GameName"), "gn", "", false, true, platforms));
  40. m_scanFolders.emplace_back(ScanFolderInfo(m_assetRootPath.filePath("GameNameButWithExtra"), "gnbwe", "", false, true, platforms));
  41. for (const ScanFolderInfo& scanFolder : m_scanFolders)
  42. {
  43. m_config.AddScanFolder(scanFolder, true);
  44. }
  45. AssetRecognizer txtRecognizer;
  46. txtRecognizer.m_name = "txt files";
  47. txtRecognizer.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher("*.txt", AssetBuilderSDK::AssetBuilderPattern::Wildcard);
  48. txtRecognizer.m_platformSpecs.insert({"pc", AssetInternalSpec::Copy});
  49. txtRecognizer.m_platformSpecs.insert({"android", AssetInternalSpec::Copy});
  50. txtRecognizer.m_platformSpecs.insert({"fandago", AssetInternalSpec::Copy});
  51. m_txtRecognizerContainer[txtRecognizer.m_name] = txtRecognizer;
  52. // test dual-recognisers - two recognisers for the same pattern.
  53. txtRecognizer.m_name = "txt files 2";
  54. m_txtRecognizerContainer[txtRecognizer.m_name] = txtRecognizer;
  55. for (const auto& recognizerKeyPair : m_txtRecognizerContainer)
  56. {
  57. m_config.AddRecognizer(recognizerKeyPair.second);
  58. }
  59. m_formatRecognizer.m_patternMatcher = AssetBuilderSDK::FilePatternMatcher(".*\\/test\\/.*\\.format", AssetBuilderSDK::AssetBuilderPattern::Regex);
  60. m_formatRecognizer.m_name = "format files that live in a folder called test";
  61. m_config.AddRecognizer(m_formatRecognizer);
  62. }
  63. protected:
  64. void CreateTestFiles()
  65. {
  66. QSet<QString> expectedFiles;
  67. // set up some interesting files:
  68. expectedFiles << m_assetRootPath.absoluteFilePath("rootfile2.txt");
  69. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder1/rootfile1.txt"); // note: Must override the actual root file
  70. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder1/basefile.txt");
  71. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/basefile.txt");
  72. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/aaa/basefile.txt");
  73. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/aaa/bbb/basefile.txt");
  74. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/aaa/bbb/ccc/basefile.txt");
  75. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/aaa/bbb/ccc/ddd/basefile.txt");
  76. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder2/subfolder1/override.txt");
  77. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder3/basefile.txt");
  78. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder4/a/testfile.txt");
  79. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder5/a/testfile.txt");
  80. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder6/a/testfile.txt");
  81. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder7/a/testfile.txt");
  82. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder8/x/a/testfile.txt");
  83. // subfolder3 is not recursive so none of these should show up in any scan or override check
  84. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder3/aaa/basefile.txt");
  85. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder3/aaa/bbb/basefile.txt");
  86. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder3/aaa/bbb/ccc/basefile.txt");
  87. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder3/rootfile3.txt"); // must override rootfile3 in root
  88. expectedFiles << m_assetRootPath.absoluteFilePath("rootfile1.txt");
  89. expectedFiles << m_assetRootPath.absoluteFilePath("rootfile3.txt");
  90. expectedFiles << m_assetRootPath.absoluteFilePath("unrecognised.file"); // a file that should not be recognised
  91. expectedFiles << m_assetRootPath.absoluteFilePath("unrecognised2.file"); // a file that should not be recognised
  92. expectedFiles << m_assetRootPath.absoluteFilePath("subfolder1/test/test.format"); // a file that should be recognised
  93. expectedFiles << m_assetRootPath.absoluteFilePath("test.format"); // a file that should NOT be recognised
  94. expectedFiles << m_assetRootPath.absoluteFilePath("GameNameButWithExtra/somefile.meo"); // a file that lives in a folder that must not be mistaken for the wrong scan folder
  95. expectedFiles << m_assetRootPath.absoluteFilePath("GameName/otherfile.meo"); // a file that lives in a folder that must not be mistaken for the wrong scan folder
  96. for (const QString& expect : expectedFiles)
  97. {
  98. EXPECT_TRUE(CreateDummyFile(expect));
  99. }
  100. }
  101. FileStatePassthrough m_fileStateCache;
  102. QDir m_assetRootPath;
  103. PlatformConfiguration m_config;
  104. AZStd::vector<ScanFolderInfo> m_scanFolders;
  105. AZStd::vector<AssetBuilderSDK::PlatformInfo> m_enabledPlatforms = {
  106. { "pc",{ "desktop", "host" }},
  107. { "android",{ "mobile", "android" }}
  108. };
  109. RecognizerContainer m_txtRecognizerContainer;
  110. AssetRecognizer m_formatRecognizer;
  111. };
  112. TEST_F(PlatformConfigurationTests, TestPlatformsAndScanFolders_FeedPlatformConfiguration_Succeeds)
  113. {
  114. EXPECT_EQ(m_config.GetEnabledPlatforms().size(), m_enabledPlatforms.size());
  115. for (int index = 0; index < m_enabledPlatforms.size(); ++index)
  116. {
  117. EXPECT_EQ(m_config.GetEnabledPlatforms()[index].m_identifier, m_enabledPlatforms[index].m_identifier);
  118. }
  119. EXPECT_EQ(m_config.GetScanFolderCount(), m_scanFolders.size());
  120. for (int index = 0; index < m_scanFolders.size(); ++index)
  121. {
  122. EXPECT_EQ(m_config.GetScanFolderAt(index).IsRoot(), m_scanFolders[index].IsRoot());
  123. EXPECT_EQ(m_config.GetScanFolderAt(index).RecurseSubFolders(), m_scanFolders[index].RecurseSubFolders());
  124. }
  125. }
  126. TEST_F(PlatformConfigurationTests, TestRecogonizer_FeedPlatformConfiguration_Succeeds)
  127. {
  128. CreateTestFiles();
  129. RecognizerPointerContainer results;
  130. EXPECT_TRUE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("subfolder1/rootfile1.txt"), results));
  131. EXPECT_EQ(results.size(), m_txtRecognizerContainer.size());
  132. for (int index = 0; index < results.size(); ++index)
  133. {
  134. EXPECT_TRUE(results[index]);
  135. EXPECT_NE(m_txtRecognizerContainer.find(results[index]->m_name), m_txtRecognizerContainer.end());
  136. }
  137. results.clear();
  138. EXPECT_FALSE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("test.format"), results));
  139. EXPECT_TRUE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("subfolder1/test/test.format"), results));
  140. EXPECT_EQ(results.size(), 1);
  141. EXPECT_EQ(results[0]->m_name, m_formatRecognizer.m_name);
  142. // double call:
  143. EXPECT_FALSE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("unrecognised.file"), results));
  144. EXPECT_FALSE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("unrecognised.file"), results));
  145. // files which do and dont exist:
  146. EXPECT_FALSE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("unrecognised2.file"), results));
  147. EXPECT_FALSE(m_config.GetMatchingRecognizers(m_assetRootPath.absoluteFilePath("unrecognised3.file"), results));
  148. }
  149. TEST_F(PlatformConfigurationTests, GetOverridingFile_FeedPlatformConfiguration_Succeeds)
  150. {
  151. CreateTestFiles();
  152. EXPECT_EQ(m_config.GetOverridingFile("rootfile3.txt", m_assetRootPath.filePath("subfolder3")), QString());
  153. EXPECT_EQ(m_config.GetOverridingFile("rootfile3.txt", m_assetRootPath.absolutePath()), m_assetRootPath.absoluteFilePath("subfolder3/rootfile3.txt"));
  154. EXPECT_EQ(m_config.GetOverridingFile("subfolder1/whatever.txt", m_assetRootPath.filePath("subfolder1")), QString());
  155. EXPECT_EQ(AssetUtilities::NormalizeFilePath(m_config.GetOverridingFile("subfolder1/override.txt", m_assetRootPath.filePath("subfolder1"))), AssetUtilities::NormalizeFilePath(m_assetRootPath.absoluteFilePath("subfolder2/subfolder1/override.txt")));
  156. EXPECT_EQ(AssetUtilities::NormalizeFilePath(m_config.GetOverridingFile("a/testfile.txt", m_assetRootPath.filePath("subfolder6"))), AssetUtilities::NormalizeFilePath(m_assetRootPath.absoluteFilePath("subfolder4/a/testfile.txt")));
  157. EXPECT_EQ(AssetUtilities::NormalizeFilePath(m_config.GetOverridingFile("a/testfile.txt", m_assetRootPath.filePath("subfolder7"))), AssetUtilities::NormalizeFilePath(m_assetRootPath.absoluteFilePath("subfolder4/a/testfile.txt")));
  158. EXPECT_EQ(AssetUtilities::NormalizeFilePath(m_config.GetOverridingFile("a/testfile.txt", m_assetRootPath.filePath("subfolder8/x"))), AssetUtilities::NormalizeFilePath(m_assetRootPath.absoluteFilePath("subfolder4/a/testfile.txt")));
  159. // files which dont exist:
  160. EXPECT_EQ(m_config.GetOverridingFile("rootfile3", m_assetRootPath.filePath("subfolder3")), QString());
  161. // watch folders which dont exist should still return the best match:
  162. EXPECT_NE(m_config.GetOverridingFile("rootfile3.txt", m_assetRootPath.filePath("nonesuch")), QString());
  163. // subfolder 3 is first, but non-recursive, so it should NOT resolve this:
  164. EXPECT_EQ(m_config.GetOverridingFile("aaa/bbb/basefile.txt", m_assetRootPath.filePath("subfolder2")), QString());
  165. }
  166. TEST_F(PlatformConfigurationTests, FindFirstMatchingFile_FeedPlatformConfiguration_Succeeds)
  167. {
  168. CreateTestFiles();
  169. // sanity
  170. EXPECT_TRUE(m_config.FindFirstMatchingFile("").isEmpty()); // empty should return empty.
  171. // must not find the one in subfolder3 because its not a recursive watch:
  172. EXPECT_EQ(m_config.FindFirstMatchingFile("aaa/bbb/basefile.txt"), m_assetRootPath.filePath("subfolder2/aaa/bbb/basefile.txt"));
  173. // however, stuff at the root is overridden:
  174. EXPECT_EQ(m_config.FindFirstMatchingFile("rootfile3.txt"), m_assetRootPath.filePath("subfolder3/rootfile3.txt"));
  175. // not allowed to find files which do not exist:
  176. EXPECT_EQ(m_config.FindFirstMatchingFile("asdasdsa.txt"), QString());
  177. // find things in the root folder, too
  178. EXPECT_EQ(m_config.FindFirstMatchingFile("rootfile2.txt"), m_assetRootPath.filePath("rootfile2.txt"));
  179. // different regex rule should not interfere
  180. EXPECT_EQ(m_config.FindFirstMatchingFile("test/test.format"), m_assetRootPath.filePath("subfolder1/test/test.format"));
  181. EXPECT_EQ(m_config.FindFirstMatchingFile("a/testfile.txt"), m_assetRootPath.filePath("subfolder4/a/testfile.txt"));
  182. }
  183. TEST_F(PlatformConfigurationTests, GetScanFolderForFile_FeedPlatformConfiguration_Succeeds)
  184. {
  185. CreateTestFiles();
  186. // other functions depend on this one, test it first:
  187. EXPECT_TRUE(m_config.GetScanFolderForFile(m_assetRootPath.filePath("rootfile3.txt")));
  188. EXPECT_EQ(m_config.GetScanFolderForFile(m_assetRootPath.filePath("subfolder3/rootfile3.txt"))->ScanPath(), m_assetRootPath.filePath("subfolder3"));
  189. // this file exists and is in subfolder3, but subfolder3 is non-recursive, so it must not find it:
  190. EXPECT_FALSE(m_config.GetScanFolderForFile(m_assetRootPath.filePath("subfolder3/aaa/bbb/basefile.txt")));
  191. // test of root files in actual root folder:
  192. EXPECT_TRUE(m_config.GetScanFolderForFile(m_assetRootPath.filePath("rootfile2.txt")));
  193. EXPECT_EQ(m_config.GetScanFolderForFile(m_assetRootPath.filePath("rootfile2.txt"))->ScanPath(), m_assetRootPath.absolutePath());
  194. }
  195. TEST_F(PlatformConfigurationTests, ConvertToRelativePath_FeedPlatformConfiguration_Succeeds)
  196. {
  197. CreateTestFiles();
  198. QString fileName;
  199. QString scanFolderPath;
  200. // scan folders themselves should still convert to relative paths.
  201. EXPECT_TRUE(m_config.ConvertToRelativePath(m_assetRootPath.absolutePath(), fileName, scanFolderPath));
  202. EXPECT_EQ(fileName, "");
  203. EXPECT_EQ(scanFolderPath, m_assetRootPath.absolutePath());
  204. // a root file that actually exists in a root folder:
  205. EXPECT_TRUE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("rootfile2.txt"), fileName, scanFolderPath));
  206. EXPECT_EQ(fileName, "rootfile2.txt");
  207. EXPECT_EQ(scanFolderPath, m_assetRootPath.absolutePath());
  208. // find overridden file from root that is overridden in a higher priority folder:
  209. EXPECT_TRUE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("subfolder3/rootfile3.txt"), fileName, scanFolderPath));
  210. EXPECT_EQ(fileName, "rootfile3.txt");
  211. EXPECT_EQ(scanFolderPath, m_assetRootPath.filePath("subfolder3"));
  212. // must not find this, since its in a non-recursive folder:
  213. EXPECT_FALSE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("subfolder3/aaa/basefile.txt"), fileName, scanFolderPath));
  214. // must not find this since its not even in any folder we care about:
  215. EXPECT_FALSE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("subfolder8/aaa/basefile.txt"), fileName, scanFolderPath));
  216. // deep folder:
  217. EXPECT_TRUE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("subfolder2/aaa/bbb/ccc/ddd/basefile.txt"), fileName, scanFolderPath));
  218. EXPECT_EQ(fileName, "aaa/bbb/ccc/ddd/basefile.txt");
  219. EXPECT_EQ(scanFolderPath, m_assetRootPath.filePath("subfolder2"));
  220. // verify that output relative paths
  221. EXPECT_TRUE(m_config.ConvertToRelativePath(m_assetRootPath.absoluteFilePath("subfolder1/whatever.txt"), fileName, scanFolderPath));
  222. EXPECT_EQ(fileName, "whatever.txt");
  223. EXPECT_EQ(scanFolderPath, m_assetRootPath.filePath("subfolder1"));
  224. }