Skip to content
Merged
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
9 changes: 9 additions & 0 deletions packages/realtime-compiler/.github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ jobs:
- name: Install dependencies
run: composer install

- name: Configure environment
run: |
echo "HYDE_RC_RUNNER_PATH=/tmp/hyde-rc-runner" >> $GITHUB_ENV
if [ ${{ github.event_name }} == 'push' ]; then
echo "HYDE_RC_RUNNER_BRANCH=master" >> $GITHUB_ENV
else
echo "HYDE_RC_BRANCH=$GITHUB_SHA" >> $GITHUB_ENV
fi

- name: Set up test runner
run: php -r 'require_once __DIR__."/vendor/autoload.php"; \Hyde\RealtimeCompiler\Tests\Integration\IntegrationTestCase::setUpTestRunner();'

Expand Down
1 change: 0 additions & 1 deletion packages/realtime-compiler/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
/composer.lock
/.phpunit.cache/
/.phpunit.result.cache
/tests/runner
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Hyde\RealtimeCompiler\Tests\Integration;

use InvalidArgumentException;
Expand Down Expand Up @@ -39,8 +41,9 @@ public static function setUpBeforeClass(): void
}

fclose($process);
$throwInsteadOfKill = false;
if ($throwInsteadOfKill) {

$throwOnBusyPort = false;
if ($throwOnBusyPort) {
throw new RuntimeException(sprintf('Port 8080 is already in use. (PID %s)', $pid));
} else {
// Kill the process using the port
Expand All @@ -50,13 +53,25 @@ public static function setUpBeforeClass(): void

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

// Wait for the server to start
$waitTime = time();
while (@fsockopen('localhost', 8080, $errno, $errstr, 1) === false) {
// Timeout after 5 seconds
if (time() - $waitTime > 5) {
throw new RuntimeException('Failed to start the test server.');
}

if (proc_get_status(self::$server)['running'] === false) {
break;
// Make a head request to the server to see if it's running
if (shell_exec('curl -I http://localhost:8080') === false) {
break;
}
}

// Sleep 20ms
usleep(20000);
}

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

protected static function getRunnerPath(): string
{
// Get path from the environment variable
$path = getenv('HYDE_RC_RUNNER_PATH');

if ($path === false) {
throw new RuntimeException('HYDE_RC_RUNNER_PATH environment variable is not set.');
}

// Check that it's not a child of the project root
$packageDir = realpath(__DIR__.'/../../');
if (str_starts_with($path, $packageDir)) {
throw new RuntimeException('HYDE_RC_RUNNER_PATH cannot be a child of the package root as junctioning will massivly inflate vendor directory.');
}

if (file_exists($path)) {
$path = realpath($path);
}

return $path;
}

public function __destruct()
{
// Ensure the server is stopped, regardless of any errors
Expand All @@ -75,22 +112,27 @@ public function __destruct()

protected static function hasTestRunnerSetUp(): bool
{
return file_exists(__DIR__.'/../runner');
return file_exists(self::getRunnerPath()) && file_exists(self::getRunnerPath().'/vendor/autoload.php');
}

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

$archive = 'https://github.com/hydephp/hyde/archive/refs/heads/master.zip';
$target = __DIR__.'/../runner';
$target = self::getRunnerPath();
if (file_exists($target)) {
rmdir($target);
}

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

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

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

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

$runner = realpath($target);

if ($runner === false) {
throw new RuntimeException('Failed to get the real path of the test runner.');
}

$workDir = getcwd();

// Junction the package source of hyde/realtime-compiler to the test runner
$branch = trim(shell_exec('git rev-parse --abbrev-ref HEAD') ?: 'master');
shell_exec("cd $runner && composer config repositories.realtime-compiler path ../../");
shell_exec("cd $runner && composer require --dev hyde/realtime-compiler:dev-$branch --no-progress > setup.log 2>&1");
$branch = getenv('HYDE_RC_BRANCH') ?: trim(shell_exec('git rev-parse --abbrev-ref HEAD') ?: 'master');
echo "\33[33mInstalling hyde/realtime-compiler:dev-$branch...\33[0m\n";
chdir($runner);
exec('composer config repositories.realtime-compiler path '.realpath(__DIR__.'/../../'), $output, $return);
if ($return !== 0) {
throw new RuntimeException('Failed to add repository path.');
}
exec("composer require --dev hyde/realtime-compiler:dev-$branch --no-progress", $output, $return);
if ($return !== 0) {
throw new RuntimeException('Failed to install hyde/realtime-compiler.');
}
chdir($workDir);
}

public function projectPath(string $path = ''): string
{
return realpath(__DIR__.'/../runner').($path ? '/'.$path : '');
return realpath(self::getRunnerPath()).($path ? '/'.$path : '');
}

public function get(string $uri): TestResponse
Expand Down