Skip to content

Commit

Permalink
Restore support for Teamcity and Testdox (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Feb 4, 2023
1 parent aa6db03 commit d3b3648
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 353 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"phpunit/php-code-coverage": "^10.0",
"phpunit/php-file-iterator": "^4.0",
"phpunit/php-timer": "^6.0",
"phpunit/phpunit": "^10.0.1",
"phpunit/phpunit": "^10.0.3",
"sebastian/environment": "^6.0",
"symfony/console": "^6.2.5",
"symfony/process": "^6.2.5"
Expand Down
6 changes: 6 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ public static function setInputDefinition(InputDefinition $inputDefinition): voi
'@see PHPUnit guide, chapter: ' . $chapter = 'Reporting',
Configuration::COLOR_DEFAULT,
),
new InputOption(
'no-progress',
null,
InputOption::VALUE_NONE,
'@see PHPUnit guide, chapter: ' . $chapter,
),
new InputOption(
'display-incomplete',
null,
Expand Down
4 changes: 1 addition & 3 deletions src/ParaTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$runner = new $runnerClass($options, $output);
assert($runner instanceof RunnerInterface);

$runner->run();

return $runner->getExitCode();
return $runner->run();
}

private function displayHelp(OutputInterface $output): int
Expand Down
4 changes: 1 addition & 3 deletions src/RunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ interface RunnerInterface
public const FAILURE_EXIT = 1;
public const EXCEPTION_EXIT = 2;

public function run(): void;

public function getExitCode(): int;
public function run(): int;
}
117 changes: 84 additions & 33 deletions src/WrapperRunner/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function fread;
use function fseek;
use function ftell;
use function fwrite;
use function preg_replace;
use function sprintf;
use function str_repeat;
Expand All @@ -46,10 +47,6 @@ final class ResultPrinter
private int $column = 0;
private int $casesProcessed = 0;
private int $numberOfColumns = 80;
/** @var bool */
private $needsTeamcity;
/** @var bool */
private $printsTeamcity;
/** @var resource|null */
private $teamcityLogFileHandle;
/** @var array<non-empty-string, int> */
Expand All @@ -74,16 +71,14 @@ public function flush(): void
{
}
};
// $this->printsTeamcity = $this->options->teamcity();
// $this->needsTeamcity = $this->options->needsTeamcity();
//
// if (($teamcityLogFile = $this->options->logTeamcity()) === null) {
// return;
// }
//
// $teamcityLogFileHandle = fopen($teamcityLogFile, 'ab+');
// assert($teamcityLogFileHandle !== false);
// $this->teamcityLogFileHandle = $teamcityLogFileHandle;

if (! $this->options->configuration->hasLogfileTeamcity()) {
return;
}

$teamcityLogFileHandle = fopen($this->options->configuration->logfileTeamcity(), 'ab+');
assert($teamcityLogFileHandle !== false);
$this->teamcityLogFileHandle = $teamcityLogFileHandle;
}

public function setTestCount(int $testCount): void
Expand Down Expand Up @@ -135,14 +130,67 @@ public function start(): void
$output->write("\n");
}

public function println(string $string = ''): void
/** @param list<SplFileInfo> $teamcityFiles */
public function printFeedback(SplFileInfo $progressFile, array $teamcityFiles): void
{
$this->column = 0;
$this->output->write($string . "\n");
if ($this->options->needsTeamcity) {
$teamcityProgress = $this->tailMultiple($teamcityFiles);

if ($this->teamcityLogFileHandle !== null) {
fwrite($this->teamcityLogFileHandle, $teamcityProgress);
}
}

if ($this->options->configuration->outputIsTeamCity()) {
assert(isset($teamcityProgress));
$this->output->write($teamcityProgress);

return;
}

if ($this->options->configuration->noProgress()) {
return;
}

$feedbackItems = $this->tail($progressFile);
$feedbackItems = preg_replace('/ +\\d+ \\/ \\d+ \\(\\d+%\\)\\s*/', '', $feedbackItems);

$actualTestCount = strlen($feedbackItems);
for ($index = 0; $index < $actualTestCount; ++$index) {
$this->printFeedbackItem($feedbackItems[$index]);
}
}

