SourceFileRelocatorTests.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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 <AssetManager/SourceFileRelocator.h>
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/Jobs/JobContext.h>
  11. #include <AzCore/Jobs/JobManager.h>
  12. #include <AzCore/Jobs/JobManagerDesc.h>
  13. #include <AzCore/Memory/PoolAllocator.h>
  14. #include <AzCore/Utils/Utils.h>
  15. #include "AssetProcessorTest.h"
  16. #include "AzToolsFramework/API/AssetDatabaseBus.h"
  17. #if !defined(Q_MOC_RUN)
  18. #include <AzCore/UnitTest/TestTypes.h>
  19. #endif
  20. #include <AzToolsFramework/SourceControl/PerforceComponent.h>
  21. #include <AzToolsFramework/SourceControl/PerforceConnection.h>
  22. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  23. #include <utility>
  24. #include <AzTest/AzTest.h>
  25. namespace UnitTests
  26. {
  27. //using namespace testing;
  28. using testing::NiceMock;
  29. using namespace AssetProcessor;
  30. using namespace AzToolsFramework::AssetDatabase;
  31. struct MockPerforceComponent
  32. : AzToolsFramework::PerforceComponent
  33. {
  34. friend class SourceFileRelocatorTest;
  35. };
  36. class SourceFileRelocatorTestsMockDatabaseLocationListener : public AssetDatabaseRequests::Bus::Handler
  37. {
  38. public:
  39. MOCK_METHOD1(GetAssetDatabaseLocation, bool(AZStd::string&));
  40. };
  41. class SourceFileRelocatorTest
  42. : public ::UnitTest::LeakDetectionFixture
  43. , public ::UnitTest::SourceControlTest
  44. {
  45. public:
  46. void SetUp() override
  47. {
  48. AZ::TickBus::AllowFunctionQueuing(true);
  49. m_data.reset(new StaticData());
  50. QDir tempPath(m_tempDir.path());
  51. m_data->m_connection.reset(new AssetProcessor::AssetDatabaseConnection());
  52. m_data->m_databaseLocation = tempPath.absoluteFilePath("test_database.sqlite").toUtf8().constData();
  53. m_data->m_databaseLocationListener.BusConnect();
  54. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  55. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  56. {
  57. using namespace testing;
  58. ON_CALL(m_data->m_databaseLocationListener, GetAssetDatabaseLocation(_))
  59. .WillByDefault(
  60. DoAll( // set the 0th argument ref (string) to the database location and return true.
  61. SetArgReferee<0>(m_data->m_databaseLocation),
  62. Return(true)));
  63. }
  64. // Initialize the database:
  65. m_data->m_connection->ClearData(); // this is expected to reset/clear/reopen
  66. m_data->m_platformConfig.EnablePlatform(AssetBuilderSDK::PlatformInfo("pc", { "desktop" }));
  67. m_data->m_platformConfig.AddMetaDataType("metadataextension", "metadatatype");
  68. m_data->m_platformConfig.AddMetaDataType("bar", "foo");
  69. m_data->m_platformConfig.ReadMetaDataFromSettingsRegistry();
  70. AZStd::vector<AssetBuilderSDK::PlatformInfo> platforms;
  71. m_data->m_platformConfig.PopulatePlatformsForScanFolder(platforms);
  72. m_data->m_scanFolder1 = { tempPath.absoluteFilePath("dev").toUtf8().constData(), "dev", "devKey"};
  73. m_data->m_scanFolder2 = { tempPath.absoluteFilePath("folder").toUtf8().constData(), "folder", "folderKey"};
  74. ASSERT_TRUE(m_data->m_connection->SetScanFolder(m_data->m_scanFolder1));
  75. ASSERT_TRUE(m_data->m_connection->SetScanFolder(m_data->m_scanFolder2));
  76. ScanFolderInfo scanFolder1(m_data->m_scanFolder1.m_scanFolder.c_str(),
  77. m_data->m_scanFolder1.m_displayName.c_str(),
  78. m_data->m_scanFolder1.m_portableKey.c_str(),
  79. false, true, platforms, 0, m_data->m_scanFolder1.m_scanFolderID);
  80. m_data->m_platformConfig.AddScanFolder(scanFolder1);
  81. ScanFolderInfo scanFolder2(m_data->m_scanFolder2.m_scanFolder.c_str(),
  82. m_data->m_scanFolder2.m_displayName.c_str(),
  83. m_data->m_scanFolder2.m_portableKey.c_str(),
  84. false, true, platforms, 0, m_data->m_scanFolder2.m_scanFolderID);
  85. m_data->m_platformConfig.AddScanFolder(scanFolder2);
  86. SourceDatabaseEntry sourceFile1{ m_data->m_scanFolder1.m_scanFolderID, "subfolder1/somefile.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  87. SourceDatabaseEntry sourceFile2{ m_data->m_scanFolder1.m_scanFolderID, "subfolder1/otherfile.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  88. SourceDatabaseEntry sourceFile3{ m_data->m_scanFolder2.m_scanFolderID, "otherfile.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  89. SourceDatabaseEntry sourceFile4{ m_data->m_scanFolder2.m_scanFolderID, "a/b/c/d/otherfile.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  90. SourceDatabaseEntry sourceFile5{ m_data->m_scanFolder1.m_scanFolderID, "duplicate/file1.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  91. SourceDatabaseEntry sourceFile6{ m_data->m_scanFolder2.m_scanFolderID, "duplicate/file1.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  92. SourceDatabaseEntry sourceFile7{ m_data->m_scanFolder1.m_scanFolderID, "subfolder2/file.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  93. SourceDatabaseEntry sourceFile8{ m_data->m_scanFolder1.m_scanFolderID, "test.txt", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  94. SourceDatabaseEntry sourceFile9{ m_data->m_scanFolder1.m_scanFolderID, "duplicate/folder/file1.tif", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  95. SourceDatabaseEntry sourceFile10{ m_data->m_scanFolder1.m_scanFolderID, "folder/file.foo", AZ::Uuid::CreateRandom(), "AnalysisFingerprint" };
  96. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile1));
  97. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile2));
  98. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile3));
  99. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile4));
  100. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile5));
  101. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile6));
  102. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile7));
  103. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile8));
  104. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile9));
  105. ASSERT_TRUE(m_data->m_connection->SetSource(sourceFile10));
  106. SourceFileDependencyEntry dependency1{ AZ::Uuid::CreateRandom(), m_data->m_dependency1Uuid, PathOrUuid("subfolder1/otherfile.tif"), SourceFileDependencyEntry::TypeOfDependency::DEP_SourceToSource, false, "" };
  107. SourceFileDependencyEntry dependency2{ AZ::Uuid::CreateRandom(), m_data->m_dependency2Uuid, PathOrUuid("otherfile.tif"), SourceFileDependencyEntry::TypeOfDependency::DEP_JobToJob, false, "" };
  108. ASSERT_TRUE(m_data->m_connection->SetSourceFileDependency(dependency1));
  109. ASSERT_TRUE(m_data->m_connection->SetSourceFileDependency(dependency2));
  110. JobDatabaseEntry job1 = { sourceFile1.m_sourceID, "JobKey", 12345, "pc", AZ::Uuid::CreateRandom(), AzToolsFramework::AssetSystem::JobStatus::Completed,
  111. 1111 };
  112. JobDatabaseEntry job2 = { sourceFile2.m_sourceID, "JobKey", 2222, "pc", AZ::Uuid::CreateRandom(), AzToolsFramework::AssetSystem::JobStatus::Completed,
  113. 1111 };
  114. JobDatabaseEntry job3 = { sourceFile3.m_sourceID, "JobKey", 4444, "pc", AZ::Uuid::CreateRandom(), AzToolsFramework::AssetSystem::JobStatus::Completed,
  115. 1111 };
  116. ASSERT_TRUE(m_data->m_connection->SetJob(job1));
  117. ASSERT_TRUE(m_data->m_connection->SetJob(job2));
  118. ASSERT_TRUE(m_data->m_connection->SetJob(job3));
  119. const AZ::u32 productSubId = 1;
  120. ProductDatabaseEntry product1 = { job1.m_jobID, productSubId, "subfolder1/somefile.dds", AZ::Data::AssetType::CreateRandom() };
  121. ProductDatabaseEntry product2 = { job2.m_jobID, productSubId, "subfolder1/otherfile.dds", AZ::Data::AssetType::CreateRandom() };
  122. ProductDatabaseEntry product3 = { job3.m_jobID, productSubId, "blah.dds", AZ::Data::AssetType::CreateRandom() };
  123. ASSERT_TRUE(m_data->m_connection->SetProduct(product1));
  124. ASSERT_TRUE(m_data->m_connection->SetProduct(product2));
  125. ASSERT_TRUE(m_data->m_connection->SetProduct(product3));
  126. ProductDependencyDatabaseEntry productDependency1 = { product1.m_productID, sourceFile2.m_sourceGuid, productSubId, {}, "pc", true };
  127. ProductDependencyDatabaseEntry productDependency2 = { product2.m_productID, sourceFile3.m_sourceGuid, productSubId, {}, "pc", false };
  128. ProductDependencyDatabaseEntry productDependency3 = { product1.m_productID, sourceFile4.m_sourceGuid, productSubId, {}, "pc", false };
  129. ProductDependencyDatabaseEntry productDependency4 = { product2.m_productID, sourceFile4.m_sourceGuid, productSubId, {}, "pc", false };
  130. ProductDependencyDatabaseEntry productDependency5 = { product3.m_productID, sourceFile4.m_sourceGuid, productSubId, {}, "pc", true };
  131. ASSERT_TRUE(m_data->m_connection->SetProductDependency(productDependency1));
  132. ASSERT_TRUE(m_data->m_connection->SetProductDependency(productDependency2));
  133. ASSERT_TRUE(m_data->m_connection->SetProductDependency(productDependency3));
  134. ASSERT_TRUE(m_data->m_connection->SetProductDependency(productDependency4));
  135. ASSERT_TRUE(m_data->m_connection->SetProductDependency(productDependency5));
  136. QString referenceString = QString(R"(<Class name="Asset" field="Asset" value="id=%1,type={C62C7A87-9C09-4148-A985-12F2C99C0A45},hint={%2}" version="1" type="{77A19D40-8731-4D3C-9041-1B43047366A4}"/>)")
  137. .arg(AZ::Data::AssetId(sourceFile2.m_sourceGuid, product2.m_subID).ToString<AZStd::string>(AZ::Data::AssetId::SubIdDisplayType::Hex).c_str())
  138. .arg(sourceFile2.m_sourceName.c_str());
  139. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/subfolder1/somefile.tif"), referenceString));
  140. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/subfolder1/otherfile.tif")));
  141. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("folder/otherfile.tif")));
  142. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("folder/a/b/c/d/otherfile.tif")));
  143. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/duplicate/file1.tif")));
  144. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("folder/duplicate/file1.tif")));
  145. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/subfolder2/file.tif")));
  146. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/duplicate/folder/file1.tif")));
  147. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/test.txt")));
  148. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/dummy/foo.metadataextension")));
  149. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/folder/file.foo")));
  150. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(tempPath.absoluteFilePath("dev/folder/file.bar")));
  151. if (AZ::IO::FileIOBase::GetInstance() == nullptr)
  152. {
  153. m_localFileIo = AZStd::make_unique<AZ::IO::LocalFileIO>();
  154. AZ::IO::FileIOBase::SetInstance(m_localFileIo.get());
  155. }
  156. m_data->m_reporter = AZStd::make_unique<SourceFileRelocator>(m_data->m_connection, &m_data->m_platformConfig);
  157. AZ::JobManagerDesc jobDesc;
  158. AZ::JobManagerThreadDesc threadDesc;
  159. jobDesc.m_workerThreads.push_back(threadDesc);
  160. jobDesc.m_workerThreads.push_back(threadDesc);
  161. jobDesc.m_workerThreads.push_back(threadDesc);
  162. m_data->m_jobManager = aznew AZ::JobManager(jobDesc);
  163. m_data->m_jobContext = aznew AZ::JobContext(*m_data->m_jobManager);
  164. AZ::JobContext::SetGlobalContext(m_data->m_jobContext);
  165. m_data->m_perforceComponent = AZStd::make_unique<MockPerforceComponent>();
  166. m_data->m_perforceComponent->Activate();
  167. m_data->m_perforceComponent->SetConnection(new ::UnitTest::MockPerforceConnection(m_command));
  168. }
  169. void TearDown() override
  170. {
  171. if (AZ::IO::FileIOBase::GetInstance() == m_localFileIo.get())
  172. {
  173. AZ::IO::FileIOBase::SetInstance(nullptr);
  174. }
  175. m_localFileIo.reset();
  176. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  177. AZ::JobContext::SetGlobalContext(nullptr);
  178. delete m_data->m_jobContext;
  179. delete m_data->m_jobManager;
  180. m_data->m_perforceComponent->Deactivate();
  181. m_data->m_databaseLocationListener.BusDisconnect();
  182. m_data.reset();
  183. AZ::TickBus::AllowFunctionQueuing(false);
  184. }
  185. void TestResultEntries(const SourceFileRelocationContainer& container, AZStd::initializer_list<AZStd::string> expectedEntryDatabaseNames) const
  186. {
  187. AZStd::vector<AZStd::string> actualEntryDatabaseNames;
  188. for(const auto& relocationInfo : container)
  189. {
  190. if (relocationInfo.m_sourceEntry.m_sourceID != -1)
  191. {
  192. actualEntryDatabaseNames.push_back(relocationInfo.m_sourceEntry.m_sourceName);
  193. }
  194. else
  195. {
  196. // its a meta data file
  197. actualEntryDatabaseNames.push_back(relocationInfo.m_oldRelativePath);
  198. }
  199. }
  200. ASSERT_THAT(actualEntryDatabaseNames, testing::UnorderedElementsAreArray(expectedEntryDatabaseNames));
  201. }
  202. void TestGetSourcesByPath(const AZStd::string& source, AZStd::initializer_list<AZStd::string> expectedEntryDatabaseNames, bool expectSuccess = true, bool excludeMetaDataFiles = true) const
  203. {
  204. SourceFileRelocationContainer relocationContainer;
  205. QDir tempPath(m_tempDir.path());
  206. const ScanFolderInfo* info;
  207. auto result = m_data->m_reporter->GetSourcesByPath(source, relocationContainer, info, excludeMetaDataFiles);
  208. ASSERT_EQ(result.IsSuccess(), expectSuccess) << (!result.IsSuccess() ? result.GetError().c_str() : "");
  209. if (expectSuccess)
  210. {
  211. TestResultEntries(relocationContainer, expectedEntryDatabaseNames);
  212. }
  213. }
  214. void TestComputeDestination(const ScanFolderDatabaseEntry& scanFolderEntry, AZStd::string_view sourceWithScanFolder, AZStd::string_view source, AZStd::string_view destination, const char* expectedPath, bool expectSuccess = true) const
  215. {
  216. SourceFileRelocationContainer entryContainer;
  217. QDir tempPath(m_tempDir.path());
  218. const ScanFolderInfo* info = nullptr;
  219. const ScanFolderInfo* destInfo = nullptr;
  220. ASSERT_TRUE(m_data->m_reporter->GetSourcesByPath(tempPath.absoluteFilePath(AZStd::string(sourceWithScanFolder).c_str()).toUtf8().constData(), entryContainer, info));
  221. auto result = m_data->m_reporter->ComputeDestination(entryContainer, m_data->m_platformConfig.GetScanFolderByPath(scanFolderEntry.m_scanFolder.c_str()), source, destination, destInfo);
  222. ASSERT_EQ(result.IsSuccess(), expectSuccess) << (!result.IsSuccess() ? result.GetError().c_str() : "");
  223. if (expectSuccess)
  224. {
  225. ASSERT_STREQ(entryContainer[0].m_newRelativePath.c_str(), expectedPath);
  226. ASSERT_TRUE(AZ::StringFunc::StartsWith(entryContainer[0].m_newAbsolutePath, scanFolderEntry.m_scanFolder));
  227. ASSERT_NE(destInfo, nullptr);
  228. }
  229. }
  230. QString ToAbsolutePath(QString path)
  231. {
  232. QDir tempPath(m_tempDir.path());
  233. return QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath(path);
  234. }
  235. void TestMove(QString fromPath, QString toPath, QString expectedSourcePath, QString expectedDestinationPath, QString expectedQueryPath, bool p4Enabled)
  236. {
  237. fromPath = ToAbsolutePath(fromPath);
  238. toPath = ToAbsolutePath(toPath);
  239. expectedDestinationPath = ToAbsolutePath(expectedDestinationPath);
  240. expectedQueryPath = ToAbsolutePath(expectedQueryPath);
  241. QDir tempPath(m_tempDir.path());
  242. QString absoluteDepotFilePath = tempPath.absoluteFilePath(expectedSourcePath);
  243. AZStd::string editParams, moveParams;
  244. m_command.m_fstatResponse =
  245. AZStd::string::format(R"(... depotFile //depot/%s)", expectedSourcePath.toUtf8().data()) + "\r\n"
  246. R"(... isMapped)" "\r\n"
  247. R"(... action edit)" "\r\n"
  248. R"(... headAction integrate)" "\r\n"
  249. R"(... headType text)" "\r\n"
  250. R"(... headTime 1454346715)" "\r\n"
  251. R"(... headRev 3)" "\r\n"
  252. R"(... headChange 147109)" "\r\n"
  253. R"(... headModTime 1452731919)" "\r\n"
  254. R"(... haveRev 3)" "\r\n";
  255. m_command.m_fstatResponse.append("... clientFile ")
  256. .append(absoluteDepotFilePath.toUtf8().constData())
  257. .append("\r\n\r\n");
  258. m_command.m_editCallback = [&editParams](AZStd::string params)
  259. {
  260. editParams = AZStd::move(params);
  261. };
  262. m_command.m_moveCallback = [this, &moveParams, expectedDestinationPath, expectedSourcePath](AZStd::string params)
  263. {
  264. moveParams = AZStd::move(params);
  265. m_command.m_fstatResponse.clear();
  266. m_command.m_fstatResponse =
  267. AZStd::string::format(R"(... depotFile //depot/%s)", expectedSourcePath.toUtf8().data()) + "\r\n"
  268. R"(... isMapped)" "\r\n"
  269. R"(... action edit)" "\r\n"
  270. R"(... headAction integrate)" "\r\n"
  271. R"(... headType text)" "\r\n"
  272. R"(... headTime 1454346715)" "\r\n"
  273. R"(... headRev 3)" "\r\n"
  274. R"(... headChange 147109)" "\r\n"
  275. R"(... headModTime 1452731919)" "\r\n"
  276. R"(... haveRev 3)" "\r\n";
  277. m_command.m_fstatResponse.append("... clientFile ")
  278. .append(expectedDestinationPath.toUtf8().constData())
  279. .append("\r\n\r\n");
  280. };
  281. auto result = m_data->m_reporter->Move(fromPath.toUtf8().constData(), toPath.toUtf8().constData(), false);
  282. if (p4Enabled)
  283. {
  284. ASSERT_FALSE(editParams.empty());
  285. ASSERT_FALSE(moveParams.empty());
  286. }
  287. ASSERT_TRUE(result.IsSuccess());
  288. // Check the result report
  289. RelocationSuccess report = result.TakeValue();
  290. ASSERT_EQ(report.m_moveFailureCount, 0); // 0 Failures
  291. ASSERT_EQ(report.m_moveSuccessCount, 1); // Exactly 1 file moved
  292. // Check the command parameters to make sure the paths are correct
  293. if (p4Enabled)
  294. {
  295. // edit -> we should see p4 edit <fromPath>
  296. AZStd::vector<AZStd::string> tokens;
  297. AZ::StringFunc::Tokenize(editParams, tokens, " ");
  298. ASSERT_EQ(tokens.size(), 4);
  299. AZ::StringFunc::Strip(tokens[3], "\\\"", true, true, true);
  300. AZStd::string pathToCheck(fromPath.toUtf8().constData());
  301. if (fromPath.endsWith("*") && toPath.endsWith("*"))
  302. {
  303. AZ::StringFunc::Replace(pathToCheck, "*", "...");
  304. }
  305. ASSERT_STREQ(tokens[3].c_str(), pathToCheck.c_str());
  306. // move -> we should see p4 move <fromPath> <expectedQueryPath>
  307. tokens = {};
  308. AZ::StringFunc::Tokenize(moveParams, tokens, " ");
  309. ASSERT_EQ(tokens.size(), 5);
  310. // Remove quotes around the path and any slashes at the end
  311. AZ::StringFunc::Strip(tokens[3], "\\\"", true, true, true);
  312. AZ::StringFunc::Strip(tokens[4], "\\\"", true, true, true);
  313. ASSERT_STREQ(tokens[3].c_str(), pathToCheck.c_str());
  314. ASSERT_STREQ(tokens[4].c_str(), expectedQueryPath.toUtf8().constData());
  315. }
  316. }
  317. struct StaticData
  318. {
  319. // these variables are created during SetUp() and destroyed during TearDown() and thus are always available during tests using this fixture:
  320. AZStd::string m_databaseLocation;
  321. NiceMock<SourceFileRelocatorTestsMockDatabaseLocationListener> m_databaseLocationListener;
  322. AZStd::shared_ptr<AssetProcessor::AssetDatabaseConnection> m_connection;
  323. PlatformConfiguration m_platformConfig;
  324. ScanFolderDatabaseEntry m_scanFolder1;
  325. ScanFolderDatabaseEntry m_scanFolder2;
  326. FileStatePassthrough m_fileStateCache;
  327. AZStd::unique_ptr<SourceFileRelocator> m_reporter;
  328. AZStd::unique_ptr<MockPerforceComponent> m_perforceComponent;
  329. AZ::Uuid m_dependency1Uuid{ "{2C083160-DD50-459A-9482-CE663F4B558B}" };
  330. AZ::Uuid m_dependency2Uuid{ "{013BF607-A52A-4D1A-B2F4-AA8222C1BD68}" };
  331. AZ::JobManager* m_jobManager = nullptr;
  332. AZ::JobContext* m_jobContext = nullptr;
  333. };
  334. QTemporaryDir m_tempDir{ QDir::tempPath() + QLatin1String("/AssetProcessorUnitTest-XXXXXX") };
  335. AzToolsFramework::UuidUtilComponent m_uuidUtil;
  336. AzToolsFramework::MetadataManager m_metadataManager;
  337. AssetProcessor::UuidManager m_uuidManager;
  338. AZStd::unique_ptr<AZ::SettingsRegistryImpl> m_settingsRegistry;
  339. // we store the above data in a unique_ptr so that its memory can be cleared during TearDown() in one call, before we destroy the memory
  340. // allocator, reducing the chance of missing or forgetting to destroy one in the future.
  341. AZStd::unique_ptr<StaticData> m_data;
  342. AZStd::unique_ptr<AZ::IO::LocalFileIO> m_localFileIo;
  343. };
  344. TEST_F(SourceFileRelocatorTest, GetSources_SingleFile_Succeeds)
  345. {
  346. TestGetSourcesByPath("subfolder1/somefile.tif", { "subfolder1/somefile.tif" });
  347. }
  348. TEST_F(SourceFileRelocatorTest, GetSources_PrefixedFile_Succeeds)
  349. {
  350. TestGetSourcesByPath("otherfile.tif", { "otherfile.tif" });
  351. }
  352. TEST_F(SourceFileRelocatorTest, GetSources_PrefixedAbsFile_Succeeds)
  353. {
  354. QDir tempDir(m_tempDir.path());
  355. TestGetSourcesByPath(tempDir.absoluteFilePath("folder/otherfile.tif").toUtf8().constData(), { "otherfile.tif" });
  356. }
  357. TEST_F(SourceFileRelocatorTest, GetSources_Folder_Fails)
  358. {
  359. TestGetSourcesByPath("subfolder1", { }, false);
  360. }
  361. TEST_F(SourceFileRelocatorTest, GetSources_SingleFileWildcard1_Succeeds)
  362. {
  363. TestGetSourcesByPath("subfolder1/some*", { "subfolder1/somefile.tif" });
  364. }
  365. TEST_F(SourceFileRelocatorTest, GetSources_NonExistentFile_Fails)
  366. {
  367. TestGetSourcesByPath("subfolder1/doesNotExist*.txt", { }, false);
  368. }
  369. TEST_F(SourceFileRelocatorTest, GetSources_ConsecutiveWildcard_Fails)
  370. {
  371. TestGetSourcesByPath("subfolder1/**.txt", { }, false);
  372. }
  373. TEST_F(SourceFileRelocatorTest, GetSources_SingleFileWildcard2_Succeeds)
  374. {
  375. TestGetSourcesByPath("subfolder1/some*.tif", { "subfolder1/somefile.tif" });
  376. }
  377. TEST_F(SourceFileRelocatorTest, GetSources_MultipleFilesWildcard1_Succeeds)
  378. {
  379. TestGetSourcesByPath("subfolder1/*file*", { "subfolder1/somefile.tif", "subfolder1/otherfile.tif" });
  380. }
  381. TEST_F(SourceFileRelocatorTest, GetSources_MultipleFilesWildcard2_Succeeds)
  382. {
  383. TestGetSourcesByPath("subfolder1/*", { "subfolder1/somefile.tif", "subfolder1/otherfile.tif" });
  384. }
  385. TEST_F(SourceFileRelocatorTest, GetSources_MultipleFilesWildcard_AbsolutePath_Succeeds)
  386. {
  387. QDir tempDir(m_tempDir.path());
  388. TestGetSourcesByPath(tempDir.absoluteFilePath("dev/subfolder1*").toUtf8().constData(), { "subfolder1/somefile.tif", "subfolder1/otherfile.tif" });
  389. }
  390. TEST_F(SourceFileRelocatorTest, GetSources_MultipleFoldersWildcard_Succeeds)
  391. {
  392. QDir tempDir(m_tempDir.path());
  393. TestGetSourcesByPath("subfolder*/*", { "subfolder1/somefile.tif", "subfolder1/otherfile.tif", "subfolder2/file.tif" });
  394. }
  395. TEST_F(SourceFileRelocatorTest, GetSources_ScanFolder1_Fails)
  396. {
  397. TestGetSourcesByPath("dev", { }, false);
  398. }
  399. TEST_F(SourceFileRelocatorTest, GetSources_ScanFolder2_Fails)
  400. {
  401. TestGetSourcesByPath("dev/", { }, false);
  402. }
  403. TEST_F(SourceFileRelocatorTest, GetSources_MultipleScanFolders_Fails)
  404. {
  405. TestGetSourcesByPath("*", { }, false);
  406. }
  407. TEST_F(SourceFileRelocatorTest, GetSources_PartialPath_FailsWithNoResults)
  408. {
  409. TestGetSourcesByPath("older/*", { }, false);
  410. }
  411. TEST_F(SourceFileRelocatorTest, GetSources_AmbiguousPath1_Fails)
  412. {
  413. TestGetSourcesByPath("duplicate/file1.tif", { }, false);
  414. }
  415. TEST_F(SourceFileRelocatorTest, GetSources_AmbiguousPathWildcard_Fails)
  416. {
  417. TestGetSourcesByPath("duplicate/*.tif", { }, false);
  418. }
  419. TEST_F(SourceFileRelocatorTest, GetSources_DuplicateFile_AbsolutePath_Succeeds)
  420. {
  421. QDir tempPath(m_tempDir.path());
  422. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  423. TestGetSourcesByPath(filePath.toUtf8().constData(), { "duplicate/file1.tif" }, true);
  424. }
  425. TEST_F(SourceFileRelocatorTest, GetMetaDataFile_AbsolutePath_Succeeds)
  426. {
  427. QDir tempPath(m_tempDir.path());
  428. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("dummy/foo.metadataextension");
  429. TestGetSourcesByPath(filePath.toUtf8().constData(), { "dummy/foo.metadataextension" }, true, false);
  430. }
  431. TEST_F(SourceFileRelocatorTest, GetSources_HaveMetadata_AbsolutePath_Succeeds)
  432. {
  433. QDir tempPath(m_tempDir.path());
  434. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("folder/file.foo");
  435. TestGetSourcesByPath(filePath.toUtf8().constData(), { "folder/file.foo" , "folder/file.bar" }, true, false);
  436. }
  437. TEST_F(SourceFileRelocatorTest, GetSources_HaveMetadata_Exclude_AbsolutePath_Succeeds)
  438. {
  439. QDir tempPath(m_tempDir.path());
  440. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("folder/file.foo");
  441. TestGetSourcesByPath(filePath.toUtf8().constData(), { "folder/file.foo" }, true, true);
  442. }
  443. TEST_F(SourceFileRelocatorTest, GetMetaDataFile_SingleFileWildcard_Succeeds)
  444. {
  445. QDir tempPath(m_tempDir.path());
  446. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("dummy/foo.metadataextension");
  447. TestGetSourcesByPath("dummy/*", { "dummy/foo.metadataextension" }, true, false);
  448. }
  449. TEST_F(SourceFileRelocatorTest, GetSources_HaveMetadata_SingleFileWildcard_Succeeds)
  450. {
  451. TestGetSourcesByPath("folder/*", { "folder/file.foo" , "folder/file.bar" }, true, false);
  452. }
  453. TEST_F(SourceFileRelocatorTest, GetSources_HaveMetadata_Exclude_SingleFileWildcard_Succeeds)
  454. {
  455. TestGetSourcesByPath("folder/*", { "folder/file.foo" }, true, true);
  456. }
  457. TEST_F(SourceFileRelocatorTest, Move_Real_SourceEndsWithWildcard_DestinationEndsWithWildcard_Succeeds)
  458. {
  459. const QString destinationPath = "someOtherPlace/";
  460. TestMove("duplicate/fi*", destinationPath + "rename*", "dev/duplicate/file1.tif", destinationPath + "renameile1.tif", destinationPath + "rename*", false);
  461. }
  462. TEST_F(SourceFileRelocatorTest, Move_Real_SourceEndsWithWildcardFolder_DestinationEndsWithWildcard_Succeeds)
  463. {
  464. const QString destinationPath = "someOtherPlace/";
  465. TestMove("duplicate/folder*", destinationPath + "rename*", "dev/duplicate/folder/file1.tif", destinationPath + "rename/file1.tif", destinationPath + "rename*", false);
  466. }
  467. TEST_F(SourceFileRelocatorTest, HandleWildcard_RepeatCharacters1_Succeeds)
  468. {
  469. auto result = m_data->m_reporter->HandleWildcard("aaaaaaab", "a*b*", "a*bb*");
  470. ASSERT_TRUE(result.IsSuccess());
  471. ASSERT_STREQ(result.GetValue().c_str(), "aaaaaaabb");
  472. }
  473. TEST_F(SourceFileRelocatorTest, HandleWildcard_RepeatCharacters2_Succeeds)
  474. {
  475. auto result = m_data->m_reporter->HandleWildcard("aaaaaaaaaa", "a*a*", "a*b*");
  476. ASSERT_TRUE(result.IsSuccess());
  477. ASSERT_STREQ(result.GetValue().c_str(), "aaaaaaaaab");
  478. }
  479. TEST_F(SourceFileRelocatorTest, HandleWildcard_RepeatCharacters3_Succeeds)
  480. {
  481. auto result = m_data->m_reporter->HandleWildcard("aaabbbaaabbb", "a*a*", "a*c*");
  482. ASSERT_TRUE(result.IsSuccess());
  483. ASSERT_STREQ(result.GetValue().c_str(), "aaabbbaacbbb");
  484. }
  485. TEST_F(SourceFileRelocatorTest, HandleWildcard_RepeatCharacters4_Succeeds)
  486. {
  487. auto result = m_data->m_reporter->HandleWildcard("aabccbedd", "a*b*dd", "1*2*3");
  488. ASSERT_TRUE(result.IsSuccess());
  489. ASSERT_STREQ(result.GetValue().c_str(), "1abcc2e3");
  490. }
  491. TEST_F(SourceFileRelocatorTest, HandleWildcard_ZeroLengthMatch_Succeeds)
  492. {
  493. auto result = m_data->m_reporter->HandleWildcard("aabb", "aabb*", "1*");
  494. ASSERT_TRUE(result.IsSuccess());
  495. ASSERT_STREQ(result.GetValue().c_str(), "1");
  496. }
  497. TEST_F(SourceFileRelocatorTest, HandleWildcard_ZeroLengthMatch_MultipleWildcards_Succeeds)
  498. {
  499. auto result = m_data->m_reporter->HandleWildcard("abcdef", "a*b*e*", "1*2*3*");
  500. ASSERT_TRUE(result.IsSuccess());
  501. ASSERT_STREQ(result.GetValue().c_str(), "12cd3f");
  502. }
  503. TEST_F(SourceFileRelocatorTest, HandleWildcard_Complex_Succeeds)
  504. {
  505. auto result = m_data->m_reporter->HandleWildcard("subfolder1somefile.tif", "*o*some*.tif", "*1*2*3");
  506. ASSERT_TRUE(result.IsSuccess());
  507. }
  508. TEST_F(SourceFileRelocatorTest, HandleWildcard_TooComplex_Fails)
  509. {
  510. auto result = m_data->m_reporter->HandleWildcard("subfolder1/somefile.tif", "*o*some*.tif", "*1*2*3");
  511. ASSERT_FALSE(result.IsSuccess());
  512. }
  513. TEST_F(SourceFileRelocatorTest, GatherDependencies_Succeeds)
  514. {
  515. SourceFileRelocationContainer entryContainer;
  516. QDir tempPath(m_tempDir.path());
  517. const ScanFolderInfo* info;
  518. ASSERT_TRUE(m_data->m_reporter->GetSourcesByPath(tempPath.absoluteFilePath(AZStd::string("folder/o*").c_str()).toUtf8().constData(), entryContainer, info));
  519. ASSERT_EQ(entryContainer.size(), 1);
  520. ASSERT_STREQ(entryContainer[0].m_sourceEntry.m_sourceName.c_str(), "otherfile.tif");
  521. m_data->m_reporter->PopulateDependencies(entryContainer);
  522. AZStd::vector<AZ::Uuid> databaseSourceNames;
  523. AZStd::vector<AZ::s64> databaseProductDependencyNames;
  524. for(const auto& relocationInfo : entryContainer)
  525. {
  526. for (const auto& dependencyEntry : relocationInfo.m_sourceDependencyEntries)
  527. {
  528. databaseSourceNames.push_back(dependencyEntry.m_sourceGuid);
  529. }
  530. }
  531. for(const auto& relocationInfo : entryContainer)
  532. {
  533. for(const auto& productDependency : relocationInfo.m_productDependencyEntries)
  534. {
  535. databaseProductDependencyNames.push_back(productDependency.m_productPK);
  536. }
  537. }
  538. ASSERT_THAT(databaseSourceNames, testing::UnorderedElementsAreArray({ m_data->m_dependency2Uuid }));
  539. ASSERT_THAT(databaseProductDependencyNames, testing::UnorderedElementsAreArray({ 2 }));
  540. }
  541. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveFolder_Succeeds)
  542. {
  543. TestComputeDestination(m_data->m_scanFolder2, "folder/o*.tif", "o*.tif", "newfolder/makeafolder/o*.tif", "newfolder/makeafolder/otherfile.tif");
  544. }
  545. TEST_F(SourceFileRelocatorTest, ComputeDestination_RenameFile_Succeeds)
  546. {
  547. TestComputeDestination(m_data->m_scanFolder2, "folder/otherfile.tif", "otherfile.tif", "anewfile.png", "anewfile.png");
  548. }
  549. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveFolderDeeper_Succeeds)
  550. {
  551. TestComputeDestination(m_data->m_scanFolder1, "dev/*o*/some*.tif", "*o*/some*.tif", "subfolder2/subfolder3/*o*/some*.tif", "subfolder2/subfolder3/subfolder1/somefile.tif");
  552. }
  553. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveFileUpAFolder_Succeeds)
  554. {
  555. TestComputeDestination(m_data->m_scanFolder1, "dev/subfolder1/somefile.tif", "subfolder1/somefile.tif", "somefile.tif", "somefile.tif");
  556. }
  557. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveFileUpAFolderAndRename_Succeeds)
  558. {
  559. TestComputeDestination(m_data->m_scanFolder1, "dev/subfolder1/somefile.tif", "subfolder1/somefile.tif", "somenewfile.tif", "somenewfile.tif");
  560. }
  561. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveFileUpPartial_Succeeds)
  562. {
  563. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*", "a/b/c/*", "a/*", "a/d/otherfile.tif");
  564. }
  565. TEST_F(SourceFileRelocatorTest, ComputeDestination_AbsolutePathBoth_Succeeds)
  566. {
  567. QDir tempPath(m_tempDir.path());
  568. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*",
  569. tempPath.absoluteFilePath("folder/a/b/*").toUtf8().constData(),
  570. tempPath.absoluteFilePath("folder/a/*").toUtf8().constData(),
  571. "a/c/d/otherfile.tif");
  572. }
  573. TEST_F(SourceFileRelocatorTest, ComputeDestination_AbsolutePathSource_Succeeds)
  574. {
  575. QDir tempPath(m_tempDir.path());
  576. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*",
  577. tempPath.absoluteFilePath("folder/a/b/*").toUtf8().constData(),
  578. "a/*",
  579. "a/c/d/otherfile.tif");
  580. }
  581. TEST_F(SourceFileRelocatorTest, ComputeDestination_AbsolutePathDestination_Succeeds)
  582. {
  583. QDir tempPath(m_tempDir.path());
  584. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*",
  585. "a/b/*",
  586. tempPath.absoluteFilePath("folder/a/*").toUtf8().constData(),
  587. "a/c/d/otherfile.tif");
  588. }
  589. TEST_F(SourceFileRelocatorTest, ComputeDestination_AbsolutePathRename_Succeeds)
  590. {
  591. QDir tempPath(m_tempDir.path());
  592. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*",
  593. "a/b/c/d/otherfile.tif",
  594. tempPath.absoluteFilePath("folder/a/c/d/newlyNamed.png").toUtf8().constData(),
  595. "a/c/d/newlyNamed.png");
  596. }
  597. TEST_F(SourceFileRelocatorTest, ComputeDestination_MoveOutsideScanfolder_Fails)
  598. {
  599. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*", "a/b/c/*", m_tempDir.path().toUtf8().constData(), "", false);
  600. }
  601. TEST_F(SourceFileRelocatorTest, ComputeDestination_PathNavigation_Fails)
  602. {
  603. TestComputeDestination(m_data->m_scanFolder2, "folder/a/*", "a/b/c/*", "../a*", "", false);
  604. }
  605. TEST_F(SourceFileRelocatorTest, ComputeDestination_WildcardAcrossDirectories_Fails)
  606. {
  607. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "*/c/*", "*/d/*", "", false);
  608. }
  609. TEST_F(SourceFileRelocatorTest, ComputeDestination_MismatchedWildcardCount_Fails)
  610. {
  611. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/*/*", "*/d", "", false);
  612. }
  613. TEST_F(SourceFileRelocatorTest, ComputeDestination_InvalidCharacters_Fails)
  614. {
  615. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/c/*", "d/*?", "", false);
  616. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/c/*", "d/*<", "", false);
  617. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/c/*", "d/*>", "", false);
  618. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/c/*", "d/*\"", "", false);
  619. TestComputeDestination(m_data->m_scanFolder2, "folder/a/b/c/*", "a/b/c/*", "d/*|", "", false);
  620. }
  621. TEST_F(SourceFileRelocatorTest, ComputeDestination_Directory_Succeeds)
  622. {
  623. TestComputeDestination(m_data->m_scanFolder1, "dev/subfolder1/somefile.tif", "subfolder1/somefile.tif", "subfolder2/", "subfolder2/somefile.tif");
  624. TestComputeDestination(m_data->m_scanFolder1, "dev/subfolder1/s*", "subfolder1/s*", "subfolder2/", "subfolder2/somefile.tif");
  625. TestComputeDestination(m_data->m_scanFolder1, "dev/test.txt", "test.txt", "subfolder2/", "subfolder2/test.txt");
  626. }
  627. TEST_F(SourceFileRelocatorTest, BuildReport_Succeeds)
  628. {
  629. SourceFileRelocationContainer entryContainer;
  630. QDir tempPath(m_tempDir.path());
  631. const ScanFolderInfo* info = nullptr;
  632. const ScanFolderInfo* destInfo = nullptr;
  633. FileUpdateTasks updateTasks;
  634. ASSERT_TRUE(m_data->m_reporter->GetSourcesByPath(tempPath.absoluteFilePath("folder/*").toUtf8().constData(), entryContainer, info));
  635. ASSERT_EQ(entryContainer.size(), 3);
  636. m_data->m_reporter->ComputeDestination(entryContainer, info, "*", "someOtherPlace/*", destInfo);
  637. m_data->m_reporter->PopulateDependencies(entryContainer);
  638. AZStd::string report = m_data->m_reporter->BuildReport(entryContainer, updateTasks, true, false);
  639. ASSERT_FALSE(report.empty());
  640. }
  641. TEST_F(SourceFileRelocatorTest, Move_Preview_Succeeds)
  642. {
  643. QDir tempPath(m_tempDir.path());
  644. auto result = m_data->m_reporter->Move(tempPath.absoluteFilePath((m_data->m_scanFolder1.m_scanFolder + "/subfolder*").c_str()).toUtf8().constData(), "someOtherPlace/*", true);
  645. ASSERT_TRUE(result.IsSuccess());
  646. ASSERT_EQ(result.GetValue().m_relocationContainer.size(), 3);
  647. }
  648. TEST_F(SourceFileRelocatorTest, TestInterface)
  649. {
  650. auto* sourceFileRelocator = AZ::Interface<ISourceFileRelocation>::Get();
  651. ASSERT_NE(sourceFileRelocator, nullptr);
  652. }
  653. TEST_F(SourceFileRelocatorTest, Move_Real_Succeeds)
  654. {
  655. QDir tempPath(m_tempDir.path());
  656. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  657. auto newFilePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("someOtherPlace/file1.tif");
  658. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  659. auto result = m_data->m_reporter->Move(filePath.toUtf8().constData(), "someOtherPlace/file1.tif", false);
  660. ASSERT_TRUE(result.IsSuccess());
  661. ASSERT_FALSE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  662. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(newFilePath.toUtf8().constData()));
  663. RelocationSuccess successResult = result.TakeValue();
  664. ASSERT_EQ(successResult.m_moveSuccessCount, 1);
  665. ASSERT_EQ(successResult.m_moveFailureCount, 0);
  666. ASSERT_EQ(successResult.m_moveTotalCount, 1);
  667. ASSERT_EQ(successResult.m_updateTotalCount, 0);
  668. }
  669. TEST_F(SourceFileRelocatorTest, MoveMetadataEnabledType_Real_Succeeds)
  670. {
  671. QDir tempPath(m_tempDir.path());
  672. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  673. auto newFilePath =
  674. QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("someOtherPlace/renamed.tif");
  675. auto metadataPath = AzToolsFramework::MetadataManager::ToMetadataPath(filePath.toUtf8().constData());
  676. auto newMetadataPath = AzToolsFramework::MetadataManager::ToMetadataPath(newFilePath.toUtf8().constData());
  677. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  678. auto* uuidInterface = AZ::Interface<AssetProcessor::IUuidRequests>::Get();
  679. ASSERT_TRUE(uuidInterface);
  680. uuidInterface->EnableGenerationForTypes({ ".tif" });
  681. AZ::Utils::WriteFile("unit test file", metadataPath.c_str());
  682. auto result = m_data->m_reporter->Move(filePath.toUtf8().constData(), "someOtherPlace/renamed.tif", false);
  683. auto* io = AZ::IO::FileIOBase::GetInstance();
  684. ASSERT_TRUE(io);
  685. ASSERT_TRUE(result.IsSuccess());
  686. ASSERT_FALSE(io->Exists(filePath.toUtf8().constData()));
  687. ASSERT_TRUE(io->Exists(newFilePath.toUtf8().constData()));
  688. ASSERT_FALSE(io->Exists(metadataPath.c_str()));
  689. ASSERT_TRUE(io->Exists(newMetadataPath.c_str()));
  690. RelocationSuccess successResult = result.TakeValue();
  691. ASSERT_EQ(successResult.m_moveSuccessCount, 2);
  692. ASSERT_EQ(successResult.m_moveFailureCount, 0);
  693. ASSERT_EQ(successResult.m_moveTotalCount, 2);
  694. ASSERT_EQ(successResult.m_updateTotalCount, 0);
  695. }
  696. TEST_F(SourceFileRelocatorTest, Move_Real_ReadOnlyFile_Fails)
  697. {
  698. QDir tempPath(m_tempDir.path());
  699. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  700. auto newFilePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("someOtherPlace/file1.tif");
  701. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  702. ASSERT_TRUE(AZ::IO::SystemFile::SetWritable(filePath.toUtf8().constData(), false));
  703. auto result = m_data->m_reporter->Move(filePath.toUtf8().constData(), "someOtherPlace/file1.tif", false);
  704. ASSERT_TRUE(result.IsSuccess());
  705. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  706. ASSERT_FALSE(AZ::IO::FileIOBase::GetInstance()->Exists(newFilePath.toUtf8().constData()));
  707. RelocationSuccess successResult = result.TakeValue();
  708. ASSERT_EQ(successResult.m_moveSuccessCount, 0);
  709. ASSERT_EQ(successResult.m_moveFailureCount, 1);
  710. ASSERT_EQ(successResult.m_moveTotalCount, 1);
  711. ASSERT_EQ(successResult.m_updateTotalCount, 0);
  712. }
  713. TEST_F(SourceFileRelocatorTest, Move_Real_WithDependencies_Fails)
  714. {
  715. QDir tempPath(m_tempDir.path());
  716. auto result = m_data->m_reporter->Move("subfolder1/otherfile.tif", "someOtherPlace/otherfile.tif", false);
  717. ASSERT_FALSE(result.IsSuccess());
  718. }
  719. TEST_F(SourceFileRelocatorTest, Move_Real_WithDependenciesUpdateReferences_Succeeds)
  720. {
  721. QDir tempPath(m_tempDir.path());
  722. auto result = m_data->m_reporter->Move("subfolder1/otherfile.tif", "someOtherPlace/otherfile.tif", RelocationParameters_RemoveEmptyFoldersFlag | RelocationParameters_UpdateReferencesFlag);
  723. ASSERT_TRUE(result.IsSuccess());
  724. RelocationSuccess successResult = result.TakeValue();
  725. ASSERT_EQ(successResult.m_moveSuccessCount, 1);
  726. ASSERT_EQ(successResult.m_moveFailureCount, 0);
  727. ASSERT_EQ(successResult.m_moveTotalCount, 1);
  728. ASSERT_EQ(successResult.m_updateSuccessCount, 1);
  729. ASSERT_EQ(successResult.m_updateFailureCount, 1); // Since we have both product and source dependencies from the same file, the 2nd attempt to update fails
  730. ASSERT_EQ(successResult.m_updateTotalCount, 2);
  731. }
  732. TEST_F(SourceFileRelocatorTest, Delete_Real_Succeeds)
  733. {
  734. QDir tempPath(m_tempDir.path());
  735. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  736. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  737. auto result = m_data->m_reporter->Delete(filePath.toUtf8().constData(), false);
  738. ASSERT_TRUE(result.IsSuccess());
  739. RelocationSuccess successResult = result.TakeValue();
  740. ASSERT_EQ(successResult.m_moveSuccessCount, 1);
  741. ASSERT_EQ(successResult.m_moveFailureCount, 0);
  742. ASSERT_EQ(successResult.m_moveTotalCount, 1);
  743. ASSERT_EQ(successResult.m_updateTotalCount, 0);
  744. ASSERT_FALSE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  745. }
  746. TEST_F(SourceFileRelocatorTest, Delete_Real_Readonly_Fails)
  747. {
  748. struct AutoResetDirectoryReadOnlyState
  749. {
  750. AutoResetDirectoryReadOnlyState(QString dirName)
  751. : m_dirName(AZStd::move(dirName))
  752. {
  753. AZ::IO::SystemFile::SetWritable(m_dirName.toUtf8().constData(), false);
  754. }
  755. ~AutoResetDirectoryReadOnlyState()
  756. {
  757. AZ::IO::SystemFile::SetWritable(m_dirName.toUtf8().constData(), true);
  758. }
  759. AZ_DISABLE_COPY_MOVE(AutoResetDirectoryReadOnlyState)
  760. private:
  761. QString m_dirName;
  762. };
  763. QDir tempPath(m_tempDir.path());
  764. auto filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  765. ASSERT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  766. AutoResetDirectoryReadOnlyState readOnlyResetter(QFileInfo(filePath).absoluteDir().absolutePath());
  767. AZ::IO::SystemFile::SetWritable(filePath.toUtf8().constData(), false);
  768. auto result = m_data->m_reporter->Delete(filePath.toUtf8().constData(), false);
  769. EXPECT_TRUE(result.IsSuccess());
  770. RelocationSuccess successResult = result.TakeValue();
  771. EXPECT_EQ(successResult.m_moveSuccessCount, 0);
  772. EXPECT_EQ(successResult.m_moveFailureCount, 1);
  773. EXPECT_EQ(successResult.m_moveTotalCount, 1);
  774. EXPECT_EQ(successResult.m_updateTotalCount, 0);
  775. EXPECT_TRUE(AZ::IO::FileIOBase::GetInstance()->Exists(filePath.toUtf8().constData()));
  776. }
  777. TEST_F(SourceFileRelocatorTest, Delete_Real_WithDependencies_Fails)
  778. {
  779. QDir tempPath(m_tempDir.path());
  780. auto result = m_data->m_reporter->Delete("subfolder1/otherfile.tif", false);
  781. ASSERT_FALSE(result.IsSuccess());
  782. }
  783. TEST_F(SourceFileRelocatorTest, Move_Real_DestinationIsPathOnly_Succeeds)
  784. {
  785. const QString destinationPath = "someOtherPlace/";
  786. TestMove("duplicate/file1.tif", destinationPath, "dev/duplicate/file1.tif", destinationPath + "file1.tif", destinationPath + "file1.tif", false);
  787. }
  788. TEST_F(SourceFileRelocatorTest, Move_Real_DestinationIsPathOnly_SourceWithWildcard_Succeeds)
  789. {
  790. const QString destinationPath = "someOtherPlace/";
  791. TestMove("duplicate/fil*1.tif", destinationPath, "dev/duplicate/file1.tif", destinationPath + "file1.tif", destinationPath + "fil*1.tif", false);
  792. }
  793. TEST_F(SourceFileRelocatorTest, Move_Real_SourceContainsWildcard_DestinationEndsWithWildcard_Succeeds)
  794. {
  795. const QString destinationPath = "someOtherPlace/";
  796. TestMove("duplicate/fil*1.tif", destinationPath + "*", "dev/duplicate/file1.tif", destinationPath + "e", destinationPath + "*", false);
  797. }
  798. TEST_F(SourceFileRelocatorTest, Move_Real_SourceEndsWithWildcard_DestinationContainsWildcard_Succeeds)
  799. {
  800. const QString destinationPath = "someOtherPlace/";
  801. TestMove("duplicate/fi*", destinationPath + "*.rename", "dev/duplicate/file1.tif", destinationPath + "le1.tif.rename", destinationPath + "*.rename", false);
  802. }
  803. struct SourceFileRelocatorPerforceMockTest
  804. : SourceFileRelocatorTest
  805. {
  806. void SetUp() override
  807. {
  808. SourceFileRelocatorTest::SetUp();
  809. EnableSourceControl();
  810. }
  811. };
  812. TEST_F(SourceFileRelocatorPerforceMockTest, GetSources_NonExistentFile_Fails)
  813. {
  814. SourceFileRelocationContainer relocationContainer;
  815. QDir tempPath(m_tempDir.path());
  816. m_command.m_persistFstatResponse = true;
  817. m_command.m_fstatErrorResponse = (tempPath.absoluteFilePath("dev/subfolder1/doesNotExist*.txt") + " - no such file(s)\n"
  818. + tempPath.absoluteFilePath("folder/subfolder1/doesNotExist*.txt") + " - no such file(s)\n").toUtf8().constData();
  819. const ScanFolderInfo* info;
  820. auto result = m_data->m_reporter->GetSourcesByPath("subfolder1/doesNotExist*.txt", relocationContainer, info);
  821. ASSERT_EQ(result.IsSuccess(), false);
  822. auto&& error = result.TakeError();
  823. ASSERT_STREQ(error.c_str(), "Wildcard search did not match any files.\n");
  824. }
  825. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_DestinationIsPathOnly_Succeeds)
  826. {
  827. const QString destinationPath = "someOtherPlace/";
  828. TestMove("duplicate/file1.tif", destinationPath, "dev/duplicate/file1.tif", destinationPath + "file1.tif", destinationPath + "file1.tif", true);
  829. }
  830. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_DestinationIsPathOnly_SourceWithWildcard_Succeeds)
  831. {
  832. const QString destinationPath = "someOtherPlace/";
  833. TestMove("duplicate/fil*1.tif", destinationPath, "dev/duplicate/file1.tif", destinationPath + "file1.tif", destinationPath + "fil*1.tif", true);
  834. }
  835. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_SourceContainsWildcard_DestinationEndsWithWildcard_Succeeds)
  836. {
  837. const QString destinationPath = "someOtherPlace/";
  838. TestMove("duplicate/fil*1.tif", destinationPath + "*", "dev/duplicate/file1.tif", destinationPath + "e", destinationPath + "*", true);
  839. }
  840. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_SourceEndsWithWildcard_DestinationContainsWildcard_Succeeds)
  841. {
  842. const QString destinationPath = "someOtherPlace/";
  843. TestMove("duplicate/f*", destinationPath + "*.rename", "dev/duplicate/file1.tif", destinationPath + "ile1.tif.rename", destinationPath + "*.rename", true);
  844. }
  845. TEST_F(SourceFileRelocatorPerforceMockTest, Delete_Real_Succeeds)
  846. {
  847. QDir tempPath(m_tempDir.path());
  848. QString filePath = QDir(tempPath.absoluteFilePath(m_data->m_scanFolder1.m_scanFolder.c_str())).absoluteFilePath("duplicate/file1.tif");
  849. AZStd::string deleteParams;
  850. m_command.m_fstatResponse =
  851. R"(... depotFile //depot/dev/duplicate/file1.tif)" "\r\n"
  852. R"(... isMapped)" "\r\n"
  853. R"(... action edit)" "\r\n"
  854. R"(... headAction integrate)" "\r\n"
  855. R"(... headType text)" "\r\n"
  856. R"(... headTime 1454346715)" "\r\n"
  857. R"(... headRev 3)" "\r\n"
  858. R"(... headChange 147109)" "\r\n"
  859. R"(... headModTime 1452731919)" "\r\n"
  860. R"(... haveRev 3)" "\r\n";
  861. m_command.m_fstatResponse.append("... clientFile ")
  862. .append(filePath.toUtf8().constData())
  863. .append("\r\n\r\n");
  864. m_command.m_deleteCallback = [this, &deleteParams, filePath](AZStd::string params)
  865. {
  866. deleteParams = AZStd::move(params);
  867. m_command.m_rawOutput.outputResult = "delete called";
  868. m_command.m_fstatResponse =
  869. R"(... depotFile //depot/dev/duplicate/file1.tif)" "\r\n"
  870. R"(... isMapped)" "\r\n"
  871. R"(... action delete)" "\r\n"
  872. R"(... headAction integrate)" "\r\n"
  873. R"(... headType text)" "\r\n"
  874. R"(... headTime 1454346715)" "\r\n"
  875. R"(... headRev 3)" "\r\n"
  876. R"(... headChange 147109)" "\r\n"
  877. R"(... headModTime 1452731919)" "\r\n"
  878. R"(... haveRev 3)" "\r\n";
  879. m_command.m_fstatResponse.append("... clientFile ")
  880. .append(filePath.toUtf8().constData())
  881. .append("\r\n\r\n");
  882. };
  883. auto result = m_data->m_reporter->Delete(filePath.toUtf8().constData(), false);
  884. ASSERT_TRUE(result.IsSuccess());
  885. RelocationSuccess report = result.TakeValue();
  886. ASSERT_EQ(report.m_moveFailureCount, 0);
  887. ASSERT_GT(report.m_moveSuccessCount, 0);
  888. ASSERT_FALSE(deleteParams.empty());
  889. AZStd::vector<AZStd::string> tokens;
  890. AZ::StringFunc::Tokenize(deleteParams, tokens, " ");
  891. ASSERT_EQ(tokens.size(), 4);
  892. AZ::StringFunc::Strip(tokens[3], "\\\"", true, true, true);
  893. ASSERT_STREQ(tokens[3].c_str(), filePath.toUtf8().constData());
  894. }
  895. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_SourceEndsWithWildcard_DestinationEndsWithWildcard_Succeeds)
  896. {
  897. const QString destinationPath = "someOtherPlace/";
  898. TestMove("duplicate/fi*", destinationPath + "rename*", "dev/duplicate/file1.tif", destinationPath + "renamele1.tif", destinationPath + "rename...", true);
  899. }
  900. TEST_F(SourceFileRelocatorPerforceMockTest, Move_Real_SourceEndsWithWildcardFolder_DestinationEndsWithWildcard_Succeeds)
  901. {
  902. const QString destinationPath = "someOtherPlace/";
  903. TestMove("duplicate/folder*", destinationPath + "rename*", "dev/duplicate/folder/file1.tif", destinationPath + "rename/file1.tif", destinationPath + "rename...", true);
  904. }
  905. }