From c02d48325b94d1211fd1b9a3dc5aa63570da711c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Sat, 9 Mar 2024 09:42:35 +0100 Subject: [PATCH] chore: add tests for context validation --- bin/generate-tests.php | 12 +++++++++- .../Generated/ContextGeneratorArg2Test.php | 22 +++++++++++++++++++ .../ContextGeneratorArg2Test.php.err.txt | 8 +++++++ .../ContextGeneratorArg2Test.php.output.txt | 0 .../Generated/ContextGeneratorArgTest.php | 22 +++++++++++++++++++ .../ContextGeneratorArgTest.php.err.txt | 8 +++++++ .../ContextGeneratorArgTest.php.output.txt | 0 .../ContextGeneratorNotCallableTest.php | 22 +++++++++++++++++++ ...ontextGeneratorNotCallableTest.php.err.txt | 8 +++++++ ...extGeneratorNotCallableTest.php.output.txt | 0 .../Generated/NoDefaultContextTest.php | 22 +++++++++++++++++++ .../NoDefaultContextTest.php.err.txt | 6 +++++ .../NoDefaultContextTest.php.output.txt | 0 .../Generated/TwoDefaultContextTest.php | 22 +++++++++++++++++++ .../TwoDefaultContextTest.php.err.txt | 8 +++++++ .../TwoDefaultContextTest.php.output.txt | 0 .../broken/context-generator-arg-2/castor.php | 10 +++++++++ .../broken/context-generator-arg/castor.php | 10 +++++++++ .../context-generator-not-callable/castor.php | 9 ++++++++ .../broken/no-default-context/castor.php | 16 ++++++++++++++ .../broken/two-default-context/castor.php | 16 ++++++++++++++ tests/TaskTestCase.php | 2 +- 22 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 tests/Examples/Generated/ContextGeneratorArg2Test.php create mode 100644 tests/Examples/Generated/ContextGeneratorArg2Test.php.err.txt create mode 100644 tests/Examples/Generated/ContextGeneratorArg2Test.php.output.txt create mode 100644 tests/Examples/Generated/ContextGeneratorArgTest.php create mode 100644 tests/Examples/Generated/ContextGeneratorArgTest.php.err.txt create mode 100644 tests/Examples/Generated/ContextGeneratorArgTest.php.output.txt create mode 100644 tests/Examples/Generated/ContextGeneratorNotCallableTest.php create mode 100644 tests/Examples/Generated/ContextGeneratorNotCallableTest.php.err.txt create mode 100644 tests/Examples/Generated/ContextGeneratorNotCallableTest.php.output.txt create mode 100644 tests/Examples/Generated/NoDefaultContextTest.php create mode 100644 tests/Examples/Generated/NoDefaultContextTest.php.err.txt create mode 100644 tests/Examples/Generated/NoDefaultContextTest.php.output.txt create mode 100644 tests/Examples/Generated/TwoDefaultContextTest.php create mode 100644 tests/Examples/Generated/TwoDefaultContextTest.php.err.txt create mode 100644 tests/Examples/Generated/TwoDefaultContextTest.php.output.txt create mode 100644 tests/Examples/fixtures/broken/context-generator-arg-2/castor.php create mode 100644 tests/Examples/fixtures/broken/context-generator-arg/castor.php create mode 100644 tests/Examples/fixtures/broken/context-generator-not-callable/castor.php create mode 100644 tests/Examples/fixtures/broken/no-default-context/castor.php create mode 100644 tests/Examples/fixtures/broken/two-default-context/castor.php diff --git a/bin/generate-tests.php b/bin/generate-tests.php index 687a07e8..af439573 100755 --- a/bin/generate-tests.php +++ b/bin/generate-tests.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Process; use function Symfony\Component\String\u; @@ -106,6 +107,15 @@ add_test($args, $class); } +$dirs = (new Finder()) + ->in($basePath = __DIR__ . '/../tests/Examples/fixtures/broken') + ->depth(1) +; +foreach ($dirs as $dir) { + $class = u($dir->getRelativePath())->camel()->title()->append('Test')->toString(); + add_test([], $class, '{{ base }}/tests/Examples/fixtures/broken/' . $dir->getRelativePath()); +} + add_test(['parallel:sleep', '--sleep5', '0', '--sleep7', '0', '--sleep10', '0'], 'ParallelSleepTest'); add_test(['context:context', '--context', 'run'], 'ContextContextRunTest'); add_test(['context:context', '--context', 'my_default', '-v'], 'ContextContextMyDefaultTest'); @@ -128,7 +138,7 @@ function add_test(array $args, string $class, ?string $cwd = null) $process = new Process( [\PHP_BINARY, __DIR__ . '/castor', '--no-ansi', ...$args], - cwd: $cwd ?: __DIR__ . '/../', + cwd: $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..', env: [ 'COLUMNS' => 120, 'ENDPOINT' => $_SERVER['ENDPOINT'], diff --git a/tests/Examples/Generated/ContextGeneratorArg2Test.php b/tests/Examples/Generated/ContextGeneratorArg2Test.php new file mode 100644 index 00000000..995c4c27 --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorArg2Test.php @@ -0,0 +1,22 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-arg-2'); + + $this->assertSame(1, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/ContextGeneratorArg2Test.php.err.txt b/tests/Examples/Generated/ContextGeneratorArg2Test.php.err.txt new file mode 100644 index 00000000..648c0e88 --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorArg2Test.php.err.txt @@ -0,0 +1,8 @@ + +In FunctionFinder.php line 218: + + Function "gen()" is not properly configured: + The context generator "foo" must not have arguments. + Defined in "castor.php" line 7. + + diff --git a/tests/Examples/Generated/ContextGeneratorArg2Test.php.output.txt b/tests/Examples/Generated/ContextGeneratorArg2Test.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Examples/Generated/ContextGeneratorArgTest.php b/tests/Examples/Generated/ContextGeneratorArgTest.php new file mode 100644 index 00000000..b9051317 --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorArgTest.php @@ -0,0 +1,22 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-arg'); + + $this->assertSame(1, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/ContextGeneratorArgTest.php.err.txt b/tests/Examples/Generated/ContextGeneratorArgTest.php.err.txt new file mode 100644 index 00000000..84f73c03 --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorArgTest.php.err.txt @@ -0,0 +1,8 @@ + +In FunctionFinder.php line 209: + + Function "gen()" is not properly configured: + The contexts generator must not have arguments. + Defined in "castor.php" line 7. + + diff --git a/tests/Examples/Generated/ContextGeneratorArgTest.php.output.txt b/tests/Examples/Generated/ContextGeneratorArgTest.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Examples/Generated/ContextGeneratorNotCallableTest.php b/tests/Examples/Generated/ContextGeneratorNotCallableTest.php new file mode 100644 index 00000000..c6ce84ed --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorNotCallableTest.php @@ -0,0 +1,22 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/broken/context-generator-not-callable'); + + $this->assertSame(1, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/ContextGeneratorNotCallableTest.php.err.txt b/tests/Examples/Generated/ContextGeneratorNotCallableTest.php.err.txt new file mode 100644 index 00000000..30497925 --- /dev/null +++ b/tests/Examples/Generated/ContextGeneratorNotCallableTest.php.err.txt @@ -0,0 +1,8 @@ + +In FunctionFinder.php line 214: + + Function "gen()" is not properly configured: + The context generator "foo" is not callable. + Defined in "castor.php" line 6. + + diff --git a/tests/Examples/Generated/ContextGeneratorNotCallableTest.php.output.txt b/tests/Examples/Generated/ContextGeneratorNotCallableTest.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Examples/Generated/NoDefaultContextTest.php b/tests/Examples/Generated/NoDefaultContextTest.php new file mode 100644 index 00000000..fe7286d0 --- /dev/null +++ b/tests/Examples/Generated/NoDefaultContextTest.php @@ -0,0 +1,22 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/broken/no-default-context'); + + $this->assertSame(1, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/NoDefaultContextTest.php.err.txt b/tests/Examples/Generated/NoDefaultContextTest.php.err.txt new file mode 100644 index 00000000..8400d780 --- /dev/null +++ b/tests/Examples/Generated/NoDefaultContextTest.php.err.txt @@ -0,0 +1,6 @@ + +In ContextRegistry.php line XXXX: + + Since there are multiple contexts "one", "two", you must set a "default: true" context. + + diff --git a/tests/Examples/Generated/NoDefaultContextTest.php.output.txt b/tests/Examples/Generated/NoDefaultContextTest.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Examples/Generated/TwoDefaultContextTest.php b/tests/Examples/Generated/TwoDefaultContextTest.php new file mode 100644 index 00000000..77667b72 --- /dev/null +++ b/tests/Examples/Generated/TwoDefaultContextTest.php @@ -0,0 +1,22 @@ +runTask([], '{{ base }}/tests/Examples/fixtures/broken/two-default-context'); + + $this->assertSame(1, $process->getExitCode()); + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + if (file_exists(__FILE__ . '.err.txt')) { + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } else { + $this->assertSame('', $process->getErrorOutput()); + } + } +} diff --git a/tests/Examples/Generated/TwoDefaultContextTest.php.err.txt b/tests/Examples/Generated/TwoDefaultContextTest.php.err.txt new file mode 100644 index 00000000..c5612419 --- /dev/null +++ b/tests/Examples/Generated/TwoDefaultContextTest.php.err.txt @@ -0,0 +1,8 @@ + +In ContextRegistry.php line XXXX: + + Function "two()" is not properly configured: + You cannot define two contexts with the same name "default". There is one already defined in "castor.php:7". + Defined in "castor.php" line 13. + + diff --git a/tests/Examples/Generated/TwoDefaultContextTest.php.output.txt b/tests/Examples/Generated/TwoDefaultContextTest.php.output.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/Examples/fixtures/broken/context-generator-arg-2/castor.php b/tests/Examples/fixtures/broken/context-generator-arg-2/castor.php new file mode 100644 index 00000000..f2f6c873 --- /dev/null +++ b/tests/Examples/fixtures/broken/context-generator-arg-2/castor.php @@ -0,0 +1,10 @@ + fn ($a) => new Context(); +} diff --git a/tests/Examples/fixtures/broken/context-generator-arg/castor.php b/tests/Examples/fixtures/broken/context-generator-arg/castor.php new file mode 100644 index 00000000..1ec3584c --- /dev/null +++ b/tests/Examples/fixtures/broken/context-generator-arg/castor.php @@ -0,0 +1,10 @@ + 'not a callable'; +} diff --git a/tests/Examples/fixtures/broken/no-default-context/castor.php b/tests/Examples/fixtures/broken/no-default-context/castor.php new file mode 100644 index 00000000..7e87b35c --- /dev/null +++ b/tests/Examples/fixtures/broken/no-default-context/castor.php @@ -0,0 +1,16 @@ + 120, ...$extraEnv,