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

#857 Treat log paths as relative to config directory #1851

Merged
merged 3 commits into from
May 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
use function array_unique;
use function array_values;
use function dirname;
use function in_array;
use Infection\Configuration\Entry\Logs;
use Infection\Configuration\Entry\PhpUnit;
use Infection\Configuration\Schema\SchemaConfiguration;
use Infection\FileSystem\SourceFileCollector;
use Infection\FileSystem\TmpDirProvider;
use Infection\Logger\FileLogger;
use Infection\Logger\GitHub\GitDiffFileProvider;
use Infection\Mutator\ConfigurableMutator;
use Infection\Mutator\Mutator;
Expand Down Expand Up @@ -153,7 +155,7 @@ public function create(
),
$this->retrieveFilter($filter, $gitDiffFilter, $isForGitDiffLines, $gitDiffBase, $schema->getSource()->getDirectories()),
$schema->getSource()->getExcludes(),
$this->retrieveLogs($schema->getLogs(), $useGitHubLogger, $htmlLogFilePath),
$this->retrieveLogs($schema->getLogs(), $configDir, $useGitHubLogger, $htmlLogFilePath),
$logVerbosity,
$namespacedTmpDir,
$this->retrievePhpUnit($schema, $configDir),
Expand Down Expand Up @@ -321,7 +323,7 @@ private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $is
return $this->gitDiffFileProvider->provide($gitDiffFilter, $baseBranch, $sourceDirectories);
}

private function retrieveLogs(Logs $logs, ?bool $useGitHubLogger, ?string $htmlLogFilePath): Logs
private function retrieveLogs(Logs $logs, string $configDir, ?bool $useGitHubLogger, ?string $htmlLogFilePath): Logs
{
if ($useGitHubLogger === null) {
$useGitHubLogger = $this->detectCiGithubActions();
Expand All @@ -335,7 +337,17 @@ private function retrieveLogs(Logs $logs, ?bool $useGitHubLogger, ?string $htmlL
$logs->setHtmlLogFilePath($htmlLogFilePath);
}

return $logs;
return new Logs(
self::pathToAbsolute($logs->getTextLogFilePath(), $configDir),
self::pathToAbsolute($logs->getHtmlLogFilePath(), $configDir),
self::pathToAbsolute($logs->getSummaryLogFilePath(), $configDir),
self::pathToAbsolute($logs->getJsonLogFilePath(), $configDir),
self::pathToAbsolute($logs->getDebugLogFilePath(), $configDir),
self::pathToAbsolute($logs->getPerMutatorFilePath(), $configDir),
$logs->getUseGitHubAnnotationsLogger(),
$logs->getStrykerConfig(),
self::pathToAbsolute($logs->getSummaryJsonLogFilePath(), $configDir),
);
}

private function detectCiGithubActions(): bool
Expand All @@ -348,4 +360,23 @@ private function detectCiGithubActions(): bool

return $ci->getCiName() === CiDetector::CI_GITHUB_ACTIONS;
}

private static function pathToAbsolute(
?string $path,
string $configDir,
): ?string {
if ($path === null) {
return null;
}

if (in_array($path, FileLogger::ALLOWED_PHP_STREAMS, true)) {
return $path;
}

if (Path::isAbsolute($path)) {
return $path;
}

return sprintf('%s/%s', $configDir, $path);
}
}
4 changes: 3 additions & 1 deletion src/Logger/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
*/
final class FileLogger implements MutationTestingResultsLogger
{
public const ALLOWED_PHP_STREAMS = ['php://stdout', 'php://stderr'];

public function __construct(private string $filePath, private Filesystem $fileSystem, private LineMutationTestingResultsLogger $lineLogger, private LoggerInterface $logger)
{
}
Expand All @@ -60,7 +62,7 @@ public function log(): void

// If the output should be written to a stream then just write it directly
if (str_starts_with($this->filePath, 'php://')) {
if (in_array($this->filePath, ['php://stdout', 'php://stderr'], true)) {
if (in_array($this->filePath, self::ALLOWED_PHP_STREAMS, true)) {
file_put_contents($this->filePath, $content);
} else {
// The Symfony filesystem component doesn't support using streams so provide a
Expand Down
54 changes: 33 additions & 21 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,33 @@ public function valueProvider(): iterable
];

yield 'null html file log path with existing path from config file' => self::createValueForHtmlLogFilePath(
'from-config.html',
'/from-config.html',
null,
'from-config.html'
'/from-config.html'
);

yield 'absolute html file log path' => self::createValueForHtmlLogFilePath(
'/path/to/from-config.html',
null,
'/path/to/from-config.html',
);

yield 'relative html file log path' => self::createValueForHtmlLogFilePath(
'relative/path/to/from-config.html',
null,
'/path/to/relative/path/to/from-config.html'
);

yield 'override html file log path from CLI option with existing path from config file' => self::createValueForHtmlLogFilePath(
'from-config.html',
'from-cli.html',
'from-cli.html'
'/from-config.html',
'/from-cli.html',
'/from-cli.html'
);

yield 'set html file log path from CLI option when config file has no setting' => self::createValueForHtmlLogFilePath(
null,
'from-cli.html',
'from-cli.html'
'/from-cli.html',
'/from-cli.html'
);

yield 'null html file log path in config and CLI' => self::createValueForHtmlLogFilePath(
Expand Down Expand Up @@ -799,15 +811,15 @@ public function valueProvider(): iterable
10,
new Source(['src/'], ['vendor/']),
new Logs(
'text.log',
'report.html',
'summary.log',
'json.log',
'debug.log',
'mutator.log',
'/text.log',
'/report.html',
'/summary.log',
'/json.log',
'/debug.log',
'/mutator.log',
true,
StrykerConfig::forFullReport('master'),
'summary.json'
'/summary.json'
),
'config/tmp',
new PhpUnit(
Expand Down Expand Up @@ -856,15 +868,15 @@ public function valueProvider(): iterable
'src/Foo.php, src/Bar.php',
['vendor/'],
new Logs(
'text.log',
'report.html',
'summary.log',
'json.log',
'debug.log',
'mutator.log',
'/text.log',
'/report.html',
'/summary.log',
'/json.log',
'/debug.log',
'/mutator.log',
true,
StrykerConfig::forFullReport('master'),
'summary.json'
'/summary.json'
),
'none',
'/path/to/config/tmp/infection',
Expand Down