FileFunc.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <FrameworkApplicationFixture.h>
  9. #include <Utils/Utils.h>
  10. #include <AzCore/IO/Path/Path.h>
  11. #include <AzCore/Outcome/Outcome.h>
  12. #include <AzCore/Serialization/Json/JsonSerialization.h>
  13. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  14. #include <AzCore/Serialization/Json/RegistrationContext.h>
  15. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  16. #include <AzCore/UnitTest/TestTypes.h>
  17. #include <AzCore/Utils/Utils.h>
  18. #include <AzFramework/FileFunc/FileFunc.h>
  19. #include <AzFramework/StringFunc/StringFunc.h>
  20. #include <AzFramework/IO/LocalFileIO.h>
  21. #include <AzTest/AzTest.h>
  22. #include <QTemporaryDir>
  23. #include <QTextStream>
  24. #include <QDir>
  25. #include <QFileInfo>
  26. namespace UnitTest
  27. {
  28. class FileFuncTest : public LeakDetectionFixture
  29. {
  30. public:
  31. void SetUp() override
  32. {
  33. m_prevFileIO = AZ::IO::FileIOBase::GetInstance();
  34. AZ::IO::FileIOBase::SetInstance(nullptr);
  35. AZ::IO::FileIOBase::SetInstance(&m_fileIO);
  36. }
  37. void TearDown() override
  38. {
  39. AZ::IO::FileIOBase::SetInstance(nullptr);
  40. AZ::IO::FileIOBase::SetInstance(m_prevFileIO);
  41. }
  42. AZ::IO::LocalFileIO m_fileIO;
  43. AZ::IO::FileIOBase* m_prevFileIO;
  44. };
  45. static bool CreateDummyFile(const QString& fullPathToFile, const QString& tempStr = {})
  46. {
  47. QFileInfo fi(fullPathToFile);
  48. QDir fp(fi.path());
  49. fp.mkpath(".");
  50. QFile writer(fullPathToFile);
  51. if (!writer.open(QFile::ReadWrite))
  52. {
  53. return false;
  54. }
  55. {
  56. QTextStream stream(&writer);
  57. stream << tempStr << Qt::endl;
  58. }
  59. writer.close();
  60. return true;
  61. }
  62. TEST_F(FileFuncTest, FindFilesTest_EmptyFolder_Failure)
  63. {
  64. QTemporaryDir tempDir;
  65. QDir tempPath(tempDir.path());
  66. const char dependenciesPattern[] = "*_dependencies.xml";
  67. bool recurse = true;
  68. AZStd::string folderPath = tempPath.absolutePath().toStdString().c_str();
  69. AZ::Outcome<AZStd::list<AZStd::string>, AZStd::string> result = AzFramework::FileFunc::FindFileList(folderPath.c_str(),
  70. dependenciesPattern, recurse);
  71. ASSERT_TRUE(result.IsSuccess());
  72. ASSERT_EQ(result.GetValue().size(), 0);
  73. }
  74. TEST_F(FileFuncTest, FindFilesTest_DependenciesWildcards_Success)
  75. {
  76. QTemporaryDir tempDir;
  77. QDir tempPath(tempDir.path());
  78. const char* expectedFileNames[] = { "a_dependencies.xml","b_dependencies.xml" };
  79. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath(expectedFileNames[0]), QString("tempdata\n")));
  80. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath(expectedFileNames[1]), QString("tempdata\n")));
  81. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("dependencies.xml"), QString("tempdata\n")));
  82. const char dependenciesPattern[] = "*_dependencies.xml";
  83. bool recurse = true;
  84. AZStd::string folderPath = tempPath.absolutePath().toStdString().c_str();
  85. AZ::Outcome<AZStd::list<AZStd::string>, AZStd::string> result = AzFramework::FileFunc::FindFileList(folderPath.c_str(),
  86. dependenciesPattern, recurse);
  87. ASSERT_TRUE(result.IsSuccess());
  88. ASSERT_EQ(result.GetValue().size(), 2);
  89. for (size_t i = 0; i < AZ_ARRAY_SIZE(expectedFileNames); ++i)
  90. {
  91. auto findElement = AZStd::find_if(result.GetValue().begin(), result.GetValue().end(), [&expectedFileNames, i](const AZStd::string& thisString)
  92. {
  93. AZStd::string thisFileName;
  94. AzFramework::StringFunc::Path::GetFullFileName(thisString.c_str(), thisFileName);
  95. return thisFileName == expectedFileNames[i];
  96. });
  97. ASSERT_NE(findElement, result.GetValue().end());
  98. }
  99. }
  100. TEST_F(FileFuncTest, FindFilesTest_DependenciesWildcardsSubfolders_Success)
  101. {
  102. QTemporaryDir tempDir;
  103. QDir tempPath(tempDir.path());
  104. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("a_dependencies.xml"), QString("tempdata\n")));
  105. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("b_dependencies.xml"), QString("tempdata\n")));
  106. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("dependencies.xml"), QString("tempdata\n")));
  107. const char dependenciesPattern[] = "*_dependencies.xml";
  108. bool recurse = true;
  109. AZStd::string folderPath = tempPath.absolutePath().toStdString().c_str();
  110. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("subfolder1/c_dependencies.xml"), QString("tempdata\n")));
  111. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("subfolder1/d_dependencies.xml"), QString("tempdata\n")));
  112. ASSERT_TRUE(CreateDummyFile(tempPath.absoluteFilePath("subfolder1/dependencies.xml"), QString("tempdata\n")));
  113. AZ::Outcome<AZStd::list<AZStd::string>, AZStd::string> result = AzFramework::FileFunc::FindFileList(folderPath.c_str(),
  114. dependenciesPattern, recurse);
  115. ASSERT_TRUE(result.IsSuccess());
  116. ASSERT_EQ(result.GetValue().size(), 4);
  117. const char* expectedFileNames[] = { "a_dependencies.xml","b_dependencies.xml", "c_dependencies.xml", "d_dependencies.xml" };
  118. for (size_t i = 0; i < AZ_ARRAY_SIZE(expectedFileNames); ++i)
  119. {
  120. auto findElement = AZStd::find_if(result.GetValue().begin(), result.GetValue().end(), [&expectedFileNames, i](const AZStd::string& thisString)
  121. {
  122. AZStd::string thisFileName;
  123. AzFramework::StringFunc::Path::GetFullFileName(thisString.c_str(), thisFileName);
  124. return thisFileName == expectedFileNames[i];
  125. });
  126. ASSERT_NE(findElement, result.GetValue().end());
  127. }
  128. }
  129. } // namespace UnitTest