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

Add option to report uncovered dependencies for GitHubActionFormatter #338

Merged
merged 4 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ this rule was violated.
1. [Console Formatter](#console-formatter)
1. [Graphviz Formatter](#graphviz-formatter)
1. [JUnit Formatter](#junit-formatter)
1. [GitHubActions Formatter](#githubactions-formatter)
1. [Uncovered dependencies](#uncovered-dependencies)
1. [Build Deptrac](#build-deptrac)
1. [Contribute](#contribute)
Expand Down Expand Up @@ -682,6 +683,22 @@ Supported options:
--formatter-junit-dump-xml= path to a dumped xml file [default: "./junit-report.xml"]
```

### GitHubActions Formatter

The GithubActions formatter is a console formater, which dumps basic information in github-actions format to *STDOUT*.
This formatter is enabled by default while running in a github actions environment. To disable the formatter just use `--formatter-github-actions=false`.

```
::error file=/home/testuser/originalA.php,line=12::ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)
```

Supported options:

```
--formatter-github-actions= to enable the github-actions formatter, set this argument to "true" [default: false]
--formatter-console-report-uncovered= report uncovered dependencies [default: false]
```

## Uncovered dependencies

Deptrac collects uncovered dependencies which could be reported with [Console Formatter](#console-formatter).
Expand Down
34 changes: 32 additions & 2 deletions src/OutputFormatter/GithubActionsOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class GithubActionsOutputFormatter implements OutputFormatterInterface
{
private const REPORT_UNCOVERED = 'report-uncovered';

/** @var Env */
private $env;

Expand All @@ -32,7 +34,9 @@ public function getName(): string
*/
public function configureOptions(): array
{
return [];
return [
OutputFormatterOption::newValueOption(static::REPORT_UNCOVERED, 'report uncovered dependencies', false),
];
}

public function enabledByDefault(): bool
Expand Down Expand Up @@ -63,9 +67,13 @@ public function finish(Context $context, OutputInterface $output, OutputFormatte
$rule->getLayerB()
));
}

if (true === $outputFormatterInput->getOptionAsBoolean(static::REPORT_UNCOVERED)) {
$this->printUncovered($context, $output);
}
}

public function determineLogLevel(Rule $rule): string
private function determineLogLevel(Rule $rule): string
{
switch (get_class($rule)) {
case Violation::class:
Expand All @@ -76,4 +84,26 @@ public function determineLogLevel(Rule $rule): string
return 'debug';
}
}

private function printUncovered(Context $context, OutputInterface $output): void
{
$uncovered = $context->uncovered();
if ([] === $uncovered) {
return;
}

foreach ($uncovered as $u) {
$dependency = $u->getDependency();
$output->writeln(
sprintf(
'::warning file=%s,line=%s::%s has uncovered dependency on %s (%s)',
$dependency->getFileOccurrence()->getFilepath(),
$dependency->getFileOccurrence()->getLine(),
$dependency->getClassLikeNameA()->toString(),
$dependency->getClassLikeNameB()->toString(),
$u->getLayer()
)
);
}
}
}
22 changes: 21 additions & 1 deletion tests/OutputFormatter/ConsoleOutputFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SensioLabs\Deptrac\OutputFormatter\OutputFormatterInput;
use SensioLabs\Deptrac\RulesetEngine\Context;
use SensioLabs\Deptrac\RulesetEngine\SkippedViolation;
use SensioLabs\Deptrac\RulesetEngine\Uncovered;
use SensioLabs\Deptrac\RulesetEngine\Violation;
use Symfony\Component\Console\Output\BufferedOutput;
use Tests\SensioLabs\Deptrac\EmptyEnv;
Expand Down Expand Up @@ -115,6 +116,25 @@ public function basicDataProvider(): iterable
Allowed: 0
',
];

yield [
[
new Uncovered(
new Dependency($originalA, $originalB, FileOccurrence::fromFilepath('originalA.php', 12)),
'LayerA'
),
],
'
Uncovered dependencies:
OriginalA has uncovered dependency on OriginalB (LayerA)
originalA.php::12
Report:
Violations: 0
Skipped violations: 0
Uncovered: 1
Allowed: 0
',
];
}

/**
Expand All @@ -128,7 +148,7 @@ public function testBasic(array $rules, string $expectedOutput): void
$formatter->finish(
new Context($rules),
$output,
new OutputFormatterInput(['report-uncovered' => false])
new OutputFormatterInput(['report-uncovered' => true])
);

$o = $output->fetch();
Expand Down
18 changes: 17 additions & 1 deletion tests/OutputFormatter/GithubActionsOutputFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SensioLabs\Deptrac\OutputFormatter\OutputFormatterInput;
use SensioLabs\Deptrac\RulesetEngine\Context;
use SensioLabs\Deptrac\RulesetEngine\SkippedViolation;
use SensioLabs\Deptrac\RulesetEngine\Uncovered;
use SensioLabs\Deptrac\RulesetEngine\Violation;
use Symfony\Component\Console\Output\BufferedOutput;
use Tests\SensioLabs\Deptrac\EmptyEnv;
Expand All @@ -32,7 +33,7 @@ public function testFinish(array $rules, string $expectedOutput): void
$formatter->finish(
new Context($rules),
$output,
new OutputFormatterInput([])
new OutputFormatterInput(['report-uncovered' => true])
);

$o = $output->fetch();
Expand Down Expand Up @@ -74,10 +75,25 @@ public function finishProvider()
],
"::warning file=/home/testuser/originalA.php,line=12::[SKIPPED] ACME\OriginalA must not depend on ACME\OriginalB (LayerA on LayerB)\n",
];

yield 'Uncovered Dependency' => [
[
new Uncovered(
new Dependency($originalA, $originalB, $originalAOccurrence),
'LayerA'
),
],
"::warning file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)\n",
];
}

public function testGithubActionsOutputFormatterIsNotEnabledByDefault(): void
{
static::assertFalse((new GithubActionsOutputFormatter(new EmptyEnv()))->enabledByDefault());
}

public function testGetOptions(): void
{
static::assertCount(1, (new GithubActionsOutputFormatter(new EmptyEnv()))->configureOptions());
}
}