PythonCoverageEditorSystemComponent.h 3.9 KB

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