Back to Repositories

Testing Maven Command Validation and Correction in TheFuck

This test suite validates the Maven command correction functionality in TheFuck, focusing on handling scenarios where Maven is executed without specifying required goals or lifecycle phases.

Test Coverage Overview

The test suite comprehensively covers Maven command validation and correction scenarios.

  • Tests invalid Maven commands without goals
  • Validates correct Maven commands with clean/install targets
  • Handles Maven help and version commands
  • Tests non-recursive build scenarios (-N flag)

Implementation Analysis

The testing approach uses pytest’s parametrize feature for data-driven testing of Maven command scenarios.

Implements pattern matching for Maven error messages and command correction logic through the match() and get_new_command() functions. Utilizes TheFuck’s Command type for standardized command handling.

Technical Details

  • Framework: pytest
  • Test Fixtures: Command object from TheFuck types
  • Test Functions: test_match(), test_not_match(), test_get_new_command()
  • Command Validation: Regular expression pattern matching

Best Practices Demonstrated

The test suite exhibits strong testing practices through comprehensive parametrized test cases and clear separation of concerns.

  • Parametrized test cases for multiple scenarios
  • Isolated test functions for specific behaviors
  • Clear test case organization
  • Proper error message validation

nvbn/thefuck

tests/rules/test_mvn_no_command.py

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


@pytest.mark.parametrize('command', [
    Command('mvn', '[ERROR] No goals have been specified for this build. 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', '[ERROR] No goals have been specified for this build. 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', 'mvn clean install']),
    (Command('mvn -N', '[ERROR] No goals have been specified for this build. 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 -N clean package', 'mvn -N clean install'])])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command