Skip to content

Commit

Permalink
Process runtime-error-in-global-code
Browse files Browse the repository at this point in the history
This must have the absolute exercise path, but cannot get it from
PHPUnit. So a new env var provides this.
  • Loading branch information
mk-mxp committed Aug 9, 2024
1 parent 95fad6c commit dc103ca
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions bin/run-locally.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash

EXERCISM_RESULT_FILE="${PWD%/}/results.json" \
EXERCISM_EXERCISE_DIR="${PWD%/}/tests/${1}" \
vendor/bin/phpunit --do-not-cache-result tests/"${1}"/*Test.php

# Sync'ed from run-tests.sh - Normalize the object ID of `var_dump(new stdClass())`
sed -i -E \
-e 's/(object\(stdClass\))(#[[:digit:]]+)/\1#79/g' \
Expand Down
18 changes: 9 additions & 9 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ function main {
fi

# Our PHPUnit extension writes directly to ${EXERCISM_RESULT_FILE}
output=$(EXERCISM_RESULT_FILE="${output_dir%/}/${EXERCISM_RESULTS}" "${PHPUNIT_BIN}" \
-d memory_limit=300M \
--do-not-cache-result \
"${test_files%%*( )}" 2>&1)
# Our PHPUnit extension requires ${EXERCISM_EXERCISE_DIR} before PHPUnit provides it
output=$( \
EXERCISM_RESULT_FILE="${output_dir%/}/${EXERCISM_RESULTS}" \
EXERCISM_EXERCISE_DIR="${solution_dir%/}" \
"${PHPUNIT_BIN}" \
-d memory_limit=300M \
--do-not-cache-result \
"${test_files%%*( )}" 2>&1 \
)
phpunit_exit_code=$?
set -e

Expand All @@ -47,11 +52,6 @@ function main {
# return 0;
# fi
# fi

if [[ "${phpunit_exit_code}" -ne 0 ]]; then
echo "$output"
cat "${output_dir%/}/${EXERCISM_RESULTS}"
fi
}

function installed {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<extensions>
<bootstrap class="Exercism\PhpTestRunner\Extension">
<parameter name="fileName" value="results.json"/>
<parameter name="outFileName" value="results.json"/>
</bootstrap>
</extensions>
</phpunit>
13 changes: 9 additions & 4 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ public function bootstrap(
Facade $facade,
ParameterCollection $parameters,
): void {
$fileName = \getenv('EXERCISM_RESULT_FILE');
if (empty($fileName)) {
$fileName = $parameters->get('fileName');
$outFileName = \getenv('EXERCISM_RESULT_FILE');
if (empty($outFileName)) {
$outFileName = $parameters->get('outFileName');
}

$facade->registerTracer(new Tracer($fileName));
$exerciseDir = \getenv('EXERCISM_EXERCISE_DIR');
if (empty($exerciseDir)) {
$exerciseDir = \getenv('PWD');
}

$facade->registerTracer(new Tracer($outFileName, $exerciseDir));
}
}
29 changes: 22 additions & 7 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Exercism\PhpTestRunner\Result;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Event;
use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
use PHPUnit\Event\Test\Errored;
use PHPUnit\Event\Test\Failed;
use PHPUnit\Event\Test\Passed;
use PHPUnit\Event\Test\PrintedUnexpectedOutput;
use PHPUnit\Event\TestRunner\Finished;
use PHPUnit\Event\Tracer\Tracer as TracerInterface;
use ReflectionClass;
use ReflectionMethod;

final class Tracer implements TracerInterface
{
Expand All @@ -25,7 +25,8 @@ final class Tracer implements TracerInterface
];

public function __construct(
private readonly string $fileName,
private readonly string $outFileName,
private readonly string $exerciseDir,
) {
}

Expand All @@ -35,6 +36,7 @@ public function trace(Event $event): void
Passed::class => $this->addTestPassed($event),
Failed::class => $this->addTestFailed($event),
Errored::class => $this->addTestErrored($event),
BeforeFirstTestMethodErrored::class => $this->addBeforeFirstTestMethodErrored($event),
PrintedUnexpectedOutput::class => $this->addTestOutput($event),
// default => $this->addUnhandledEvent($event),
default => true,
Expand Down Expand Up @@ -73,8 +75,8 @@ private function addTestFailed(Failed $event): void
$testMethod = $event->test();

$phpUnitMessage = \trim($event->throwable()->asString());
$phpUnitMessage =\str_replace(
\dirname($testMethod->file()) . '/',
$phpUnitMessage = \str_replace(
$this->exerciseDir . '/',
'',
$phpUnitMessage
);
Expand All @@ -95,8 +97,8 @@ private function addTestErrored(Errored $event): void
$testMethod = $event->test();

$phpUnitMessage = \trim($event->throwable()->asString());
$phpUnitMessage =\str_replace(
\dirname($testMethod->file()) . '/',
$phpUnitMessage = \str_replace(
$this->exerciseDir . '/',
'',
$phpUnitMessage
);
Expand All @@ -111,6 +113,19 @@ private function addTestErrored(Errored $event): void
);
}

private function addBeforeFirstTestMethodErrored(BeforeFirstTestMethodErrored $event): void
{
$phpUnitMessage = \trim($event->throwable()->asString());
$phpUnitMessage = \str_replace(
$this->exerciseDir . '/',
'',
$phpUnitMessage
);

$this->result['status'] = 'error';
$this->result['message'] = $phpUnitMessage;
}

private function addTestOutput(PrintedUnexpectedOutput $event): void
{
// This must rely on the sequence of events!
Expand All @@ -129,7 +144,7 @@ private function saveResults(): void
}

\file_put_contents(
$this->fileName,
$this->outFileName,
\json_encode($this->result) . "\n",
// \json_encode($this->result, JSON_PRETTY_PRINT) . "\n",
);
Expand Down
2 changes: 1 addition & 1 deletion tests/runtime-error-in-global-code/expected_results.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":3,"status":"error","message":"\n\nThere was 1 error:\n\n1) HelloWorldTest\nTypeError: helloWorld(): Argument #1 ($value) must be of type int, string given, called in HelloWorld.php on line 11\n\nHelloWorld.php:5\nHelloWorld.php:11\nHelloWorldTest.php:9\n\nERRORS!\nTests: 1, Assertions: 0, Errors: 1.","tests":[]}
{"version":3,"status":"error","tests":[],"message":"TypeError: helloWorld(): Argument #1 ($value) must be of type int, string given, called in HelloWorld.php on line 11\n\nHelloWorld.php:5\nHelloWorld.php:11\nHelloWorldTest.php:9"}

0 comments on commit dc103ca

Please sign in to comment.