Back to Repositories

Testing Docker Image Removal Error Handling in thefuck

This test suite validates Docker image removal functionality in thefuck, specifically handling scenarios where images are being used by running containers. It ensures proper error handling and command correction for Docker image deletion conflicts.

Test Coverage Overview

The test suite provides comprehensive coverage of Docker image removal scenarios, focusing on error handling when attempting to delete images used by running containers.

Key areas tested include:
  • Matching Docker image removal error patterns
  • Handling non-Docker commands
  • Command not found scenarios
  • Container-image dependency resolution

Implementation Analysis

The testing approach utilizes pytest framework to validate the match and get_new_command functions. It implements pattern matching for Docker daemon error responses and verifies command transformation logic.

Key patterns include:
  • Error response parsing
  • Command string manipulation
  • Container ID extraction
  • Command chaining validation

Technical Details

Testing infrastructure includes:
  • Python unittest framework
  • Mock Docker error responses
  • Command object testing
  • Assertion-based validation
  • Error scenario simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, comprehensive error scenario coverage, and clear test naming conventions.

Notable practices:
  • Separate positive and negative test cases
  • Explicit error message validation
  • Command transformation verification
  • Clean test organization

nvbn/thefuck

tests/rules/test_docker_image_being_used_by_container.py

            
from thefuck.rules.docker_image_being_used_by_container import match, get_new_command
from thefuck.types import Command


def test_match():
    err_response = """Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image is being used by running container e5e2591040d1"""
    assert match(Command('docker image rm -f cd809b04b6ff', err_response))


def test_not_match():
    err_response = 'bash: docker: command not found'
    assert not match(Command('docker image rm -f cd809b04b6ff', err_response))


def test_not_docker_command():
    err_response = """Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image is being used by running container e5e2591040d1"""
    assert not match(Command('git image rm -f cd809b04b6ff', err_response))


def test_get_new_command():
    err_response = """
        Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image
        is being used by running container e5e2591040d1
        """
    result = get_new_command(Command('docker image rm -f cd809b04b6ff', err_response))
    expected = 'docker container rm -f e5e2591040d1 && docker image rm -f cd809b04b6ff'
    assert result == expected