From 5d514ed00c6ddb88de8ff7d8b3b34ed8d6fd66d2 Mon Sep 17 00:00:00 2001 From: Lina Wolf <48202465+linawolf@users.noreply.github.com> Date: Sun, 17 Dec 2023 16:51:36 +0100 Subject: [PATCH] [TASK] Introdule integration-test copy baseline tool (#31) --- tools/integration-test-baseline.php | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tools/integration-test-baseline.php diff --git a/tools/integration-test-baseline.php b/tools/integration-test-baseline.php new file mode 100644 index 0000000..c9ff0f3 --- /dev/null +++ b/tools/integration-test-baseline.php @@ -0,0 +1,80 @@ +directories() + ->in($directory) + ->depth('>= 0'); + + foreach ($finder as $dir) { + $inputDirectory = $dir->getRealPath() . '/input'; + if (!is_dir($inputDirectory)) { + continue; + } + $tempDir = $dir->getPathname() . '/temp'; + $expectedDir = $dir->getPathname() . '/expected'; + if (!file_exists($tempDir)) { + echo sprintf("Skipped %s - temp directory does not exist\n", $dir); + continue; + } + + $fileFinder = new \Symfony\Component\Finder\Finder(); + $fileFinder + ->files() + ->in($dir->getPathname() . '/expected') + ->depth('>= 0'); + foreach ($fileFinder as $file) { + $relativePath = $file->getRelativePathname(); + $tempFile = $tempDir . '/' . $relativePath; + $outputFile = $expectedDir . '/' . $relativePath; + if (file_exists($tempFile)) { + if (pathinfo($tempFile, PATHINFO_EXTENSION) === 'log') { + echo sprintf("Ignoring log file %s\n", $outputFile); + } elseif ($shortenHtml && pathinfo($tempFile, PATHINFO_EXTENSION) === 'html') { + // Handle HTML file with content markers + copyHtmlWithMarkers($tempFile, $outputFile); + echo sprintf("Updated %s\n", $outputFile); + } else { + // Copy non-HTML files as-is + copy($tempFile, $outputFile); + echo sprintf("Updated %s\n", $outputFile); + } + } else { + echo sprintf("Skipped %s - %s does not exist\n", $outputFile, $tempFile); + } + } + } +} + +/** + * Copy HTML file with content markers. + */ +function copyHtmlWithMarkers(string $sourceFile, string $destinationFile): void +{ + $startMarker = ''; + $endMarker = ''; + + $fileContent = file_get_contents($sourceFile); + assert(is_string($fileContent)); + $startPos = strpos($fileContent, $startMarker); + $endPos = strpos($fileContent, $endMarker, $startPos + strlen($startMarker)); + + if ($startPos === false || $endPos === false) { + echo sprintf("Skipped %s - Start or end marker not found\n", $destinationFile); + return; + } + + $contentBetweenMarkers = substr($fileContent, $startPos, $endPos + strlen($endMarker) - $startPos); + $lines = explode("\n", $contentBetweenMarkers); + $trimmedLines = array_map('rtrim', $lines); + $trimmedContent = implode("\n", $trimmedLines) . "\n"; + + file_put_contents($destinationFile, $trimmedContent); + echo sprintf("Updated %s\n", $destinationFile); +}