Back to Repositories

Testing AWS CLI Command Correction Logic in TheFuck

This test suite validates the AWS CLI command correction functionality in TheFuck, focusing on handling misspelled commands and subcommands. It ensures the application can properly identify and suggest corrections for invalid AWS CLI inputs.

Test Coverage Overview

The test suite provides comprehensive coverage of AWS CLI command correction scenarios:

  • Command-level misspellings (e.g., ‘dynamdb’ → ‘dynamodb’)
  • Subcommand misspellings (e.g., ‘scn’ → ‘scan’)
  • Multiple suggestion handling (e.g., ‘t-item’ → ‘put-item’/’get-item’)
  • Cases with no available suggestions

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management. It implements two main test functions: match() for command validation and get_new_command() for correction generation. The implementation uses fixture data to simulate various AWS CLI error responses.

Technical Details

  • Testing Framework: pytest
  • Mock Data: Predefined AWS CLI error messages
  • Test Components: Command matching and correction generation
  • Custom Types: Command class for input handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Parameterized testing for multiple scenarios
  • Clear separation of test cases
  • Comprehensive edge case coverage
  • Mock data usage for consistent testing
  • Explicit assertion statements

nvbn/thefuck

tests/rules/test_aws_cli.py

            
import pytest

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


no_suggestions = '''\
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument command: Invalid choice, valid choices are:

dynamodb                                 | dynamodbstreams
ec2                                      | ecr
'''


misspelled_command = '''\
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument command: Invalid choice, valid choices are:

dynamodb                                 | dynamodbstreams
ec2                                      | ecr


Invalid choice: 'dynamdb', maybe you meant:

  * dynamodb
'''


misspelled_subcommand = '''\
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:

query                                    | scan
update-item                              | update-table


Invalid choice: 'scn', maybe you meant:

  * scan
'''


misspelled_subcommand_with_multiple_options = '''\
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:

describe-table                           | get-item
list-tables                              | put-item


Invalid choice: 't-item', maybe you meant:

  * put-item
  * get-item
'''


@pytest.mark.parametrize('command', [
    Command('aws dynamdb scan', misspelled_command),
    Command('aws dynamodb scn', misspelled_subcommand),
    Command('aws dynamodb t-item',
            misspelled_subcommand_with_multiple_options)])
def test_match(command):
    assert match(command)


def test_not_match():
    assert not match(Command('aws dynamodb invalid', no_suggestions))


@pytest.mark.parametrize('command, result', [
    (Command('aws dynamdb scan', misspelled_command),
     ['aws dynamodb scan']),
    (Command('aws dynamodb scn', misspelled_subcommand),
     ['aws dynamodb scan']),
    (Command('aws dynamodb t-item',
             misspelled_subcommand_with_multiple_options),
     ['aws dynamodb put-item', 'aws dynamodb get-item'])])
def test_get_new_command(command, result):
    assert get_new_command(command) == result