Back to Repositories

Testing APT List Upgradable Command Detection in TheFuck

This test suite validates the apt_list_upgradable rule functionality in TheFuck project, focusing on package upgrade detection and command correction behaviors. The tests ensure proper handling of package management commands and multilingual support.

Test Coverage Overview

The test suite provides comprehensive coverage of the apt_list_upgradable rule functionality:

  • Tests command matching for both English and German system outputs
  • Validates upgrade detection across different package states
  • Covers various apt-related commands to ensure correct rule application
  • Tests both sudo and non-sudo command variants

Implementation Analysis

The testing approach employs pytest’s parametrization feature to efficiently test multiple scenarios. The implementation uses fixture-based testing with Command objects, separating the command matching logic from command generation functionality.

  • Parametrized test cases for different system outputs
  • Modular test structure with separate match and command generation tests
  • Mock command objects to simulate real-world usage

Technical Details

  • Uses pytest framework with parametrize decorators
  • Implements Command type from thefuck.types
  • Tests both match() and get_new_command() functions
  • Includes multilingual test cases
  • Simulates various apt command outputs

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python:

  • Clear separation of test cases for different functionalities
  • Comprehensive negative testing with non-matching scenarios
  • Efficient test organization using pytest features
  • Maintainable test structure with reusable test data
  • Thorough input validation across different command patterns

nvbn/thefuck

tests/rules/test_apt_list_upgradable.py

            
# -*- coding: utf-8 -*-

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

full_english_output = '''
Hit:1 http://us.archive.ubuntu.com/ubuntu zesty InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu zesty-updates InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu zesty-backports InRelease [89.2 kB]
Hit:4 http://security.ubuntu.com/ubuntu zesty-security InRelease
Hit:5 http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu zesty InRelease
Hit:6 https://download.docker.com/linux/ubuntu zesty InRelease
Hit:7 https://cli-assets.heroku.com/branches/stable/apt ./ InRelease
Fetched 89.2 kB in 0s (122 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
8 packages can be upgraded. Run 'apt list --upgradable' to see them.
'''

match_output = [
    full_english_output,
    'Führen Sie »apt list --upgradable« aus, um sie anzuzeigen.'  # German
]

no_match_output = '''
Hit:1 http://us.archive.ubuntu.com/ubuntu zesty InRelease
Get:2 http://us.archive.ubuntu.com/ubuntu zesty-updates InRelease [89.2 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu zesty-backports InRelease [89.2 kB]
Get:4 http://security.ubuntu.com/ubuntu zesty-security InRelease [89.2 kB]
Hit:5 https://cli-assets.heroku.com/branches/stable/apt ./ InRelease
Hit:6 http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu zesty InRelease
Hit:7 https://download.docker.com/linux/ubuntu zesty InRelease
Get:8 http://us.archive.ubuntu.com/ubuntu zesty-updates/main i386 Packages [232 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu zesty-updates/main amd64 Packages [235 kB]
Get:10 http://us.archive.ubuntu.com/ubuntu zesty-updates/main amd64 DEP-11 Metadata [55.2 kB]
Get:11 http://us.archive.ubuntu.com/ubuntu zesty-updates/main DEP-11 64x64 Icons [32.3 kB]
Get:12 http://us.archive.ubuntu.com/ubuntu zesty-updates/universe amd64 Packages [156 kB]
Get:13 http://us.archive.ubuntu.com/ubuntu zesty-updates/universe i386 Packages [156 kB]
Get:14 http://us.archive.ubuntu.com/ubuntu zesty-updates/universe amd64 DEP-11 Metadata [175 kB]
Get:15 http://us.archive.ubuntu.com/ubuntu zesty-updates/universe DEP-11 64x64 Icons [253 kB]
Get:16 http://us.archive.ubuntu.com/ubuntu zesty-updates/multiverse amd64 DEP-11 Metadata [5,840 B]
Get:17 http://us.archive.ubuntu.com/ubuntu zesty-backports/universe amd64 DEP-11 Metadata [4,588 B]
Get:18 http://security.ubuntu.com/ubuntu zesty-security/main amd64 DEP-11 Metadata [12.7 kB]
Get:19 http://security.ubuntu.com/ubuntu zesty-security/main DEP-11 64x64 Icons [17.6 kB]
Get:20 http://security.ubuntu.com/ubuntu zesty-security/universe amd64 DEP-11 Metadata [21.6 kB]
Get:21 http://security.ubuntu.com/ubuntu zesty-security/universe DEP-11 64x64 Icons [47.7 kB]
Get:22 http://security.ubuntu.com/ubuntu zesty-security/multiverse amd64 DEP-11 Metadata [208 B]
Fetched 1,673 kB in 0s (1,716 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
'''


@pytest.mark.parametrize('output', match_output)
def test_match(output):
    assert match(Command('sudo apt update', output))


@pytest.mark.parametrize('command', [
    Command('apt-cache search foo', ''),
    Command('aptitude search foo', ''),
    Command('apt search foo', ''),
    Command('apt-get install foo', ''),
    Command('apt-get source foo', ''),
    Command('apt-get clean', ''),
    Command('apt-get remove', ''),
    Command('apt-get update', ''),
    Command('sudo apt update', no_match_output)
])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('output', match_output)
def test_get_new_command(output):
    new_command = get_new_command(Command('sudo apt update', output))
    assert new_command == 'sudo apt list --upgradable'

    new_command = get_new_command(Command('apt update', output))
    assert new_command == 'apt list --upgradable'