From 1798f2fe193c00c5907d933a5fc9101920de5069 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 17 Nov 2026 16:00:08 +0400 Subject: [PATCH 1/2] feat(core): announce the located suites with `TestSuitesLocated` before the first suite runs Suites are scanned one at a time, so before this event nothing could size a run-wide progress indicator. It carries the final suite list with `--suite` already applied, and the first scan still comes after it. Assisted-By: Claude Opus 5.5 --- core/Application/Application.php | 6 +- core/Event/TestSuite/TestSuitesLocated.php | 28 +++++++++ docs/spec/events-naming.md | 2 + skills/testo-plugin-author/SKILL.md | 5 ++ .../Feature/Runner/SuitesLocatedTest.php | 61 +++++++++++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 core/Event/TestSuite/TestSuitesLocated.php create mode 100644 tests/Application/Feature/Runner/SuitesLocatedTest.php diff --git a/core/Application/Application.php b/core/Application/Application.php index 9f2baeb9..3c5cfba7 100644 --- a/core/Application/Application.php +++ b/core/Application/Application.php @@ -30,6 +30,7 @@ use Testo\Event\Framework\SessionStarting; use Testo\Event\Framework\WorkerFinished; use Testo\Event\Framework\WorkerStarting; +use Testo\Event\TestSuite\TestSuitesLocated; use Testo\Filter; final readonly class Application @@ -125,9 +126,12 @@ public function run(PluginConfigurator|string ...$latePlugins): RunResult $discovery = 0.0; $tests = 0.0; + $suites = $container->make(SuiteLocator::class)->locate($appConfig); + $dispatcher->dispatch(new TestSuitesLocated($suites)); + # Iterate and run Test Suites $suiteResults = []; - foreach ($container->make(SuiteLocator::class)->locate($appConfig) as $config) { + foreach ($suites as $config) { # Resolve a Test Suite from config and # run a separate container scope to isolate services and plugins. $suiteResult = self::scope( diff --git a/core/Event/TestSuite/TestSuitesLocated.php b/core/Event/TestSuite/TestSuitesLocated.php new file mode 100644 index 00000000..84edd107 --- /dev/null +++ b/core/Event/TestSuite/TestSuitesLocated.php @@ -0,0 +1,28 @@ + $suites + */ + public function __construct( + public array $suites, + ) {} +} diff --git a/docs/spec/events-naming.md b/docs/spec/events-naming.md index 3af80c1a..6420b468 100644 --- a/docs/spec/events-naming.md +++ b/docs/spec/events-naming.md @@ -56,6 +56,7 @@ Session → Outermost level (entire test run lifecycle) ```php SessionStarting // Before any tests are discovered or executed WorkerStarting // Before a subprocess starts (parallel/isolated mode) + TestSuitesLocated // The suites the run will go through, before the first is scanned TestSuiteStarting // Before a test suite (collection of cases) starts TestCaseStarting // Before a test case (file-scope group of tests) starts TestPipelineStarting // Before run interceptors start @@ -203,6 +204,7 @@ WorkerFinished // After a subprocess finishes ### Suite and Case Events ```php +TestSuitesLocated // Once, with every suite the run will go through (filters applied), before the first starts TestSuiteStarting // Before a suite (configured collection of test cases) starts TestSuiteFinished // After a suite finishes TestCaseStarting // Before a test case (one class, or the functions of one file) starts diff --git a/skills/testo-plugin-author/SKILL.md b/skills/testo-plugin-author/SKILL.md index e631074e..0f885770 100644 --- a/skills/testo-plugin-author/SKILL.md +++ b/skills/testo-plugin-author/SKILL.md @@ -242,6 +242,11 @@ over those. `addListener()` takes a third `int $priority` argument, highest first. It matters when several listeners share one event and the order is part of the behaviour. +To size anything to the whole run (a progress bar, a pre-allocated report), listen to `TestSuitesLocated`: +it fires once, before the first suite is scanned, with the final `list` (`--suite` already +applied). Test counts are not known that early: suites are scanned one at a time, so a suite's cases +and tests are known only from its `TestSuiteStarting`. A suite that turns out empty never starts. + Writing a report file? Announce it via `ReportFileGenerating`/`ReportFileGenerated` instead of printing anything yourself — the flow, the `ReportInfo` card, and the priority trick for large files are in `references/reporting.md`. diff --git a/tests/Application/Feature/Runner/SuitesLocatedTest.php b/tests/Application/Feature/Runner/SuitesLocatedTest.php new file mode 100644 index 00000000..b867fabb --- /dev/null +++ b/tests/Application/Feature/Runner/SuitesLocatedTest.php @@ -0,0 +1,61 @@ + $events + */ + public function __construct(private array &$events) {} + + #[\Override] + public function configure(Container $container): void + { + $listeners = $container->get(EventListenerCollector::class); + $listeners->addListener(TestSuitesLocated::class, function (TestSuitesLocated $event): void { + $names = \array_map(static fn(SuiteConfig $suite): string => $suite->name, $event->suites); + $this->events[] = 'located: ' . \implode(', ', $names); + }); + $listeners->addListener(TestSuiteStarting::class, function (TestSuiteStarting $event): void { + $this->events[] = 'starting: ' . $event->suiteInfo->name; + }); + } + }; + + Application::createFromConfig(new ApplicationConfig( + src: [], + suites: [ + new SuiteConfig('Empty', location: new FinderConfig(include: [__DIR__ . '/../../Stub/EmptyRun'])), + new SuiteConfig('Runner', location: new FinderConfig(include: [__DIR__ . '/../../Stub/Runner'])), + ], + ))->run($recorder); + + Assert::same($events, ['located: Empty, Runner', 'starting: Runner']); + } +} From 41b4c7969455f7b79821e80af3f38299085d075b Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 17 Nov 2026 16:36:00 +0400 Subject: [PATCH 2/2] feat(output): report terminal program status (OSC 7501) for `run` and `init` The percent goes in segments: an equal share per suite, a suite's share per test case, a case's share per test. Each suite and case start resets it to its own segment start, so a miscount does not carry over. The status is written only to an interactive terminal (on Windows, only with VT processing on). It is off for quiet and plain output, and `TESTO_TERMINAL_STATUS=0` turns it off. Refs #413 Assisted-By: Claude Opus 5.5 --- bridge/symfony-console/src/Command/Init.php | 57 +++- core/Output/Terminal/ProgramStatus.php | 157 +++++++++++ core/Output/Terminal/Renderer/RunStatus.php | 153 +++++++++++ core/Output/Terminal/TerminalPlugin.php | 50 ++++ skills/testo-run-tests/SKILL.md | 5 + .../Unit/Terminal/ProgramStatusTest.php | 143 ++++++++++ tests/Output/Unit/Terminal/RunStatusTest.php | 255 ++++++++++++++++++ .../Unit/Terminal/TerminalPluginTest.php | 50 +++- 8 files changed, 860 insertions(+), 10 deletions(-) create mode 100644 core/Output/Terminal/ProgramStatus.php create mode 100644 core/Output/Terminal/Renderer/RunStatus.php create mode 100644 tests/Output/Unit/Terminal/ProgramStatusTest.php create mode 100644 tests/Output/Unit/Terminal/RunStatusTest.php diff --git a/bridge/symfony-console/src/Command/Init.php b/bridge/symfony-console/src/Command/Init.php index 33303d5e..1798aa1c 100644 --- a/bridge/symfony-console/src/Command/Init.php +++ b/bridge/symfony-console/src/Command/Init.php @@ -10,7 +10,9 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Style\SymfonyStyle; +use Testo\Output\Terminal\ProgramStatus; /** * Initializes Testo in the project: @@ -50,12 +52,14 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); + $status = self::programStatus($output); + $status->working(); try { $basePath = Path::create((string) $input->getOption('path')); self::ensureDirectory($basePath, $io); - $srcPath = self::discoverSourceDirectory($input, $basePath, $io); + $srcPath = self::discoverSourceDirectory($input, $basePath, $io, $status); $testsPath = $basePath->join('tests'); self::ensureDirectory($testsPath, $io); @@ -68,17 +72,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int # Non-interactive + existing config: bail out before touching the file. if ($configPath->isFile() && !$input->isInteractive()) { $io->warning(\sprintf('%s already exists. Skipping (non-interactive mode).', $configPath)); + $status->done('Configuration left untouched'); return Command::SUCCESS; } - if (self::writeConfig($configPath, $srcPath, $suites, $io)) { + if (self::writeConfig($configPath, $srcPath, $suites, $io, $status)) { self::printSummary($configPath, $composerKeys, $io); + $status->done('Configuration created'); } else { $io->note(\sprintf('Left existing %s untouched.', $configPath)); + $status->done('Configuration left untouched'); } } catch (\Throwable $exception) { $output->writeln(''); $output->writeln(\sprintf('%s', $exception->getMessage())); + $status->error('Initialization failed'); return Command::FAILURE; } @@ -102,8 +110,12 @@ private static function ensureDirectory(Path $path, SymfonyStyle $io): void * keeps the generated config self-consistent — paths like `src` always resolve * next to testo.php, regardless of where the user runs the command from. */ - private static function discoverSourceDirectory(InputInterface $input, Path $basePath, SymfonyStyle $io): Path - { + private static function discoverSourceDirectory( + InputInterface $input, + Path $basePath, + SymfonyStyle $io, + ProgramStatus $status, + ): Path { if ($basePath->join('src')->isDir()) { return Path::create('src'); } @@ -115,7 +127,8 @@ private static function discoverSourceDirectory(InputInterface $input, Path $bas )); } - return Path::create((string) $io->ask( + $status->blocked('Path to source code'); + $path = Path::create((string) $io->ask( question: 'Path to source code (relative to project root)', default: 'src', validator: static function (?string $value) use ($basePath): string { @@ -130,6 +143,21 @@ private static function discoverSourceDirectory(InputInterface $input, Path $bas return $value; }, )); + $status->working(); + + return $path; + } + + /** + * Program status goes where the prompts are drawn, and only when they are drawn for a person: a quiet + * or undecorated run gets none. The prompts go through {@see SymfonyStyle}, which the question helper + * does not treat as a console output, so they are drawn on stdout rather than on the error output. + */ + private static function programStatus(OutputInterface $output): ProgramStatus + { + return $output instanceof StreamOutput && $output->isDecorated() && !$output->isQuiet() + ? ProgramStatus::detect($output->getStream()) + : ProgramStatus::disabled(); } /** @@ -213,10 +241,21 @@ private static function updateComposerScripts(array $suites, Path $basePath, Sym * * @param list $suites */ - private static function writeConfig(Path $configPath, Path $srcPath, array $suites, SymfonyStyle $io): bool - { - if ($configPath->isFile() && !$io->confirm(\sprintf('%s already exists. Overwrite?', $configPath), false)) { - return false; + private static function writeConfig( + Path $configPath, + Path $srcPath, + array $suites, + SymfonyStyle $io, + ProgramStatus $status, + ): bool { + if ($configPath->isFile()) { + $status->blocked('Overwrite the existing configuration?'); + $overwrite = $io->confirm(\sprintf('%s already exists. Overwrite?', $configPath), false); + $status->working(); + + if (!$overwrite) { + return false; + } } $suitesCode = ''; diff --git a/core/Output/Terminal/ProgramStatus.php b/core/Output/Terminal/ProgramStatus.php new file mode 100644 index 00000000..27b77922 --- /dev/null +++ b/core/Output/Terminal/ProgramStatus.php @@ -0,0 +1,157 @@ +). + * + * The terminal shows the state in its tab or window chrome; terminals that do not know the protocol + * ignore it. A disabled instance writes nothing, so callers report unconditionally. + * + * ```php + * $status = ProgramStatus::detect(\STDOUT); + * $status->working(progress: 40, message: 'Unit'); + * $status->done('120 passed'); + * ``` + * + * @api + */ +final class ProgramStatus +{ + /** + * Opt-out switch: `0`, `false`, `off` or `no` turns the reports off. + */ + public const ENV_SWITCH = 'TESTO_TERMINAL_STATUS'; + + private const APP = 'testo'; + + /** + * The protocol's limit on a decoded `msg`. + */ + private const MESSAGE_LIMIT = 2048; + + private ?string $last = null; + + /** + * @param resource|null $stream Where the sequences go; null disables the reports. + */ + public function __construct( + private readonly mixed $stream, + ) {} + + /** + * A reporter for `$stream` that writes only where a person watches a terminal: the stream is an + * interactive TTY that understands escape sequences, and the user has not switched the reports off. + * + * @param resource $stream + */ + public static function detect(mixed $stream): self + { + return new self(self::isSupported($stream) && !self::isSwitchedOff() ? $stream : null); + } + + public static function disabled(): self + { + return new self(null); + } + + public function isEnabled(): bool + { + return $this->stream !== null; + } + + /** + * @param int<0, 100>|null $progress Percent done; null for an indeterminate state. + */ + public function working(?int $progress = null, string $message = ''): void + { + $this->report('working', $progress, $message); + } + + /** + * Waiting for the user to answer a question. + */ + public function blocked(string $message = ''): void + { + $this->report('blocked', null, $message, kind: 'question'); + } + + public function done(string $message = ''): void + { + $this->report('done', null, $message); + } + + public function error(string $message = ''): void + { + $this->report('error', null, $message); + } + + /** + * Removes the program's state from the terminal. + */ + public function clear(): void + { + $this->report('clear', null, ''); + } + + /** + * @param resource $stream + */ + private static function isSupported(mixed $stream): bool + { + if (!\is_resource($stream) || !\stream_isatty($stream)) { + return false; + } + + # A Windows console prints escape sequences as text unless VT processing is on for it. + return \DIRECTORY_SEPARATOR !== '\\' + || (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support($stream)); + } + + private static function isSwitchedOff(): bool + { + $value = $_SERVER[self::ENV_SWITCH] ?? $_ENV[self::ENV_SWITCH] ?? \getenv(self::ENV_SWITCH); + + return \is_string($value) && \in_array(\strtolower(\trim($value)), ['0', 'false', 'off', 'no'], true); + } + + /** + * The protocol discards a whole report whose message is not valid UTF-8, holds a control character + * (C0, DEL or C1) or runs past the limit, so all three are fixed here rather than losing the report. + */ + private static function singleLine(string $message): string + { + $message = \mb_scrub($message, 'UTF-8'); + $message = \trim((string) \preg_replace('/\p{Cc}+/u', ' ', $message)); + + return \strlen($message) <= self::MESSAGE_LIMIT + ? $message + : \mb_strcut($message, 0, self::MESSAGE_LIMIT, 'UTF-8'); + } + + private function report(string $state, ?int $progress, string $message, ?string $kind = null): void + { + if ($this->stream === null) { + return; + } + + $pairs = ['state=' . $state, 'app=' . self::APP]; + $progress === null or $pairs[] = 'progress=' . \max(0, \min(100, $progress)); + $kind === null or $pairs[] = 'kind=' . $kind; + $message = self::singleLine($message); + $message === '' or $pairs[] = 'msg=' . \base64_encode($message); + + $sequence = "\033]7501;" . \implode(':', $pairs) . "\033\\"; + + # Each report replaces the previous one whole, so a repeat changes nothing on the terminal side. + if ($sequence === $this->last) { + return; + } + + $this->last = $sequence; + @\fwrite($this->stream, $sequence); + } +} diff --git a/core/Output/Terminal/Renderer/RunStatus.php b/core/Output/Terminal/Renderer/RunStatus.php new file mode 100644 index 00000000..e0b57197 --- /dev/null +++ b/core/Output/Terminal/Renderer/RunStatus.php @@ -0,0 +1,153 @@ + */ + private array $suites = []; + + /** + * Index of the running suite in {@see $suites}; null while it has no segment. + */ + private ?int $suite = null; + + /** + * Index in {@see $suites} the next suite is looked up from, so a repeated name takes its next slot. + */ + private int $nextSuite = 0; + + private int $suiteStart = 0; + private int $suiteEnd = 0; + + /** @var list */ + private array $cases = []; + + private bool $inCase = false; + private int $caseStart = 0; + private int $caseEnd = 0; + private int $caseTests = 0; + private int $caseFinished = 0; + private string $suiteName = ''; + + public function __construct( + private readonly ProgramStatus $status, + ) {} + + public function sessionStarted(): void + { + $this->status->working(); + } + + /** + * @param list $suites + */ + public function suitesLocated(array $suites): void + { + $this->suites = \array_map(static fn(SuiteConfig $suite): string => $suite->name, $suites); + $this->nextSuite = 0; + $this->suiteEnd = 0; + } + + public function suiteStarted(SuiteInfo $suite): void + { + $this->suiteName = $suite->name; + $this->cases = $suite->testCases->getCases(); + $this->inCase = false; + + $index = \array_search($suite->name, \array_slice($this->suites, $this->nextSuite, preserve_keys: true), true); + if ($index === false) { + # A suite the located list did not announce has no segment to stand on. + $this->suite = null; + $this->status->working(message: $this->suiteName); + return; + } + + $this->suite = $index; + $this->nextSuite = $index + 1; + $this->suiteStart = $this->suiteEnd; + $this->suiteEnd = $this->suiteStart + + \intdiv(self::SCALE - $this->suiteStart, \count($this->suites) - $index); + $this->report($this->suiteStart); + } + + public function caseStarted(CaseInfo $case): void + { + $index = \array_search($case->definition, $this->cases, true); + $this->inCase = $this->suite !== null && $index !== false; + if (!$this->inCase) { + return; + } + + $width = $this->suiteEnd - $this->suiteStart; + $count = \count($this->cases); + $this->caseStart = $this->suiteStart + \intdiv($width * $index, $count); + $this->caseEnd = $this->suiteStart + \intdiv($width * ($index + 1), $count); + $this->caseTests = \count($case->definition->tests->getTests()); + $this->caseFinished = 0; + $this->report($this->caseStart); + } + + public function testFinished(): void + { + if (!$this->inCase || $this->caseTests === 0) { + return; + } + + $this->caseFinished = \min($this->caseFinished + 1, $this->caseTests); + $this->report( + $this->caseStart + \intdiv(($this->caseEnd - $this->caseStart) * $this->caseFinished, $this->caseTests), + ); + } + + public function sessionFinished(RunResult $result): void + { + $summary = $result->summary; + $total = $summary->total(); + $skipped = $summary->count(Status::Skipped); + $failed = $summary->failed() + $summary->count(Status::Aborted); + + match (true) { + $result->status->isSuccessful() => $this->status->done( + \sprintf('%d passed', $summary->passed()) . ($skipped > 0 ? \sprintf(', %d skipped', $skipped) : ''), + ), + $total === 0 => $this->status->error('No tests executed'), + $failed === 0 => $this->status->error('Run failed'), + default => $this->status->error(\sprintf('%d failed of %d', $failed, $total)), + }; + } + + private function report(int $position): void + { + $this->status->working(\intdiv(100 * $position, self::SCALE), $this->suiteName); + } +} diff --git a/core/Output/Terminal/TerminalPlugin.php b/core/Output/Terminal/TerminalPlugin.php index 675c4663..1c71be99 100644 --- a/core/Output/Terminal/TerminalPlugin.php +++ b/core/Output/Terminal/TerminalPlugin.php @@ -7,6 +7,7 @@ use Internal\Container\Container; use Testo\Common\EventListenerCollector; use Testo\Common\PluginConfigurator; +use Testo\Core\Value\Verbosity; use Testo\Event\Framework\SessionFinished; use Testo\Event\Framework\SessionStarting; use Testo\Event\Message\MessageReceived; @@ -19,8 +20,11 @@ use Testo\Event\TestCase\TestCaseFinished; use Testo\Event\TestCase\TestCaseStarting; use Testo\Event\TestSuite\TestSuiteFinished; +use Testo\Event\TestSuite\TestSuitesLocated; use Testo\Event\TestSuite\TestSuiteStarting; +use Testo\Output\ConsoleStreams; use Testo\Output\Terminal\Renderer\ColorMode; +use Testo\Output\Terminal\Renderer\RunStatus; use Testo\Output\Terminal\Renderer\Style; use Testo\Output\Terminal\Renderer\TerminalLogger; @@ -64,6 +68,8 @@ public function configure(Container $container): void $listeners->addListener(SessionStarting::class, $this->onSessionStarting(...)); $listeners->addListener(SessionFinished::class, $this->onSessionFinished(...)); + $this->listenRunStatus($listeners, self::programStatus($container, $colorMode)); + // Report files announced by any reporter plugin $listeners->addListener(ReportFileGenerated::class, $this->onReportFileGenerated(...)); @@ -90,6 +96,50 @@ public function configure(Container $container): void $listeners->addListener(TestSuiteFinished::class, $this->onTestSuiteFinished(...)); } + /** + * A bound {@see ProgramStatus} wins; otherwise the reports follow the terminal and stay off when the + * user asked for quiet or plain output. + */ + private static function programStatus(Container $container, ColorMode $colorMode): ProgramStatus + { + if ($container->has(ProgramStatus::class)) { + return $container->get(ProgramStatus::class); + } + + $quiet = $container->has(Verbosity::class) && $container->get(Verbosity::class) === Verbosity::Quiet; + + return $quiet || $colorMode === ColorMode::Never + ? ProgramStatus::disabled() + : ProgramStatus::detect($container->get(ConsoleStreams::class)->stdout); + } + + private function listenRunStatus(EventListenerCollector $listeners, ProgramStatus $status): void + { + if (!$status->isEnabled()) { + return; + } + + $run = new RunStatus($status); + $listeners->addListener(SessionStarting::class, static fn() => $run->sessionStarted()); + $listeners->addListener( + TestSuitesLocated::class, + static fn(TestSuitesLocated $event) => $run->suitesLocated($event->suites), + ); + $listeners->addListener( + TestSuiteStarting::class, + static fn(TestSuiteStarting $event) => $run->suiteStarted($event->suiteInfo), + ); + $listeners->addListener( + TestCaseStarting::class, + static fn(TestCaseStarting $event) => $run->caseStarted($event->caseInfo), + ); + $listeners->addListener(TestPipelineFinished::class, static fn() => $run->testFinished()); + $listeners->addListener( + SessionFinished::class, + static fn(SessionFinished $event) => $run->sessionFinished($event->result), + ); + } + private function onSessionStarting(SessionStarting $event): void { $this->logger->ensureHeader(); diff --git a/skills/testo-run-tests/SKILL.md b/skills/testo-run-tests/SKILL.md index 9069bd60..2d1ce1bd 100644 --- a/skills/testo-run-tests/SKILL.md +++ b/skills/testo-run-tests/SKILL.md @@ -115,4 +115,9 @@ over-narrow filter — widen it (a typo in `--filter`, a `--path` that matches n `--log-report=` (the full run as a versioned JSON document — the data behind the HTML), `--teamcity` — reports for CI and IDEs, not for agent parsing. - `--config=path/to/testo.php` when the config is not at the project root. +- In an interactive terminal the default renderer also reports the run's progress and outcome to the + terminal itself (OSC 7501 program status, shown in the tab by terminals that support it). It is off + for `--json`, `--teamcity`, `--quiet`, `--no-ansi`, `NO_COLOR` and redirected output; set + `TESTO_TERMINAL_STATUS=0` to turn it off everywhere. A Composer script (`composer test`) on Windows + counts as redirected output: Composer pipes it, so run `vendor/bin/testo` directly to see the status. - Full flag list: `vendor/bin/testo --help`. diff --git a/tests/Output/Unit/Terminal/ProgramStatusTest.php b/tests/Output/Unit/Terminal/ProgramStatusTest.php new file mode 100644 index 00000000..91d12988 --- /dev/null +++ b/tests/Output/Unit/Terminal/ProgramStatusTest.php @@ -0,0 +1,143 @@ + $status->working(40, 'Unit')); + + Assert::same($output, "\033]7501;state=working:app=testo:progress=40:msg=VW5pdA==\033\\"); + } + + public function indeterminateWorkingOmitsProgressAndEmptyMessage(): void + { + $output = self::capture(static fn(ProgramStatus $status) => $status->working()); + + Assert::same($output, "\033]7501;state=working:app=testo\033\\"); + } + + public function blockedIsAQuestion(): void + { + $output = self::capture(static fn(ProgramStatus $status) => $status->blocked('Overwrite?')); + + Assert::string($output)->contains('state=blocked:app=testo:kind=question:msg='); + } + + public function finalStatesCarryNoProgress(): void + { + $output = self::capture(static function (ProgramStatus $status): void { + $status->done('ok'); + $status->error('bad'); + $status->clear(); + }); + + Assert::same(\substr_count($output, 'progress='), 0); + Assert::string($output)->contains('state=done:app=testo:msg=b2s='); + Assert::string($output)->contains('state=error:app=testo:msg=YmFk'); + Assert::string($output)->contains('state=clear:app=testo'); + } + + public function progressIsClampedToPercent(): void + { + $output = self::capture(static function (ProgramStatus $status): void { + $status->working(150); + $status->working(-5); + }); + + Assert::string($output)->contains('progress=100'); + Assert::string($output)->contains('progress=0'); + } + + public function messageIsFoldedIntoOneLine(): void + { + $output = self::capture(static fn(ProgramStatus $status) => $status->done("two\nlines\t")); + + // The terminal discards a report whose message decodes to a control character. + Assert::string($output)->contains('msg=' . \base64_encode('two lines')); + } + + public function c1ControlsAndBrokenUtf8AreNotSent(): void + { + $output = self::capture(static fn(ProgramStatus $status) => $status->done("a\u{0085}b\xFFc")); + + \preg_match('/msg=([^\033]+)/', $output, $match); + $decoded = (string) \base64_decode($match[1], true); + Assert::true(\mb_check_encoding($decoded, 'UTF-8')); + Assert::same(\preg_match('/\p{Cc}/u', $decoded), 0); + Assert::string($decoded)->startsWith('a b'); + } + + public function oversizedMessageIsCutOnACharacterBoundary(): void + { + $output = self::capture(static fn(ProgramStatus $status) => $status->done(\str_repeat('я', 1500))); + + \preg_match('/msg=([^\033]+)/', $output, $match); + $decoded = \base64_decode($match[1], true); + Assert::same(\strlen((string) $decoded), 2048); + Assert::true(\mb_check_encoding((string) $decoded, 'UTF-8')); + } + + public function aRepeatedReportIsNotWrittenAgain(): void + { + $output = self::capture(static function (ProgramStatus $status): void { + $status->working(10, 'Unit'); + $status->working(10, 'Unit'); + $status->working(11, 'Unit'); + }); + + Assert::same(\substr_count($output, "\033]7501;"), 2); + } + + public function aDisabledReporterTakesReportsSilently(): void + { + $status = ProgramStatus::disabled(); + + $status->working(10); + $status->done(); + + Assert::false($status->isEnabled()); + } + + public function aStreamThatIsNotATerminalIsNotReportedTo(): void + { + $stream = \fopen('php://memory', 'w+'); + \assert($stream !== false); + + Assert::false(ProgramStatus::detect($stream)->isEnabled()); + + \fclose($stream); + } + + /** + * @param \Closure(ProgramStatus): void $scenario + */ + private static function capture(\Closure $scenario): string + { + $stream = \fopen('php://memory', 'w+'); + \assert($stream !== false); + + try { + $scenario(new ProgramStatus($stream)); + + \rewind($stream); + return (string) \stream_get_contents($stream); + } finally { + \fclose($stream); + } + } +} diff --git a/tests/Output/Unit/Terminal/RunStatusTest.php b/tests/Output/Unit/Terminal/RunStatusTest.php new file mode 100644 index 00000000..1113884e --- /dev/null +++ b/tests/Output/Unit/Terminal/RunStatusTest.php @@ -0,0 +1,255 @@ +suitesLocated([self::config('A'), self::config('B')]); + self::runSuite($run, $first); + self::runSuite($run, $second); + }); + + // A is 0–50: its first case takes 0–25 in two tests, its second 25–50 in one. B is 50–100, and + // its start repeats 50 to name the new suite. + Assert::same(self::progress($output), [0, 12, 25, 50, 50, 62, 75, 87, 100]); + } + + public function aFinishedRunReachesExactlyOneHundred(): void + { + $suite = self::suite('A', ...\array_map(static fn(): CaseDefinition => self::case(1), \range(1, 23))); + + $output = self::capture(static function (RunStatus $run) use ($suite): void { + $run->suitesLocated([self::config('A')]); + self::runSuite($run, $suite); + }); + + Assert::same(\array_slice(self::progress($output), -1), [100]); + } + + public function progressNeverStepsBackAtASegmentBoundary(): void + { + $names = \array_map(static fn(int $i): string => "S$i", \range(1, 9)); + $suites = \array_map( + static fn(string $name): SuiteInfo => self::suite( + $name, + ...\array_map(static fn(): CaseDefinition => self::case(1), \range(1, 10)), + ), + $names, + ); + + $output = self::capture(static function (RunStatus $run) use ($names, $suites): void { + $run->suitesLocated(\array_map(self::config(...), $names)); + foreach ($suites as $suite) { + self::runSuite($run, $suite); + } + }); + + $progress = self::progress($output); + $sorted = $progress; + \sort($sorted); + Assert::same($progress, $sorted); + Assert::same(\end($progress), 100); + } + + public function aRepeatedSuiteNameTakesItsNextSlot(): void + { + $first = self::suite('A', self::case(1)); + $second = self::suite('A', self::case(1)); + + $output = self::capture(static function (RunStatus $run) use ($first, $second): void { + $run->suitesLocated([self::config('A'), self::config('A')]); + self::runSuite($run, $first); + self::runSuite($run, $second); + }); + + Assert::same(self::progress($output), [0, 50, 100]); + } + + public function suitesSkippedForHavingNoCasesLeaveTheirShareToTheRest(): void + { + $last = self::suite('C', self::case(1)); + + $output = self::capture(static function (RunStatus $run) use ($last): void { + $run->suitesLocated([self::config('A'), self::config('B'), self::config('C')]); + self::runSuite($run, $last); + }); + + // A filtered run that keeps only the last suite still goes from 0 to 100. + Assert::same(self::progress($output), [0, 100]); + } + + public function aCaseRunningMoreTestsThanCountedStaysInsideItsSegment(): void + { + $suite = self::suite('A', self::case(1), self::case(1)); + + $output = self::capture(static function (RunStatus $run) use ($suite): void { + $run->suitesLocated([self::config('A')]); + $run->suiteStarted($suite); + [$first] = $suite->testCases->getCases(); + $run->caseStarted(self::caseInfo($first)); + $run->testFinished(); + $run->testFinished(); + }); + + Assert::same(self::progress($output), [0, 50]); + } + + public function withoutLocatedSuitesTheProgressIsIndeterminate(): void + { + $suite = self::suite('A', self::case(2)); + + $output = self::capture(static fn(RunStatus $run) => self::runSuite($run, $suite)); + + Assert::same(self::progress($output), []); + Assert::string($output)->contains('state=working'); + } + + public function aSuccessfulRunIsDone(): void + { + $output = self::finish(Status::Passed, ['Passed' => 3]); + + Assert::string($output)->contains('state=done:app=testo:msg=' . \base64_encode('3 passed')); + } + + public function aFailedRunIsAnError(): void + { + $output = self::finish(Status::Failed, ['Passed' => 3, 'Failed' => 1]); + + Assert::string($output)->contains('state=error:app=testo:msg=' . \base64_encode('1 failed of 4')); + } + + public function anAbortedTestCountsAsFailed(): void + { + $output = self::finish(Status::Failed, ['Passed' => 3, 'Aborted' => 2]); + + Assert::string($output)->contains('state=error:app=testo:msg=' . \base64_encode('2 failed of 5')); + } + + public function skippedTestsAreNamedInTheOutcome(): void + { + $output = self::finish(Status::Passed, ['Skipped' => 4]); + + Assert::string($output)->contains('state=done:app=testo:msg=' . \base64_encode('0 passed, 4 skipped')); + } + + public function anEmptyRunIsAnError(): void + { + $output = self::finish(Status::Risky, []); + + Assert::string($output)->contains('state=error:app=testo:msg=' . \base64_encode('No tests executed')); + } + + /** + * @param array> $counts + */ + private static function finish(Status $status, array $counts): string + { + return self::capture(static fn(RunStatus $run) => $run->sessionFinished( + new RunResult([], status: $status, summary: new Summary($counts)), + )); + } + + private static function runSuite(RunStatus $run, SuiteInfo $suite): void + { + $run->suiteStarted($suite); + foreach ($suite->testCases->getCases() as $case) { + $run->caseStarted(self::caseInfo($case)); + foreach ($case->tests->getTests() as $_) { + $run->testFinished(); + } + } + } + + /** + * @return list + */ + private static function progress(string $output): array + { + \preg_match_all('/progress=(\d+)/', $output, $matches); + + return \array_map(\intval(...), $matches[1]); + } + + /** + * @param non-empty-string $name + */ + private static function config(string $name): SuiteConfig + { + return new SuiteConfig($name, location: []); + } + + /** + * @param non-empty-string $name + */ + private static function suite(string $name, CaseDefinition ...$cases): SuiteInfo + { + return new SuiteInfo($name, CaseDefinitions::fromArray(...$cases)); + } + + private static function case(int $tests): CaseDefinition + { + $test = new TestDefinition(new \ReflectionFunction(static function (): void {})); + + return new CaseDefinition( + name: null, + type: 'test', + file: Path::create(__FILE__), + tests: TestDefinitions::fromArray(...\array_fill(0, $tests, $test)), + ); + } + + private static function caseInfo(CaseDefinition $case): CaseInfo + { + return new CaseInfo($case, new SuiteIdentity('A')); + } + + /** + * @param \Closure(RunStatus): void $scenario + */ + private static function capture(\Closure $scenario): string + { + $stream = \fopen('php://memory', 'w+'); + \assert($stream !== false); + + try { + $scenario(new RunStatus(new ProgramStatus($stream))); + + \rewind($stream); + return (string) \stream_get_contents($stream); + } finally { + \fclose($stream); + } + } +} diff --git a/tests/Output/Unit/Terminal/TerminalPluginTest.php b/tests/Output/Unit/Terminal/TerminalPluginTest.php index 8f4ba052..8e13d0e3 100644 --- a/tests/Output/Unit/Terminal/TerminalPluginTest.php +++ b/tests/Output/Unit/Terminal/TerminalPluginTest.php @@ -7,20 +7,27 @@ use Internal\Path; use Internal\Container\ObjectContainer; use Testo\Application\Internal\EventDispatcher; +use Testo\Application\Config\SuiteConfig; use Testo\Assert; use Testo\Codecov\Covers; use Testo\Common\EventListenerCollector; use Testo\Core\Context\CaseInfo; use Testo\Core\Context\Identity\SuiteIdentity; +use Testo\Core\Context\RunResult; +use Testo\Core\Context\SuiteInfo; use Testo\Core\Context\TestInfo; use Testo\Core\Context\TestResult; use Testo\Core\Definition\CaseDefinition; +use Testo\Core\Definition\CaseDefinitions; use Testo\Core\Definition\TestDefinition; use Testo\Core\Log\Level; use Testo\Core\Log\Message; use Testo\Core\Value\Status; +use Testo\Core\Value\Summary; use Testo\Core\Value\Verbosity; use Testo\Core\Report\ReportInfo; +use Testo\Event\Framework\SessionFinished; +use Testo\Event\Framework\SessionStarting; use Testo\Event\Message\MessageReceived; use Testo\Event\Report\ReportFileGenerated; use Testo\Event\Report\ReportFileGenerating; @@ -29,7 +36,11 @@ use Testo\Event\Test\TestDataSetFinished; use Testo\Event\Test\TestDataSetStarting; use Testo\Event\Test\TestPipelineFinished; +use Testo\Event\TestCase\TestCaseStarting; +use Testo\Event\TestSuite\TestSuitesLocated; +use Testo\Event\TestSuite\TestSuiteStarting; use Testo\Output\ConsoleStreams; +use Testo\Output\Terminal\ProgramStatus; use Testo\Output\Terminal\Renderer\ColorMode; use Testo\Output\Terminal\Renderer\Style; use Testo\Output\Terminal\TerminalPlugin; @@ -172,6 +183,41 @@ public function theStartOfAWriteIsNotStatedInATerminal(): void Assert::string($output)->notContains('Testo HTML report'); } + public function aRunIsReportedAsProgramStatus(): void + { + $info = self::makeTestInfo('passingTest'); + $suite = new SuiteInfo('Output/Unit', CaseDefinitions::fromArray($info->caseInfo->definition)); + + $output = self::capture( + static function (EventDispatcher $dispatcher) use ($info, $suite): void { + $dispatcher->dispatch(new SessionStarting()); + $dispatcher->dispatch(new TestSuitesLocated([new SuiteConfig('Output/Unit', location: [])])); + $dispatcher->dispatch(new TestSuiteStarting($suite)); + $dispatcher->dispatch(new TestCaseStarting($info->caseInfo)); + $dispatcher->dispatch(new TestPipelineFinished($info, self::passed($info))); + $dispatcher->dispatch(new SessionFinished(new RunResult( + [], + status: Status::Passed, + summary: new Summary(['Passed' => 1]), + ))); + }, + status: true, + ); + + Assert::string($output)->contains("\033]7501;state=working:app=testo\033\\"); + Assert::string($output)->contains('state=working:app=testo:progress=0:msg=' . \base64_encode('Output/Unit')); + Assert::string($output)->contains('state=done:app=testo'); + } + + public function withoutATerminalNoProgramStatusIsWritten(): void + { + $output = self::capture(static function (EventDispatcher $dispatcher): void { + $dispatcher->dispatch(new SessionStarting()); + }); + + Assert::string($output)->notContains("\033]7501;"); + } + private static function report(): ReportInfo { return new ReportInfo('html', 'Testo HTML report', Path::create('runtime/report/index.html')); @@ -182,8 +228,9 @@ private static function report(): ReportInfo * terminal stream. Verbose, because that is the verbosity at which output streams live. * * @param \Closure(EventDispatcher): void $scenario + * @param bool $status Report program status to the terminal stream, as on an interactive terminal. */ - private static function capture(\Closure $scenario): string + private static function capture(\Closure $scenario, bool $status = false): string { $stdout = \fopen('php://memory', 'w+'); $stderr = \fopen('php://memory', 'w+'); @@ -199,6 +246,7 @@ private static function capture(\Closure $scenario): string $container->set($dispatcher, EventListenerCollector::class); $container->set(new ConsoleStreams($stdout, $stderr), ConsoleStreams::class); $container->set(Verbosity::Verbose, Verbosity::class); + $status and $container->set(new ProgramStatus($stdout), ProgramStatus::class); (new TerminalPlugin(ColorMode::Never))->configure($container); $scenario($dispatcher);