Skip to content

Commit

Permalink
ADD ability for results parser to throw exception if SA tool reports …
Browse files Browse the repository at this point in the history
…a failure
  • Loading branch information
DaveLiddament committed Jan 11, 2024
1 parent b13ea39 commit 724cd08
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Domain/Creator/BaseLineCreatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryAnalyserException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactory;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsImportException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ErrorReportedByStaticAnalysisTool;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParser;

interface BaseLineCreatorInterface
Expand All @@ -21,6 +22,7 @@ interface BaseLineCreatorInterface
* @throws BaseLineImportException
* @throws AnalysisResultsImportException
* @throws HistoryAnalyserException
* @throws ErrorReportedByStaticAnalysisTool
*/
public function createBaseLine(
HistoryFactory $historyFactory,
Expand Down
2 changes: 2 additions & 0 deletions src/Domain/Pruner/ResultsPruner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryAnalyserException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsImporter;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsImportException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ErrorReportedByStaticAnalysisTool;

class ResultsPruner implements ResultsPrunerInterface
{
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(
* @throws FileAccessException
* @throws AnalysisResultsImportException
* @throws HistoryAnalyserException
* @throws ErrorReportedByStaticAnalysisTool
*/
public function getPrunedResults(
BaseLineFileName $baseLineFileName,
Expand Down
1 change: 1 addition & 0 deletions src/Domain/ResultsParser/AnalysisResultsImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AnalysisResultsImporter
{
/**
* @throws AnalysisResultsImportException
* @throws ErrorReportedByStaticAnalysisTool
*/
public function import(
ResultsParser $resultsParser,
Expand Down
22 changes: 22 additions & 0 deletions src/Domain/ResultsParser/ErrorReportedByStaticAnalysisTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Static Analysis Results Baseliner (sarb).
*
* (c) Dave Liddament
*
* For the full copyright and licence information please view the LICENSE file distributed with this source code.
*/

declare(strict_types=1);

namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser;

use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;

/**
* Thrown if the static analysis tool reports an error.
*/
final class ErrorReportedByStaticAnalysisTool extends SarbException
{
}
1 change: 1 addition & 0 deletions src/Domain/ResultsParser/ResultsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface ResultsParser
*
* @throws ParseAtLocationException
* @throws InvalidContentTypeException
* @throws ErrorReportedByStaticAnalysisTool
*/
public function convertFromString(string $resultsAsString, ProjectRoot $projectRoot): AnalysisResults;

Expand Down
5 changes: 5 additions & 0 deletions src/Framework/Command/internal/ErrorReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\FileAccessException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryAnalyserException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsImportException;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ErrorReportedByStaticAnalysisTool;
use Symfony\Component\Console\Output\OutputInterface;

class ErrorReporter
Expand Down Expand Up @@ -37,6 +38,10 @@ public static function reportError(OutputInterface $output, \Throwable $throwabl
OutputWriter::writeToStdError($output, $e->getMessage(), true);

return 15;
} catch (ErrorReportedByStaticAnalysisTool $e) {
OutputWriter::writeToStdError($output, $e->getMessage(), true);

return 16;
} catch (\Throwable $e) {
// This should never happen. All exceptions should extend SarbException
OutputWriter::writeToStdError($output, "Unexpected critical error: {$e->getMessage()}", true);
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Framework/Command/CreateBaseLineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactory;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\UnifiedDiffParser\Parser;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ErrorReportedByStaticAnalysisTool;
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParser;
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\CreateBaseLineCommand;
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Container\HistoryFactoryRegistry;
Expand Down Expand Up @@ -224,6 +225,26 @@ public function testSimulateThrowable(): void
$this->assertReturnCode(100, $commandTester);
}

public function testSimulateStaticAnalysisToolFailed(): void
{
$commandTester = $this->createCommandTester(
$this->historyFactoryStub,
$this->defaultResultsParser,
self::BASELINE_FILENAME,
null,
new ErrorReportedByStaticAnalysisTool('Tool failed'),
);

$commandTester->execute([
self::HISTORY_ANALYSER => HistoryFactoryStub::CODE,
self::BASELINE_FILE_ARGUMENT => self::BASELINE_FILENAME,
self::PROJECT_ROOT => '/tmp',
]);

$this->assertReturnCode(16, $commandTester);
$this->assertResponseContains('Tool failed', $commandTester);
}

private function createCommandTester(
HistoryFactory $expectedHistoryFactory,
ResultsParser $expectedResultsParser,
Expand Down

0 comments on commit 724cd08

Please sign in to comment.