Skip to content

Commit 3df85dc

Browse files
committed
The unit test runner now loads the test sniff outside of the standard's ruleset so that exclude rules do not get applied (ref #1169)
1 parent 7ede265 commit 3df85dc

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

CodeSniffer.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -533,34 +533,51 @@ public function initStandard($standards, array $restrictions=array(), array $exc
533533
// be detected properly for files created on a Mac with the /r line ending.
534534
ini_set('auto_detect_line_endings', true);
535535

536-
$sniffs = array();
537-
foreach ($standards as $standard) {
538-
$installed = $this->getInstalledStandardPath($standard);
536+
if (defined('PHP_CODESNIFFER_IN_TESTS') === true) {
537+
// Should be one standard and one sniff being tested at a time.
538+
$installed = $this->getInstalledStandardPath($standards[0]);
539539
if ($installed !== null) {
540540
$standard = $installed;
541541
} else {
542-
$standard = self::realpath($standard);
542+
$standard = self::realpath($standards[0]);
543543
if (is_dir($standard) === true
544544
&& is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
545545
) {
546546
$standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
547547
}
548548
}
549549

550-
if (PHP_CODESNIFFER_VERBOSITY === 1) {
551-
$ruleset = simplexml_load_string(file_get_contents($standard));
552-
if ($ruleset !== false) {
553-
$standardName = (string) $ruleset['name'];
550+
$sniffs = $this->_expandRulesetReference($restrictions[0], dirname($standard));
551+
} else {
552+
$sniffs = array();
553+
foreach ($standards as $standard) {
554+
$installed = $this->getInstalledStandardPath($standard);
555+
if ($installed !== null) {
556+
$standard = $installed;
557+
} else {
558+
$standard = self::realpath($standard);
559+
if (is_dir($standard) === true
560+
&& is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true
561+
) {
562+
$standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml');
563+
}
554564
}
555565

556-
echo "Registering sniffs in the $standardName standard... ";
557-
if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
558-
echo PHP_EOL;
566+
if (PHP_CODESNIFFER_VERBOSITY === 1) {
567+
$ruleset = simplexml_load_string(file_get_contents($standard));
568+
if ($ruleset !== false) {
569+
$standardName = (string) $ruleset['name'];
570+
}
571+
572+
echo "Registering sniffs in the $standardName standard... ";
573+
if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
574+
echo PHP_EOL;
575+
}
559576
}
560-
}
561577

562-
$sniffs = array_merge($sniffs, $this->processRuleset($standard));
563-
}//end foreach
578+
$sniffs = array_merge($sniffs, $this->processRuleset($standard));
579+
}//end foreach
580+
}//end if
564581

565582
$sniffRestrictions = array();
566583
foreach ($restrictions as $sniffCode) {

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
3333
- Developers of custom standards with custom test runners can now have their standards ignored by the built-in test runner
3434
-- Set the value of an environment variable called PHPCS_IGNORE_TESTS with a comma separated list of your standard names
3535
-- Thanks to Juliette Reinders Folmer for the patch
36+
- The unit test runner now loads the test sniff outside of the standard's ruleset so that exclude rules do not get applied
37+
-- This may have caused problems when testing custom sniffs inside custom standards
38+
-- Also makes the unit tests runs a little faster
3639
- Fixed bug #1135 : PEAR.ControlStructures.MultiLineCondition.CloseBracketNewLine not detected if preceded by multiline function call
3740
- Fixed bug #1138 : PEAR.ControlStructures.MultiLineCondition.Alignment not detected if closing brace is first token on line
3841
- Fixed bug #1141 : Sniffs that check EOF newlines don't detect newlines properly when the last token is a doc block

0 commit comments

Comments
 (0)