Fixed AzToolsFramework tests (#2887)
* Fixed AzToolsFramework unit tests. Signed-off-by: moraaar <moraaar@amazon.com> * Include missing header. Signed-off-by: moraaar <moraaar@amazon.com> * Using util's class to generate temp directory, instead of qt. Signed-off-by: moraaar <moraaar@amazon.com> * Added empty line Signed-off-by: moraaar <moraaar@amazon.com> * Fixed warning in MessageTest fixture that CacheProjectRootFolder was not set Signed-off-by: moraaar <moraaar@amazon.com> * Additional checks in CreateDefaultEditorEntity helper function. Signed-off-by: moraaar <moraaar@amazon.com> * Updated the AzToolsFrameworkTest logic to set the project cache path The Project Cache Path and Project Path is set through the CommandLine functionality of the ComponentApplication. This allows those Project Cache Path and Project Path to be set within the Settings Registry during the ComponentApplication constructor Removed the explicitly calls to delete the temporary directory and fixed the ScopedTemporaryDirectory class to recursively delete the temporary directory Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com> * Setup correctly @assets@ alias for PlatformAddressedAssetCatalogManagerTest and AssetSeedManagerTest fixtures. - These 2 test fixtures need to manually set the @asset@ alias to not include the platform at the end (which it does by default), because they are looping over platforms in their setup. - Also initializing pointers to nullptr, so if setup fail in the future the teardown doesn't crash trying to delete garbage. Signed-off-by: moraaar <moraaar@amazon.com> Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
This commit is contained in:
@@ -8,72 +8,100 @@
|
||||
|
||||
#include "Utils.h"
|
||||
#include <AzCore/IO/Path/Path.h>
|
||||
#include <AzCore/std/functional.h>
|
||||
#include <AzCore/StringFunc/StringFunc.h>
|
||||
|
||||
UnitTest::ScopedTemporaryDirectory::ScopedTemporaryDirectory()
|
||||
namespace UnitTest
|
||||
{
|
||||
constexpr int MaxAttempts = 255;
|
||||
void DeleteFolderRecursive(const AZ::IO::PathView& path)
|
||||
{
|
||||
auto callback = [&path](AZStd::string_view filename, bool isFile) -> bool
|
||||
{
|
||||
if (isFile)
|
||||
{
|
||||
auto filePath = AZ::IO::FixedMaxPath(path) / filename;
|
||||
AZ::IO::SystemFile::Delete(filePath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (filename != "." && filename != "..")
|
||||
{
|
||||
auto folderPath = AZ::IO::FixedMaxPath(path) / filename;
|
||||
DeleteFolderRecursive(folderPath);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
auto searchPath = AZ::IO::FixedMaxPath(path) / "*";
|
||||
AZ::IO::SystemFile::FindFiles(searchPath.c_str(), callback);
|
||||
AZ::IO::SystemFile::DeleteDir(AZ::IO::FixedMaxPathString(path.Native()).c_str());
|
||||
}
|
||||
|
||||
|
||||
ScopedTemporaryDirectory::ScopedTemporaryDirectory()
|
||||
{
|
||||
constexpr int MaxAttempts = 255;
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
const auto userTempFolder = std::filesystem::temp_directory_path();
|
||||
const auto userTempFolder = std::filesystem::temp_directory_path();
|
||||
#else
|
||||
AZ::IO::Path userTempFolder("/tmp");
|
||||
AZ::IO::Path userTempFolder("/tmp");
|
||||
#endif
|
||||
|
||||
for (int i = 0; i < MaxAttempts; ++i)
|
||||
{
|
||||
auto randomFolder = AZ::Uuid::CreateRandom().ToString<AZStd::fixed_string<512>>(false, false);
|
||||
AZ::IO::FixedMaxPath testPath;
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
auto path = userTempFolder / ("UnitTest-" + randomFolder).c_str();
|
||||
testPath = path.string().c_str();
|
||||
#else
|
||||
userTempFolder /= ("UnitTest-" + randomFolder).c_str();
|
||||
testPath = userTempFolder.c_str();
|
||||
#endif
|
||||
if (!AZ::IO::SystemFile::Exists(testPath.c_str()))
|
||||
for (int i = 0; i < MaxAttempts; ++i)
|
||||
{
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
m_path = path;
|
||||
m_tempDirectory = m_path.string().c_str();
|
||||
auto randomFolder = AZ::Uuid::CreateRandom().ToString<AZStd::fixed_string<512>>(false, false);
|
||||
AZ::IO::FixedMaxPath testPath;
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
auto path = userTempFolder / ("UnitTest-" + randomFolder).c_str();
|
||||
testPath = path.string().c_str();
|
||||
#else
|
||||
m_tempDirectory = testPath;
|
||||
userTempFolder /= ("UnitTest-" + randomFolder).c_str();
|
||||
testPath = userTempFolder.c_str();
|
||||
#endif
|
||||
m_directoryExists = AZ::IO::SystemFile::CreateDir(m_tempDirectory.c_str());
|
||||
break;
|
||||
if (!AZ::IO::SystemFile::Exists(testPath.c_str()))
|
||||
{
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
m_path = path;
|
||||
m_tempDirectory = m_path.string().c_str();
|
||||
#else
|
||||
m_tempDirectory = testPath;
|
||||
#endif
|
||||
m_directoryExists = AZ::IO::SystemFile::CreateDir(m_tempDirectory.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
AZ_Error("ScopedTemporaryDirectory", !m_tempDirectory.empty(), "Failed to create unique temporary directory after attempting %d random folder names", MaxAttempts);
|
||||
}
|
||||
|
||||
ScopedTemporaryDirectory::~ScopedTemporaryDirectory()
|
||||
{
|
||||
if (m_directoryExists)
|
||||
{
|
||||
DeleteFolderRecursive(m_tempDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
AZ_Error("ScopedTemporaryDirectory", !m_tempDirectory.empty(), "Failed to create unique temporary directory after attempting %d random folder names", MaxAttempts);
|
||||
}
|
||||
|
||||
UnitTest::ScopedTemporaryDirectory::~ScopedTemporaryDirectory()
|
||||
{
|
||||
if (m_directoryExists)
|
||||
bool ScopedTemporaryDirectory::IsValid() const
|
||||
{
|
||||
AZ::IO::SystemFile::DeleteDir(m_tempDirectory.c_str());
|
||||
return m_directoryExists;
|
||||
}
|
||||
}
|
||||
|
||||
bool UnitTest::ScopedTemporaryDirectory::IsValid() const
|
||||
{
|
||||
return m_directoryExists;
|
||||
}
|
||||
|
||||
const char* UnitTest::ScopedTemporaryDirectory::GetDirectory() const
|
||||
{
|
||||
return m_tempDirectory.c_str();
|
||||
}
|
||||
const char* ScopedTemporaryDirectory::GetDirectory() const
|
||||
{
|
||||
return m_tempDirectory.c_str();
|
||||
}
|
||||
|
||||
|
||||
#if !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
const std::filesystem::path& UnitTest::ScopedTemporaryDirectory::GetPath() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
std::filesystem::path UnitTest::ScopedTemporaryDirectory::operator/(const std::filesystem::path& rhs) const
|
||||
{
|
||||
return m_path / rhs;
|
||||
}
|
||||
const std::filesystem::path& ScopedTemporaryDirectory::GetPath() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
std::filesystem::path ScopedTemporaryDirectory::operator/(const std::filesystem::path& rhs) const
|
||||
{
|
||||
return m_path / rhs;
|
||||
}
|
||||
#endif // !AZ_TRAIT_USE_POSIX_TEMP_FOLDER
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user