Skip to content

Create shared test utilities module for command handlers#84

Merged
josecelano merged 3 commits intomainfrom
copilot/create-shared-test-utilities
Oct 29, 2025
Merged

Create shared test utilities module for command handlers#84
josecelano merged 3 commits intomainfrom
copilot/create-shared-test-utilities

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

Test utilities were duplicated across create/tests/fixtures.rs and inconsistently implemented in destroy/tests/. This created maintenance burden and inconsistent test patterns.

Changes

New shared test utilities (src/presentation/commands/tests/mod.rs):

  • TestContext - Manages temporary directories with automatic cleanup
  • create_valid_config() - Generates valid environment configurations
  • create_invalid_json_config() - Creates malformed JSON for error testing
  • create_config_with_invalid_name() - Invalid environment name for validation testing
  • create_config_with_missing_keys() - Non-existent SSH keys for file validation testing

Test file updates:

  • create/tests/integration.rs - 8 tests migrated to shared utilities
  • create/tests/template.rs - 4 tests migrated to shared utilities
  • destroy/tests/integration.rs - 5 tests migrated to shared utilities
  • Removed duplicate create/tests/fixtures.rs (162 lines)

Usage

use crate::presentation::commands::tests::{TestContext, create_valid_config};

#[test]
fn it_should_create_environment_from_valid_config() {
    let context = TestContext::new();
    let config_path = create_valid_config(context.working_dir(), "test-env");
    
    let result = handle_environment_creation(&config_path, context.working_dir());
    assert!(result.is_ok());
    // Temp directory cleaned up automatically when context drops
}

Net reduction of ~190 lines with improved consistency across all command tests.

Original prompt

This section details on the original issue you should resolve

<issue_title>Create Shared Test Utilities Module</issue_title>
<issue_description>Parent Issue: #63
Type: 🔨 Structural Improvement
Impact: 🟢🟢 Medium
Effort: 🔵🔵 Medium
Priority: P1

Problem

Test utilities are duplicated and inconsistent:

  • create/tests/ has comprehensive fixtures module
  • destroy/tests/ lacks test helpers
  • Common patterns like creating temp directories, generating test configs are repeated

Proposed Solution

Create shared test utilities at src/presentation/commands/tests/mod.rs:

//! Shared test utilities for command handlers

use std::path::{Path, PathBuf};
use std::sync::Arc;
use tempfile::TempDir;

/// Test context with temp directory and common test dependencies
pub struct TestContext {
    _temp_dir: TempDir,
    pub working_dir: PathBuf,
    pub repository: Arc<MockEnvironmentRepository>,
    pub clock: Arc<MockClock>,
}

impl TestContext {
    pub fn new() -> Self {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let working_dir = temp_dir.path().to_path_buf();
        
        let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
        let repository = Arc::new(repository_factory.create(working_dir.clone()));
        
        let clock = Arc::new(MockClock::new(chrono::Utc::now()));
        
        Self { _temp_dir: temp_dir, working_dir, repository, clock }
    }
    
    pub fn working_dir(&self) -> &Path {
        &self.working_dir
    }
}

/// Create a valid test configuration file
pub fn create_test_config(path: &Path, env_name: &str) -> PathBuf {
    // Implementation
}

/// Create an invalid JSON configuration file for error testing
pub fn create_invalid_json_config(path: &Path) -> PathBuf {
    // Implementation
}

/// Create a configuration with invalid environment name
pub fn create_config_with_invalid_name(path: &Path) -> PathBuf {
    // Implementation
}

Then use in tests:

use crate::presentation::commands::tests::{TestContext, create_test_config};

#[test]
fn it_should_create_environment_from_valid_config() {
    let context = TestContext::new();
    let config_path = create_test_config(context.working_dir(), "test-env");
    
    let result = handle_environment_creation(&config_path, context.working_dir());
    assert!(result.is_ok());
}

Benefits

  • ✅ Reduces test code duplication
  • ✅ Consistent test patterns across commands
  • ✅ Easier to write new tests
  • ✅ Shared improvements benefit all tests

Implementation Checklist

  • Create src/presentation/commands/tests/mod.rs
  • Implement TestContext struct with temp dir management
  • Migrate create_test_config() from fixtures.rs
  • Migrate create_invalid_json_config() from fixtures.rs
  • Migrate create_config_with_invalid_name() from fixtures.rs
  • Add comprehensive documentation
  • Update create/tests/ to use shared utilities
  • Update destroy/tests/ to use shared utilities
  • Remove duplicate code from old test files
  • Verify all tests pass
  • Run linter

Acceptance Criteria

  • Shared test utilities module exists
  • TestContext provides consistent test setup
  • Common test config generators are shared
  • Both create and destroy tests use shared utilities
  • No duplicate test helper code remains
  • All tests pass: cargo test presentation::commands
  • Pre-commit checks pass: ./scripts/pre-commit.sh
  • Code follows project conventions

Related Documentation

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 29, 2025 12:04
- Created `src/presentation/commands/tests/mod.rs` with shared test utilities
- Implemented `TestContext` struct with automatic temp directory management
- Implemented reusable test config generators: `create_valid_config`, `create_invalid_json_config`, `create_config_with_invalid_name`, `create_config_with_missing_keys`
- Added comprehensive documentation following module organization guidelines
- Updated `create/tests/integration.rs` to use shared utilities
- Updated `create/tests/template.rs` to use shared utilities
- Updated `destroy/tests/integration.rs` to use shared utilities
- Removed duplicate `create/tests/fixtures.rs` file
- All tests pass successfully

Co-authored-by: josecelano <[email protected]>
- Added #[must_use] attributes to all config generator functions
- Added #[must_use] to TestContext methods
- Added Panics documentation sections for all functions that can panic
- Updated doc examples to use no_run to avoid test execution in doctests
- Renamed `context` variables to `test_context` to avoid similar_names warnings
- Renamed `content` to `file_content` where needed
- Renamed tests submodule to `unit_tests` to avoid module_inception warning
- Formatted all code with rustfmt
- All linters pass successfully

Co-authored-by: josecelano <[email protected]>
Copilot AI changed the title [WIP] Create shared test utilities module Create shared test utilities module for command handlers Oct 29, 2025
Copilot AI requested a review from josecelano October 29, 2025 12:22
Copy link
Member

@josecelano josecelano left a comment

Choose a reason for hiding this comment

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

ACK 045f131

@josecelano josecelano marked this pull request as ready for review October 29, 2025 13:12
@josecelano josecelano merged commit 0d56c69 into main Oct 29, 2025
32 checks passed
Sign up for free to 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.

Create Shared Test Utilities Module

2 participants