Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI IgnoreViolationsOnExit option flag #380

Merged
merged 3 commits into from Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.rst
Expand Up @@ -94,6 +94,9 @@ Command line options

- ``--strict`` - Also report those nodes with a @SuppressWarnings annotation.

- ``--ignore-violations-on-exit`` - will exit with a zero code, even if any
violations are found.

An example command line: ::

phpmd PHP/Depend/DbusUI xml codesize --reportfile phpmd.xml --suffixes php,phtml
Expand Down Expand Up @@ -144,7 +147,9 @@ PHPMD's command line tool currently defines three different exit codes.
interrupted PHPMD during execution.
- *2*, This exit code means that PHPMD has processed the code under test
without the occurence of an error/exception, but it has detected rule
violations in the analyzed source code.
violations in the analyzed source code. You can also prevent this behaviour
with the ``--ignore-violations-on-exit`` flag, which will result to a *0*
even if any violations are found.

Renderers
---------
Expand Down
5 changes: 4 additions & 1 deletion src/main/php/PHPMD/TextUI/Command.php
Expand Up @@ -70,6 +70,9 @@ class Command
* found in the analyzed code. Otherwise this method will return a value
* equal to <b>EXIT_VIOLATION</b>.
*
* The use of flag <b>--ignore-violations-on-exit</b> will result to a
* <b>EXIT_SUCCESS</b> even if any violation is found.
*
* @param \PHPMD\TextUI\CommandLineOptions $opts
* @param \PHPMD\RuleSetFactory $ruleSetFactory
* @return integer
Expand Down Expand Up @@ -129,7 +132,7 @@ public function run(CommandLineOptions $opts, RuleSetFactory $ruleSetFactory)
$ruleSetFactory
);

if ($phpmd->hasViolations()) {
if ($phpmd->hasViolations() && !$opts->ignoreViolationsOnExit()) {
return self::EXIT_VIOLATION;
}
return self::EXIT_SUCCESS;
Expand Down
24 changes: 23 additions & 1 deletion src/main/php/PHPMD/TextUI/CommandLineOptions.php
Expand Up @@ -139,6 +139,13 @@ class CommandLineOptions
*/
protected $strict = false;

/**
* Should PHPMD exit without error code even if violation is found?
*
* @var boolean
*/
protected $ignoreViolationsOnExit = false;

/**
* List of available rule-sets.
*
Expand Down Expand Up @@ -193,6 +200,9 @@ public function __construct(array $args, array $availableRuleSets = array())
case '--strict':
$this->strict = true;
break;
case '--ignore-violations-on-exit':
$this->ignoreViolationsOnExit = true;
break;
case (preg_match('(^\-\-reportfile\-(xml|html|text)$)', $arg, $match) > 0):
$this->reportFiles[$match[1]] = array_shift($args);
break;
Expand Down Expand Up @@ -327,6 +337,16 @@ public function hasStrict()
return $this->strict;
}

/**
* Was the <b>--ignore-violations-on-exit</b> passed to PHPMD's command line interface?
*
* @return boolean
*/
public function ignoreViolationsOnExit()
{
return $this->ignoreViolationsOnExit;
}

/**
* Creates a report renderer instance based on the user's command line
* argument.
Expand Down Expand Up @@ -438,7 +458,9 @@ public function usage()
'--exclude: comma-separated string of patterns that are used to ' .
'ignore directories' . \PHP_EOL .
'--strict: also report those nodes with a @SuppressWarnings ' .
'annotation' . \PHP_EOL;
'annotation' . \PHP_EOL .
'--ignore-violations-on-exit: will exit with a zero code, ' .
'even if any violations are found' . \PHP_EOL;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/site/rst/documentation/index.rst
Expand Up @@ -54,6 +54,9 @@ Command line options

- ``--strict`` - Also report those nodes with a @SuppressWarnings annotation.

- ``--ignore-violations-on-exit`` - will exit with a zero code, even if any
violations are found.

An example command line: ::

phpmd PHP/Depend/DbusUI xml codesize --reportfile phpmd.xml --suffixes php,phtml
Expand Down Expand Up @@ -96,7 +99,9 @@ PHPMD's command line tool currently defines three different exit codes.
interrupted PHPMD during execution.
- *2*, This exit code means that PHPMD has processed the code under test
without the occurrence of an error/exception, but it has detected rule
violations in the analyzed source code.
violations in the analyzed source code. You can also prevent this behaviour
with the ``--ignore-violations-on-exit`` flag, which will result to a *0*
even if any violations are found.

Renderers
=========
Expand Down
41 changes: 40 additions & 1 deletion src/test/php/PHPMD/TextUI/CommandLineOptionsTest.php
Expand Up @@ -174,7 +174,7 @@ public function testThrowsExpectedExceptionWhenInputFileNotExists()
}

/**
* testCliOptionsAcceptsVersionArgument
* testHasVersionReturnsFalseByDefault
*
* @return void
*/
Expand All @@ -199,6 +199,45 @@ public function testCliOptionsAcceptsVersionArgument()
self::assertTrue($opts->hasVersion());
}

