Skip to content

Add module scaffolding script and test infrastructure - #12

Merged
antosubash merged 7 commits into
mainfrom
claude/add-module-scaffolding-cli-73wQ3
Apr 14, 2026
Merged

Add module scaffolding script and test infrastructure#12
antosubash merged 7 commits into
mainfrom
claude/add-module-scaffolding-cli-73wQ3

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

Add a comprehensive module scaffolding system that enables developers to quickly generate new modules for the Simple Module Python framework with a single command. This includes a production-ready new_module.py script, extensive test coverage, and updates to the test infrastructure to support dynamically discovered modules.

Key Changes

New Module Scaffolding Script (scripts/new_module.py)

  • 802-line scaffolding tool that generates complete module structure with:

    • Module directory layout under modules/<name>/
    • Full pyproject.toml with entry points and dependencies
    • SQLAlchemy models with audit mixins
    • Service layer with complete CRUD operations
    • Pydantic schemas for API contracts
    • FastAPI endpoints (REST API + Inertia views)
    • Dependency injection setup
    • Comprehensive test suite template
    • Module registration and permission definitions
  • Smart naming conventions:

    • Validates module names (lowercase, alphanumeric, underscores)
    • Auto-converts snake_case to PascalCase for classes
    • Naive singularization for entity names (e.g., ordersOrder)
  • Automatic project file updates:

    • Registers module in host/pyproject.toml dependencies
    • Adds module paths to root pyproject.toml for type-checking and tests
    • Uses regex-based insertion to find correct locations in existing files

Comprehensive Test Suite (scripts/tests/test_new_module.py)

  • 439 lines of tests covering:
    • Name validation (valid/invalid patterns)
    • Naming transformations (snake_case → PascalCase, pluralization)
    • Full scaffolding integration tests
    • Generated file syntax validation (AST parsing, TOML parsing)
    • Template content verification (CRUD methods, endpoints, permissions)
    • Project file update logic with edge cases
    • CLI entry point behavior

Test Infrastructure Updates (conftest.py)

  • Dynamic module discovery: Added _ensure_models_imported() to import all discovered module models
  • Unified table creation: New _create_all_tables() function creates tables for all module bases in a single transaction
  • Caching: Used @lru_cache to avoid redundant module discovery during test runs

Build System Updates (Makefile)

  • Added new-module target for convenient scaffolding via make new-module name=<module_name>

Notable Implementation Details

  • Template generation: Uses f-strings with textwrap.dedent for clean, readable generated code
  • Insertion strategy: _insert_after_last_match() helper finds the last matching line in TOML files to insert new entries in the correct location, handling edge cases like multiple existing modules
  • Error handling: Validates module names early, prevents overwriting existing modules, warns when insertion points cannot be found
  • Generated code quality: All generated Python files are syntactically valid, all TOML is parseable, and templates follow framework conventions
  • Test fixtures: Comprehensive pytest fixtures (module_root, workspace, scaffolded_orders) enable isolated testing of scaffolding logic

Usage

python scripts/new_module.py orders
# or
make new-module name=orders

This generates a fully functional module with models, services, API endpoints, views, and tests ready for customization.

https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj

Automates new module creation, eliminating the need to manually create
14+ files and update config. The script generates the full module
structure (pyproject.toml, module class, models, service, schemas,
contracts, endpoints, deps, tests) and registers the module in
host/pyproject.toml and root pyproject.toml.
Also updates conftest.py to auto-discover module Base classes via
entry points, so new modules' tables are automatically created in
the test database.
https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj
- conftest.py: reuse framework's discover_modules() + all_module_bases
instead of reimplementing entry point discovery; cache with lru_cache;
batch create_all into single run_sync; extract _create_all_tables helper
- new_module.py: replace hardcoded anchor strings with last-entry
insertion via _insert_before_last(); add warnings on missing anchors
- test_new_module.py: extract repeated setup into module_root fixture
https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj
Updates the scaffolding to match PR #8's layout change that removed
the src/ directory from all workspace packages:
- Generate files at modules/<name>/sm_<name>/ instead of
modules/<name>/src/sm_<name>/
- Update root pyproject.toml to insert modules/<name> in ty paths
(no /src suffix)
- Rewrite insertion helpers with cleaner regex-based approach that
correctly handles the new host/pyproject.toml structure with
multiple sm-* dependencies
https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj
Adds coverage for previously untested code paths and behaviors:
- _insert_after_last_match helper: 4 direct unit tests
- create_file utility: 3 tests (content, parent dir creation, dedent)
- main() CLI entry point: 2 tests (happy path + invalid name)
- End-to-end subprocess test: verifies script runs standalone
New TestUpdateRootPyproject cases:
- Realistic multi-module pyproject.toml matching real repo
- Skips when already present
- Warns to stderr when no insertion point found
New TestGeneratedFilesSyntaxValidity:
- All generated Python files parse with ast.parse
- Generated pyproject.toml parses with tomllib
- Compound names (blog_posts) produce valid TOML
New TestGeneratedTemplateContent covers:
- service.py has full CRUD methods
- endpoints/api.py has all REST endpoints with correct status codes
- endpoints/views.py uses Inertia properly
- deps.py provides DI function
- contracts/service.py defines Protocol
- contracts/__init__.py exports public API
- module.py registers routes and permissions
- models.py uses AuditMixin with correct tablename
- test_<name>.py contains all expected test classes
https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj
- Add scaffolded_orders fixture: scaffold_module() now runs once per test
rather than 10x in TestGeneratedTemplateContent
- Add workspace fixture: deduplicates the host/pyproject + root pyproject
setup between TestMainCLI tests
- Extract MINIMAL_HOST_PYPROJECT and MINIMAL_ROOT_PYPROJECT constants
- TestCreateFile: replace inline monkeypatch with the existing module_root
fixture (3 tests collapsed to clean one-liners)
- TestScaffoldModule: use scaffolded_orders fixture; eliminate repeated
`module_root / "modules" / "orders" / "sm_orders"` path strings
- TestGeneratedFilesSyntaxValidity: reuse scaffolded_orders for the
orders-based tests
- Drop TestCLIAsSubprocess: TestMainCLI::test_main_invokes_full_pipeline
with capsys covers the same ground without a subprocess fork
- Move `import new_module` to module level (reused by all fixtures)
- Strip narrative comments
49 tests → 48 tests, suite time 0.58s → 0.42s
https://claude.ai/code/session_01G5HHXJfxAQ53grAwiyUzyj
@antosubash
antosubash merged commit 737e490 into mainApr 14, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@antosubash@claude