Skip to content

Commit

Permalink
chore(tests): add a flag to reset vendor for some tests if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Apr 10, 2024
1 parent 0a58ca6 commit 078df36
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 4 additions & 3 deletions bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

add_test(['list'], 'LayoutWithFolder', '{{ base }}/tests/fixtures/valid/layout-with-folder');
add_test(['list'], 'LayoutWithOldFolder', '{{ base }}/tests/fixtures/valid/layout-with-old-folder');
add_test([], 'ImportSamePackageWithDefaultVersion', '{{ base }}/tests/fixtures/valid/import-same-package-with-default-version', needRemote: true);
add_test([], 'ImportSamePackageWithDefaultVersion', '{{ base }}/tests/fixtures/valid/import-same-package-with-default-version', needRemote: true, needResetVendor: true);
add_test(['fs-watch'], 'WatchWithForcedTimeout', '{{ base }}/tests/fixtures/valid/watch-with-forced-timeout');

echo "\nDone.\n";
Expand Down Expand Up @@ -188,7 +188,7 @@

echo "\nDone.\n";

function add_test(array $args, string $class, ?string $cwd = null, bool $needRemote = false, bool $skipOnBinary = false)
function add_test(array $args, string $class, ?string $cwd = null, bool $needRemote = false, bool $skipOnBinary = false, bool $needResetVendor = false)
{
$class .= 'Test';
$fp = fopen(__FILE__, 'r');
Expand Down Expand Up @@ -216,6 +216,7 @@ function add_test(array $args, string $class, ?string $cwd = null, bool $needRem
'{{ exitCode }}' => $process->getExitCode(),
'{{ cwd }}' => $cwd ? ', ' . var_export($cwd, true) : '',
'{{ needRemote }}' => $needRemote ? ', needRemote: true' : '',
'{{ needResetVendor }}' => $needResetVendor ? ', needResetVendor: true' : '',
'{{ skip-on-binary }}' => match ($skipOnBinary) {
true => <<<'PHP'
Expand Down Expand Up @@ -260,7 +261,7 @@ class {{ class_name }} extends TaskTestCase
// {{ task }}
public function test(): void
{{{ skip-on-binary }}
$process = $this->runTask([{{ args }}]{{ cwd }}{{ needRemote }});
$process = $this->runTask([{{ args }}]{{ cwd }}{{ needRemote }}{{ needResetVendor }});

$this->assertSame({{ exitCode }}, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ImportSamePackageWithDefaultVersionTest extends TaskTestCase
// no task
public function test(): void
{
$process = $this->runTask([], '{{ base }}/tests/fixtures/valid/import-same-package-with-default-version', needRemote: true);
$process = $this->runTask([], '{{ base }}/tests/fixtures/valid/import-same-package-with-default-version', needRemote: true, needResetVendor: true);

$this->assertSame(0, $process->getExitCode());
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput());
Expand Down
11 changes: 9 additions & 2 deletions tests/TaskTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Castor\Tests\Helper\OutputCleaner;
use Castor\Tests\Helper\WebServerHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

abstract class TaskTestCase extends TestCase
Expand All @@ -20,7 +21,7 @@ public static function setUpBeforeClass(): void
self::$binary = 'application/x-executable' === mime_content_type(self::$castorBin);
}

public function runTask(array $args, ?string $cwd = null, bool $needRemote = false): Process
public function runTask(array $args, ?string $cwd = null, bool $needRemote = false, bool $needResetVendor = false): Process
{
$coverage = $this->getTestResultObject()?->getCodeCoverage();

Expand All @@ -40,9 +41,15 @@ public function runTask(array $args, ?string $cwd = null, bool $needRemote = fal
$extraEnv['CC_TEST_NAME'] = $testName;
}

$workingDirectory = $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..';

if ($needResetVendor) {
(new Filesystem())->remove($workingDirectory . '/.castor/vendor');
}

$process = new Process(
[self::$castorBin, '--no-ansi', ...$args],
cwd: $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..',
cwd: $workingDirectory,
env: [
'COLUMNS' => 1000,
...$extraEnv,
Expand Down

0 comments on commit 078df36

Please sign in to comment.