3
0

UtilsTests.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <AzFramework/API/ApplicationAPI.h>
  9. #include <source/utils/utils.h>
  10. #include <AzCore/Settings/SettingsRegistryImpl.h>
  11. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <Utils/Utils.h>
  14. #include <AzFramework/IO/LocalFileIO.h>
  15. namespace AssetBundler
  16. {
  17. class MockUtilsTest
  18. : public UnitTest::LeakDetectionFixture
  19. , public AzFramework::ApplicationRequests::Bus::Handler
  20. {
  21. public:
  22. void SetUp() override
  23. {
  24. LeakDetectionFixture::SetUp();
  25. AzFramework::ApplicationRequests::Bus::Handler::BusConnect();
  26. m_localFileIO = aznew AZ::IO::LocalFileIO();
  27. m_priorFileIO = AZ::IO::FileIOBase::GetInstance();
  28. // we need to set it to nullptr first because otherwise the
  29. // underneath code assumes that we might be leaking the previous instance
  30. AZ::IO::FileIOBase::SetInstance(nullptr);
  31. AZ::IO::FileIOBase::SetInstance(m_localFileIO);
  32. m_tempDir = new AZ::Test::ScopedAutoTempDirectory();
  33. auto settingsRegistry = AZ::SettingsRegistry::Get();
  34. if (settingsRegistry == nullptr)
  35. {
  36. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  37. settingsRegistry = m_settingsRegistry.get();
  38. AZ::SettingsRegistry::Register(settingsRegistry);
  39. }
  40. settingsRegistry->Get(m_oldEngineRoot.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  41. settingsRegistry->Set(AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder, m_tempDir->GetDirectory());
  42. }
  43. void TearDown() override
  44. {
  45. // Reset Engine Path if there was an existing Settings Registry from before
  46. auto settingsRegistry = AZ::SettingsRegistry::Get();
  47. settingsRegistry->Set(AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder, m_oldEngineRoot.Native());
  48. if(settingsRegistry == m_settingsRegistry.get())
  49. {
  50. AZ::SettingsRegistry::Unregister(settingsRegistry);
  51. m_settingsRegistry.reset();
  52. }
  53. delete m_tempDir;
  54. AZ::IO::FileIOBase::SetInstance(nullptr);
  55. delete m_localFileIO;
  56. AZ::IO::FileIOBase::SetInstance(m_priorFileIO);
  57. AzFramework::ApplicationRequests::Bus::Handler::BusDisconnect();
  58. LeakDetectionFixture::TearDown();
  59. }
  60. // AzFramework::ApplicationRequests::Bus::Handler interface
  61. void NormalizePath(AZStd::string& /*path*/) override {}
  62. void NormalizePathKeepCase(AZStd::string& /*path*/) override {}
  63. void CalculateBranchTokenForEngineRoot(AZStd::string& /*token*/) const override {}
  64. const char* GetTempDir() const
  65. {
  66. return m_tempDir->GetDirectory();
  67. }
  68. AZ::IO::FileIOBase* m_priorFileIO = nullptr;
  69. AZ::IO::FileIOBase* m_localFileIO = nullptr;
  70. AZ::Test::ScopedAutoTempDirectory* m_tempDir = nullptr;
  71. AZStd::unique_ptr<AZ::SettingsRegistryInterface> m_settingsRegistry;
  72. AZ::IO::Path m_oldEngineRoot;
  73. };
  74. TEST_F(MockUtilsTest, DISABLED_TestFilePath_StartsWithAFileSeparator_Valid)
  75. {
  76. AZ::IO::Path relFilePath = "Foo/foo.xml";
  77. AZ::IO::Path absoluteFilePath = AZ::IO::PathView(GetTempDir()).RootPath();
  78. absoluteFilePath /= relFilePath;
  79. absoluteFilePath = absoluteFilePath.LexicallyNormal();
  80. FilePath filePath(relFilePath.Native());
  81. EXPECT_STREQ(filePath.AbsolutePath().c_str(), absoluteFilePath.c_str());
  82. }
  83. TEST_F(MockUtilsTest, TestFilePath_RelativePath_Valid)
  84. {
  85. AZ::IO::Path relFilePath = "Foo\\foo.xml";
  86. AZ::IO::Path absoluteFilePath = (AZ::IO::Path(GetTempDir()) / relFilePath).LexicallyNormal();
  87. FilePath filePath(relFilePath.Native());
  88. EXPECT_EQ(AZ::IO::PathView{ filePath.AbsolutePath() }, absoluteFilePath);
  89. }
  90. #if !AZ_TRAIT_USE_WINDOWS_FILE_API
  91. // When using Windows file API the the AZ::IO::Path comparisons are case insensitive
  92. TEST_F(MockUtilsTest, TestFilePath_CasingMismatch_Error_valid)
  93. {
  94. AZ::IO::Path relFilePath = "Foo\\Foo.xml";
  95. AZ::IO::Path wrongCaseRelFilePath = "Foo\\foo.xml";
  96. AZ::IO::Path correctAbsoluteFilePath = (AZ::IO::Path(GetTempDir()) / relFilePath).LexicallyNormal();
  97. AZ::IO::Path wrongCaseAbsoluteFilePath = (AZ::IO::Path(GetTempDir()) / wrongCaseRelFilePath).LexicallyNormal();
  98. AZ::IO::HandleType fileHandle = AZ::IO::InvalidHandle;
  99. AZ::IO::FileIOBase::GetInstance()->Open(correctAbsoluteFilePath.c_str(), AZ::IO::OpenMode::ModeWrite | AZ::IO::OpenMode::ModeCreatePath, fileHandle);
  100. FilePath filePath(wrongCaseAbsoluteFilePath.Native(), true, false);
  101. EXPECT_FALSE(filePath.IsValid());
  102. EXPECT_TRUE(filePath.ErrorString().contains("File case mismatch"));
  103. }
  104. #endif
  105. TEST_F(MockUtilsTest, TestFilePath_NoFileExists_NoError_valid)
  106. {
  107. AZ::IO::Path relFilePath = "Foo\\Foo.xml";
  108. AZ::IO::Path absoluteFilePath = (AZ::IO::Path(GetTempDir()) / relFilePath).LexicallyNormal();
  109. FilePath filePath(absoluteFilePath.Native(), true, false);
  110. EXPECT_TRUE(filePath.IsValid());
  111. EXPECT_TRUE(filePath.ErrorString().empty());
  112. }
  113. TEST_F(MockUtilsTest, TestFilePath_CasingMismatch_Ignore_Filecase_valid)
  114. {
  115. AZStd::string relFilePath = "Foo\\Foo.xml";
  116. AZStd::string wrongCaseRelFilePath = "Foo\\foo.xml";
  117. AZ::IO::Path correctAbsoluteFilePath = (AZ::IO::Path(GetTempDir()) / relFilePath).LexicallyNormal();
  118. AZ::IO::Path wrongCaseAbsoluteFilePath = (AZ::IO::Path(GetTempDir()) / wrongCaseRelFilePath).LexicallyNormal();
  119. AZ::IO::HandleType fileHandle = AZ::IO::InvalidHandle;
  120. AZ::IO::FileIOBase::GetInstance()->Open(correctAbsoluteFilePath.c_str(), AZ::IO::OpenMode::ModeWrite | AZ::IO::OpenMode::ModeCreatePath, fileHandle);
  121. FilePath filePath(wrongCaseAbsoluteFilePath.Native(), true, true);
  122. EXPECT_TRUE(filePath.IsValid());
  123. EXPECT_STREQ(filePath.AbsolutePath().c_str(), correctAbsoluteFilePath.c_str());
  124. }
  125. TEST_F(MockUtilsTest, LooksLikeWildcardPattern_IsWildcardPattern_ExpectTrue)
  126. {
  127. EXPECT_TRUE(LooksLikeWildcardPattern("*"));
  128. EXPECT_TRUE(LooksLikeWildcardPattern("?"));
  129. EXPECT_TRUE(LooksLikeWildcardPattern("*/*"));
  130. EXPECT_TRUE(LooksLikeWildcardPattern("*/test?/*.xml"));
  131. }
  132. TEST_F(MockUtilsTest, LooksLikeWildcardPattern_IsNotWildcardPattern_ExpectFalse)
  133. {
  134. EXPECT_FALSE(LooksLikeWildcardPattern(""));
  135. EXPECT_FALSE(LooksLikeWildcardPattern("test"));
  136. EXPECT_FALSE(LooksLikeWildcardPattern("test/path.xml"));
  137. }
  138. }