Arsse/RoboFile.php

174 lines
6.5 KiB
PHP
Raw Normal View History

<?php
use Robo\Result;
class RoboFile extends \Robo\Tasks {
const BASE = __DIR__.\DIRECTORY_SEPARATOR;
const BASE_TEST = self::BASE."tests".\DIRECTORY_SEPARATOR;
2017-12-22 11:41:54 -05:00
/**
2018-08-15 09:59:07 -04:00
* Runs the typical test suite
2017-12-22 11:41:54 -05:00
*
* Arguments passed to the task are passed on to PHPUnit. Thus one may, for
2017-12-17 11:37:07 -05:00
* example, run the following command and get the expected results:
2017-12-22 11:41:54 -05:00
*
* ./robo test --testsuite TTRSS --exclude-group slow --testdox
*
* Please see the PHPUnit documentation for available options.
2017-12-17 11:37:07 -05:00
*/
public function test(array $args): Result {
2018-08-15 09:59:07 -04:00
return $this->runTests("php", "typical", $args);
}
2017-12-22 11:41:54 -05:00
/**
* Runs the full test suite
*
2018-08-15 09:59:07 -04:00
* This includes pedantic tests which may help to identify problems.
* See help for the "test" task for more details.
2017-12-17 11:37:07 -05:00
*/
public function testFull(array $args): Result {
2018-08-15 09:59:07 -04:00
return $this->runTests("php", "full", $args);
2017-12-17 11:37:07 -05:00
}
2017-12-22 11:41:54 -05:00
/**
* Runs a quick subset of the test suite
*
* See help for the "test" task for more details.
2017-12-17 11:37:07 -05:00
*/
public function testQuick(array $args): Result {
2018-08-15 09:59:07 -04:00
return $this->runTests("php", "quick", $args);
}
2017-12-22 11:41:54 -05:00
/** Produces a code coverage report
*
2017-12-17 11:37:07 -05:00
* By default this task produces an HTML-format coverage report in
2018-08-15 09:59:07 -04:00
* tests/coverage/. Additional reports may be produced by passing
2017-12-17 11:37:07 -05:00
* arguments to this task as one would to PHPUnit.
2017-12-22 11:41:54 -05:00
*
2017-12-17 11:37:07 -05:00
* Robo first tries to use phpdbg and will fall back to Xdebug if available.
2017-12-22 11:41:54 -05:00
* Because Xdebug slows down non-coverage tasks, however, phpdbg is highly
2018-08-15 09:59:07 -04:00
* recommended if debugging facilities are not otherwise needed.
2017-12-17 11:37:07 -05:00
*/
public function coverage(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
return $this->runTests($exec, "coverage", array_merge(["--coverage-html", self::BASE_TEST."coverage"], $args));
}
/** Produces a code coverage report, with redundant tests
*
* Depending on the environment, some tests that normally provide
* coverage may be skipped, while working alternatives are normally
* suppressed for reasons of time. This coverage report will try to
* run all tests which may cover code.
2018-12-05 17:28:11 -05:00
*
* See also help for the "coverage" task for more details.
*/
public function coverageFull(array $args): Result {
// run tests with code coverage reporting enabled
$exec = $this->findCoverageEngine();
2018-08-15 09:59:07 -04:00
return $this->runTests($exec, "typical", array_merge(["--coverage-html", self::BASE_TEST."coverage"], $args));
}
/** Runs the coding standards fixer */
public function clean($opts = ['demo|d' => false]): Result {
$t = $this->taskExec(realpath(self::BASE."vendor/bin/php-cs-fixer"));
$t->arg("fix");
if ($opts['demo']) {
$t->args("--dry-run", "--diff")->option("--diff-format", "udiff");
}
return $t->run();
}
protected function findCoverageEngine(): string {
if ($this->isWindows()) {
$dbg = dirname(\PHP_BINARY)."\\phpdbg.exe";
$dbg = file_exists($dbg) ? $dbg : "";
} else {
$dbg = `which phpdbg`;
}
if ($dbg) {
return escapeshellarg($dbg)." -qrr";
} else {
return escapeshellarg(\PHP_BINARY);
}
}
2018-11-06 12:32:28 -05:00
protected function isWindows(): bool {
return defined("PHP_WINDOWS_VERSION_MAJOR");
}
2018-08-15 09:59:07 -04:00
protected function runTests(string $executor, string $set, array $args) : Result {
switch ($set) {
case "typical":
$set = ["--exclude-group", "optional"];
break;
case "quick":
$set = ["--exclude-group", "optional,slow"];
break;
case "coverage":
2018-12-10 12:39:09 -05:00
$set = ["--exclude-group", "optional,coverageOptional"];
break;
2018-08-15 09:59:07 -04:00
case "full":
$set = [];
break;
default:
throw new \Exception;
}
$execpath = realpath(self::BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit");
$confpath = realpath(self::BASE_TEST."phpunit.xml");
$this->taskServer(8000)->host("localhost")->dir(self::BASE_TEST."docroot")->rawArg("-n")->arg(self::BASE_TEST."server.php")->background()->run();
return $this->taskExec($executor)->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->run();
2018-08-15 09:59:07 -04:00
}
2017-12-22 11:41:54 -05:00
/** Packages a given commit of the software into a release tarball
*
2017-12-17 11:37:07 -05:00
* The version to package may be any Git tree-ish identifier: a tag, a branch,
* or any commit hash. If none is provided on the command line, Robo will prompt
* for a commit to package; the default is "head".
2017-12-22 11:41:54 -05:00
*
2017-12-17 11:37:07 -05:00
* Note that while it is possible to re-package old versions, the resultant tarball
2017-12-22 11:41:54 -05:00
* may not be equivalent due to subsequent changes in the exclude list, or because
2017-12-17 11:37:07 -05:00
* of new tooling.
*/
public function package(string $version = null): Result {
// establish which commit to package
2017-12-17 11:37:07 -05:00
$version = $version ?? $this->askDefault("Commit to package:", "head");
$archive = self::BASE."arsse-$version.tar.gz";
// start a collection
$t = $this->collectionBuilder();
// create a temporary directory
$dir = $t->tmpDir().\DIRECTORY_SEPARATOR;
// create a Git worktree for the selected commit in the temp location
$t->taskExec("git worktree add ".escapeshellarg($dir)." ".escapeshellarg($version));
// perform Composer installation in the temp location
$t->taskComposerInstall()->dir($dir)->noDev()->optimizeAutoloader()->arg("--no-scripts");
// delete unwanted files
$t->taskFilesystemStack()->remove([
$dir.".git",
$dir.".gitignore",
$dir.".gitattributes",
$dir."composer.json",
$dir."composer.lock",
$dir.".php_cs.dist",
$dir."phpdoc.dist.xml",
$dir."build.xml",
$dir."RoboFile.php",
$dir."CONTRIBUTING.md",
$dir."tests",
$dir."vendor-bin",
$dir."robo",
$dir."robo.bat",
]);
// generate a sample configuration file
$t->taskExec("php arsse.php conf save-defaults config.defaults.php")->dir($dir);
// package it all up
$t->taskPack($archive)->addDir("arsse", $dir);
// execute the collection
$out = $t->run();
// clean the Git worktree list
$this->_exec("git worktree prune");
return $out;
}
2017-12-22 11:41:54 -05:00
}