1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
<?php
/**
* Tests for diff generation.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Fixer;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
/**
* Tests for diff generation.
*
* Note: these tests are specifically about the Fixer::generateDiff() method and do not
* test running the fixer itself, nor generating a diff based on a fixer run.
*
* @covers PHP_CodeSniffer\Fixer::generateDiff
* @group Windows
*/
final class GenerateDiffTest extends TestCase
{
/**
* A \PHP_CodeSniffer\Files\File object to compare the files against.
*
* @var \PHP_CodeSniffer\Files\LocalFile
*/
private static $phpcsFile;
/**
* Initialize an \PHP_CodeSniffer\Files\File object with code.
*
* Things to take note of in the code snippet used for these tests:
* - Line endings are \n.
* - Tab indent.
* - Trailing whitespace.
*
* Also note that the Config object is deliberately created without a `tabWidth` setting to
* prevent doing tab replacement when parsing the file. This is to allow for testing a
* diff with tabs vs spaces (which wouldn't yield a diff if tabs had already been replaced).
*
* @beforeClass
*
* @return void
*/
public static function initializeFile()
{
$config = new ConfigDouble();
$ruleset = new Ruleset($config);
self::$phpcsFile = new LocalFile(__DIR__.'/Fixtures/GenerateDiffTest.inc', $ruleset, $config);
self::$phpcsFile->parse();
self::$phpcsFile->fixer->startFile(self::$phpcsFile);
}//end initializeFile()
/**
* Test generating a diff on the file object itself.
*
* @return void
*/
public function testGenerateDiffNoFile()
{
$diff = self::$phpcsFile->fixer->generateDiff(null, false);
$this->assertSame('', $diff);
}//end testGenerateDiffNoFile()
/**
* Test generating a diff between a PHPCS File object and a file on disk.
*
* @param string $filePath The path to the file to compare the File object against.
*
* @dataProvider dataGenerateDiff
*
* @return void
*/
public function testGenerateDiff($filePath)
{
$diff = self::$phpcsFile->fixer->generateDiff($filePath, false);
// Allow for the tests to pass on Windows too.
$diff = str_replace('--- tests\Core\Fixer/', '--- tests/Core/Fixer/', $diff);
$expectedDiffFile = str_replace('.inc', '.diff', $filePath);
$this->assertStringEqualsFile($expectedDiffFile, $diff);
}//end testGenerateDiff()
/**
* Data provider.
*
* @see testGenerateDiff()
*
* @return array<string, array<string, string>>
*/
public static function dataGenerateDiff()
{
return [
'no difference' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-NoDiff.inc',
],
'line removed' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-LineRemoved.inc',
],
'line added' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-LineAdded.inc',
],
'var name changed' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-VarNameChanged.inc',
],
'trailing whitespace removed' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-NoTrailingWhitespace.inc',
],
'tab replaced with spaces' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-TabsToSpaces.inc',
],
'blank lines at start of file' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-BlankLinesAtStart.inc',
],
'whitespace diff at start of file' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-WhiteSpaceAtStart.inc',
],
'blank lines at end of file' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-BlankLinesAtEnd.inc',
],
'whitespace diff at end of file' => [
'filePath' => __DIR__.'/Fixtures/GenerateDiffTest-WhiteSpaceAtEnd.inc',
],
];
}//end dataGenerateDiff()
/**
* Test generating a diff between a PHPCS File object and a file on disk and colourizing the output.
*
* @return void
*/
public function testGenerateDiffColoured()
{
$expected = "\033[31m--- tests/Core/Fixer/Fixtures/GenerateDiffTest-VarNameChanged.inc\033[0m".PHP_EOL;
$expected .= "\033[32m+++ PHP_CodeSniffer\033[0m".PHP_EOL;
$expected .= '@@ -1,7 +1,7 @@'.PHP_EOL;
$expected .= ' <?php'.PHP_EOL;
$expected .= ' // Comment with 2 spaces trailing whitespace. '.PHP_EOL;
$expected .= "\033[31m".'-$rav = FALSE;'."\033[0m".PHP_EOL;
$expected .= "\033[32m".'+$var = FALSE;'."\033[0m".PHP_EOL;
$expected .= ' '.PHP_EOL;
$expected .= "\033[31m".'-if ($rav) {'."\033[0m".PHP_EOL;
$expected .= "\033[32m".'+if ($var) {'."\033[0m".PHP_EOL;
$expected .= ' echo \'This line is tab indented\';'.PHP_EOL;
$expected .= ' }';
$filePath = __DIR__.'/Fixtures/GenerateDiffTest-VarNameChanged.inc';
$diff = self::$phpcsFile->fixer->generateDiff($filePath);
// Allow for the tests to pass on Windows too.
$diff = str_replace('--- tests\Core\Fixer/', '--- tests/Core/Fixer/', $diff);
$this->assertSame($expected, $diff);
}//end testGenerateDiffColoured()
/**
* Test generating a diff between a PHPCS File object using *nix line endings and a file on disk
* using Windows line endings.
*
* The point of this test is to verify that all lines are marked as having a difference.
* The actual lines endings used in the diff shown to the end-user are not relevant for this
* test.
* As the "diff" command is finicky with what type of line endings are used when the only
* difference on a line is the line ending, the test normalizes the line endings of the
* received diff before testing it.
*
* @return void
*/
public function testGenerateDiffDifferentLineEndings()
{
// By the looks of it, if the only diff between two files is line endings, the
// diff generated by the *nix "diff" command will always contain *nix line endings.
$expected = '--- tests/Core/Fixer/Fixtures/GenerateDiffTest-WindowsLineEndings.inc'."\n";
$expected .= '+++ PHP_CodeSniffer'."\n";
$expected .= '@@ -1,7 +1,7 @@'."\n";
$expected .= '-<?php'."\n";
$expected .= '-// Comment with 2 spaces trailing whitespace. '."\n";
$expected .= '-$var = FALSE;'."\n";
$expected .= '-'."\n";
$expected .= '-if ($var) {'."\n";
$expected .= '- echo \'This line is tab indented\';'."\n";
$expected .= '-}'."\n";
$expected .= '+<?php'."\n";
$expected .= '+// Comment with 2 spaces trailing whitespace. '."\n";
$expected .= '+$var = FALSE;'."\n";
$expected .= '+'."\n";
$expected .= '+if ($var) {'."\n";
$expected .= '+ echo \'This line is tab indented\';'."\n";
$expected .= '+}'."\n";
$filePath = __DIR__.'/Fixtures/GenerateDiffTest-WindowsLineEndings.inc';
$diff = self::$phpcsFile->fixer->generateDiff($filePath, false);
// Allow for the tests to pass on Windows too.
$diff = str_replace('--- tests\Core\Fixer/', '--- tests/Core/Fixer/', $diff);
// Normalize line endings of the diff.
$diff = preg_replace('`\R`', "\n", $diff);
$this->assertSame($expected, $diff);
}//end testGenerateDiffDifferentLineEndings()
}//end class
|