Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PHPUnit's --filter option #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"require": {
"php": ">=7.0",
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^6.0|^7.0"
},
"license": "BSD-3-Clause",
"authors": [
Expand Down
33 changes: 14 additions & 19 deletions src/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Git implements VCS
{
private $binary = '/usr/bin/git';
private $binary = 'git';

public function __construct($binary = "")
{
Expand All @@ -24,7 +24,7 @@ public function __construct($binary = "")
}

if ($this->gitExists() !== true) {
throw new GitException("Git doesn't appear to exist in: " . $this->binary);
throw new GitException($this->binary . " doesn't appear to exist in: " . $this->binary);
}

if ($this->isProjectGit() !== true) {
Expand All @@ -43,18 +43,12 @@ private function isOSWindows(): bool

public function gitExists(): bool
{
$returnVar = intval(trim(shell_exec($this->binary . " &> /dev/null; echo $?")));

if ($returnVar === 1) {
return true;
}

return false;
return !empty('which ' . $this->binary);
}

private function isProjectGit(): bool
{
$isProjectGit = trim(shell_exec("git rev-parse --is-inside-work-tree"));
$isProjectGit = trim(shell_exec($this->binary . " rev-parse --is-inside-work-tree"));

if ($isProjectGit === "true") {
return true;
Expand All @@ -65,27 +59,28 @@ private function isProjectGit(): bool

public function getChangedFiles(): array
{

$changes = $this->execute("git diff --name-only");
$arguments = $_SERVER['argv'];

$from = isset($arguments[1]) ? $arguments[1] : 'HEAD^';
$to = isset($arguments[2]) ? $arguments[2] : 'HEAD';

$changes = $this->execute($this->binary . " diff --name-only");
$changes .= PHP_EOL;

if ($this->countCommits() > 1) {
$changes .= $this->execute("git diff --name-only HEAD^ HEAD");
}

$changes .= $this->execute($this->binary . " diff --name-only $from $to");

$changes = trim($changes);
$files = explode(PHP_EOL, $changes);
return $files;
}

private function countCommits(): int
{
return intval(trim($this->execute("git shortlog | grep -E '^[ ]+\\w+' | wc -l")));
return intval(trim($this->execute($this->binary . " shortlog | grep -E '^[ ]+\\w+' | wc -l")));
}

private function execute(string $command): string
{
return trim(shell_exec($command));
}

}
}
29 changes: 10 additions & 19 deletions src/WhatsChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,16 @@ public function testChanges()
{
$changes = $this->VCS->getChangedFiles();
$testFiles = $this->getTestFiles($changes);

$mask = "%-30s %-30s\n";

$fails = array();

foreach ($testFiles as $file) {
$exec = exec("./vendor/bin/phpunit " . $file, $full);

if (strpos($exec, 'OK ') !== false) {
printf($mask, $file, $exec);
} else {
printf($mask, $file, 'FAIL - '.$exec);
array_push($fails, implode(PHP_EOL, $full));
}
}

if (!empty($fails)) {
echo PHP_EOL.PHP_EOL."FAILED TEST OUTPUTS:".PHP_EOL.PHP_EOL.implode(" ", $fails).PHP_EOL.PHP_EOL;

if(empty($testFiles)) {
die('No tests to run' . PHP_EOL);
}

$regex = '(' . str_replace(['tests/', '/', '.php'], ['', '\\\\', ''], implode('|', $testFiles)) . ')';

$exec = system("./vendor/bin/phpunit --filter '" . $regex . "'", $code);

exit($code);
}

public function getTestFiles(array $changes): array
Expand Down Expand Up @@ -112,4 +103,4 @@ private function turnIntoTestFile(string $file): string
return $newFile;

}
}
}