From 078df36245979dee23063f1c7a92daafbb61c902 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 10 Apr 2024 21:21:03 +0200 Subject: [PATCH] chore(tests): add a flag to reset vendor for some tests if needed --- bin/generate-tests.php | 7 ++++--- .../ImportSamePackageWithDefaultVersionTest.php | 2 +- tests/TaskTestCase.php | 11 +++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bin/generate-tests.php b/bin/generate-tests.php index ea739d07..3d31a4be 100755 --- a/bin/generate-tests.php +++ b/bin/generate-tests.php @@ -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"; @@ -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'); @@ -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' @@ -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()); diff --git a/tests/Generated/ImportSamePackageWithDefaultVersionTest.php b/tests/Generated/ImportSamePackageWithDefaultVersionTest.php index a0b90b6f..d681a148 100644 --- a/tests/Generated/ImportSamePackageWithDefaultVersionTest.php +++ b/tests/Generated/ImportSamePackageWithDefaultVersionTest.php @@ -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()); diff --git a/tests/TaskTestCase.php b/tests/TaskTestCase.php index 290f9763..d81c191d 100644 --- a/tests/TaskTestCase.php +++ b/tests/TaskTestCase.php @@ -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 @@ -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(); @@ -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,