Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Introduce integration-test copy baseline tool #31

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions tools/integration-test-baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

copyTests(__DIR__ . '/../tests/integration');

function copyTests(string $directory, bool $shortenHtml = true): void
{
$finder = new \Symfony\Component\Finder\Finder();
$finder
->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 = '<!-- content start -->';
$endMarker = '<!-- content end -->';

$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);
}