Back to Repositories

Testing URL Detection and Command Transformation in TheFuck Open Rule

This test suite validates the URL handling and file opening functionality in The Fuck’s open rule. It verifies URL detection, command matching, and new command generation for various file and URL scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of URL detection and file opening commands across different scenarios.

  • Tests URL validation for various domain extensions (.com, .edu, .info, etc.)
  • Validates different open commands (open, xdg-open, gnome-open, kde-open)
  • Covers file creation fallback scenarios
  • Tests both positive and negative URL detection cases

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient test case management.

The implementation follows a clear pattern of testing three main functions: is_arg_url, match, and get_new_command. Each function is tested with multiple input variations using pytest fixtures and parametrization.

Technical Details

  • Uses pytest framework with parametrize decorators
  • Implements fixture for output generation
  • Tests Command object interactions
  • Validates URL detection logic
  • Tests command transformation rules

Best Practices Demonstrated

The test suite demonstrates several testing best practices.

  • Clear separation of test cases using parametrize
  • Efficient test fixture usage
  • Comprehensive edge case coverage
  • Consistent test structure and organization
  • Clear input/output validation patterns

nvbn/thefuck

tests/rules/test_open.py

            
import pytest
from thefuck.rules.open import is_arg_url, match, get_new_command
from thefuck.types import Command


@pytest.fixture
def output(script):
    return 'The file {} does not exist.
'.format(script.split(' ', 1)[1])


@pytest.mark.parametrize('script', [
    'open foo.com',
    'open foo.edu',
    'open foo.info',
    'open foo.io',
    'open foo.ly',
    'open foo.me',
    'open foo.net',
    'open foo.org',
    'open foo.se',
    'open www.foo.ru'])
def test_is_arg_url(script):
    assert is_arg_url(Command(script, ''))


@pytest.mark.parametrize('script', ['open foo', 'open bar.txt', 'open egg.doc'])
def test_not_is_arg_url(script):
    assert not is_arg_url(Command(script, ''))


@pytest.mark.parametrize('script', [
    'open foo.com',
    'xdg-open foo.com',
    'gnome-open foo.com',
    'kde-open foo.com',
    'open nonest'])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, new_command', [
    ('open foo.io', ['open http://foo.io']),
    ('xdg-open foo.io', ['xdg-open http://foo.io']),
    ('gnome-open foo.io', ['gnome-open http://foo.io']),
    ('kde-open foo.io', ['kde-open http://foo.io']),
    ('open nonest', ['touch nonest && open nonest',
                     'mkdir nonest && open nonest'])])
def test_get_new_command(script, new_command, output):
    assert get_new_command(Command(script, output)) == new_command