Testing Generated Templates
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Meltano Extension Developer Kit (EDK) includes a robust testing framework for validating the templates used to generate new extensions. This process ensures that the copier-based template produces high-quality, valid, and lint-free Python code that adheres to the EDK's structural requirements. By automating the generation and validation of sample extensions, the project maintains the integrity of its scaffolding tools.
Testing generated templates involves a multi-stage process: rendering a template into a temporary directory using predefined data, performing static type checking with mypy, and enforcing style and formatting standards with ruff. This ensures that any extension initialized via the EDK starts with a functional and compliant codebase.
Sources: README.md, tests/copier_template/test_generated.py:1-12
Template Configuration and Variables
The generation process is driven by copier.yml, which defines the input variables required to scaffold a new extension. These variables control naming conventions, descriptions, and the specific CLI tools being wrapped by the extension.
Core Template Variables
| Variable | Type | Description | Default Value |
|---|---|---|---|
extension_name | string | The name of the extension (e.g., MyExtension) | N/A |
extension_id | string | The unique ID for the extension | {{ extension_name_lower }}-ext |
library_name | string | The Python package name | {{ extension_id.replace('-', '_') }} |
wrapper_target_name | string | The CLI command path being wrapped | N/A |
cli_prefix | string | The prefix for CLI commands | {{ extension_name_lower }} |
Sources: copier.yml:1-35
Validation Workflow
The testing logic, primarily located in tests/copier_template/test_generated.py, follows a structured sequence to ensure the generated code is production-ready.
Automated Test Sequence
The following diagram illustrates the lifecycle of a template validation test:
This flow ensures that the generated output directory is isolated and thoroughly inspected before being discarded.
Sources: tests/copier_template/test_generated.py:19-75
Static Analysis and Linting
The EDK utilizes specific tools to validate the generated output:
- Mypy: Executes static type checking on the generated files. A non-zero exit status indicates type inconsistencies in the template output.
- Ruff Check: Validates linting rules including
E(Errors),F(Pyflakes),W(Warnings), andQ(Quotes). - Ruff Format: Ensures the generated code adheres to the project's formatting standards using the
--checkflag to verify compliance without modifying files.
Sources: tests/copier_template/test_generated.py:59-75, pyproject.toml:96-107
Extension Interface Compatibility
Generated templates are designed to implement the ExtensionBase class. Testing confirms that generated extensions correctly override and implement necessary methods such as invoke and describe.
Extension Class Architecture
Testing ensures the generated class correctly populates the Describe model, which includes available commands and capabilities like pass_through_cli.
Sources: meltano/edk/extension.py:32-85, tests/test_meltano_edk.py:13-33, meltano/edk/models.py:7-41
Conclusion
Testing generated templates is a critical component of the Meltano EDK CI/CD pipeline. By programmatically invoking copier and subjecting the results to mypy and ruff, the project guarantees that the "new extension" experience is seamless and leads to technically sound code. This validation covers both the structural integrity of the files and their adherence to the ExtensionBase interface.