Skip to content

Commit

Permalink
Adding clickable editorUrl from codeception.yml (#6261)
Browse files Browse the repository at this point in the history
* Adding clickable `editorUrl` from `codeception.yml`

Closes #6259

I followed the syntax of https://phpstan.org/user-guide/output-format#opening-file-in-an-editor even though we would need the second `%` here. (This is due to some phpstan internals, the code ultimately reads `\str_replace(['%file%', '%line%'], ...`
But I'd say having an *identical* `editorUrl` syntax between Codeception and phpstan by far outweighs the "cost" of some unneeded `%` characters... ;-)

* Update Console.php

* Update Console.php

* Update Console.php

* Update Step.php

* Update Console.php

* Update Console.php

* Update Console.php

* Update Console.php

* Update Console.php

* Update Console.php

* Update Console.php

* Bringing back but `@deprecate`ing `getLine()`
  • Loading branch information
ThomasLandauer committed Dec 21, 2021
1 parent 8a3ec8c commit 8d0c45e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/Codeception/Step.php
Expand Up @@ -79,13 +79,30 @@ public function getAction()
return $this->action;
}

/**
* @deprecated To be removed in Codeception 5.0
*/
public function getLine()
{
if ($this->line && $this->file) {
return codecept_relative_path($this->file) . ':' . $this->line;
}
}

public function getFilePath()
{
if ($this->file) {
return codecept_relative_path($this->file);
}
}

public function getLineNumber()
{
if ($this->line) {
return $this->line;
}
}

public function hasFailed()
{
return $this->failed;
Expand Down
49 changes: 39 additions & 10 deletions src/Codeception/Subscriber/Console.php
Expand Up @@ -165,7 +165,7 @@ public function startTest(TestEvent $e)
$this->printedTest = $test;
$this->message = null;

if (!$this->output->isInteractive() and !$this->isDetailed($test)) {
if (!$this->output->isInteractive() && !$this->isDetailed($test)) {
return;
}
$this->writeCurrentTest($test);
Expand Down Expand Up @@ -207,7 +207,7 @@ public function afterStep(StepEvent $e)
public function afterResult(PrintResultEvent $event)
{
$result = $event->getResult();
if ($result->skippedCount() + $result->notImplementedCount() > 0 and $this->options['verbosity'] < OutputInterface::VERBOSITY_VERBOSE) {
if ($result->skippedCount() + $result->notImplementedCount() > 0 && $this->options['verbosity'] < OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln("run with `-v` to get more info about skipped or incomplete tests");
}
foreach ($this->reports as $message) {
Expand Down Expand Up @@ -303,7 +303,7 @@ public function beforeStep(StepEvent $e)
return;
}
$metaStep = $e->getStep()->getMetaStep();
if ($metaStep and $this->metaStep != $metaStep) {
if ($metaStep && $this->metaStep != $metaStep) {
$this->message(' ' . $metaStep->getPrefix())
->style('bold')
->append($metaStep->__toString())
Expand All @@ -316,7 +316,7 @@ public function beforeStep(StepEvent $e)

private function printStep(Step $step)
{
if ($step instanceof Comment and $step->__toString() == '') {
if ($step instanceof Comment && $step->__toString() == '') {
return; // don't print empty comments
}
$msg = $this->message(' ');
Expand Down Expand Up @@ -356,8 +356,22 @@ public function printFail(FailEvent $e)
$this->output->write($e->getCount() . ") ");
$this->writeCurrentTest($failedTest, false);
$this->output->writeln('');

// Clickable `editorUrl`:
if (isset($this->options['editorUrl']) && is_string($this->options['editorUrl'])) {
$filePath = codecept_absolute_path(Descriptor::getTestFileName($failedTest));
$line = 1;
foreach ($fail->getTrace() as $trace) {
if (isset($trace['file']) && $filePath === $trace['file'] && isset($trace['line'])) {
$line = $trace['line'];
}
}
$message = str_replace(['%%file%%', '%%line%%'], [$filePath, $line], $this->options['editorUrl']);
} else {
$message = codecept_relative_path(Descriptor::getTestFullName($failedTest));
}
$this->message("<error> Test </error> ")
->append(codecept_relative_path(Descriptor::getTestFullName($failedTest)))
->append($message)
->write();

if ($failedTest instanceof ScenarioDriven) {
Expand Down Expand Up @@ -492,7 +506,14 @@ public function printExceptionTrace($e)
$message->writeln();
continue;
}
$message->append($step['file'] . ':' . $step['line']);

// Clickable `editorUrl`:
if (isset($this->options['editorUrl']) && is_string($this->options['editorUrl'])) {
$lineString = str_replace(['%%file%%', '%%line%%'], [$step['file'], $step['line']], $this->options['editorUrl']);
} else {
$lineString = $step['file'] . ':' . $step['line'];
}
$message->append($lineString);
$message->writeln();
}

Expand Down Expand Up @@ -536,9 +557,17 @@ public function printScenarioTrace(ScenarioDriven $failedTest)
$message->style('bold');
}

$line = $step->getLine();
if ($line and (!$step instanceof Comment)) {
$message->append(" at <info>$line</info>");
if (!$step instanceof Comment) {
$filePath = $step->getFilePath();
if ($filePath) {
// Clickable `editorUrl`:
if (isset($this->options['editorUrl']) && is_string($this->options['editorUrl'])) {
$lineString = str_replace(['%%file%%', '%%line%%'], [codecept_absolute_path($step->getFilePath()), $step->getLineNumber()], $this->options['editorUrl']);
} else {
$lineString = $step->getFilePath() . ':' . $step->getLineNumber();
}
$message->append(" at <info>$lineString</info>");
}
}

$stepNumber--;
Expand Down Expand Up @@ -587,7 +616,7 @@ private function isWin()
*/
protected function writeCurrentTest(\PHPUnit\Framework\SelfDescribing $test, $inProgress = true)
{
$prefix = ($this->output->isInteractive() and !$this->isDetailed($test) and $inProgress) ? '- ' : '';
$prefix = ($this->output->isInteractive() && !$this->isDetailed($test) && $inProgress) ? '- ' : '';

$testString = Descriptor::getTestAsString($test);
$testString = preg_replace('~^([^:]+):\s~', "<focus>$1{$this->chars['of']}</focus> ", $testString);
Expand Down

0 comments on commit 8d0c45e

Please sign in to comment.