PythonCoverageEditorSystemComponent.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #pragma once
  13. #include <AzCore/Component/Component.h>
  14. #include <AzCore/Component/EntityBus.h>
  15. #include <AzCore/IO/Path/Path.h>
  16. #include <AzCore/std/optional.h>
  17. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  18. #include <AzToolsFramework/API/EditorPythonScriptNotificationsBus.h>
  19. namespace AZ
  20. {
  21. class ComponentDescriptor;
  22. }
  23. namespace PythonCoverage
  24. {
  25. //! System component for PythonCoverage editor.
  26. class PythonCoverageEditorSystemComponent
  27. : public AZ::Component
  28. , private AZ::EntitySystemBus::Handler
  29. , private AzToolsFramework::EditorPythonScriptNotificationsBus::Handler
  30. {
  31. public:
  32. AZ_COMPONENT(PythonCoverageEditorSystemComponent, "{33370075-3aea-49c4-823d-476f8ac95b6f}");
  33. static void Reflect(AZ::ReflectContext* context);
  34. PythonCoverageEditorSystemComponent() = default;
  35. private:
  36. //! The coverage state for Python tests.
  37. enum class CoverageState : AZ::u8
  38. {
  39. Disabled, //!< Python coverage is disabled.
  40. Idle, //!< Python coverage is enabled but not actively gathering coverage data.
  41. Gathering //!< Python coverage is enabled and actively gathering coverage data.
  42. };
  43. // AZ::Component overrides...
  44. void Activate() override;
  45. void Deactivate() override;
  46. // AZ::EntitySystemBus overrides...
  47. void OnEntityActivated(const AZ::EntityId& entityId) override;
  48. // AZ::EditorPythonScriptNotificationsBus ...
  49. void OnStartExecuteByFilenameAsTest(AZStd::string_view filename, AZStd::string_view testCase, const AZStd::vector<AZStd::string_view>& args) override;
  50. //! Enumerates all of the loaded shared library modules and the component descriptors that belong to them.
  51. void EnumerateAllModuleComponents();
  52. //! Enumerates all of the component descriptors for the specified entity.
  53. void EnumerateComponentsForEntity(const AZ::EntityId& entityId);
  54. //! Attempts to parse the test impact analysis framework configuration file.
  55. //! If either the test impact analysis framework is disabled or the configuration file cannot be parsed, python coverage is disabled.
  56. //! @returns The coverage state after the parsing attempt.
  57. CoverageState ParseCoverageOutputDirectory();
  58. //! Returns all of the shared library modules that parent the component descriptors of the specified set of activated entities.
  59. //! @note Entity component descriptors are still retrieved even if the entity in question has since been deactivated.
  60. //! @param entityComponents The set of activated entities and their component descriptors to get the parent modules for.
  61. AZStd::unordered_set<AZStd::string> GetParentComponentModulesForAllActivatedEntities(
  62. const AZStd::unordered_map<AZ::Uuid, AZ::ComponentDescriptor*>& entityComponents) const;
  63. //! Writes the current coverage data snapshot to disk.
  64. void WriteCoverageFile();
  65. CoverageState m_coverageState = CoverageState::Disabled; //!< Current coverage state.
  66. AZStd::unordered_map<AZStd::string, AZStd::unordered_map<AZ::Uuid, AZ::ComponentDescriptor*>> m_entityComponentMap; //!< Map of
  67. //!< component IDs to component descriptors for all activated entities, organized by test cases.
  68. AZStd::unordered_map<AZ::Uuid, AZStd::string> m_moduleComponents; //!< Map of component IDs to module names for all modules.
  69. AZ::IO::Path m_coverageDir; //!< Directory to write coverage data to.
  70. AZ::IO::Path m_coverageFile; //!< Full file path to write coverage data to.
  71. AZStd::string m_testCase; //!< Name of current test case that coverage data is being gathered for.
  72. };
  73. } // namespace PythonCoverage