Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding clickable editorUrl from codeception.yml #6261

Merged
merged 13 commits into from Dec 21, 2021
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()
Naktibalda marked this conversation as resolved.
Show resolved Hide resolved
{
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)
ThomasLandauer marked this conversation as resolved.
Show resolved Hide resolved
->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