/**
* Tests if ignoreViolationsOnExit returns false by default
*
* @return void
*/
public function testIgnoreViolationsOnExitReturnsFalseByDefault()
{
$args = array(__FILE__, __FILE__, 'text', 'unusedcode');
$opts = new CommandLineOptions($args);

self::assertFalse($opts->ignoreViolationsOnExit());
}

/**
* Tests if CLI options accepts ignoreViolationsOnExit argument
*
* @return void
*/
public function testCliOptionsAcceptsIgnoreViolationsOnExitArgument()
{
$args = array(__FILE__, __FILE__, 'text', 'unusedcode', '--ignore-violations-on-exit');
$opts = new CommandLineOptions($args);

self::assertTrue($opts->ignoreViolationsOnExit());
}

/**
* Tests if CLI usage contains ignoreViolationsOnExit option
*
* @return void
*/
public function testCliUsageContainsIgnoreViolationsOnExitOption()
{
$args = array(__FILE__, __FILE__, 'text', 'codesize');
$opts = new CommandLineOptions($args);

$this->assertContains('--ignore-violations-on-exit:', $opts->usage());
}

/**
* testCliUsageContainsStrictOption
*
Expand Down
30 changes: 29 additions & 1 deletion src/test/php/PHPMD/TextUI/CommandTest.php
Expand Up @@ -77,7 +77,7 @@ public function testMainReturnsSuccessExitCodeForSourceWithoutViolations()
}

/**
* testMainReturnsViolationExitCodeForSourceWithNPathViolation
* Tests if main returns violation Exit Code for Source with NPath Violation
*
* @return void
* @covers \PHPMD\TextUI\Command
Expand All @@ -99,4 +99,32 @@ public function testMainReturnsViolationExitCodeForSourceWithNPathViolation()
);
$this->assertEquals(Command::EXIT_VIOLATION, $exitCode);
}

/**
* Tests if main returns success Exit Code for Source with NPath Violation and IgnoreViolationsOnExit Flag
*
* @see ::testMainReturnsViolationExitCodeForSourceWithNPathViolation
* Same as the other test, but with '--ignore-violations-on-exit' set.
*
* @return void
* @covers \PHPMD\TextUI\Command
* @group phpmd
* @group phpmd::textui
* @group unittest
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a @see ::testMainReturnsViolationExitCodeForSourceWithNPathViolation Same as the other test, but with '--ignore-violations-on-exit' set.

public function testMainReturnsSuccessExitCodeForSourceWithNPathViolationAndIgnoreViolationsOnExitFlag()
{
$exitCode = Command::main(
array(
__FILE__,
self::createFileUri('source/source_with_npath_violation.php'),
'text',
'codesize',
'--reportfile',
self::createTempFileUri(),
'--ignore-violations-on-exit',
)
);
$this->assertEquals(Command::EXIT_SUCCESS, $exitCode);
}
}