Skip to content

Commit 3eca7f7

Browse files
jrfnlfredden
andcommitted
Ruleset::expandRulesetReference(): test handling "homepath" references
Co-authored-by: Dan Wallis <[email protected]>
1 parent 72949bb commit 3eca7f7

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceHomePathTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- With a mocked "home" path to an existing directory, this reference should still fail
5+
as the underlying subdirectory doesn't exist. -->
6+
<rule ref="~/src/MyStandard/Sniffs/DoesntExist/"/>
7+
8+
</ruleset>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::expandRulesetReference() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test home path handling in the Ruleset::expandRulesetReference() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference
20+
*/
21+
final class ExpandRulesetReferenceHomePathTest extends AbstractRulesetTestCase
22+
{
23+
24+
/**
25+
* Original value of the user's home path environment variable.
26+
*
27+
* @var string|false Path or false is the `HOME` environment variable is not available.
28+
*/
29+
private static $homepath = false;
30+
31+
32+
/**
33+
* Store the user's home path.
34+
*
35+
* @beforeClass
36+
*
37+
* @return void
38+
*/
39+
protected function storeHomePath()
40+
{
41+
$this->homepath = getenv('HOME');
42+
43+
}//end storeHomePath()
44+
45+
46+
/**
47+
* Restore the user's home path environment variable in case the test changed it or created it.
48+
*
49+
* @afterClass
50+
*
51+
* @return void
52+
*/
53+
protected function restoreHomePath()
54+
{
55+
if (is_string($this->homepath) === true) {
56+
putenv('HOME='.$this->homepath);
57+
} else {
58+
// Remove the environment variable as it didn't exist before.
59+
putenv('HOME');
60+
}
61+
62+
}//end restoreHomePath()
63+
64+
65+
/**
66+
* Set the home path to an alternative location.
67+
*
68+
* @before
69+
*
70+
* @return void
71+
*/
72+
protected function setHomePath()
73+
{
74+
$fakeHomePath = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FakeHomePath';
75+
putenv("HOME=$fakeHomePath");
76+
77+
}//end setHomePath()
78+
79+
80+
/**
81+
* Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly
82+
* and finds sniffs if the path exists underneath the "home path".
83+
*
84+
* @return void
85+
*/
86+
public function testHomePathRefGetsExpandedAndFindsSniff()
87+
{
88+
// Set up the ruleset.
89+
$standard = __DIR__.'/ExpandRulesetReferenceHomePathTest.xml';
90+
$config = new ConfigDouble(["--standard=$standard"]);
91+
$ruleset = new Ruleset($config);
92+
93+
$expected = ['MyStandard.Category.Valid' => 'FakeHomePath\\MyStandard\\Sniffs\\Category\\ValidSniff'];
94+
95+
$this->assertSame($expected, $ruleset->sniffCodes);
96+
97+
}//end testHomePathRefGetsExpandedAndFindsSniff()
98+
99+
100+
/**
101+
* Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly
102+
* and still fails to find sniffs if the path doesn't exists underneath the "home path".
103+
*
104+
* @return void
105+
*/
106+
public function testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid()
107+
{
108+
// Set up the ruleset.
109+
$standard = __DIR__.'/ExpandRulesetReferenceHomePathFailTest.xml';
110+
$config = new ConfigDouble(["--standard=$standard"]);
111+
112+
$exceptionMessage = 'Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist';
113+
$this->expectRuntimeExceptionMessage($exceptionMessage);
114+
115+
new Ruleset($config);
116+
117+
}//end testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid()
118+
119+
120+
}//end class
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceHomePathTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- With a mocked "home" path, this reference should work. -->
5+
<rule ref="~/src/MyStandard/Sniffs/Category/"/>
6+
7+
</ruleset>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandRulesetReferenceHomePathTest
6+
*/
7+
8+
namespace FakeHomePath\MyStandard\Sniffs\Category;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class ValidSniff implements Sniff
14+
{
15+
16+
public function register()
17+
{
18+
return [T_CLASS];
19+
}
20+
21+
public function process(File $phpcsFile, $stackPtr)
22+
{
23+
// Do something.
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="MyStandard" namespace="FakeHomePath\MyStandard" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
</ruleset>

0 commit comments

Comments
 (0)