public function printResults(TestResult $testResult): void
/**
* @param list<SplFileInfo> $teamcityFiles
* @param list<SplFileInfo> $testdoxFiles
*/
public function printResults(TestResult $testResult, array $teamcityFiles, array $testdoxFiles): void
{
if ($this->options->needsTeamcity) {
$teamcityProgress = $this->tailMultiple($teamcityFiles);

if ($this->teamcityLogFileHandle !== null) {
fwrite($this->teamcityLogFileHandle, $teamcityProgress);
$resource = $this->teamcityLogFileHandle;
$this->teamcityLogFileHandle = null;
fclose($resource);
}
}

if ($this->options->configuration->outputIsTeamCity()) {
assert(isset($teamcityProgress));
$this->output->write($teamcityProgress);

return;
}

if ($this->options->configuration->outputIsTestDox()) {
$this->output->write($this->tailMultiple($testdoxFiles));

return;
}

$resultPrinter = new DefaultResultPrinter(
$this->printer,
$this->options->configuration->displayDetailsOnIncompleteTests(),
Expand All @@ -164,17 +212,6 @@ public function printResults(TestResult $testResult): void
$summaryPrinter->print($testResult);
}

public function printFeedback(SplFileInfo $progressFile): void
{
$feedbackItems = $this->tail($progressFile);
$feedbackItems = preg_replace('/ +\\d+ \\/ \\d+ \\(\\d+%\\)\\s*/', '', $feedbackItems);

$actualTestCount = strlen($feedbackItems);
for ($index = 0; $index < $actualTestCount; ++$index) {
$this->printFeedbackItem($feedbackItems[$index]);
}
}

private function printFeedbackItem(string $item): void
{
$this->printFeedbackItemColor($item);
Expand All @@ -192,8 +229,7 @@ private function printFeedbackItem(string $item): void
$this->output->write(str_repeat(' ', $pad));
}

$this->output->write($this->getProgress());
$this->println();
$this->output->write($this->getProgress() . "\n");
}

private function printFeedbackItemColor(string $item): void
Expand Down Expand Up @@ -227,9 +263,24 @@ private function colorizeTextBox(string $color, string $buffer): string
return Color::colorizeTextBox($color, $buffer);
}

private function tail(SplFileInfo $progressFile): string
/** @param list<SplFileInfo> $files */
private function tailMultiple(array $files): string
{
$content = '';
foreach ($files as $file) {
if (! $file->isFile()) {
continue;
}

$content .= $this->tail($file);
}

return $content;
}

