Back to Repositories

Testing Vagrant Command Corrections in thefuck

This test suite validates the functionality of Vagrant command corrections in the thefuck utility, specifically handling scenarios where VMs need to be started before SSH or RDP connections can be established.

Test Coverage Overview

The test suite comprehensively covers Vagrant command error scenarios and their corrections.

  • Tests SSH and RDP connection attempts to inactive VMs
  • Validates command correction for both named and unnamed VM instances
  • Covers error message pattern matching for different Vagrant states
  • Tests command chaining with ‘&&’ operator

Implementation Analysis

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

  • Implements parameterized test cases for both matching and non-matching scenarios
  • Uses Command class to structure input and expected output
  • Tests both single and multiple command correction options

Technical Details

  • pytest framework for test organization and execution
  • Custom Command type for input validation
  • Parametrized test cases for comprehensive coverage
  • Mock command outputs for various error scenarios

Best Practices Demonstrated

The test suite demonstrates several testing best practices for command-line tool validation.

  • Clear separation of matching and command generation tests
  • Comprehensive error case coverage
  • Efficient test parameterization
  • Explicit test case documentation through descriptive parameters

nvbn/thefuck

tests/rules/test_vagrant_up.py

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


@pytest.mark.parametrize('command', [
    Command('vagrant ssh', 'VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.'),
    Command('vagrant ssh devbox', 'VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.'),
    Command('vagrant rdp',
            'VM must be created before running this command. Run `vagrant up` first.'),
    Command('vagrant rdp devbox',
            'VM must be created before running this command. Run `vagrant up` first.')])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('vagrant ssh', ''),
    Command('vagrant ssh jeff', 'The machine with the name \'jeff\' was not found configured for this Vagrant environment.'),
    Command('vagrant ssh', 'A Vagrant environment or target machine is required to run this command. Run `vagrant init` to create a new Vagrant environment. Or, get an ID of a target machine from `vagrant global-status` to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.'),
    Command('', '')])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('vagrant ssh', 'VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.'), 'vagrant up && vagrant ssh'),
    (Command('vagrant ssh devbox', 'VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.'), ['vagrant up devbox && vagrant ssh devbox', 'vagrant up && vagrant ssh devbox']),
    (Command('vagrant rdp',
             'VM must be created before running this command. Run `vagrant up` first.'), 'vagrant up && vagrant rdp'),
    (Command('vagrant rdp devbox',
             'VM must be created before running this command. Run `vagrant up` first.'), ['vagrant up devbox && vagrant rdp devbox', 'vagrant up && vagrant rdp devbox'])])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command