Skip to content

Conversation

@maxisbey
Copy link
Contributor

@maxisbey maxisbey commented Nov 6, 2025

Refactored tests/client/test_config.py to use proper unit testing practices instead of executing external commands.

Motivation and Context

The test_command_execution test was executing uv run via subprocess, which violated unit testing principles and caused several issues:

  • Side effects: Modified uv.lock file during test execution (without --frozen flag)
  • External dependencies: Required uv binary, network access, and package availability
  • Wrong abstraction layer: Tested uv CLI behavior rather than the config generation logic
  • Slow and flaky: Subject to network issues and environmental variations

The function under test (update_claude_config) is responsible for JSON file manipulation, not command execution. Tests should verify the generated config structure, not whether external tools work.

How Has This Been Tested?

Replaced 1 integration test with 15 focused unit tests covering:

  • JSON structure and file creation
  • Argument array construction (["run", "--with", "mcp[cli]", ...])
  • Path resolution (relative → absolute)
  • Environment variable merging and preservation
  • Package deduplication
  • Multi-server configurations
  • Error handling (missing config directory)
  • Edge cases (file specs with/without :object suffix)

All tests pass in ~1.5s with no external dependencies or side effects.

Breaking Changes

None. This is purely a test refactor with no changes to production code.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The test suite now properly isolates the unit under test using mocked file system operations. Tests verify the actual contract of update_claude_config():

  • Correct JSON structure generation
  • Proper command/args formatting
  • Path normalization
  • Configuration merging behavior

If end-to-end validation of the generated commands is needed, it should be in a separate integration test suite marked with @pytest.mark.integration and run independently from unit tests.

@maxisbey maxisbey added enhancement New feature or request P3 Nice to haves, rare edge cases labels Nov 6, 2025
The test_command_execution test was an integration test disguised as a unit
test. It executed external commands (uv run) which caused several problems:
- Modified uv.lock file as a side effect
- Required external dependencies (uv binary, network access)
- Was slow and potentially flaky
- Tested the wrong layer (uv CLI instead of config generation)

Replaced with 15 comprehensive unit tests that properly test the
update_claude_config function's actual responsibilities:
- JSON structure generation
- Argument array construction
- Path resolution to absolute paths
- Environment variable merging
- Package deduplication
- Server preservation and updates
- Error handling

Benefits:
- Fast: runs in ~1.5s vs several seconds
- No external dependencies or side effects
- Tests the correct abstraction layer
- Better coverage of edge cases
- More maintainable and reliable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request P3 Nice to haves, rare edge cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants