diff --git a/composer.json b/composer.json index 0acae13..1e1b096 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/src/Git.php b/src/Git.php index 833a77a..7e83fd1 100644 --- a/src/Git.php +++ b/src/Git.php @@ -11,7 +11,7 @@ class Git implements VCS { - private $binary = '/usr/bin/git'; + private $binary = 'git'; public function __construct($binary = "") { @@ -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) { @@ -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; @@ -65,14 +59,15 @@ 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; @@ -80,7 +75,7 @@ public function getChangedFiles(): array 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 @@ -88,4 +83,4 @@ private function execute(string $command): string return trim(shell_exec($command)); } -} \ No newline at end of file +} diff --git a/src/WhatsChanged.php b/src/WhatsChanged.php index 933face..253f7af 100644 --- a/src/WhatsChanged.php +++ b/src/WhatsChanged.php @@ -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 @@ -112,4 +103,4 @@ private function turnIntoTestFile(string $file): string return $newFile; } -} \ No newline at end of file +}