Back to Repositories

Testing Virtual Environment Command Correction in TheFuck

This test suite validates the functionality of virtual environment management commands in The Fuck tool, specifically focusing on the ‘workon’ command behavior and environment name corrections. The tests ensure proper handling of environment names and fallback creation of new environments.

Test Coverage Overview

The test suite provides comprehensive coverage of the ‘workon’ command functionality in virtual environment management.

Key areas tested include:
  • Matching incorrect environment names against existing ones
  • Handling of environment name variations (with hyphens vs underscores)
  • Fallback to creating new environments when no match is found
  • Edge cases with similar environment names

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management and mocking capabilities for environment simulation.

Technical implementation details:
  • Uses pytest fixtures for environment mocking
  • Implements parametrized test cases for various command scenarios
  • Validates both matching and non-matching cases
  • Tests command correction suggestions

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • Mock objects for environment simulation
  • Command type handling from thefuck.types
  • Automated fixture setup with autouse=True
  • Parametrized test cases for multiple scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Separation of matching and command generation tests
  • Comprehensive edge case coverage
  • Efficient use of fixtures and parametrization
  • Clear test case organization
  • Focused test functions with single responsibilities

nvbn/thefuck

tests/rules/test_workon_doesnt_exists.py

            
import pytest
from thefuck.rules.workon_doesnt_exists import match, get_new_command
from thefuck.types import Command


@pytest.fixture(autouse=True)
def envs(mocker):
    return mocker.patch(
        'thefuck.rules.workon_doesnt_exists._get_all_environments',
        return_value=['thefuck', 'code_view'])


@pytest.mark.parametrize('script', [
    'workon tehfuck', 'workon code-view', 'workon new-env'])
def test_match(script):
    assert match(Command(script, ''))


@pytest.mark.parametrize('script', [
    'workon thefuck', 'workon code_view', 'work on tehfuck'])
def test_not_match(script):
    assert not match(Command(script, ''))


@pytest.mark.parametrize('script, result', [
    ('workon tehfuck', 'workon thefuck'),
    ('workon code-view', 'workon code_view'),
    ('workon zzzz', 'mkvirtualenv zzzz')])
def test_get_new_command(script, result):
    assert get_new_command(Command(script, ''))[0] == result