Skip to content

Conversation

@cidrblock
Copy link
Contributor

@cidrblock cidrblock commented Aug 15, 2025

Add shared_state configuration option with CLI override support

User Benefit: Teams can now set shared_state: true in molecule.yml to enable shared state by default for multi-scenario testing, while retaining the ability to override via --shared-state/--no-shared-state CLI flags.

This is neccessary to keep the use of shared-state in the molecule configuration for CI and other users of the content.

Implementation Flow:

  1. Schema & Types: Added shared_state boolean to JSON schema, DEFAULT_CONFIG, and TypedDict definitions
  2. CLI Processing: All Molecule subcommands inherit shared_state via COMMON_OPTIONS, building identical command_args structures
  3. Config Override Logic: New _apply_cli_overrides() method in Config.__init__() uses Click's get_parameter_source() to detect explicit CLI usage vs defaults, applying CLI values only when explicitly provided
  4. Execution Flow:
    • scenarios.shared_state property determines if any selected scenario requires shared infrastructure
    • execute_subcommand_default() runs global create/destroy when CLI flag is set OR any scenario config has shared_state: true
    • execute_scenario() receives global shared state context to skip individual create/destroy actions
  5. Single Source of Truth: CLI overrides are baked into Config.config dictionary during instantiation, making Config.shared_state property the authoritative source

CLI Scope: The --shared-state/--no-shared-state flags apply to all scenarios in the current execution, overriding individual scenario configurations. This ensures consistent shared state behavior across multi-scenario runs.

Technical Details:

  • Uses click.core.ParameterSource.COMMANDLINE to distinguish explicit CLI usage from defaults
  • Maintains backward compatibility: existing CLI-only behavior unchanged
  • All 12 Molecule subcommands support shared_state through common option inheritance
  • Comprehensive test coverage validates all CLI/config override combinations

Migration: Fully backward compatible. Existing workflows continue to work unchanged.

Per-file Changes

Core Configuration

  • src/molecule/data/molecule.json: Added shared_state boolean field to JSON schema
  • src/molecule/types.py: Added shared_state to DefaultConfigData and ConfigData TypedDict definitions
  • src/molecule/constants.py: Added shared_state: False to DEFAULT_CONFIG

Configuration Processing

  • src/molecule/config.py:
    • Added _apply_cli_overrides() method with Click parameter source detection
    • Added shared_state property as single source of truth
    • Updated state property to use self.shared_state

Execution Logic

  • src/molecule/command/base.py:
    • Updated execute_subcommand_default() to accept keyword-only shared_state parameter
    • Modified _run_scenarios() to use scenarios.shared_state property
    • Updated execute_scenario() to receive shared_state context parameter
  • src/molecule/scenarios.py: Added shared_state property to check if any selected scenario requires shared infrastructure
  • src/molecule/command/test.py: Removed temporary CLI override tracking

Test Coverage

  • tests/unit/command/test_base.py: Added comprehensive parameterized test covering all 9 CLI/config combinations
  • tests/unit/command/test_reset.py: Updated mocks to support new shared_state property and function signatures
  • tests/unit/test_migration_logic.py: Updated expected configurations to include shared_state default

- Add shared_state boolean to JSON schema, types, and constants
- Implement CLI override logic using Click ParameterSource detection
- Update execution flow to support config-driven shared state
- Add comprehensive test coverage for all CLI/config combinations
- Maintain full backward compatibility
- Resolve linter error about missing arguments to main() function
- Use Click's proper invocation mechanism instead of direct call
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds shared_state configuration option support to Molecule, allowing teams to enable shared state infrastructure by default in molecule.yml while maintaining CLI override capabilities. This enables consistent shared state behavior for CI/CD environments while preserving existing CLI workflows.

Key changes:

  • Added shared_state boolean field to configuration schema, types, and default values
  • Implemented CLI override logic using Click's parameter source detection to apply explicit CLI flags over config file values
  • Updated execution flow to use shared state when either CLI flags are set OR any scenario has shared_state: true in configuration

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/molecule/data/molecule.json Added shared_state boolean field to JSON schema with default false
src/molecule/types.py Added shared_state to TypedDict definitions for configuration types
src/molecule/constants.py Added shared_state: False to DEFAULT_CONFIG
src/molecule/config.py Added CLI override logic and shared_state property as single source of truth
src/molecule/scenarios.py Added shared_state property to check if any scenario requires shared infrastructure
src/molecule/command/base.py Updated execution functions to accept and use shared_state parameter consistently
tests/unit/test_migration_logic.py Updated expected configurations to include new shared_state default
tests/unit/command/test_reset.py Updated test fixtures to support new function signatures and shared_state property
tests/unit/command/test_base.py Added comprehensive test covering all CLI/config override combinations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@Qalthos Qalthos enabled auto-merge (squash) August 15, 2025 15:17
@Qalthos Qalthos merged commit db4ea18 into ansible:main Aug 15, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants