Skip to content

Commit

Permalink
Repeatable --coverage-src option (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
harmim authored and milo committed Sep 11, 2018
1 parent 8bd511b commit 2286131
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
56 changes: 36 additions & 20 deletions src/CodeCoverage/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ abstract class AbstractGenerator
/** @var array */
protected $data;

/** @var string */
protected $source;
/** @var array */
protected $sources;

/** @var int */
protected $totalSum = 0;
Expand All @@ -38,9 +38,9 @@ abstract class AbstractGenerator

/**
* @param string $file path to coverage.dat file
* @param string $source path to covered source file or directory
* @param array $sources paths to covered source files or directories
*/
public function __construct(string $file, string $source = null)
public function __construct(string $file, array $sources = [])
{
if (!is_file($file)) {
throw new \Exception("File '$file' is missing.");
Expand All @@ -55,23 +55,18 @@ public function __construct(string $file, string $source = null)
return @is_file($path); // @ some files or wrappers may not exist, i.e. mock://
}, ARRAY_FILTER_USE_KEY);

if (!$source) {
$source = key($this->data);
for ($i = 0; $i < strlen($source); $i++) {
foreach ($this->data as $s => $foo) {
if (!isset($s[$i]) || $source[$i] !== $s[$i]) {
$source = substr($source, 0, $i);
break 2;
}
if (!$sources) {
$sources = [self::getCommonFilesPath(array_keys($this->data))];

} else {
foreach ($sources as $source) {
if (!file_exists($source)) {
throw new \Exception("File or directory '$source' is missing.");
}
}
$source = dirname($source . 'x');

} elseif (!file_exists($source)) {
throw new \Exception("File or directory '$source' is missing.");
}

$this->source = realpath($source);
$this->sources = array_map('realpath', $sources);
}


Expand Down Expand Up @@ -107,9 +102,14 @@ public function getCoveredPercent(): float

protected function getSourceIterator(): \Iterator
{
$iterator = is_dir($this->source)
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->source))
: new \ArrayIterator([new \SplFileInfo($this->source)]);
$iterator = new \AppendIterator;
foreach ($this->sources as $source) {
$iterator->append(
is_dir($source)
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source))
: new \ArrayIterator([new \SplFileInfo($source)])
);
}

return new \CallbackFilterIterator($iterator, function (\SplFileInfo $file): bool {
return $file->getBasename()[0] !== '.' // . or .. or .gitignore
Expand All @@ -118,5 +118,21 @@ protected function getSourceIterator(): \Iterator
}


protected static function getCommonFilesPath(array $files): string
{
$path = reset($files);
for ($i = 0; $i < strlen($path); $i++) {
foreach ($files as $file) {
if (!isset($file[$i]) || $path[$i] !== $file[$i]) {
$path = substr($path, 0, $i);
break 2;
}
}
}

return rtrim(is_dir($path) ? $path : dirname($path), DIRECTORY_SEPARATOR);
}


abstract protected function renderSelf();
}
4 changes: 2 additions & 2 deletions src/CodeCoverage/Generators/CloverXMLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class CloverXMLGenerator extends AbstractGenerator
];


public function __construct(string $file, string $source = null)
public function __construct(string $file, array $sources = [])
{
if (!extension_loaded('dom')) {
throw new \LogicException('CloverXML generator requires DOM extension to be loaded.');
}
parent::__construct($file, $source);
parent::__construct($file, $sources);
}


Expand Down
9 changes: 5 additions & 4 deletions src/CodeCoverage/Generators/HtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class HtmlGenerator extends AbstractGenerator

/**
* @param string $file path to coverage.dat file
* @param string $source file/directory
* @param array $sources files/directories
*/
public function __construct(string $file, string $source = null, string $title = null)
public function __construct(string $file, array $sources = [], string $title = null)
{
parent::__construct($file, $source);
parent::__construct($file, $sources);
$this->title = $title;
}

Expand Down Expand Up @@ -71,6 +71,7 @@ private function parse(): void
}

$this->files = [];
$commonSourcesPath = self::getCommonFilesPath($this->sources) . DIRECTORY_SEPARATOR;
foreach ($this->getSourceIterator() as $entry) {
$entry = (string) $entry;

Expand All @@ -96,7 +97,7 @@ private function parse(): void

$light = $total ? $total < 5 : count(file($entry)) < 50;
$this->files[] = (object) [
'name' => str_replace((is_dir($this->source) ? $this->source : dirname($this->source)) . DIRECTORY_SEPARATOR, '', $entry),
'name' => str_replace($commonSourcesPath, '', $entry),
'file' => $entry,
'lines' => $lines,
'coverage' => $coverage,
Expand Down
2 changes: 1 addition & 1 deletion src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private function loadOptions(): CommandLine
'--temp' => [CommandLine::REALPATH => true],
'paths' => [CommandLine::REPEATABLE => true, CommandLine::VALUE => getcwd()],
'--debug' => [],
'--coverage-src' => [CommandLine::REALPATH => true],
'--coverage-src' => [CommandLine::REALPATH => true, CommandLine::REPEATABLE => true],
]);

if (isset($_SERVER['argv'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/CodeCoverage/CloverXMLGenerator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $coverageData = Tester\FileMock::create(serialize([
)),
]));

$generator = new CodeCoverage\Generators\CloverXMLGenerator($coverageData, $coveredDir);
$generator = new CodeCoverage\Generators\CloverXMLGenerator($coverageData, [$coveredDir]);
$generator->render($output = Tester\FileMock::create('', 'xml'));

$dom = new DOMDocument;
Expand Down

0 comments on commit 2286131

Please sign in to comment.