StrictUnifiedDiffOutputBuilderIntegrationTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff\Output;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\Diff\Differ;
  13. use SebastianBergmann\Diff\Utils\FileUtils;
  14. use SebastianBergmann\Diff\Utils\UnifiedDiffAssertTrait;
  15. use Symfony\Component\Process\Process;
  16. /**
  17. * @covers SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder
  18. *
  19. * @uses SebastianBergmann\Diff\Differ
  20. * @uses SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator
  21. *
  22. * @requires OS Linux
  23. */
  24. final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
  25. {
  26. use UnifiedDiffAssertTrait;
  27. private $dir;
  28. private $fileFrom;
  29. private $fileTo;
  30. private $filePatch;
  31. protected function setUp(): void
  32. {
  33. $this->dir = \realpath(__DIR__ . '/../../fixtures/out') . '/';
  34. $this->fileFrom = $this->dir . 'from.txt';
  35. $this->fileTo = $this->dir . 'to.txt';
  36. $this->filePatch = $this->dir . 'diff.patch';
  37. if (!\is_dir($this->dir)) {
  38. throw new \RuntimeException('Integration test working directory not found.');
  39. }
  40. $this->cleanUpTempFiles();
  41. }
  42. protected function tearDown(): void
  43. {
  44. $this->cleanUpTempFiles();
  45. }
  46. /**
  47. * Integration test
  48. *
  49. * - get a file pair
  50. * - create a `diff` between the files
  51. * - test applying the diff using `git apply`
  52. * - test applying the diff using `patch`
  53. *
  54. * @param string $fileFrom
  55. * @param string $fileTo
  56. *
  57. * @dataProvider provideFilePairs
  58. */
  59. public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, string $fileTo): void
  60. {
  61. $from = FileUtils::getFileContent($fileFrom);
  62. $to = FileUtils::getFileContent($fileTo);
  63. $diff = (new Differ(new StrictUnifiedDiffOutputBuilder(['fromFile' => 'Original', 'toFile' => 'New'])))->diff($from, $to);
  64. if ('' === $diff && $from === $to) {
  65. // odd case: test after executing as it is more efficient than to read the files and check the contents every time
  66. $this->addToAssertionCount(1);
  67. return;
  68. }
  69. $this->doIntegrationTestGitApply($diff, $from, $to);
  70. }
  71. /**
  72. * Integration test
  73. *
  74. * - get a file pair
  75. * - create a `diff` between the files
  76. * - test applying the diff using `git apply`
  77. * - test applying the diff using `patch`
  78. *
  79. * @param string $fileFrom
  80. * @param string $fileTo
  81. *
  82. * @dataProvider provideFilePairs
  83. */
  84. public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, string $fileTo): void
  85. {
  86. $from = FileUtils::getFileContent($fileFrom);
  87. $to = FileUtils::getFileContent($fileTo);
  88. $diff = (new Differ(new StrictUnifiedDiffOutputBuilder(['fromFile' => 'Original', 'toFile' => 'New'])))->diff($from, $to);
  89. if ('' === $diff && $from === $to) {
  90. // odd case: test after executing as it is more efficient than to read the files and check the contents every time
  91. $this->addToAssertionCount(1);
  92. return;
  93. }
  94. $this->doIntegrationTestPatch($diff, $from, $to);
  95. }
  96. /**
  97. * @param string $expected
  98. * @param string $from
  99. * @param string $to
  100. *
  101. * @dataProvider provideOutputBuildingCases
  102. * @dataProvider provideSample
  103. * @dataProvider provideBasicDiffGeneration
  104. */
  105. public function testIntegrationOfUnitTestCasesGitApply(string $expected, string $from, string $to): void
  106. {
  107. $this->doIntegrationTestGitApply($expected, $from, $to);
  108. }
  109. /**
  110. * @param string $expected
  111. * @param string $from
  112. * @param string $to
  113. *
  114. * @dataProvider provideOutputBuildingCases
  115. * @dataProvider provideSample
  116. * @dataProvider provideBasicDiffGeneration
  117. */
  118. public function testIntegrationOfUnitTestCasesPatch(string $expected, string $from, string $to): void
  119. {
  120. $this->doIntegrationTestPatch($expected, $from, $to);
  121. }
  122. public function provideOutputBuildingCases(): array
  123. {
  124. return StrictUnifiedDiffOutputBuilderDataProvider::provideOutputBuildingCases();
  125. }
  126. public function provideSample(): array
  127. {
  128. return StrictUnifiedDiffOutputBuilderDataProvider::provideSample();
  129. }
  130. public function provideBasicDiffGeneration(): array
  131. {
  132. return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration();
  133. }
  134. public function provideFilePairs(): array
  135. {
  136. $cases = [];
  137. $fromFile = __FILE__;
  138. $vendorDir = \realpath(__DIR__ . '/../../../vendor');
  139. $fileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($vendorDir, \RecursiveDirectoryIterator::SKIP_DOTS));
  140. /** @var \SplFileInfo $file */
  141. foreach ($fileIterator as $file) {
  142. if ('php' !== $file->getExtension()) {
  143. continue;
  144. }
  145. $toFile = $file->getPathname();
  146. $cases[\sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", \realpath($fromFile), \realpath($toFile))] = [$fromFile, $toFile];
  147. $fromFile = $toFile;
  148. }
  149. return $cases;
  150. }
  151. /**
  152. * Compare diff create by builder and against one create by `diff` command.
  153. *
  154. * @param string $diff
  155. * @param string $from
  156. * @param string $to
  157. *
  158. * @dataProvider provideBasicDiffGeneration
  159. */
  160. public function testIntegrationDiffOutputBuilderVersusDiffCommand(string $diff, string $from, string $to): void
  161. {
  162. $this->assertNotSame('', $diff);
  163. $this->assertValidUnifiedDiffFormat($diff);
  164. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  165. $this->assertNotFalse(\file_put_contents($this->fileTo, $to));
  166. $p = new Process(\sprintf('diff -u %s %s', \escapeshellarg($this->fileFrom), \escapeshellarg($this->fileTo)));
  167. $p->run();
  168. $this->assertSame(1, $p->getExitCode()); // note: Process assumes exit code 0 for `isSuccessful`, however `diff` uses the exit code `1` for success with diff
  169. $output = $p->getOutput();
  170. $diffLines = \preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  171. $diffLines[0] = \preg_replace('#^\-\-\- .*#', '--- /' . $this->fileFrom, $diffLines[0], 1);
  172. $diffLines[1] = \preg_replace('#^\+\+\+ .*#', '+++ /' . $this->fileFrom, $diffLines[1], 1);
  173. $diff = \implode('', $diffLines);
  174. $outputLines = \preg_split('/(.*\R)/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  175. $outputLines[0] = \preg_replace('#^\-\-\- .*#', '--- /' . $this->fileFrom, $outputLines[0], 1);
  176. $outputLines[1] = \preg_replace('#^\+\+\+ .*#', '+++ /' . $this->fileFrom, $outputLines[1], 1);
  177. $output = \implode('', $outputLines);
  178. $this->assertSame($diff, $output);
  179. }
  180. private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
  181. {
  182. $this->assertNotSame('', $diff);
  183. $this->assertValidUnifiedDiffFormat($diff);
  184. $diff = self::setDiffFileHeader($diff, $this->fileFrom);
  185. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  186. $this->assertNotFalse(\file_put_contents($this->filePatch, $diff));
  187. $p = new Process(\sprintf(
  188. 'git --git-dir %s apply --check -v --unsafe-paths --ignore-whitespace %s',
  189. \escapeshellarg($this->dir),
  190. \escapeshellarg($this->filePatch)
  191. ));
  192. $p->run();
  193. $this->assertProcessSuccessful($p);
  194. }
  195. private function doIntegrationTestPatch(string $diff, string $from, string $to): void
  196. {
  197. $this->assertNotSame('', $diff);
  198. $this->assertValidUnifiedDiffFormat($diff);
  199. $diff = self::setDiffFileHeader($diff, $this->fileFrom);
  200. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  201. $this->assertNotFalse(\file_put_contents($this->filePatch, $diff));
  202. $command = \sprintf(
  203. 'patch -u --verbose --posix %s < %s',
  204. \escapeshellarg($this->fileFrom),
  205. \escapeshellarg($this->filePatch)
  206. );
  207. $p = new Process($command);
  208. $p->run();
  209. $this->assertProcessSuccessful($p);
  210. $this->assertStringEqualsFile(
  211. $this->fileFrom,
  212. $to,
  213. \sprintf('Patch command "%s".', $command)
  214. );
  215. }
  216. private function assertProcessSuccessful(Process $p): void
  217. {
  218. $this->assertTrue(
  219. $p->isSuccessful(),
  220. \sprintf(
  221. "Command exec. was not successful:\n\"%s\"\nOutput:\n\"%s\"\nStdErr:\n\"%s\"\nExit code %d.\n",
  222. $p->getCommandLine(),
  223. $p->getOutput(),
  224. $p->getErrorOutput(),
  225. $p->getExitCode()
  226. )
  227. );
  228. }
  229. private function cleanUpTempFiles(): void
  230. {
  231. @\unlink($this->fileFrom . '.orig');
  232. @\unlink($this->fileFrom . '.rej');
  233. @\unlink($this->fileFrom);
  234. @\unlink($this->fileTo);
  235. @\unlink($this->filePatch);
  236. }
  237. private static function setDiffFileHeader(string $diff, string $file): string
  238. {
  239. $diffLines = \preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  240. $diffLines[0] = \preg_replace('#^\-\-\- .*#', '--- /' . $file, $diffLines[0], 1);
  241. $diffLines[1] = \preg_replace('#^\+\+\+ .*#', '+++ /' . $file, $diffLines[1], 1);
  242. return \implode('', $diffLines);
  243. }
  244. }