Back to Repositories

Testing Gradle Wrapper Command Detection in TheFuck

This test suite validates the Gradle wrapper functionality in TheFuck, a command-line tool that corrects mistyped console commands. The tests focus on verifying the automatic detection and correction of Gradle commands when the wrapper should be used instead of the global Gradle installation.

Test Coverage Overview

The test suite provides comprehensive coverage of Gradle wrapper command detection and correction scenarios.

Key areas tested include:
  • Command matching for missing Gradle installation
  • Non-matching conditions with various command configurations
  • Command transformation from ‘gradle’ to ‘./gradlew’
Edge cases cover scenarios with existing Gradle installations and non-Gradle commands.

Implementation Analysis

The testing approach utilizes pytest’s parameterized testing to efficiently verify multiple input scenarios and expected outputs. The implementation leverages pytest fixtures and mocking to control environment conditions and file system checks.

Technical patterns include:
  • Fixture-based dependency injection
  • Parameterized test cases
  • Mock object usage for filesystem operations

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • pytest-mock for mocking system calls
  • Custom Command type for input handling
  • Automated fixture setup for file existence checks

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing. Notable practices include:
  • Separation of test cases by functionality
  • Comprehensive mock usage for external dependencies
  • Clear test method naming conventions
  • Efficient test parameterization

nvbn/thefuck

tests/rules/test_gradle_wrapper.py

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


@pytest.fixture(autouse=True)
def exists(mocker):
    return mocker.patch('thefuck.rules.gradle_wrapper.os.path.isfile',
                        return_value=True)


@pytest.mark.parametrize('command', [
    Command('gradle tasks', 'gradle: not found'),
    Command('gradle build', 'gradle: not found')])
def test_match(mocker, command):
    mocker.patch('thefuck.rules.gradle_wrapper.which', return_value=None)

    assert match(command)


@pytest.mark.parametrize('command, gradlew, which', [
    (Command('gradle tasks', 'gradle: not found'), False, None),
    (Command('gradle tasks', 'command not found'), True, '/usr/bin/gradle'),
    (Command('npm tasks', 'npm: not found'), True, None)])
def test_not_match(mocker, exists, command, gradlew, which):
    mocker.patch('thefuck.rules.gradle_wrapper.which', return_value=which)
    exists.return_value = gradlew

    assert not match(command)


@pytest.mark.parametrize('script, result', [
    ('gradle assemble', './gradlew assemble'),
    ('gradle --help', './gradlew --help'),
    ('gradle build -c', './gradlew build -c')])
def test_get_new_command(script, result):
    command = Command(script, '')
    assert get_new_command(command) == result