2
0

PythonCoverageEditorSystemComponent.h 3.9 KB

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