Skip to content

Conversation

cidrblock
Copy link
Contributor

@cidrblock cidrblock commented Aug 4, 2025

Enable Native Collection Support Through Auto-Detection

This PR enables Molecule to operate natively from Ansible collection roots by automatically detecting collections and encouraging proper scenario placement.

How It Works

When executed from a collection root (directory containing galaxy.yml with required name and namespace fields), Molecule now:

  • Automatically detects the collection and logs the detected collection name
  • Encourages best practices by defaulting to search for scenarios in extensions/molecule/*/molecule.yml
  • Supports legacy setups with a migration warning when molecule/ exists in the collection root
  • Respects user overrides via the MOLECULE_GLOB environment variable

Behavior Details

  1. Environment Override: If MOLECULE_GLOB is set, that pattern is used (highest priority)

  2. No Collection: If no valid galaxy.yml is found, use standard molecule/*/molecule.yml pattern

  3. Legacy Collection: If collection detected AND molecule/ directory exists in root:

    • Warns: "Molecule scenarios should migrate to 'extensions/molecule'"
    • Uses standard molecule/*/molecule.yml pattern for backward compatibility
  4. Modern Collection: If collection detected but no molecule/ in root:

    • Logs: "Collection 'namespace.name' detected"
    • Logs: "Scenarios will be used from 'extensions/molecule'"
    • Uses collection pattern: extensions/molecule/*/molecule.yml

Benefits

  • Zero Configuration: Works automatically when galaxy.yml is present
  • Backward Compatible: Existing collections with molecule/ in root continue working
  • Encourages Best Practices: Guides users toward the recommended extensions/molecule/ structure
  • Clear Migration Path: Provides helpful warnings for legacy setups
  • User Control: Can be overridden via environment variable if needed

Example Usage

# In a collection root with extensions/molecule/ structure
$ molecule test
INFO Collection 'community.general' detected
INFO Scenarios will be used from 'extensions/molecule'

# In a collection root with legacy molecule/ structure  
$ molecule test
WARNING Molecule scenarios should migrate to 'extensions/molecule'
# Uses molecule/*/molecule.yml for compatibility

Files Changed

  • Core Logic: src/molecule/util.py - Implements the auto-detection algorithm
  • Integration: Various command files updated to use the new detection
  • Tests: Updated unit and integration tests to match new behavior
  • Sample Structure: Moved test collection to proper extensions/molecule/ layout

This change makes Molecule collection-aware by default while maintaining full backward compatibility and providing clear guidance for modernization.

Copilot

This comment was marked as outdated.

Copilot

This comment was marked as outdated.

Copilot

This comment was marked as outdated.

- Provide both message and code arguments as required by constructor
- Add proper exception chaining with 'from exc'
- Document raised exception in docstring

Fixes linting errors: B904, DOC501, DOC503, E1120, call-arg, arg-type
Tests cover all major functionality:
- VCS root search (highest priority)
- Collection extensions directory search
- Home directory search (lowest priority)
- Search priority ordering
- File not found scenarios
- Path filename extraction
- Collection without extensions directory fallback

Provides 100% coverage for the lookup_config_file function
Replace unused 'default=None' lambda arguments with '_' to satisfy ARG005 rule.
No functional changes, only style fixes.
The find_vcs_root function expects a 'default' keyword argument,
but test mocks were using lambda _ which doesn't accept it.
Updated all mocks to use lambda default=None to match actual signature.
The default parameter is required for function signature compatibility
but not used in test mocks, so suppress the unused argument warnings.
@cidrblock cidrblock marked this pull request as ready for review August 7, 2025 00:26
@cidrblock cidrblock merged commit e50f4c6 into ansible:main Aug 8, 2025
21 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in 🧰 devtools project board Aug 8, 2025
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.

2 participants