Back to Repositories

Validating Maven Lifecycle Phase Detection in TheFuck

This test suite validates the Maven unknown lifecycle phase correction functionality in TheFuck, ensuring proper handling and suggestions for invalid Maven commands. It focuses on detecting and correcting common Maven phase typos and command errors.

Test Coverage Overview

The test suite provides comprehensive coverage of Maven command correction scenarios.

Key areas tested include:
  • Detection of unknown lifecycle phases
  • Handling of common Maven command typos
  • Validation of correct command suggestions
  • Edge cases with multiple phase arguments

Implementation Analysis

The testing approach uses pytest’s parametrize feature to efficiently test multiple scenarios with different command inputs and expected outputs.

Technical implementation includes:
  • Command object testing with error messages
  • Pattern matching for Maven phase detection
  • Command correction suggestion validation

Technical Details

Testing infrastructure includes:
  • pytest framework for test organization
  • Command class from thefuck.types
  • Parametrized test cases for varied scenarios
  • Mock Maven command outputs and error messages

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Comprehensive positive and negative test cases
  • Isolated test functions for specific behaviors
  • Clear test case organization using parametrize
  • Realistic command output simulation

nvbn/thefuck

tests/rules/test_mvn_unknown_lifecycle_phase.py

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


@pytest.mark.parametrize('command', [
    Command('mvn cle', '[ERROR] Unknown lifecycle phase "cle". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]')])
def test_match(command):
    assert match(command)


@pytest.mark.parametrize('command', [
    Command('mvn clean', """
[INFO] Scanning for projects...[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test 0.2
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test ---
[INFO] Deleting /home/mlk/code/test/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.477s
[INFO] Finished at: Wed Aug 26 13:05:47 BST 2015
[INFO] Final Memory: 6M/240M
[INFO] ------------------------------------------------------------------------
"""),  # noqa
    Command('mvn --help', ''),
    Command('mvn -v', '')
])
def test_not_match(command):
    assert not match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command('mvn cle', '[ERROR] Unknown lifecycle phase "cle". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]'), ['mvn clean', 'mvn compile']),
    (Command('mvn claen package', '[ERROR] Unknown lifecycle phase "claen". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]'), ['mvn clean package'])])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command