Skip to content

Commit

Permalink
Merge pull request #195 from ThomasDelteil/fix_exclude_folder
Browse files Browse the repository at this point in the history
[Issue 180] Fix exclude folder
  • Loading branch information
julianseeger committed Jan 24, 2016
2 parents 7125550 + 57b7e21 commit 1dbc4ac
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 98 deletions.
33 changes: 24 additions & 9 deletions src/ParaTest/Runners/PHPUnit/Configuration.php
Expand Up @@ -23,7 +23,7 @@ class Configuration
*/
protected $xml;

protected $availableNodes = array('file', 'directory', 'testsuite');
protected $availableNodes = array('exclude', 'file', 'directory', 'testsuite');

/**
* A collection of datastructures
Expand Down Expand Up @@ -71,15 +71,15 @@ public function getPath()
* Return the contents of the <testsuite> nodes
* contained in a PHPUnit configuration
*
* @return array|null
* @return SuitePath[][]|null
*/
public function getSuites()
{
if (!$this->xml) {
return null;
}
$suites = array();
$nodes = $this->xml->xpath('//testsuites/testsuite');
$nodes = $this->xml->xpath('//testsuites/testsuite');

while (list(, $node) = each($nodes)) {
$suites = array_merge_recursive($suites, $this->getSuiteByName((string)$node['name']));
Expand All @@ -93,24 +93,40 @@ public function getSuites()
*
* @param string $suiteName
*
* @return array
* @return SuitePath[]|null
*/
public function getSuiteByName($suiteName)
{
$nodes = $this->xml->xpath(sprintf('//testsuite[@name="%s"]', $suiteName));

$suites = array();
$suites = array();
$excludedPaths = array();
while (list(, $node) = each($nodes)) {
foreach ($this->availableNodes as $nodeName) {
foreach ($node->{$nodeName} as $nodeContent) {
switch ($nodeName) {
case 'exclude':
foreach ($this->getSuitePaths((string)$nodeContent) as $excludedPath) {
$excludedPaths[$excludedPath] = $excludedPath;
}
break;
case 'testsuite':
$suites = array_merge_recursive($suites, $this->getSuiteByName((string)$nodeContent));
break;
case 'directory':
// Replicate behaviour of PHPUnit
// if a directory is included and excluded at the same time, then it is considered included
foreach ($this->getSuitePaths((string)$nodeContent) as $dir) {
if (array_key_exists($dir, $excludedPaths)) {
unset($excludedPaths[$dir]);
}
}
// not breaking on purpose
default:
foreach ($this->getSuitePaths((string)$nodeContent) as $path) {
$suites[(string)$node['name']][] = new SuitePath(
$path,
$excludedPaths,
$nodeContent->attributes()->suffix
);
}
Expand All @@ -131,7 +147,7 @@ public function getSuiteByName($suiteName)
*/
public function getConfigDir()
{
return dirname($this->path) . DIRECTORY_SEPARATOR;
return dirname($this->path).DIRECTORY_SEPARATOR;
}

/**
Expand All @@ -142,20 +158,19 @@ public function getConfigDir()
*/
public function getSuitePaths($path)
{
$real = realpath($this->getConfigDir() . $path);
$real = realpath($this->getConfigDir().$path);

if ($real !== false) {
return array($real);
}

if ($this->isGlobRequired($path)) {
$paths = array();
foreach (glob($this->getConfigDir() . $path, GLOB_ONLYDIR) as $path) {
foreach (glob($this->getConfigDir().$path, GLOB_ONLYDIR) as $path) {
if (($path = realpath($path)) !== false) {
$paths[] = $path;
}
}

return $paths;
}

Expand Down
94 changes: 7 additions & 87 deletions src/ParaTest/Runners/PHPUnit/SuiteLoader.php
Expand Up @@ -8,17 +8,6 @@

class SuiteLoader
{
/**
* The pattern used for grabbing test files. Uses the *Test.php convention
* that PHPUnit defaults to.
*/
const TEST_PATTERN = '/.+Test\.php$/';

/**
* Matches php files
*/
const FILE_PATTERN = '/.+\.php$/';

/**
* The collection of loaded files
*
Expand All @@ -33,13 +22,6 @@ class SuiteLoader
*/
protected $loadedSuites = array();

/**
* Used to ignore directory paths '.' and '..'
*
* @var string
*/
private static $dotPattern = '/([.]+)$/';

public function __construct($options = null)
{
if ($options && !$options instanceof Options) {
Expand Down Expand Up @@ -92,21 +74,25 @@ public function load($path = '')
}

if ($path) {
$this->loadPath($path);
$testFileLoader = new TestFileLoader($this->options);
$this->files = array_merge($this->files, $testFileLoader->loadPath($path));
} elseif (isset($this->options->testsuite) && $this->options->testsuite) {
foreach ($configuration->getSuiteByName($this->options->testsuite) as $suite) {
foreach ($suite as $suitePath) {
$this->loadPath($suitePath);
$testFileLoader = new TestFileLoader($this->options);
$this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath));
}
}
} elseif ($suites = $configuration->getSuites()) {
foreach ($suites as $suite) {
foreach ($suite as $suitePath) {
$this->loadPath($suitePath);
$testFileLoader = new TestFileLoader($this->options);
$this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath));
}
}
}


if (!$this->files) {
throw new \RuntimeException("No path or configuration provided (tests must end with Test.php)");
}
Expand All @@ -116,72 +102,6 @@ public function load($path = '')
$this->initSuites();
}

/**
* Loads suites based on a specific path.
* A valid path can be a directory or file
*
* @param $path
* @throws \InvalidArgumentException
*/
private function loadPath($path)
{
$path = $path ? : $this->options->path;
if ($path instanceof SuitePath) {
$pattern = $path->getPattern();
$path = $path->getPath();
} else {
$pattern = self::TEST_PATTERN;
}
if (!file_exists($path)) {
throw new \InvalidArgumentException("$path is not a valid directory or file");
}
if (is_dir($path)) {
$this->loadDir($path, $pattern);
} elseif (file_exists($path)) {
$this->loadFile($path);
}
}

/**
* Loads suites from a directory
*
* @param string $path
* @param string $pattern
*/
private function loadDir($path, $pattern = self::TEST_PATTERN)
{
$files = scandir($path);
foreach ($files as $file) {
$this->tryLoadTests($path . DIRECTORY_SEPARATOR . $file, $pattern);
}
}

/**
* Load a single suite file
*
* @param $path
*/
private function loadFile($path)
{
$this->tryLoadTests($path, self::FILE_PATTERN);
}

/**
* Attempts to load suites from a path.
*
* @param string $path
* @param string $pattern regular expression for matching file names
*/
private function tryLoadTests($path, $pattern = self::TEST_PATTERN)
{
if (preg_match($pattern, $path)) {
$this->files[] = $path;
}

if (!preg_match(self::$dotPattern, $path) && is_dir($path)) {
$this->loadDir($path, $pattern);
}
}

/**
* Called after all files are loaded. Parses loaded files into
Expand Down
16 changes: 15 additions & 1 deletion src/ParaTest/Runners/PHPUnit/SuitePath.php
Expand Up @@ -17,13 +17,19 @@ class SuitePath
* @var string
*/
protected $suffix;

/**
* @var string[]s
*/
protected $excludedPaths;

public function __construct($path, $suffix)
public function __construct($path, $excludedPaths, $suffix)
{
if ($suffix === null) {
$suffix = self::DEFAULT_SUFFIX;
}
$this->path = $path;
$this->excludedPaths = $excludedPaths;
$this->suffix = $suffix;
}

Expand All @@ -35,6 +41,14 @@ public function getPath()
return $this->path;
}

/**
* @return string[]
*/
public function getExcludedPaths()
{
return $this->excludedPaths;
}

/**
* @return string
*/
Expand Down

0 comments on commit 1dbc4ac

Please sign in to comment.