UnifiedDiffOutputBuilderIntegrationTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Utils\UnifiedDiffAssertTrait;
  13. use Symfony\Component\Process\Process;
  14. /**
  15. * @covers SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
  16. *
  17. * @uses SebastianBergmann\Diff\Differ
  18. * @uses SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator
  19. *
  20. * @requires OS Linux
  21. */
  22. final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
  23. {
  24. use UnifiedDiffAssertTrait;
  25. private $dir;
  26. private $fileFrom;
  27. private $filePatch;
  28. protected function setUp(): void
  29. {
  30. $this->dir = \realpath(__DIR__ . '/../../fixtures/out/') . '/';
  31. $this->fileFrom = $this->dir . 'from.txt';
  32. $this->filePatch = $this->dir . 'patch.txt';
  33. $this->cleanUpTempFiles();
  34. }
  35. protected function tearDown(): void
  36. {
  37. $this->cleanUpTempFiles();
  38. }
  39. /**
  40. * @dataProvider provideDiffWithLineNumbers
  41. *
  42. * @param mixed $expected
  43. * @param mixed $from
  44. * @param mixed $to
  45. */
  46. public function testDiffWithLineNumbersPath($expected, $from, $to): void
  47. {
  48. $this->doIntegrationTestPatch($expected, $from, $to);
  49. }
  50. /**
  51. * @dataProvider provideDiffWithLineNumbers
  52. *
  53. * @param mixed $expected
  54. * @param mixed $from
  55. * @param mixed $to
  56. */
  57. public function testDiffWithLineNumbersGitApply($expected, $from, $to): void
  58. {
  59. $this->doIntegrationTestGitApply($expected, $from, $to);
  60. }
  61. public function provideDiffWithLineNumbers()
  62. {
  63. return \array_filter(
  64. UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers(),
  65. static function ($key) {
  66. return !\is_string($key) || false === \strpos($key, 'non_patch_compat');
  67. },
  68. ARRAY_FILTER_USE_KEY
  69. );
  70. }
  71. private function doIntegrationTestPatch(string $diff, string $from, string $to): void
  72. {
  73. $this->assertNotSame('', $diff);
  74. $this->assertValidUnifiedDiffFormat($diff);
  75. $diff = self::setDiffFileHeader($diff, $this->fileFrom);
  76. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  77. $this->assertNotFalse(\file_put_contents($this->filePatch, $diff));
  78. $command = \sprintf(
  79. 'patch -u --verbose --posix %s < %s', // --posix
  80. \escapeshellarg($this->fileFrom),
  81. \escapeshellarg($this->filePatch)
  82. );
  83. $p = new Process($command);
  84. $p->run();
  85. $this->assertProcessSuccessful($p);
  86. $this->assertStringEqualsFile(
  87. $this->fileFrom,
  88. $to,
  89. \sprintf('Patch command "%s".', $command)
  90. );
  91. }
  92. private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
  93. {
  94. $this->assertNotSame('', $diff);
  95. $this->assertValidUnifiedDiffFormat($diff);
  96. $diff = self::setDiffFileHeader($diff, $this->fileFrom);
  97. $this->assertNotFalse(\file_put_contents($this->fileFrom, $from));
  98. $this->assertNotFalse(\file_put_contents($this->filePatch, $diff));
  99. $command = \sprintf(
  100. 'git --git-dir %s apply --check -v --unsafe-paths --ignore-whitespace %s',
  101. \escapeshellarg($this->dir),
  102. \escapeshellarg($this->filePatch)
  103. );
  104. $p = new Process($command);
  105. $p->run();
  106. $this->assertProcessSuccessful($p);
  107. }
  108. private function assertProcessSuccessful(Process $p): void
  109. {
  110. $this->assertTrue(
  111. $p->isSuccessful(),
  112. \sprintf(
  113. "Command exec. was not successful:\n\"%s\"\nOutput:\n\"%s\"\nStdErr:\n\"%s\"\nExit code %d.\n",
  114. $p->getCommandLine(),
  115. $p->getOutput(),
  116. $p->getErrorOutput(),
  117. $p->getExitCode()
  118. )
  119. );
  120. }
  121. private function cleanUpTempFiles(): void
  122. {
  123. @\unlink($this->fileFrom . '.orig');
  124. @\unlink($this->fileFrom);
  125. @\unlink($this->filePatch);
  126. }
  127. private static function setDiffFileHeader(string $diff, string $file): string
  128. {
  129. $diffLines = \preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  130. $diffLines[0] = \preg_replace('#^\-\-\- .*#', '--- /' . $file, $diffLines[0], 1);
  131. $diffLines[1] = \preg_replace('#^\+\+\+ .*#', '+++ /' . $file, $diffLines[1], 1);
  132. return \implode('', $diffLines);
  133. }
  134. }