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 1 commit
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() && !$opts->hasIgnoreExitViolations()) {
if ($phpmd->hasViolations() && !$opts->ignoreExitViolations()) {
return self::EXIT_VIOLATION;
}
return self::EXIT_SUCCESS;
Expand Down
10 changes: 5 additions & 5 deletions src/main/php/PHPMD/TextUI/CommandLineOptions.php
Expand Up @@ -200,7 +200,7 @@ public function __construct(array $args, array $availableRuleSets = array())
case '--strict':
$this->strict = true;
break;
case '--ignore-exit-violations':
case '--ignore-violations-on-exit':
$this->ignoreExitViolations = true;
break;
case (preg_match('(^\-\-reportfile\-(xml|html|text)$)', $arg, $match) > 0):
Expand Down Expand Up @@ -338,11 +338,11 @@ public function hasStrict()
}

/**
* Was the <b>--ignore-exit-violations</b> passed to PHPMD's command line interface?
* Was the <b>--ignore-violations-on-exit</b> passed to PHPMD's command line interface?
*
* @return boolean
*/
public function hasIgnoreExitViolations()
public function ignoreExitViolations()
Copy link
Member

Choose a reason for hiding this comment

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

On a secont thought, after we renamed the CLI option, we should rename this appropriately, too.

Please rename to ignoreViolationsOnExit().

🙇

{
return $this->ignoreExitViolations;
Copy link
Member

Choose a reason for hiding this comment

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

Rename to ignoreViolationsOnExit please.

}
Expand Down Expand Up @@ -459,8 +459,8 @@ public function usage()
'ignore directories' . \PHP_EOL .
'--strict: also report those nodes with a @SuppressWarnings ' .
'annotation' . \PHP_EOL .
'--ignore-exit-violations: will exit with 0 even if a ' .
'violation is found ' . \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());
}

/**
* testIgnoreExitViolationsReturnsFalseByDefault
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I don't like it, when people just cpy and paste it.

Why not say what it does, like:
Tests if ignoreViolationsOnExit returns false by default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it was a Lead Developper choice so I just followed the testcase coding style

Copy link
Member

Choose a reason for hiding this comment

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

It is or better said was. But it's never too late to start improving 😼

*
* @return void
*/
public function testIgnoreExitViolationsReturnsFalseByDefault()
Copy link
Member

Choose a reason for hiding this comment

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

Rename to testIgnoreViolationsOnExitReturnsFalseByDefault() please.

{
$args = array(__FILE__, __FILE__, 'text', 'unusedcode');
$opts = new CommandLineOptions($args);

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

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

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

/**
* testCliUsageContainsStrictOption
*
* @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
25 changes: 25 additions & 0 deletions src/test/php/PHPMD/TextUI/CommandTest.php
Expand Up @@ -99,4 +99,29 @@ public function testMainReturnsViolationExitCodeForSourceWithNPathViolation()
);
$this->assertEquals(Command::EXIT_VIOLATION, $exitCode);
}

/**
* testMainReturnsViolationExitCodeForSourceWithNPathViolation
Copy link
Member

@ravage84 ravage84 Jun 27, 2016

Choose a reason for hiding this comment

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

Copy paste error, better explain the meaning of the test instead of copy pasting the name of the function/method.
E.g.:
Tests if main returns success Exit Code for Source with NPath Violation and IgnoreViolationsOnExit Flag

This kind of documentation is meant for a developer, not for a computer... 😼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it was a Lead Developper choice so I just followed the testcase coding style

*
* @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);
}
}