Files
o3de/Code/Tools/AssetProcessor/native/unittests/UnitTestRunner.h
T
lumberyard-employee-dm f648cb1fd8 Update the FileIO Aliases (#4186)
* Update the FileIOAlias naming to make the cache, project root and engine
root paths more clear

The alias of `@root@`, `@assets@`, and `@projectplatformcache@` has been
collapsed to `@projectproductassets@`

The alias of `@devroot@` and `@engroot@` has been collapsed to
`@engroot@`

The alias of `@devassets@` and `@projectroot@` has been collapsed to
`@projectroot@`

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated use of devassets and devroot properties in python

Those properties now use projectroot and engroot

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updating the alias @engroot@ alias path comment in each platform specific LocalFileIO_*.cpp file

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed hardcoded size of 9 for the product asset alias.

The ResolvePath function now just appends the @projectproductassets@
alias with the input path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove duplicate @projectproductassets@ check in ProcessFileTreeRequest

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fix for typos in Hydra python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated LocalFileIO::Copy call on Windows to use the Unicode aware CopyFileW API

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the AWSMetreicsGemAllocatorFixture to properly suppress asset
cache write errors for Test file creation.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed unneeded call to set the @projectproductasstes@ alias at the bottom of the AssetSeedManagerTest SetUp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added a deprecated alias map to the FileIO System

When a deprecated alias is accessed, the FileIO System logs an AZ_Error and indicates the alias that should be used

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated python test scripts to use the projectroot binding

Retrieving the AutomatedTesting project path based on "<devroot>/AutomatedTesting" has been removed.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to devroot and devgame within the codebase

The GetAbsoluteDevGameFolderPath functions has been replaced with direct call to AZ::Utils::GetProjectPath
The GetAbsoluteDevRootFolderPath functions has been replaced with direct calls to AZ::Utils::GetEnginePath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated <engroot>/AutomatedTesting references to projectroot


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced references that assumes the project path is <engroot>/AutomatedTesting with <projectroot> in the AutomatedTesting python test

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Correct casing in emfxworkspace file


Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed newly added AppendPathParts function
Removed the Path constructors which accepts a PathIterable instance

The PathIterable isn't safe to return to a user of the Path class as it might be referencing temporary paths supplied via PathView arguments

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed unused parameter warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Undid change to the LexicallyProximate function to set the path result to the base path.

It needs to return the *this path if the pathResult is empty

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Moved the LocalFileIO ConvertToAbsolutePath implementations to AZ::Utils

Fixed the ConvertToAbsolutePath implementation for Unix platforms to use a buffer that is size PATH_MAX(4096 on all our supported Unix platforms).
Because the buffer before was AZ::IO::MaxPathLength which as a size of 1024, this was resulting in the Unix `realpath` function smashing the stack when it converts a path to an absolute path that is longer than 1024 characters

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the EditorCore.Tests to attach the AZ Environment to the EditorCore shared library that is statically loaded on launch.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed for DeprecatedAlaisesKeyVisitor Visit function causing the non string_view overloads being hidden causing a hidden base class function warning

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Changed the AWSMetricsGemMock to use a temporary for writing test assets

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the LocalFileIO::ResolvePath function to use HasRootPath to determine if a path starts with a root path such as "D:", "D:/" or "/"

IsAbsolute was not the corect check as the path of "D:" is a relative
path.
To be absolute according to the Windows the path must have a root
directory. i.e "D:/" or "D:\"

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed absolute path comment from LocalFile_UnixLike.cpp and LocalFile_Android.cpp FindFiles implementations
Updated the ConvertToAlias to supply an AZ::IO::FixedMaxPath

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced usage of the @projectproductassets@ alias with @engroot@ when referring to the LmbrCentral source folder in the CopyDependencyBuilderTest and the SeedBuilderTests

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the ScriptCanvas Upgrade Tool to output backed up files to the
Project User Path instead of the engine root

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed whitespacing issues in Application.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Remove unnecessary creation of a FixedMaxPath in the UpgradeTool.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Modified testSeedFolder variable in the SeedBuilderTests to use the
@engroot@ alias instead of @projectproductassets@/.. alias when
referring to the LmbrCentral Gem source path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated references to the Project Asset Cache in the PythonTests.

Those tests no longer use the logic `azlmbr.paths.projectroot / "Cache" / "pc"` to retrieve a path to the cache root but instead the `azlmbr.paths.projectproductassets` constant

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fixed the FileIO Deprecated Alias test on Windows

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removing @projectsourceassets@ alias, as it is only used once.

Updated the PhysX EditorSystemComponent.cpp to query the ProjectPath
form the SettingsRegistry.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced @projectproductassets@ alias with @products@

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Rollback changes to the PhysX EditorSystemComponent.cpp

The changes to use the ProjectPath from the SettingsRegistry has been implemented in PR #4497

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-10-05 15:12:53 -05:00

331 lines
12 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef UNITTESTRUNNER_H
#define UNITTESTRUNNER_H
#if !defined(Q_MOC_RUN)
#include <QObject>
#include <QMetaObject>
#include <QString>
#include <QDebug>
#include <AzCore/Debug/TraceMessageBus.h>
#include <QDir>
#include <QTemporaryDir>
#include <AzCore/IO/FileIO.h>
#include <AzFramework/IO/LocalFileIO.h>
#endif
#include <gtest/gtest.h>
//! These macros can be used for checking your unit tests,
//! you can check AssetScannerUnitTest.cpp for usage
#define UNIT_TEST_EXPECT_TRUE(condition) \
UNIT_TEST_CHECK((condition))
#define UNIT_TEST_EXPECT_FALSE(condition) \
UNIT_TEST_CHECK(!(condition))
#define UNIT_TEST_CHECK(condition) \
if (!(condition)) \
{ \
QString failMessage = QString("%1(%2): ---- FAIL: %3").arg(__FILE__).arg(__LINE__).arg(#condition); \
Q_EMIT UnitTestFailed(failMessage); \
return; \
}
//! This is the base class. Derive from this class, implement StartTest
//! and emit UnitTestPassed when done or UnitTestFailed when you fail
//! You must emit either one or the other for the next unit test to start.
class UnitTestRun
: public QObject
{
Q_OBJECT
public:
virtual ~UnitTestRun() {}
///implement all your unit tests in this function
virtual void StartTest() = 0;
///Unit tests having higher priority will run first,
///negative value means higher priority, default priority is zero
virtual int UnitTestPriority() const;
const char* GetName() const;
void SetName(const char* name);
Q_SIGNALS:
void UnitTestFailed(QString message);
void UnitTestPassed();
private:
const char* m_name = nullptr; // expected to be static memory!
};
// after deriving from this class, place the following somewhere
// REGISTER_UNIT_TEST(YourClassName)
// ----------------- UTILITY FUNCTIONS --------------------
namespace UnitTestUtils
{
/** sleep for the minimum amount of time that the file system can store.
* Different file systems (windows, mac, for example) have differing resolutions that they have for
* file times. HFS stores only 'seconds' precision, for example. so tests that need to wait so that
* mod times have changed, must wait for this amount of time
**/
void SleepForMinimumFileSystemTime();
//! Create a dummy file, with optional contents. Will create directories for it too.
bool CreateDummyFile(const QString& fullPathToFile, QString contents = "");
//! This function pumps the Qt event queue until either the varToWatch becomes true or the specified millisecond elapse.
bool BlockUntil(bool& varToWatch, int millisecondsMax);
// the Assert Absorber here is used to absorb asserts and errors during unit tests.
// it only absorbs asserts spawned by this thread;
class AssertAbsorber : public AZ::Debug::TraceMessageBus::Handler
{
public:
AssertAbsorber(bool debugMessages = false) : m_debugMessages{debugMessages}
{
m_debugMessages = debugMessages;
// only absorb asserts when this object is on scope in the thread that this object is on scope in.
BusConnect();
}
void ExpectCheck(const int& numAbsorbed, int expectedAbsorbed, const char* errorType, const AZStd::vector<AZStd::string>& messageList)
{
if (numAbsorbed != expectedAbsorbed)
{
BusDisconnect();
AZ_Printf("AssertAbsorber", "Incorrect number of %s absobed:\n\n", errorType);
for (auto& thisMessage : messageList)
{
AZ_Printf("Absorbed", thisMessage.c_str());
}
BusConnect();
}
ASSERT_EQ(numAbsorbed, expectedAbsorbed);
}
void AssertCheck(const int& numAbsorbed, int expectedAbsorbed, const char* errorType, const AZStd::vector<AZStd::string>& messageList)
{
if (numAbsorbed != expectedAbsorbed)
{
BusDisconnect();
AZ_Printf("AssertAbsorber", "Incorrect number of %s absorbed:\n\n", errorType);
for (auto& thisMessage : messageList)
{
AZ_Printf("Absorbed", thisMessage.c_str());
}
BusConnect();
}
ASSERT_EQ(numAbsorbed, expectedAbsorbed);
}
void ExpectWarnings(int expectValue)
{
ExpectCheck(m_numWarningsAbsorbed, expectValue, "warnings", m_warningMessages);
}
void ExpectErrors(int expectValue)
{
ExpectCheck(m_numErrorsAbsorbed, expectValue, "errors", m_errorMessages);
}
void ExpectAsserts(int expectValue)
{
ExpectCheck(m_numAssertsAbsorbed, expectValue, "asserts", m_assertMessages);
}
void AssertWarnings(int expectValue)
{
AssertCheck(m_numWarningsAbsorbed, expectValue, "warnings", m_warningMessages);
}
void AssertErrors(int expectValue)
{
AssertCheck(m_numErrorsAbsorbed, expectValue, "errors", m_errorMessages);
}
void AssertAsserts(int expectValue)
{
AssertCheck(m_numAssertsAbsorbed, expectValue, "asserts", m_assertMessages);
}
bool OnPreWarning([[maybe_unused]] const char* window, [[maybe_unused]] const char* fileName, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* message) override
{
++m_numWarningsAbsorbed;
if (m_debugMessages)
{
m_warningMessages.push_back(AZStd::string::format("%s\n File: %s Line: %d Func: %s\n", message, fileName, line, func));
}
return true;
}
bool OnPreAssert([[maybe_unused]] const char* fileName, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* message) override
{
++m_numAssertsAbsorbed;
if (m_debugMessages)
{
m_assertMessages.push_back(AZStd::string::format("%s\n File: %s Line: %d Func: %s\n", message, fileName, line, func));
}
return true; // I handled this, do not forward it
}
bool OnPreError([[maybe_unused]] const char* window, [[maybe_unused]] const char* fileName, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* message) override
{
++m_numErrorsAbsorbed;
if (m_debugMessages)
{
m_errorMessages.push_back(AZStd::string::format("%s\n File: %s Line: %d Func: %s\n", message, fileName, line, func));
}
return true; // I handled this, do not forward it
}
bool OnPrintf(const char* /*window*/, const char* /*message*/) override
{
++m_numMessagesAbsorbed;
return true;
}
void PrintAbsorbed()
{
BusDisconnect();
AZ_Printf("AssertAbsorber", "Warnings Absorbed:\n");
for (auto& thisMessage : m_warningMessages)
{
AZ_Printf("AbsorbedWarning", thisMessage.c_str());
}
AZ_Printf("AssertAbsorber", "Errors Absorbed:\n");
for (auto& thisMessage : m_errorMessages)
{
AZ_Printf("AbsorbedError", thisMessage.c_str());
}
AZ_Printf("AssertAbsorber", "Warnings Absorbed:\n");
for (auto& thisMessage : m_assertMessages)
{
AZ_Printf("AbsorbedAssert", thisMessage.c_str());
}
BusConnect();
}
~AssertAbsorber()
{
BusDisconnect();
}
void Clear()
{
m_numMessagesAbsorbed = 0;
m_numWarningsAbsorbed = 0;
m_numAssertsAbsorbed = 0;
m_numErrorsAbsorbed = 0;
m_warningMessages.clear();
m_errorMessages.clear();
m_assertMessages.clear();
}
AZStd::vector<AZStd::string> m_assertMessages;
AZStd::vector<AZStd::string> m_warningMessages;
AZStd::vector<AZStd::string> m_errorMessages;
int m_numMessagesAbsorbed = 0;
int m_numWarningsAbsorbed = 0;
int m_numAssertsAbsorbed = 0;
int m_numErrorsAbsorbed = 0;
bool m_debugMessages{ false };
};
//! Automatically restore current directory when this leaves scope:
class ScopedDir
{
public:
ScopedDir() = default;
ScopedDir(QString newDir)
{
Setup(newDir);
}
void Setup(QString newDir)
{
m_originalDir = QDir::currentPath();
newDir = QDir::cleanPath(newDir);
QDir::setCurrent(newDir);
m_localFileIO = aznew AZ::IO::LocalFileIO();
m_priorFileIO = AZ::IO::FileIOBase::GetInstance();
if (m_priorFileIO)
{
AZ::IO::FileIOBase::SetInstance(nullptr);
}
AZ::IO::FileIOBase::SetInstance(m_localFileIO);
m_localFileIO->SetAlias("@products@", (newDir + QString("/ALIAS/assets")).toUtf8().constData());
m_localFileIO->SetAlias("@log@", (newDir + QString("/ALIAS/logs")).toUtf8().constData());
m_localFileIO->SetAlias("@usercache@", (newDir + QString("/ALIAS/cache")).toUtf8().constData());
m_localFileIO->SetAlias("@user@", (newDir + QString("/ALIAS/user")).toUtf8().constData());
}
~ScopedDir()
{
AZ::IO::FileIOBase::SetInstance(nullptr);
delete m_localFileIO;
m_localFileIO = nullptr;
if (m_priorFileIO)
{
AZ::IO::FileIOBase::SetInstance(m_priorFileIO);
}
QDir::setCurrent(m_originalDir);
}
private:
QString m_originalDir;
AZ::IO::FileIOBase* m_priorFileIO = nullptr;
AZ::IO::FileIOBase* m_localFileIO = nullptr;
};
}
// ----------------------------------- IMPLEMENTATION DETAILS ------------------------
class UnitTestRegistry
{
public:
explicit UnitTestRegistry(const char* name);
virtual ~UnitTestRegistry() {}
static UnitTestRegistry* first() { return s_first; }
UnitTestRegistry* next() const { return m_next; }
const char* getName() const { return m_name; }
virtual UnitTestRun* create() = 0;
protected:
// it forms a static linked-list using the following internal:
static UnitTestRegistry* s_first;
UnitTestRegistry* m_next;
const char* m_name = nullptr; // expected to be static memory
};
template<typename T>
class UnitTestRegistryEntry
: public UnitTestRegistry
{
public:
UnitTestRegistryEntry(const char* name)
: UnitTestRegistry(name) {}
virtual UnitTestRun* create()
{
static_assert(std::is_base_of<UnitTestRun, T>::value, "You must derive from UnitTestRun if you want to use REGISTER_UNIT_TEST");
T* created = new T();
created->SetName(m_name);
return created;
}
};
/// Derive from UnitTestRun, then put the following macro in your cpp file:
#define REGISTER_UNIT_TEST(classType) \
UnitTestRegistryEntry<classType> g_autoRegUnitTest##classType(#classType);
#endif // UNITTESTRUNNER_H