Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion .github/scripts/run-random-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,10 @@ cleanup_finished_pids() {
active+=("$pid")
fi
done
bg_pids=("${active[@]:-}")
bg_pids=()
if [[ ${#active[@]} -gt 0 ]]; then
bg_pids=("${active[@]}")
fi
}

spawn_limited_job() {
Expand Down
109 changes: 109 additions & 0 deletions tests/system/AutoReview/RunRandomTestsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\AutoReview;

use FilesystemIterator;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
* @internal
*/
#[CoversNothing]
#[Group('AutoReview')]
final class RunRandomTestsTest extends TestCase
{
private string $project;

protected function setUp(): void
{
parent::setUp();

if (PHP_OS_FAMILY === 'Windows') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can use is_windows()

$this->markTestSkipped('The random test runner requires Bash and GNU coreutils.');
}

$this->project = sys_get_temp_dir() . '/ci4-random-tests-' . bin2hex(random_bytes(4));

foreach (['.github/scripts', 'vendor/bin', 'tests/system/First', 'tests/system/Second'] as $directory) {
mkdir($this->project . '/' . $directory, 0777, true);
}

copy(ROOTPATH . '.github/scripts/run-random-tests.sh', $this->project . '/.github/scripts/run-random-tests.sh');
file_put_contents($this->project . '/.github/scripts/random-tests-config.txt', "First\nSecond\n");
file_put_contents($this->project . '/vendor/bin/phpunit', <<<'BASH'
#!/usr/bin/env bash
printf '%s\n' "$1" >> calls.log
sleep 0.05

BASH);
chmod($this->project . '/vendor/bin/phpunit', 0755);
}

protected function tearDown(): void
{
if (isset($this->project)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->project, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);

foreach ($files as $file) {
$file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
}

rmdir($this->project);
}

parent::tearDown();
}

#[DataProvider('provideRunsEveryComponentOnEachRepetition')]
public function testRunsEveryComponentOnEachRepetition(int $maxJobs): void
{
// Bound the command so a scheduler regression fails instead of hanging PHPUnit.
exec(sprintf(
'GITHUB_ACTIONS=true timeout --kill-after=1s 5s bash %s --max-jobs %d --repeat 2 --timeout 0 2>&1',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to set GITHUB_ACTIONS env explicitly here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout is unavailable on my macos. can this unconditional timeout be tested for availability first, then for gtimeout, then skip if none available?

escapeshellarg($this->project . '/.github/scripts/run-random-tests.sh'),
$maxJobs,
), $output, $exitCode);
$output = implode("\n", $output);

$this->assertSame(0, $exitCode, $output);
$this->assertStringContainsString('All components passed random execution tests!', $output);

$calls = file($this->project . '/calls.log', FILE_IGNORE_NEW_LINES);

$this->assertIsArray($calls);
$this->assertCount(4, $calls);

foreach (['First', 'Second'] as $component) {
$this->assertCount(2, array_keys($calls, 'tests/system/' . $component, true));
}
}

/**
* @return iterable<string, array{int}>
*/
public static function provideRunsEveryComponentOnEachRepetition(): iterable
{
yield 'one job' => [1];

yield 'two jobs' => [2];
}
}
Loading