Skip to content

Commit 811718f

Browse files
committed
Skip tests when 'git' command is not available
1 parent caf8060 commit 811718f

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

tests/Core/Filters/AbstractFilterTestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,30 @@ protected static function mapPathsToRuntimeOs(array $paths)
231231
}//end mapPathsToRuntimeOs()
232232

233233

234+
/**
235+
* Mark the current test as 'skipped' if the 'git' command-line tool is not available.
236+
*
237+
* @return void
238+
*/
239+
protected function checkGitCommandAvailable()
240+
{
241+
static $result = null;
242+
243+
if (is_bool($result) === false) {
244+
if (strpos(PHP_OS, 'WIN') === 0) {
245+
$test = 'where';
246+
} else {
247+
$test = 'command -v';
248+
}
249+
250+
$result = is_executable(trim((string) shell_exec("$test git")));
251+
}
252+
253+
if ($result === false) {
254+
$this->markTestSkipped('git command not available');
255+
}
256+
257+
}//end checkGitCommandAvailable()
258+
259+
234260
}//end class

tests/Core/Filters/GitModifiedTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class GitModifiedTest extends AbstractFilterTestCase
3030
*/
3131
public function testFileNamePassesAsBasePathWillTranslateToDirname()
3232
{
33+
$this->checkGitCommandAvailable();
34+
3335
$rootFile = self::getBaseDir().'/autoload.php';
3436

3537
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
@@ -63,6 +65,8 @@ public function testFileNamePassesAsBasePathWillTranslateToDirname()
6365
*/
6466
public function testAcceptOnlyGitModified($inputPaths, $outputGitModified, $expectedOutput)
6567
{
68+
$this->checkGitCommandAvailable();
69+
6670
$fakeDI = new RecursiveArrayIterator($inputPaths);
6771
$constructorArgs = [
6872
$fakeDI,
@@ -216,6 +220,8 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
216220
$this->markTestSkipped('Not a git repository');
217221
}
218222

223+
$this->checkGitCommandAvailable();
224+
219225
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
220226
$filter = new GitModified($fakeDI, '/', self::$config, self::$ruleset);
221227

tests/Core/Filters/GitStagedTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class GitStagedTest extends AbstractFilterTestCase
3030
*/
3131
public function testFileNamePassesAsBasePathWillTranslateToDirname()
3232
{
33+
$this->checkGitCommandAvailable();
34+
3335
$rootFile = self::getBaseDir().'/autoload.php';
3436

3537
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
@@ -63,6 +65,8 @@ public function testFileNamePassesAsBasePathWillTranslateToDirname()
6365
*/
6466
public function testAcceptOnlyGitStaged($inputPaths, $outputGitStaged, $expectedOutput)
6567
{
68+
$this->checkGitCommandAvailable();
69+
6670
$fakeDI = new RecursiveArrayIterator($inputPaths);
6771
$constructorArgs = [
6872
$fakeDI,
@@ -216,6 +220,8 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
216220
$this->markTestSkipped('Not a git repository');
217221
}
218222

223+
$this->checkGitCommandAvailable();
224+
219225
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
220226
$filter = new GitStaged($fakeDI, '/', self::$config, self::$ruleset);
221227

0 commit comments

Comments
 (0)