private function tail(SplFileInfo $file): string
{
$path = $progressFile->getPathname();
$path = $file->getPathname();
$handle = fopen($path, 'r');
assert($handle !== false);
$fseek = fseek($handle, $this->tailPositions[$path] ?? 0);
Expand Down
47 changes: 37 additions & 10 deletions src/WrapperRunner/WrapperRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function file_get_contents;
use function max;
use function realpath;
use function unlink;
use function unserialize;
use function usleep;

Expand Down Expand Up @@ -86,7 +87,7 @@ public function __construct(
$this->parameters = $parameters;
}

public function run(): void
public function run(): int
{
ExcludeList::addDirectory(dirname(__DIR__));
TestResultFacade::init();
Expand All @@ -100,7 +101,8 @@ public function run(): void
$this->startWorkers();
$this->assignAllPendingTests();
$this->waitForAllToFinish();
$this->complete($result);

return $this->complete($result);
}

public function getExitCode(): int
Expand Down Expand Up @@ -138,10 +140,7 @@ private function assignAllPendingTests(): void

if (
$this->exitcode > 0
&& (
$this->options->configuration->stopOnFailure()
|| $this->options->configuration->stopOnError()
)
&& $this->options->configuration->stopOnFailure()
) {
$this->pending = [];
} elseif (($pending = array_shift($this->pending)) !== null) {
Expand All @@ -157,7 +156,10 @@ private function assignAllPendingTests(): void
private function flushWorker(WrapperWorker $worker): void
{
$this->exitcode = max($this->exitcode, $worker->getExitCode());
$this->printer->printFeedback($worker->progressFile);
$this->printer->printFeedback(
$worker->progressFile,
$this->teamcityFiles,
);
$worker->reset();
}

Expand Down Expand Up @@ -229,7 +231,7 @@ private function destroyWorker(int $token): void
unset($this->workers[$token]);
}

private function complete(TestResult $testResultSum): void
private function complete(TestResult $testResultSum): int
{
foreach ($this->testresultFiles as $testresultFile) {
if (! $testresultFile->isFile()) {
Expand All @@ -248,6 +250,7 @@ private function complete(TestResult $testResultSum): void
array_merge_recursive($testResultSum->testErroredEvents(), $testResult->testErroredEvents()),
array_merge_recursive($testResultSum->testFailedEvents(), $testResult->testFailedEvents()),
array_merge_recursive($testResultSum->testConsideredRiskyEvents(), $testResult->testConsideredRiskyEvents()),
array_merge_recursive($testResultSum->testSuiteSkippedEvents(), $testResult->testSuiteSkippedEvents()),
array_merge_recursive($testResultSum->testSkippedEvents(), $testResult->testSkippedEvents()),
array_merge_recursive($testResultSum->testMarkedIncompleteEvents(), $testResult->testMarkedIncompleteEvents()),
array_merge_recursive($testResultSum->testTriggeredDeprecationEvents(), $testResult->testTriggeredDeprecationEvents()),
Expand All @@ -265,18 +268,30 @@ private function complete(TestResult $testResultSum): void
);
}

$this->printer->printResults($testResultSum);
$this->printer->printResults(
$testResultSum,
$this->teamcityFiles,
$this->testdoxFiles,
);
$this->generateCodeCoverageReports();
$this->generateLogs();

$this->exitcode = (new ShellExitCodeCalculator())->calculate(
$exitcode = (new ShellExitCodeCalculator())->calculate(
$this->options->configuration->failOnEmptyTestSuite(),
$this->options->configuration->failOnRisky(),
$this->options->configuration->failOnWarning(),
$this->options->configuration->failOnIncomplete(),
$this->options->configuration->failOnSkipped(),
$testResultSum,
);

$this->clearFiles($this->testresultFiles);
$this->clearFiles($this->coverageFiles);
$this->clearFiles($this->junitFiles);
$this->clearFiles($this->teamcityFiles);
$this->clearFiles($this->testdoxFiles);

return $exitcode;
}

protected function generateCodeCoverageReports(): void
Expand Down Expand Up @@ -309,4 +324,16 @@ private function generateLogs(): void
$this->options->configuration->logfileJunit(),
);
}

/** @param list<SplFileInfo> $files */
private function clearFiles(array $files): void
{
foreach ($files as $file) {
if (! $file->isFile()) {
continue;
}

unlink($file->getPathname());
}
}
}
22 changes: 5 additions & 17 deletions test/RunnerResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@

namespace ParaTest\Tests;

/** @immutable */
final class RunnerResult
{
private int $exitCode;
private string $output;

public function __construct(int $exitCode, string $output)
{
$this->exitCode = $exitCode;
$this->output = $output;
}

public function getOutput(): string
{
return $this->output;
}

public function getExitCode(): int
{
return $this->exitCode;
public function __construct(
public readonly int $exitCode,
public readonly string $output
) {
}
}
4 changes: 2 additions & 2 deletions test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ final protected function runRunner(?string $cwd = null): RunnerResult
unset($_SERVER[Options::ENV_KEY_UNIQUE_TOKEN]);
}

$runner->run();
$exitCode = $runner->run();
if ($shouldPutEnvForParatestTestingItSelf) {
putenv(Options::ENV_KEY_TOKEN . '=' . $prevToken);
putenv(Options::ENV_KEY_UNIQUE_TOKEN . '=' . $prevUniqueToken);
$_SERVER[Options::ENV_KEY_TOKEN] = $prevToken;
$_SERVER[Options::ENV_KEY_UNIQUE_TOKEN] = $prevUniqueToken;
}

return new RunnerResult($runner->getExitCode(), $output->fetch());
return new RunnerResult($exitCode, $output->fetch());
}

final protected function fixture(string $fixture): string
Expand Down
Loading

0 comments on commit d3b3648

Please sign in to comment.