Back to Repositories

Testing Azure CLI Command Correction in TheFuck

This test suite validates the Azure CLI command correction functionality in TheFuck, focusing on handling misspelled commands and subcommands in the Azure CLI interface. The tests ensure proper error detection and command suggestion generation.

Test Coverage Overview

The test suite provides comprehensive coverage of Azure CLI command correction scenarios.

Key areas tested include:
  • Command group misspellings (providers vs provider)
  • Subcommand misspellings (lis vs list)
  • Error message pattern matching
  • Command suggestion accuracy

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple command scenarios with minimal code duplication.

Notable patterns include:
  • Parametrized test cases for different command variations
  • Separate test cases for matching and command generation
  • Mock command objects to simulate CLI interactions

Technical Details

Testing infrastructure includes:
  • Pytest framework for test organization
  • Command object mocking
  • Static test data for error messages
  • Parametrized test functions
  • Assertion-based validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear separation of test cases
  • Comprehensive error scenario coverage
  • Efficient test data organization
  • Maintainable test structure
  • Focused test scope per function

nvbn/thefuck

tests/rules/test_az_cli.py

            
import pytest

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


no_suggestions = '''\
az provider: error: the following arguments are required: _subcommand
usage: az provider [-h] {list,show,register,unregister,operation} ...
'''


misspelled_command = '''\
az: 'providers' is not in the 'az' command group. See 'az --help'.

The most similar choice to 'providers' is:
    provider
'''

misspelled_subcommand = '''\
az provider: 'lis' is not in the 'az provider' command group. See 'az provider --help'.

The most similar choice to 'lis' is:
    list
'''


@pytest.mark.parametrize('command', [
    Command('az providers', misspelled_command),
    Command('az provider lis', misspelled_subcommand)])
def test_match(command):
    assert match(command)


def test_not_match():
    assert not match(Command('az provider', no_suggestions))


@pytest.mark.parametrize('command, result', [
    (Command('az providers list', misspelled_command), ['az provider list']),
    (Command('az provider lis', misspelled_subcommand), ['az provider list'])
])
def test_get_new_command(command, result):
    assert get_new_command(command) == result