Permalink
Browse files

Repeatable --coverage-src option (#388)

  • Loading branch information...
harmim authored and milo committed Sep 11, 2018
1 parent 8bd511b commit 2286131c7bd9d47ea7fc638480d4ea1f2ace37aa
@@ -26,8 +26,8 @@ abstract class AbstractGenerator
/** @var array */
protected $data;
/** @var string */
protected $source;
/** @var array */
protected $sources;
/** @var int */
protected $totalSum = 0;
@@ -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.");
@@ -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);
}
@@ -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
@@ -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();
}
@@ -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);
}
@@ -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;
}
@@ -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;
@@ -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,
@@ -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'])) {
@@ -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;

0 comments on commit 2286131

Please sign in to comment.