diff --git a/Code/Tools/TestImpactFramework/Frontend/Console/Code/CMakeLists.txt b/Code/Tools/TestImpactFramework/Frontend/Console/Code/CMakeLists.txt index 7a043a30ca..da2c707cb8 100644 --- a/Code/Tools/TestImpactFramework/Frontend/Console/Code/CMakeLists.txt +++ b/Code/Tools/TestImpactFramework/Frontend/Console/Code/CMakeLists.txt @@ -20,4 +20,4 @@ ly_add_target( BUILD_DEPENDENCIES PRIVATE AZ::TestImpact.Runtime.Static -) +) \ No newline at end of file diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Artifact/Dynamic/TestImpactCoverage.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Artifact/Dynamic/TestImpactCoverage.h index 3c5b5353a7..54b78d682e 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Artifact/Dynamic/TestImpactCoverage.h +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Artifact/Dynamic/TestImpactCoverage.h @@ -12,9 +12,8 @@ #pragma once -#include +#include #include -#include namespace TestImpact { @@ -28,14 +27,14 @@ namespace TestImpact //! Coverage information about a particular source file. struct SourceCoverage { - AZ::IO::Path m_path; //!< Source file path. - AZStd::optional> m_coverage; //!< Source file line coverage (empty if source level coverage only). + AZStd::string m_path; //!< Source file path. + AZStd::vector m_coverage; //!< Source file line coverage (empty if source level coverage only). }; //! Coverage information about a particular module (executable, shared library). struct ModuleCoverage { - AZ::IO::Path m_path; //!< Module path. + AZStd::string m_path; //!< Module path. AZStd::vector m_sources; //!< Sources of this module that are covered. }; } // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.cpp b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.cpp new file mode 100644 index 0000000000..53e71d3a30 --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.cpp @@ -0,0 +1,41 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include + +namespace TestImpact +{ + ChangeDependencyList::ChangeDependencyList( + AZStd::vector&& createSourceDependencies, + AZStd::vector&& updateSourceDependencies, + AZStd::vector&& deleteSourceDependencies) + : m_createSourceDependencies(AZStd::move(createSourceDependencies)) + , m_updateSourceDependencies(AZStd::move(updateSourceDependencies)) + , m_deleteSourceDependencies(AZStd::move(deleteSourceDependencies)) + { + } + + const AZStd::vector& ChangeDependencyList::GetCreateSourceDependencies() const + { + return m_createSourceDependencies; + } + + const AZStd::vector& ChangeDependencyList::GetUpdateSourceDependencies() const + { + return m_updateSourceDependencies; + } + + const AZStd::vector& ChangeDependencyList::GetDeleteSourceDependencies() const + { + return m_deleteSourceDependencies; + } +} // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.h new file mode 100644 index 0000000000..bf8b0f558d --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactChangeDependencyList.h @@ -0,0 +1,41 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#pragma once + +#include + +namespace TestImpact +{ + //! Representation of a change list where all CRUD sources have been resolved to source dependencies from the dynamic dependency map. + class ChangeDependencyList + { + public: + ChangeDependencyList( + AZStd::vector&& createSourceDependencies, + AZStd::vector&& updateSourceDependencies, + AZStd::vector&& deleteSourceDependencies); + + //! Gets the sources dependencies of the created source files from the change list. + const AZStd::vector& GetCreateSourceDependencies() const; + + //! Gets the sources dependencies of the updated source files from the change list. + const AZStd::vector& GetUpdateSourceDependencies() const; + + //! Gets the sources dependencies of the deleted source files from the change list. + const AZStd::vector& GetDeleteSourceDependencies() const; + private: + AZStd::vector m_createSourceDependencies; + AZStd::vector m_updateSourceDependencies; + AZStd::vector m_deleteSourceDependencies; + }; +} // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDependencyException.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDependencyException.h new file mode 100644 index 0000000000..e256fa3ccc --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDependencyException.h @@ -0,0 +1,26 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#pragma once + +#include + +namespace TestImpact +{ + //! Exception for dependency related operations. + class DependencyException + : public Exception + { + public: + using Exception::Exception; + }; +} // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.cpp b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.cpp new file mode 100644 index 0000000000..f3edbe2507 --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.cpp @@ -0,0 +1,414 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include +#include + +namespace TestImpact +{ + DynamicDependencyMap::DynamicDependencyMap( + AZStd::vector&& productionTargetDescriptors, + AZStd::vector&& testTargetDescriptors) + : m_productionTargets(AZStd::move(productionTargetDescriptors)) + , m_testTargets(AZStd::move(testTargetDescriptors)) + { + const auto mapBuildTargetSources = [this](const auto* target) + { + for (const auto& source : target->GetSources().m_staticSources) + { + if (auto mapping = m_sourceDependencyMap.find(source); mapping != m_sourceDependencyMap.end()) + { + // This is an existing entry in the dependency map so update the parent build targets with this target + mapping->second.m_parentTargets.insert(target); + } + else + { + // This is a new entry on the dependency map so create an entry with this parent target and no covering targets + m_sourceDependencyMap.emplace(source, DependencyData{ {target}, {} }); + } + } + + // Populate the autogen input to output mapping with any autogen sources + for (const auto& autogen : target->GetSources().m_autogenSources) + { + for (const auto& output : autogen.m_outputs) + { + m_autogenInputToOutputMap[autogen.m_input].push_back(output); + } + } + }; + + for (const auto& target : m_productionTargets.GetTargets()) + { + mapBuildTargetSources(&target); + } + + for (const auto& target : m_testTargets.GetTargets()) + { + mapBuildTargetSources(&target); + } + } + + size_t DynamicDependencyMap::GetNumTargets() const + { + return m_productionTargets.GetNumTargets() + m_testTargets.GetNumTargets(); + } + + size_t DynamicDependencyMap::GetNumSources() const + { + return m_sourceDependencyMap.size(); + } + + const BuildTarget* DynamicDependencyMap::GetBuildTarget(const AZStd::string& name) const + { + const BuildTarget* buildTarget = nullptr; + AZStd::visit([&buildTarget](auto&& target) + { + if constexpr (IsProductionTarget || IsTestTarget) + { + buildTarget = target; + } + + }, GetTarget(name)); + + return buildTarget; + } + + const BuildTarget* DynamicDependencyMap::GetBuildTargetOrThrow(const AZStd::string& name) const + { + const BuildTarget* buildTarget = nullptr; + AZStd::visit([&buildTarget](auto&& target) + { + if constexpr (IsProductionTarget || IsTestTarget) + { + buildTarget = target; + } + }, GetTargetOrThrow(name)); + + return buildTarget; + } + + AZStd::variant DynamicDependencyMap::GetTarget(const AZStd::string& name) const + { + if (auto testTarget = m_testTargets.GetTarget(name); testTarget != nullptr) + { + return testTarget; + } + else if (auto productionTarget = m_productionTargets.GetTarget(name); productionTarget != nullptr) + { + return productionTarget; + } + + return AZStd::monostate{}; + } + + AZStd::variant DynamicDependencyMap::GetTargetOrThrow(const AZStd::string& name) const + { + AZStd::variant buildTarget; + AZStd::visit([&buildTarget, &name](auto&& target) + { + if constexpr (IsProductionTarget || IsTestTarget) + { + buildTarget = target; + } + else + { + throw(TargetException(AZStd::string::format("Couldn't find target %s", name.c_str()).c_str())); + } + }, GetTarget(name)); + + return buildTarget; + } + + void DynamicDependencyMap::ReplaceSourceCoverage(const SourceCoveringTestsList& sourceCoverageDelta) + { + for (const auto& sourceCoverage : sourceCoverageDelta.GetCoverage()) + { + // Autogen input files are not compiled sources and thus supplying coverage data for them makes no sense + AZ_TestImpact_Eval( + m_autogenInputToOutputMap.find(sourceCoverage.GetPath()) == m_autogenInputToOutputMap.end(), + DependencyException, AZStd::string::format("Couldn't replace source coverage for %s, source file is an autogen input file", + sourceCoverage.GetPath().c_str()).c_str()); + + auto [it, inserted] = m_sourceDependencyMap.insert(sourceCoverage.GetPath()); + auto& [key, sourceDependency] = *it; + + // Clear any existing coverage for the delta + sourceDependency.m_coveringTestTargets.clear(); + + // Update the dependency with any new coverage data + for (const auto& unresolvedTestTarget : sourceCoverage.GetCoveringTestTargets()) + { + const TestTarget* testTarget = m_testTargets.GetTarget(unresolvedTestTarget); + if (testTarget) + { + // Source to covering test target mapping + sourceDependency.m_coveringTestTargets.insert(testTarget); + + // Build target to covering test target mapping + for (const auto& parentTarget : sourceDependency.m_parentTargets) + { + m_buildTargetCoverage[parentTarget.GetBuildTarget()].insert(testTarget); + } + } + else + { + AZ_Warning("ReplaceSourceCoverage", false, AZStd::string::format("Test target %s exists in the coverage data " + "but has since been removed from the build system", unresolvedTestTarget.c_str()).c_str()); + } + } + + // If the new coverage data results in a parentless and coverageless entry, consider it a dead entry and remove accordingly + if (sourceDependency.m_coveringTestTargets.empty() && sourceDependency.m_parentTargets.empty()) + { + m_sourceDependencyMap.erase(it); + } + } + } + + void DynamicDependencyMap::ClearSourceCoverage(const AZStd::vector& paths) + { + for (const auto& path : paths) + { + if (const auto outputSources = m_autogenInputToOutputMap.find(path); outputSources != m_autogenInputToOutputMap.end()) + { + // Clearing the coverage data of an autogen input source instead clears the coverage data of its output sources + for (const auto& outputSource : outputSources->second) + { + ReplaceSourceCoverage(SourceCoveringTestsList({ SourceCoveringTests(outputSource, { }) })); + } + } + else + { + ReplaceSourceCoverage(SourceCoveringTestsList({ SourceCoveringTests(path, { }) })); + } + } + } + + const ProductionTargetList& DynamicDependencyMap::GetProductionTargetList() const + { + return m_productionTargets; + } + + const TestTargetList& DynamicDependencyMap::GetTestTargetList() const + { + return m_testTargets; + } + + AZStd::vector DynamicDependencyMap::GetCoveringTestTargetsForProductionTarget(const ProductionTarget& productionTarget) const + { + AZStd::vector coveringTestTargets; + if (const auto coverage = m_buildTargetCoverage.find(&productionTarget); coverage != m_buildTargetCoverage.end()) + { + coveringTestTargets.reserve(coverage->second.size()); + AZStd::copy(coverage->second.begin(), coverage->second.end(), AZStd::back_inserter(coveringTestTargets)); + } + + return coveringTestTargets; + } + + AZStd::optional DynamicDependencyMap::GetSourceDependency(const AZStd::string& path) const + { + AZStd::unordered_set parentTargets; + AZStd::unordered_set coveringTestTargets; + + const auto getSourceDependency = [&parentTargets, &coveringTestTargets, this](const AZStd::string& path) + { + const auto sourceDependency = m_sourceDependencyMap.find(path); + if (sourceDependency != m_sourceDependencyMap.end()) + { + for (const auto& parentTarget : sourceDependency->second.m_parentTargets) + { + parentTargets.insert(parentTarget); + } + + for (const auto& testTarget : sourceDependency->second.m_coveringTestTargets) + { + coveringTestTargets.insert(testTarget); + } + } + }; + + if (const auto outputSources = m_autogenInputToOutputMap.find(path); outputSources != m_autogenInputToOutputMap.end()) + { + // Consolidate the parentage and coverage of each of the autogen input file's generated output files + for (const auto& outputSource : outputSources->second) + { + getSourceDependency(outputSource); + } + } + else + { + getSourceDependency(path); + } + + if (!parentTargets.empty() || !coveringTestTargets.empty()) + { + return SourceDependency(path, DependencyData{ AZStd::move(parentTargets), AZStd::move(coveringTestTargets) }); + } + + return AZStd::nullopt; + } + + SourceDependency DynamicDependencyMap::GetSourceDependencyOrThrow(const AZStd::string& path) const + { + auto sourceDependency = GetSourceDependency(path); + AZ_TestImpact_Eval(sourceDependency.has_value(), DependencyException, AZStd::string::format("Couldn't find source %s", path.c_str()).c_str()); + return sourceDependency.value(); + } + + SourceCoveringTestsList DynamicDependencyMap::ExportSourceCoverage() const + { + AZStd::vector coverage; + for (const auto& [path, dependency] : m_sourceDependencyMap) + { + AZStd::vector souceCoveringTests; + for (const auto& testTarget : dependency.m_coveringTestTargets) + { + souceCoveringTests.push_back(testTarget->GetName()); + } + + coverage.push_back(SourceCoveringTests(path, AZStd::move(souceCoveringTests))); + } + + return coverage; + } + + AZStd::vector DynamicDependencyMap::GetOrphanSourceFiles() const + { + AZStd::vector orphans; + for (const auto& [source, dependency] : m_sourceDependencyMap) + { + if (dependency.m_parentTargets.empty()) + { + orphans.push_back(source); + } + } + + return orphans; + } + + ChangeDependencyList DynamicDependencyMap::ApplyAndResoveChangeList(const ChangeList& changeList) + { + AZStd::vector createDependencies; + AZStd::vector updateDependencies; + AZStd::vector deleteDependencies; + + // Keep track of the coverage to delete as a post step rather than deleting it in situ so that erroneous change lists + // do not corrupt the dynamic dependency map + AZStd::vector coverageToDelete; + + // Create operations + for (const auto& createdFile : changeList.m_createdFiles) + { + auto sourceDependency = GetSourceDependency(createdFile); + if (sourceDependency.has_value()) + { + if (sourceDependency->GetNumParentTargets()) + { + if (sourceDependency->GetNumCoveringTestTargets()) + { + const AZStd::string msg = AZStd::string::format("The newly-created file %s belongs to a build target yet " + "still has coverage data in the source covering test list implying that a delete CRUD operation has been " + "missed, thus the integrity of the source covering test list has been compromised", createdFile.c_str()); + AZ_Error("File Creation", false, msg.c_str()); + throw DependencyException(msg); + } + else + { + createDependencies.emplace_back(AZStd::move(*sourceDependency)); + } + } + else + { + if (sourceDependency->GetNumCoveringTestTargets()) + { + const AZStd::string msg = AZStd::string::format("The newly-created file %s does not belong to a build target " + "yet still has coverage data in the source covering test list, implying that a delete CRUD operation has been " + "missed, thus the integrity of the source covering test list has been compromised", createdFile.c_str()); + AZ_Error("File Creation", false, msg.c_str()); + throw DependencyException(msg); + } + } + } + } + + // Update operations + for (const auto& updatedFile : changeList.m_updatedFiles) + { + auto sourceDependency = GetSourceDependency(updatedFile); + if (sourceDependency.has_value()) + { + if (sourceDependency->GetNumParentTargets()) + { + updateDependencies.emplace_back(AZStd::move(*sourceDependency)); + } + else + { + if (sourceDependency->GetNumCoveringTestTargets()) + { + AZ_Warning( + "File Update", false, AZStd::string::format("Source file %s is potentially an orphan (used by build targets " + "without explicitly being added to the build system, e.g. an include directive pulling in a header from the " + "repository). Running the covering tests for this file with instrumentation will confirm whether or nor this " + "is the case", updatedFile.c_str()).c_str()); + + updateDependencies.emplace_back(AZStd::move(*sourceDependency)); + coverageToDelete.push_back(updatedFile); + } + } + } + } + + // Delete operations + for (const auto& deletedFile : changeList.m_deletedFiles) + { + auto sourceDependency = GetSourceDependency(deletedFile); + if (sourceDependency.has_value()) + { + if (sourceDependency->GetNumParentTargets()) + { + if (sourceDependency->GetNumCoveringTestTargets()) + { + const AZStd::string msg = AZStd::string::format("The deleted file %s still belongs to a build target and still " + "has coverage data in the source covering test list, implying that the integrity of both the source to target " + "mappings and the source covering test list has been compromised", deletedFile.c_str()); + AZ_Error("File Delete", false, msg.c_str()); + throw DependencyException(msg); + } + else + { + const AZStd::string msg = AZStd::string::format("The deleted file %s still belongs to a build target implying " + "that the integrity of the source to target mappings has been compromised", deletedFile.c_str()); + AZ_Error("File Delete", false, msg.c_str()); + throw DependencyException(msg); + } + } + else + { + if (sourceDependency->GetNumCoveringTestTargets()) + { + deleteDependencies.emplace_back(AZStd::move(*sourceDependency)); + coverageToDelete.push_back(deletedFile); + } + } + } + } + + if (!coverageToDelete.empty()) + { + ClearSourceCoverage(coverageToDelete); + } + + return ChangeDependencyList(AZStd::move(createDependencies), AZStd::move(updateDependencies), AZStd::move(deleteDependencies)); + } +} diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.h new file mode 100644 index 0000000000..0057fdc264 --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactDynamicDependencyMap.h @@ -0,0 +1,120 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace TestImpact +{ + //! Representation of the repository source tree and its relation to the build targets and coverage data. + class DynamicDependencyMap + { + public: + //! Constructs the dependency map with entries for each build target's source files with empty test coverage data. + DynamicDependencyMap( + AZStd::vector&& productionTargetDescriptors, + AZStd::vector&& testTargetDescriptors); + + //! Gets the total number of production and test targets in the repository. + size_t GetNumTargets() const; + + //! Gets the total number of unique source files in the repository. + //! @note This includes autogen output sources. + size_t GetNumSources() const; + + //! Attempts to get the specified build target. + //! @param name The name of the build target to get. + //! @returns If found, the pointer to the specified build target, otherwise nullptr. + const BuildTarget* GetBuildTarget(const AZStd::string& name) const; + + //! Attempts to get the specified build target or throw TargetException. + //! @param name The name of the build target to get. + const BuildTarget* GetBuildTargetOrThrow(const AZStd::string& name) const; + + //! Attempts to get the specified target's specialized type. + //! @param name The name of the target to get. + //! @returns If found, the pointer to the specialized target, otherwise AZStd::monostate. + AZStd::variant GetTarget(const AZStd::string& name) const; + + //! Attempts to get the specified target's specialized type or throw TargetException. + //! @param name The name of the target to get. + AZStd::variant GetTargetOrThrow(const AZStd::string& name) const; + + //! Get the list of production targets in the repository. + const ProductionTargetList& GetProductionTargetList() const; + + //! Get the list of test targets in the repository. + const TestTargetList& GetTestTargetList() const; + + //! Gets the test targets covering the specified production target. + //! @param productionTarget The production target to retrieve the covering tests for. + AZStd::vector GetCoveringTestTargetsForProductionTarget(const ProductionTarget& productionTarget) const; + + //! Gets the source dependency for the specified source file. + //! @note Autogen input source dependencies are the consolidated source dependencies of all of their generated output sources. + //! @returns If found, the source dependency information for the specified source file, otherwise empty. + AZStd::optional GetSourceDependency(const AZStd::string& path) const; + + //! Gets the source dependency for the specified source file or throw DependencyException. + SourceDependency GetSourceDependencyOrThrow(const AZStd::string& path) const; + + //! Replaces the source coverage of the specified sources with the specified source coverage. + //! @note The covering targets for the parent test target(s) will not be pruned if those covering targets are removed. + //! @param sourceCoverageDelta The source coverage delta to replace in the dependency map. + void ReplaceSourceCoverage(const SourceCoveringTestsList& sourceCoverageDelta); + + //! Exports the coverage of all sources in the dependency map. + SourceCoveringTestsList ExportSourceCoverage() const; + + //! Gets the list of orphaned source files in the dependency map that have coverage data but belong to no parent build targets. + AZStd::vector GetOrphanSourceFiles() const; + + //! Applies the specified change list to the dynamic dependency map and resolves the change list to a change dependency list + //! containing the updated source dependencies for each source file in the change list. + //! @param changeList The change list to apply and resolve. + //! @returns The change list as resolved to the appropriate source dependencies. + [[nodiscard]] ChangeDependencyList ApplyAndResoveChangeList(const ChangeList& changeList); + + private: + //! Clears the source coverage of the specified sources. + //! @note The covering targets for the parent test target(s) will not be pruned if those covering targets are removed. + void ClearSourceCoverage(const AZStd::vector& paths); + + //! The sorted list of unique production targets in the repository. + ProductionTargetList m_productionTargets; + + //! The sorted list of unique test targets in the repository. + TestTargetList m_testTargets; + + //! The dependency map of sources to their parent build targets and covering test targets. + AZStd::unordered_map m_sourceDependencyMap; + + //! The map of build targets and their covering test targets. + AZStd::unordered_map> m_buildTargetCoverage; + + //! Mapping of autogen input sources to their generated output sources. + AZStd::unordered_map> m_autogenInputToOutputMap; + }; +} // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.cpp b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.cpp new file mode 100644 index 0000000000..57a11e9f69 --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.cpp @@ -0,0 +1,58 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#include + +#include + +namespace TestImpact +{ + SourceCoveringTests::SourceCoveringTests(const AZStd::string& path, AZStd::vector&& coveringTestTargets) + : m_path(path) + , m_coveringTestTargets(AZStd::move(coveringTestTargets)) + { + } + + const AZStd::string& SourceCoveringTests::GetPath() const + { + return m_path; + } + + size_t SourceCoveringTests::GetNumCoveringTestTargets() const + { + return m_coveringTestTargets.size(); + } + + const AZStd::vector& SourceCoveringTests::GetCoveringTestTargets() const + { + return m_coveringTestTargets; + } + + SourceCoveringTestsList::SourceCoveringTestsList(AZStd::vector&& sourceCoveringTests) + : m_coverage(AZStd::move(sourceCoveringTests)) + { + AZStd::sort(m_coverage.begin(), m_coverage.end(), [](const SourceCoveringTests& lhs, const SourceCoveringTests& rhs) + { + return lhs.GetPath() < rhs.GetPath(); + }); + } + + size_t SourceCoveringTestsList::GetNumSources() const + { + return m_coverage.size(); + } + + const AZStd::vector& SourceCoveringTestsList::GetCoverage() const + { + return m_coverage; + } +} diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.h new file mode 100644 index 0000000000..68b09d9e5a --- /dev/null +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Dependency/TestImpactSourceCoveringTestsList.h @@ -0,0 +1,53 @@ +/* + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ + +#pragma once + +#include +#include + +namespace TestImpact +{ + //! Represents the unresolved test target coverage for a given source file. + class SourceCoveringTests + { + public: + SourceCoveringTests(const AZStd::string& path, AZStd::vector&& coveringTestTargets); + + //! Returns the path of this source file. + const AZStd::string& GetPath() const; + + //! Returns the number of unresolved test targets covering this source file. + size_t GetNumCoveringTestTargets() const; + + //! Returns the unresolved test targets covering this source file. + const AZStd::vector& GetCoveringTestTargets() const; + private: + AZStd::string m_path; //!< The path of this source file. + AZStd::vector m_coveringTestTargets; //!< The unresolved test targets that cover this source file. + }; + + //! Sorted collection of source file test coverage. + class SourceCoveringTestsList + { + public: + SourceCoveringTestsList(AZStd::vector&& sourceCoveringTests); + + //! Returns the number of source files in the collection. + size_t GetNumSources() const; + + //! Returns the source file coverages. + const AZStd::vector& GetCoverage() const; + private: + AZStd::vector m_coverage; //!< The collection of source file coverages. + }; +} // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.cpp b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.cpp index 3a6f5d0b79..0d50dbbf9a 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.cpp +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.cpp @@ -25,7 +25,7 @@ namespace TestImpact for (const auto& sourceCovered : moduleCovered.m_sources) { m_sourcesCovered.emplace_back(sourceCovered.m_path); - if (sourceCovered.m_coverage.has_value()) + if (!sourceCovered.m_coverage.empty()) { m_coverageLevel = CoverageLevel::Line; } @@ -51,7 +51,7 @@ namespace TestImpact return m_modules.size(); } - const AZStd::vector& TestCoverage::GetSourcesCovered() const + const AZStd::vector& TestCoverage::GetSourcesCovered() const { return m_sourcesCovered; } diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.h b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.h index ab92b1bd80..7253a29df0 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.h +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Source/Test/Run/TestImpactTestCoverage.h @@ -38,7 +38,7 @@ namespace TestImpact size_t GetNumModulesCovered() const; //! Returns the sorted set of unique sources covered (empty if no coverage). - const AZStd::vector& GetSourcesCovered() const; + const AZStd::vector& GetSourcesCovered() const; //! Returns the modules covered (empty if no coverage). const AZStd::vector& GetModuleCoverages() const; @@ -48,7 +48,7 @@ namespace TestImpact private: AZStd::vector m_modules; - AZStd::vector m_sourcesCovered; + AZStd::vector m_sourcesCovered; AZStd::optional m_coverageLevel; }; } // namespace TestImpact diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/Tests/Test/TestImpactTestCoverageTest.cpp b/Code/Tools/TestImpactFramework/Runtime/Code/Tests/Test/TestImpactTestCoverageTest.cpp index 538097d11e..c1d7ba3e4b 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/Tests/Test/TestImpactTestCoverageTest.cpp +++ b/Code/Tools/TestImpactFramework/Runtime/Code/Tests/Test/TestImpactTestCoverageTest.cpp @@ -19,12 +19,12 @@ namespace UnitTest { namespace { - AZ::IO::Path GenerateSourcePath(AZ::u32 index) + AZStd::string GenerateSourcePath(AZ::u32 index) { return AZStd::string::format("SourceFile%u", index); } - AZ::IO::Path GenerateModulePath(AZ::u32 index) + AZStd::string GenerateModulePath(AZ::u32 index) { return AZStd::string::format("Module%u", index); } @@ -47,7 +47,7 @@ namespace UnitTest sourceCoverage.m_path = GenerateSourcePath(index); if (coverageLevel == TestImpact::CoverageLevel::Line) { - sourceCoverage.m_coverage.emplace(GenerateLineCoverages(index + 1)); + sourceCoverage.m_coverage = GenerateLineCoverages(index + 1); } return sourceCoverage; @@ -152,9 +152,9 @@ namespace UnitTest if (m_coverageLevel == TestImpact::CoverageLevel::Line) { // Expect there to actually be line coverage data if this coverage was procedurally generated with line data - EXPECT_TRUE(sourceCoverage.m_coverage.has_value()); + EXPECT_FALSE(sourceCoverage.m_coverage.empty()); - const AZStd::vector& lineCoverages = sourceCoverage.m_coverage.value(); + const AZStd::vector& lineCoverages = sourceCoverage.m_coverage; // Expect the source's number of lines to match that of the corresponding procedurally generated source EXPECT_EQ(lineCoverages.size(), sourceIndex + 1); @@ -171,7 +171,7 @@ namespace UnitTest else { // Do not expect there to actually be line coverage data if this coverage was not procedurally generated with line data - EXPECT_FALSE(sourceCoverage.m_coverage.has_value()); + EXPECT_TRUE(sourceCoverage.m_coverage.empty()); } } } diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_files.cmake b/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_files.cmake index fd65a16e39..f4128f7002 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_files.cmake +++ b/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_files.cmake @@ -10,11 +10,37 @@ # set(FILES + Include/TestImpactFramework/TestImpactBitwise.h + Include/TestImpactFramework/TestImpactCallback.h Include/TestImpactFramework/TestImpactException.h Include/TestImpactFramework/TestImpactFrameworkPath.h - Include/TestImpactFramework/TestImpactCallback.h - Source/TestImpactException.cpp - Source/TestImpactFrameworkPath.cpp + Source/Artifact/TestImpactArtifactException.h + Source/Artifact/Factory/TestImpactBuildTargetDescriptorFactory.cpp + Source/Artifact/Factory/TestImpactBuildTargetDescriptorFactory.h + Source/Artifact/Factory/TestImpactChangeListFactory.cpp + Source/Artifact/Factory/TestImpactChangeListFactory.h + Source/Artifact/Factory/TestImpactTestEnumerationSuiteFactory.cpp + Source/Artifact/Factory/TestImpactTestEnumerationSuiteFactory.h + Source/Artifact/Factory/TestImpactTestRunSuiteFactory.cpp + Source/Artifact/Factory/TestImpactTestRunSuiteFactory.h + Source/Artifact/Factory/TestImpactTestTargetMetaMapFactory.cpp + Source/Artifact/Factory/TestImpactTestTargetMetaMapFactory.h + Source/Artifact/Factory/TestImpactModuleCoverageFactory.cpp + Source/Artifact/Factory/TestImpactModuleCoverageFactory.h + Source/Artifact/Static/TestImpactBuildTargetDescriptor.cpp + Source/Artifact/Static/TestImpactBuildTargetDescriptor.h + Source/Artifact/Static/TestImpactTargetDescriptorCompiler.cpp + Source/Artifact/Static/TestImpactTargetDescriptorCompiler.h + Source/Artifact/Static/TestImpactProductionTargetDescriptor.cpp + Source/Artifact/Static/TestImpactProductionTargetDescriptor.h + Source/Artifact/Static/TestImpactTestTargetMeta.h + Source/Artifact/Static/TestImpactTestTargetDescriptor.cpp + Source/Artifact/Static/TestImpactTestTargetDescriptor.h + Source/Artifact/Dynamic/TestImpactChangelist.h + Source/Artifact/Dynamic/TestImpactTestEnumerationSuite.h + Source/Artifact/Dynamic/TestImpactTestRunSuite.h + Source/Artifact/Dynamic/TestImpactTestSuite.h + Source/Artifact/Dynamic/TestImpactCoverage.h Source/Process/TestImpactProcess.cpp Source/Process/TestImpactProcess.h Source/Process/TestImpactProcessException.h @@ -26,4 +52,40 @@ set(FILES Source/Process/JobRunner/TestImpactProcessJobRunner.h Source/Process/Scheduler/TestImpactProcessScheduler.cpp Source/Process/Scheduler/TestImpactProcessScheduler.h + + Source/Target/TestImpactBuildTarget.cpp + Source/Target/TestImpactBuildTarget.h + Source/Target/TestImpactBuildTargetList.h + Source/Target/TestImpactProductionTarget.cpp + Source/Target/TestImpactProductionTarget.h + Source/Target/TestImpactProductionTargetList.h + Source/Target/TestImpactTargetException.h + Source/Target/TestImpactTestTarget.cpp + Source/Target/TestImpactTestTarget.h + Source/Target/TestImpactTestTargetList.h + Source/Test/Enumeration/TestImpactTestEnumeration.h + Source/Test/Enumeration/TestImpactTestEnumerationException.h + Source/Test/Enumeration/TestImpactTestEnumerationSerializer.cpp + Source/Test/Enumeration/TestImpactTestEnumerationSerializer.h + Source/Test/Enumeration/TestImpactTestEnumerator.cpp + Source/Test/Enumeration/TestImpactTestEnumerator.h + Source/Test/Run/TestImpactTestRunSerializer.cpp + Source/Test/Run/TestImpactTestRunSerializer.h + Source/Test/Run/TestImpactTestRunner.cpp + Source/Test/Run/TestImpactTestRunner.h + Source/Test/Run/TestImpactInstrumentedTestRunner.cpp + Source/Test/Run/TestImpactInstrumentedTestRunner.h + Source/Test/Run/TestImpactTestRun.cpp + Source/Test/Run/TestImpactTestRun.h + Source/Test/Run/TestImpactTestRunJobData.cpp + Source/Test/Run/TestImpactTestRunJobData.h + Source/Test/Run/TestImpactTestCoverage.cpp + Source/Test/Run/TestImpactTestCoverage.h + Source/Test/Run/TestImpactTestRunException.h + Source/Test/Job/TestImpactTestJobRunner.h + Source/Test/Job/TestImpactTestJobException.h + Source/Test/Job/TestImpactTestJobCommon.h + Source/Test/TestImpactTestSuiteContainer.h + Source/TestImpactException.cpp + Source/TestImpactFrameworkPath.cpp ) diff --git a/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_tests_files.cmake b/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_tests_files.cmake index 61059dc543..13f788c37b 100644 --- a/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_tests_files.cmake +++ b/Code/Tools/TestImpactFramework/Runtime/Code/testimpactframework_runtime_tests_files.cmake @@ -10,13 +10,29 @@ # set(FILES + Tests/Artifact/TestImpactTargetDescriptorCompilerTest.cpp + Tests/Artifact/TestImpactBuildTargetDescriptorFactoryTest.cpp + Tests/Artifact/TestImpactModuleCoverageFactoryTest.cpp + Tests/Artifact/TestImpactChangeListFactoryTest.cpp + Tests/Artifact/TestImpactTestEnumerationSuiteFactoryTest.cpp + Tests/Artifact/TestImpactTestRunSuiteFactoryTest.cpp + Tests/Artifact/TestImpactTestTargetMetaMapFactoryTest.cpp + Tests/Process/TestImpactProcessSchedulerTest.cpp + Tests/Process/TestImpactProcessTest.cpp + Tests/Target/TestImpactBuildTargetTest.cpp Tests/TestImpactExceptionTest.cpp Tests/TestImpactFrameworkPathTest.cpp - Tests/TestImpactProcessSchedulerTest.cpp - Tests/TestImpactProcessTest.cpp - Tests/TestImpactProcessTestShared.cpp - Tests/TestImpactProcessTestShared.h + Tests/Test/TestImpactTestEnumeratorTest.cpp + Tests/Test/TestImpactTestEumerationSerializerTest.cpp + Tests/Test/TestImpactTestRunSerializerTest.cpp + Tests/Test/TestImpactTestRunnerTest.cpp + Tests/Test/TestImpactInstrumentedTestRunnerTest.cpp + Tests/Test/TestImpactTestCoverageTest.cpp + Tests/TestImpactTestJobRunnerCommon.h Tests/TestImpactTestMain.cpp + Tests/TestImpactTestUtils.cpp + Tests/TestImpactTestUtils.h + )