Skip to content

Commit

Permalink
#857 Treat log paths as relative to config directory (#1851)
Browse files Browse the repository at this point in the history
* #857 Treat log paths as relative to config directory

* Do not make abusolute path if `php://stdout`, `php://stderr` are used

* Do not make abusolute path if `php://stdout`, `php://stderr` are used

---------

Co-authored-by: Leo Viezens <leo.viezens@web.de>
  • Loading branch information
maks-rafalko and LeoVie committed May 11, 2023
1 parent 671110f commit fae8d1f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
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

0 comments on commit fae8d1f

Please sign in to comment.