Back to Repositories

Testing APT Command Upgrade Detection in TheFuck

This test suite validates the apt_upgrade rule functionality in TheFuck, a command-line tool that corrects mistyped console commands. The tests specifically focus on the automatic conversion of ‘apt list –upgradable’ commands to ‘apt upgrade’ when updates are available.

Test Coverage Overview

The test suite provides comprehensive coverage of the apt_upgrade command correction functionality:

  • Tests matching logic for both regular and sudo commands
  • Verifies correct handling of cases with and without available upgrades
  • Validates command transformation accuracy
  • Covers edge cases with different output formats

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case handling. The implementation follows a clear pattern of separating matching logic from command generation:

  • Modular test structure with distinct match and command generation tests
  • Parametrized testing for multiple command variations
  • Mock command outputs for consistent testing

Technical Details

Testing infrastructure includes:

  • Pytest framework for test execution
  • Command object mocking for input simulation
  • Static test data for consistent validation
  • Custom assertion patterns for command matching

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear separation of test cases
  • Comprehensive positive and negative testing
  • Effective use of pytest decorators
  • Maintainable test data organization
  • Consistent assertion patterns

nvbn/thefuck

tests/rules/test_apt_upgrade.py

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

match_output = '''
Listing... Done
heroku/stable 6.15.2-1 amd64 [upgradable from: 6.14.43-1]
resolvconf/zesty-updates,zesty-updates 1.79ubuntu4.1 all [upgradable from: 1.79ubuntu4]
squashfs-tools/zesty-updates 1:4.3-3ubuntu2.17.04.1 amd64 [upgradable from: 1:4.3-3ubuntu2]
unattended-upgrades/zesty-updates,zesty-updates 0.93.1ubuntu2.4 all [upgradable from: 0.93.1ubuntu2.3]
'''

no_match_output = '''
Listing... Done
'''


def test_match():
    assert match(Command('apt list --upgradable', match_output))
    assert match(Command('sudo apt list --upgradable', match_output))


@pytest.mark.parametrize('command', [
    Command('apt list --upgradable', no_match_output),
    Command('sudo apt list --upgradable', no_match_output)
])
def test_not_match(command):
    assert not match(command)


def test_get_new_command():
    new_command = get_new_command(Command('apt list --upgradable', match_output))
    assert new_command == 'apt upgrade'

    new_command = get_new_command(Command('sudo apt list --upgradable', match_output))
    assert new_command == 'sudo apt upgrade'