Skip to content

Commit 320e442

Browse files
authored
Merge pull request #1855 from hydephp/refactor-realtime-compiler-tests
Internal: Update realtime compiler test suite setup
2 parents aeccb5e + f01d60e commit 320e442

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

packages/realtime-compiler/.github/workflows/test.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ jobs:
1919
- name: Install dependencies
2020
run: composer install
2121

22+
- name: Configure environment
23+
run: |
24+
echo "HYDE_RC_RUNNER_PATH=/tmp/hyde-rc-runner" >> $GITHUB_ENV
25+
if [ ${{ github.event_name }} == 'push' ]; then
26+
echo "HYDE_RC_RUNNER_BRANCH=master" >> $GITHUB_ENV
27+
else
28+
echo "HYDE_RC_BRANCH=$GITHUB_SHA" >> $GITHUB_ENV
29+
fi
30+
2231
- name: Set up test runner
2332
run: php -r 'require_once __DIR__."/vendor/autoload.php"; \Hyde\RealtimeCompiler\Tests\Integration\IntegrationTestCase::setUpTestRunner();'
2433

packages/realtime-compiler/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
/composer.lock
44
/.phpunit.cache/
55
/.phpunit.result.cache
6-
/tests/runner

packages/realtime-compiler/tests/Integration/IntegrationTestCase.php

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Hyde\RealtimeCompiler\Tests\Integration;
46

57
use InvalidArgumentException;
@@ -39,8 +41,9 @@ public static function setUpBeforeClass(): void
3941
}
4042

4143
fclose($process);
42-
$throwInsteadOfKill = false;
43-
if ($throwInsteadOfKill) {
44+
45+
$throwOnBusyPort = false;
46+
if ($throwOnBusyPort) {
4447
throw new RuntimeException(sprintf('Port 8080 is already in use. (PID %s)', $pid));
4548
} else {
4649
// Kill the process using the port
@@ -50,13 +53,25 @@ public static function setUpBeforeClass(): void
5053

5154
// Start the server in a background process, keeping the task ID for later
5255
$null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
53-
self::$server = proc_open("php hyde serve > $null", [], $pipes, realpath(__DIR__.'/../runner'));
56+
self::$server = proc_open("php hyde serve > $null", [], $pipes, realpath(self::getRunnerPath()));
5457

5558
// Wait for the server to start
59+
$waitTime = time();
5660
while (@fsockopen('localhost', 8080, $errno, $errstr, 1) === false) {
61+
// Timeout after 5 seconds
62+
if (time() - $waitTime > 5) {
63+
throw new RuntimeException('Failed to start the test server.');
64+
}
65+
5766
if (proc_get_status(self::$server)['running'] === false) {
58-
break;
67+
// Make a head request to the server to see if it's running
68+
if (shell_exec('curl -I http://localhost:8080') === false) {
69+
break;
70+
}
5971
}
72+
73+
// Sleep 20ms
74+
usleep(20000);
6075
}
6176

6277
// Assert that the server was started successfully
@@ -65,6 +80,28 @@ public static function setUpBeforeClass(): void
6580
}
6681
}
6782

83+
protected static function getRunnerPath(): string
84+
{
85+
// Get path from the environment variable
86+
$path = getenv('HYDE_RC_RUNNER_PATH');
87+
88+
if ($path === false) {
89+
throw new RuntimeException('HYDE_RC_RUNNER_PATH environment variable is not set.');
90+
}
91+
92+
// Check that it's not a child of the project root
93+
$packageDir = realpath(__DIR__.'/../../');
94+
if (str_starts_with($path, $packageDir)) {
95+
throw new RuntimeException('HYDE_RC_RUNNER_PATH cannot be a child of the package root as junctioning will massivly inflate vendor directory.');
96+
}
97+
98+
if (file_exists($path)) {
99+
$path = realpath($path);
100+
}
101+
102+
return $path;
103+
}
104+
68105
public function __destruct()
69106
{
70107
// Ensure the server is stopped, regardless of any errors
@@ -75,22 +112,27 @@ public function __destruct()
75112

76113
protected static function hasTestRunnerSetUp(): bool
77114
{
78-
return file_exists(__DIR__.'/../runner');
115+
return file_exists(self::getRunnerPath()) && file_exists(self::getRunnerPath().'/vendor/autoload.php');
79116
}
80117

81118
public static function setUpTestRunner(): void
82119
{
83120
echo "\33[33mSetting up test runner...\33[0m This may take a while.\n";
84121

85122
$archive = 'https://github.com/hydephp/hyde/archive/refs/heads/master.zip';
86-
$target = __DIR__.'/../runner';
123+
$target = self::getRunnerPath();
124+
if (file_exists($target)) {
125+
rmdir($target);
126+
}
87127

128+
echo "\33[33mDownloading test runner scaffolding...\33[0m\n";
88129
$raw = file_get_contents($archive);
89130

90131
if ($raw === false) {
91132
throw new RuntimeException('Failed to download test runner.');
92133
}
93134

135+
echo "\33[33mExtracting archive...\33[0m\n";
94136
$zipPath = tempnam(sys_get_temp_dir(), 'hyde-master');
95137

96138
if ($zipPath === false) {
@@ -124,15 +166,30 @@ public static function setUpTestRunner(): void
124166

125167
$runner = realpath($target);
126168

169+
if ($runner === false) {
170+
throw new RuntimeException('Failed to get the real path of the test runner.');
171+
}
172+
173+
$workDir = getcwd();
174+
127175
// Junction the package source of hyde/realtime-compiler to the test runner
128-
$branch = trim(shell_exec('git rev-parse --abbrev-ref HEAD') ?: 'master');
129-
shell_exec("cd $runner && composer config repositories.realtime-compiler path ../../");
130-
shell_exec("cd $runner && composer require --dev hyde/realtime-compiler:dev-$branch --no-progress > setup.log 2>&1");
176+
$branch = getenv('HYDE_RC_BRANCH') ?: trim(shell_exec('git rev-parse --abbrev-ref HEAD') ?: 'master');
177+
echo "\33[33mInstalling hyde/realtime-compiler:dev-$branch...\33[0m\n";
178+
chdir($runner);
179+
exec('composer config repositories.realtime-compiler path '.realpath(__DIR__.'/../../'), $output, $return);
180+
if ($return !== 0) {
181+
throw new RuntimeException('Failed to add repository path.');
182+
}
183+
exec("composer require --dev hyde/realtime-compiler:dev-$branch --no-progress", $output, $return);
184+
if ($return !== 0) {
185+
throw new RuntimeException('Failed to install hyde/realtime-compiler.');
186+
}
187+
chdir($workDir);
131188
}
132189

133190
public function projectPath(string $path = ''): string
134191
{
135-
return realpath(__DIR__.'/../runner').($path ? '/'.$path : '');
192+
return realpath(self::getRunnerPath()).($path ? '/'.$path : '');
136193
}
137194

138195
public function get(string $uri): TestResponse

0 commit comments

Comments
 (0)