Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F996039
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
46 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/unit/engine/ArcanistBaseUnitTestEngine.php b/src/unit/engine/ArcanistBaseUnitTestEngine.php
index 802cc15d..9f3ae5e2 100644
--- a/src/unit/engine/ArcanistBaseUnitTestEngine.php
+++ b/src/unit/engine/ArcanistBaseUnitTestEngine.php
@@ -1,106 +1,116 @@
<?php
/**
* Manages unit test execution.
*
* @group unit
*/
abstract class ArcanistBaseUnitTestEngine {
private $workingCopy;
private $paths;
private $arguments = array();
protected $diffID;
private $enableAsyncTests;
private $enableCoverage;
private $runAllTests;
protected $renderer;
public function setRunAllTests($run_all_tests) {
if (!$this->supportsRunAllTests() && $run_all_tests) {
$class = get_class($this);
throw new Exception(
"Engine '{$class}' does not support --everything.");
}
$this->runAllTests = $run_all_tests;
return $this;
}
public function getRunAllTests() {
return $this->runAllTests;
}
protected function supportsRunAllTests() {
return false;
}
final public function __construct() {
}
+ public function setConfigurationManager(
+ ArcanistConfigurationManager $configuration_manager) {
+ $this->configurationManager = $configuration_manager;
+ return $this;
+ }
+
+ public function getConfigurationManager() {
+ return $this->configurationManager;
+ }
+
final public function setWorkingCopy(
ArcanistWorkingCopyIdentity $working_copy) {
$this->workingCopy = $working_copy;
return $this;
}
final public function getWorkingCopy() {
return $this->workingCopy;
}
final public function setPaths(array $paths) {
$this->paths = $paths;
return $this;
}
final public function getPaths() {
return $this->paths;
}
final public function setArguments(array $arguments) {
$this->arguments = $arguments;
return $this;
}
final public function getArgument($key, $default = null) {
return idx($this->arguments, $key, $default);
}
final public function setEnableAsyncTests($enable_async_tests) {
$this->enableAsyncTests = $enable_async_tests;
return $this;
}
final public function getEnableAsyncTests() {
return $this->enableAsyncTests;
}
final public function setEnableCoverage($enable_coverage) {
$this->enableCoverage = $enable_coverage;
return $this;
}
final public function getEnableCoverage() {
return $this->enableCoverage;
}
public function setRenderer(ArcanistUnitRenderer $renderer) {
$this->renderer = $renderer;
return $this;
}
abstract public function run();
/**
* Modify the return value of this function in the child class, if
* you do not need to echo the test results after all the tests have
* been run. This is the case for example when the child class
* prints the tests results while the tests are running.
*/
public function shouldEchoTestResults() {
return true;
}
}
diff --git a/src/unit/engine/CSharpToolsTestEngine.php b/src/unit/engine/CSharpToolsTestEngine.php
index c4de29b5..03369798 100644
--- a/src/unit/engine/CSharpToolsTestEngine.php
+++ b/src/unit/engine/CSharpToolsTestEngine.php
@@ -1,283 +1,287 @@
<?php
/**
* Uses cscover (http://github.com/hach-que/cstools) to report code coverage.
*
* This engine inherits from `XUnitTestEngine`, where xUnit is used to actually
* run the unit tests and this class provides a thin layer on top to collect
* code coverage data with a third-party tool.
*
* @group unitrun
*/
final class CSharpToolsTestEngine extends XUnitTestEngine {
private $cscoverHintPath;
private $coverEngine;
private $cachedResults;
private $matchRegex;
private $excludedFiles;
/**
* Overridden version of `loadEnvironment` to support a different set
* of configuration values and to pull in the cstools config for
* code coverage.
*/
protected function loadEnvironment(
$config_item = 'unit.csharp.xunit.binary') {
- $working = $this->getWorkingCopy();
- $this->xunitHintPath = $working->getConfig('unit.csharp.xunit.binary');
- $this->cscoverHintPath = $working->getConfig('unit.csharp.cscover.binary');
- $this->matchRegex = $working->getConfig('unit.csharp.coverage.match');
- $this->excludedFiles = $working->getConfig('unit.csharp.coverage.excluded');
+ $config = $this->getConfigurationManager();
+ $this->xunitHintPath =
+ $config->getConfigFromAnySource('unit.csharp.xunit.binary');
+ $this->cscoverHintPath =
+ $config->getConfigFromAnySource('unit.csharp.cscover.binary');
+ $this->matchRegex =
+ $config->getConfigFromAnySource('unit.csharp.coverage.match');
+ $this->excludedFiles =
+ $config->getConfigFromAnySource('unit.csharp.coverage.excluded');
parent::loadEnvironment($config_item);
if ($this->getEnableCoverage() === false) {
return;
}
// Determine coverage path.
if ($this->cscoverHintPath === null) {
throw new Exception(
"Unable to locate cscover. Configure it with ".
"the `unit.csharp.coverage.binary' option in .arcconfig");
}
$cscover = $this->projectRoot."/".$this->cscoverHintPath;
if (file_exists($cscover)) {
$this->coverEngine = Filesystem::resolvePath($cscover);
} else {
throw new Exception(
"Unable to locate cscover coverage runner ".
"(have you built yet?)");
}
}
/**
* Returns whether the specified assembly should be instrumented for
* code coverage reporting. Checks the excluded file list and the
* matching regex if they are configured.
*
* @return boolean Whether the assembly should be instrumented.
*/
private function assemblyShouldBeInstrumented($file) {
if ($this->excludedFiles !== null) {
if (array_key_exists((string)$file, $this->excludedFiles)) {
return false;
}
}
if ($this->matchRegex !== null) {
if (preg_match($this->matchRegex, $file) === 1) {
return true;
} else {
return false;
}
}
return true;
}
/**
* Overridden version of `buildTestFuture` so that the unit test can be run
* via `cscover`, which instruments assemblies and reports on code coverage.
*
* @param string Name of the test assembly.
* @return array The future, output filename and coverage filename
* stored in an array.
*/
protected function buildTestFuture($test_assembly) {
if ($this->getEnableCoverage() === false) {
return parent::buildTestFuture($test_assembly);
}
// FIXME: Can't use TempFile here as xUnit doesn't like
// UNIX-style full paths. It sees the leading / as the
// start of an option flag, even when quoted.
$xunit_temp = $test_assembly.".results.xml";
if (file_exists($xunit_temp)) {
unlink($xunit_temp);
}
$cover_temp = new TempFile();
$cover_temp->setPreserveFile(true);
$xunit_cmd = $this->runtimeEngine;
$xunit_args = null;
if ($xunit_cmd === "") {
$xunit_cmd = $this->testEngine;
$xunit_args = csprintf(
"%s /xml %s /silent",
$test_assembly.".dll",
$xunit_temp);
} else {
$xunit_args = csprintf(
"%s %s /xml %s /silent",
$this->testEngine,
$test_assembly.".dll",
$xunit_temp);
}
$assembly_dir = $test_assembly."/bin/Debug/";
$assemblies_to_instrument = array();
foreach (Filesystem::listDirectory($assembly_dir) as $file) {
if (substr($file, -4) == ".dll" || substr($file, -4) == ".exe") {
if ($this->assemblyShouldBeInstrumented($file)) {
$assemblies_to_instrument[] = $assembly_dir.$file;
}
}
}
$future = new ExecFuture(
"%C -o %s -c %s -a %s -w %s %Ls",
trim($this->runtimeEngine." ".$this->coverEngine),
$cover_temp,
$xunit_cmd,
$xunit_args,
$assembly_dir,
$assemblies_to_instrument);
$future->setCWD(Filesystem::resolvePath($this->projectRoot));
return array(
$future,
$this->projectRoot."/".$assembly_dir.$xunit_temp,
$cover_temp);
}
/**
* Returns coverage results for the unit tests.
*
* @param string The name of the coverage file if one was provided by
* `buildTestFuture`.
* @return array Code coverage results, or null.
*/
protected function parseCoverageResult($cover_file) {
if ($this->getEnableCoverage() === false) {
return parent::parseCoverageResult($cover_file);
}
return $this->readCoverage($cover_file);
}
/**
* Retrieves the cached results for a coverage result file. The coverage
* result file is XML and can be large depending on what has been instrumented
* so we cache it in case it's requested again.
*
* @param string The name of the coverage file.
* @return array Code coverage results, or null if not cached.
*/
private function getCachedResultsIfPossible($cover_file) {
if ($this->cachedResults == null) {
$this->cachedResults = array();
}
if (array_key_exists((string)$cover_file, $this->cachedResults)) {
return $this->cachedResults[(string)$cover_file];
}
return null;
}
/**
* Stores the code coverage results in the cache.
*
* @param string The name of the coverage file.
* @param array The results to cache.
*/
private function addCachedResults($cover_file, array $results) {
if ($this->cachedResults == null) {
$this->cachedResults = array();
}
$this->cachedResults[(string)$cover_file] = $results;
}
/**
* Processes a set of XML tags as code coverage results. We parse
* the `instrumented` and `executed` tags with this method so that
* we can access the data multiple times without a performance hit.
*
* @param array The array of XML tags to parse.
* @return array A PHP array containing the data.
*/
private function processTags($tags) {
$results = array();
foreach ($tags as $tag) {
$results[] = array(
"file" => $tag->getAttribute("file"),
"start" => $tag->getAttribute("start"),
"end" => $tag->getAttribute("end"));
}
return $results;
}
/**
* Reads the code coverage results from the cscover results file.
*
* @param string The path to the code coverage file.
* @return array The code coverage results.
*/
public function readCoverage($cover_file) {
$cached = $this->getCachedResultsIfPossible($cover_file);
if ($cached !== null) {
return $cached;
}
$coverage_dom = new DOMDocument();
$coverage_dom->loadXML(Filesystem::readFile($cover_file));
$modified = $this->getPaths();
$files = array();
$reports = array();
$instrumented = array();
$executed = array();
$instrumented = $this->processTags(
$coverage_dom->getElementsByTagName("instrumented"));
$executed = $this->processTags(
$coverage_dom->getElementsByTagName("executed"));
foreach ($instrumented as $instrument) {
$absolute_file = $instrument["file"];
$relative_file = substr($absolute_file, strlen($this->projectRoot) + 1);
if (!in_array($relative_file, $files)) {
$files[] = $relative_file;
}
}
foreach ($files as $file) {
$absolute_file = $this->projectRoot."/".$file;
// get total line count in file
$line_count = count(file($absolute_file));
$coverage = array();
for ($i = 0; $i < $line_count; $i++) {
$coverage[$i] = 'N';
}
foreach ($instrumented as $instrument) {
if ($instrument["file"] !== $absolute_file) {
continue;
}
for (
$i = $instrument["start"];
$i <= $instrument["end"];
$i++) {
$coverage[$i - 1] = 'U';
}
}
foreach ($executed as $execute) {
if ($execute["file"] !== $absolute_file) {
continue;
}
for (
$i = $execute["start"];
$i <= $execute["end"];
$i++) {
$coverage[$i - 1] = 'C';
}
}
$reports[$file] = implode($coverage);
}
$this->addCachedResults($cover_file, $reports);
return $reports;
}
}
diff --git a/src/unit/engine/PhpunitTestEngine.php b/src/unit/engine/PhpunitTestEngine.php
index 55be1e8e..4a29af66 100644
--- a/src/unit/engine/PhpunitTestEngine.php
+++ b/src/unit/engine/PhpunitTestEngine.php
@@ -1,278 +1,282 @@
<?php
/**
* PHPUnit wrapper
*
* To use, set unit.engine in .arcconfig, or use --engine flag
* with arc unit. Currently supports only class & test files
* (no directory support).
* To use custom phpunit configuration, set phpunit_config in
* .arcconfig (e.g. app/phpunit.xml.dist).
*
* @group unitrun
*/
final class PhpunitTestEngine extends ArcanistBaseUnitTestEngine {
private $configFile;
private $phpunitBinary = 'phpunit';
private $affectedTests;
private $projectRoot;
public function run() {
$this->projectRoot = $this->getWorkingCopy()->getProjectRoot();
$this->affectedTests = array();
foreach ($this->getPaths() as $path) {
$path = Filesystem::resolvePath($path, $this->projectRoot);
// TODO: add support for directories
// Users can call phpunit on the directory themselves
if (is_dir($path)) {
continue;
}
// Not sure if it would make sense to go further if
// it is not a .php file
if (substr($path, -4) != '.php') {
continue;
}
if (substr($path, -8) == 'Test.php') {
// Looks like a valid test file name.
$this->affectedTests[$path] = $path;
continue;
}
if ($test = $this->findTestFile($path)) {
$this->affectedTests[$path] = $test;
}
}
if (empty($this->affectedTests)) {
throw new ArcanistNoEffectException('No tests to run.');
}
$this->prepareConfigFile();
$futures = array();
$tmpfiles = array();
foreach ($this->affectedTests as $class_path => $test_path) {
if(!Filesystem::pathExists($test_path)) {
continue;
}
$json_tmp = new TempFile();
$clover_tmp = null;
$clover = null;
if ($this->getEnableCoverage() !== false) {
$clover_tmp = new TempFile();
$clover = csprintf('--coverage-clover %s', $clover_tmp);
}
$config = $this->configFile ? csprintf('-c %s', $this->configFile) : null;
$futures[$test_path] = new ExecFuture('%C %C --log-json %s %C %s',
$this->phpunitBinary, $config, $json_tmp, $clover, $test_path);
$tmpfiles[$test_path] = array(
'json' => $json_tmp,
'clover' => $clover_tmp,
);
}
$results = array();
foreach (Futures($futures)->limit(4) as $test => $future) {
list($err, $stdout, $stderr) = $future->resolve();
$results[] = $this->parseTestResults(
$test,
$tmpfiles[$test]['json'],
$tmpfiles[$test]['clover']);
}
return array_mergev($results);
}
/**
* Parse test results from phpunit json report
*
* @param string $path Path to test
* @param string $json_tmp Path to phpunit json report
* @param string $clover_tmp Path to phpunit clover report
*
* @return array
*/
private function parseTestResults($path, $json_tmp, $clover_tmp) {
$test_results = Filesystem::readFile($json_tmp);
return id(new PhpunitResultParser())
->setEnableCoverage($this->getEnableCoverage())
->setProjectRoot($this->projectRoot)
->setCoverageFile($clover_tmp)
->setAffectedTests($this->affectedTests)
->parseTestResults($path, $test_results);
}
/**
* Search for test cases for a given file in a large number of "reasonable"
* locations. See @{method:getSearchLocationsForTests} for specifics.
*
* TODO: Add support for finding tests in testsuite folders from
* phpunit.xml configuration.
*
* @param string PHP file to locate test cases for.
* @return string|null Path to test cases, or null.
*/
private function findTestFile($path) {
$root = $this->projectRoot;
$path = Filesystem::resolvePath($path, $root);
$file = basename($path);
$possible_files = array(
$file,
substr($file, 0, -4).'Test.php',
);
$search = self::getSearchLocationsForTests($path);
foreach ($search as $search_path) {
foreach ($possible_files as $possible_file) {
$full_path = $search_path.$possible_file;
if (!Filesystem::pathExists($full_path)) {
// If the file doesn't exist, it's clearly a miss.
continue;
}
if (!Filesystem::isDescendant($full_path, $root)) {
// Don't look above the project root.
continue;
}
if (0 == strcasecmp(Filesystem::resolvePath($full_path), $path)) {
// Don't return the original file.
continue;
}
return $full_path;
}
}
return null;
}
/**
* Get places to look for PHP Unit tests that cover a given file. For some
* file "/a/b/c/X.php", we look in the same directory:
*
* /a/b/c/
*
* We then look in all parent directories for a directory named "tests/"
* (or "Tests/"):
*
* /a/b/c/tests/
* /a/b/tests/
* /a/tests/
* /tests/
*
* We also try to replace each directory component with "tests/":
*
* /a/b/tests/
* /a/tests/c/
* /tests/b/c/
*
* We also try to add "tests/" at each directory level:
*
* /a/b/c/tests/
* /a/b/tests/c/
* /a/tests/b/c/
* /tests/a/b/c/
*
* This finds tests with a layout like:
*
* docs/
* src/
* tests/
*
* ...or similar. This list will be further pruned by the caller; it is
* intentionally filesystem-agnostic to be unit testable.
*
* @param string PHP file to locate test cases for.
* @return list<string> List of directories to search for tests in.
*/
public static function getSearchLocationsForTests($path) {
$file = basename($path);
$dir = dirname($path);
$test_dir_names = array('tests', 'Tests');
$try_directories = array();
// Try in the current directory.
$try_directories[] = array($dir);
// Try in a tests/ directory anywhere in the ancestry.
foreach (Filesystem::walkToRoot($dir) as $parent_dir) {
if ($parent_dir == '/') {
// We'll restore this later.
$parent_dir = '';
}
foreach ($test_dir_names as $test_dir_name) {
$try_directories[] = array($parent_dir, $test_dir_name);
}
}
// Try replacing each directory component with 'tests/'.
$parts = trim($dir, DIRECTORY_SEPARATOR);
$parts = explode(DIRECTORY_SEPARATOR, $parts);
foreach (array_reverse(array_keys($parts)) as $key) {
foreach ($test_dir_names as $test_dir_name) {
$try = $parts;
$try[$key] = $test_dir_name;
array_unshift($try, '');
$try_directories[] = $try;
}
}
// Try adding 'tests/' at each level.
foreach (array_reverse(array_keys($parts)) as $key) {
foreach ($test_dir_names as $test_dir_name) {
$try = $parts;
$try[$key] = $test_dir_name.DIRECTORY_SEPARATOR.$try[$key];
array_unshift($try, '');
$try_directories[] = $try;
}
}
$results = array();
foreach ($try_directories as $parts) {
$results[implode(DIRECTORY_SEPARATOR, $parts).DIRECTORY_SEPARATOR] = true;
}
return array_keys($results);
}
/**
* Tries to find and update phpunit configuration file
* based on phpunit_config option in .arcconfig
*/
private function prepareConfigFile() {
$project_root = $this->projectRoot . DIRECTORY_SEPARATOR;
+ $config = $this->getConfigurationManager()->getConfigFromAnySource(
+ 'phpunit_config');
- if ($config = $this->getWorkingCopy()->getConfig('phpunit_config')) {
+ if ($config) {
if (Filesystem::pathExists($project_root . $config)) {
$this->configFile = $project_root . $config;
} else {
throw new Exception('PHPUnit configuration file was not ' .
'found in ' . $project_root . $config);
}
}
- if ($bin = $this->getWorkingCopy()->getConfig('unit.phpunit.binary')) {
+ $bin = $this->getConfigurationManager()->getConfigFromAnySource(
+ 'unit.phpunit.binary');
+ if ($bin) {
if (Filesystem::binaryExists($bin)) {
$this->phpunitBinary = $bin;
}
else {
$this->phpunitBinary = Filesystem::resolvePath($bin, $project_root);
}
}
}
}
diff --git a/src/unit/engine/XUnitTestEngine.php b/src/unit/engine/XUnitTestEngine.php
index 79887f5b..e2cd105a 100644
--- a/src/unit/engine/XUnitTestEngine.php
+++ b/src/unit/engine/XUnitTestEngine.php
@@ -1,451 +1,452 @@
<?php
/**
* Uses xUnit (http://xunit.codeplex.com/) to test C# code.
*
* Assumes that when modifying a file with a path like `SomeAssembly/MyFile.cs`,
* that the test assembly that verifies the functionality of `SomeAssembly` is
* located at `SomeAssembly.Tests`.
*
* @group unitrun
* @concrete-extensible
*/
class XUnitTestEngine extends ArcanistBaseUnitTestEngine {
protected $runtimeEngine;
protected $buildEngine;
protected $testEngine;
protected $projectRoot;
protected $xunitHintPath;
/**
* This test engine supports running all tests.
*/
protected function supportsRunAllTests() {
return true;
}
/**
* Determines what executables and test paths to use. Between platforms
* this also changes whether the test engine is run under .NET or Mono. It
* also ensures that all of the required binaries are available for the tests
* to run successfully.
*
* @return void
*/
protected function loadEnvironment($config_item = 'unit.xunit.binary') {
$this->projectRoot = $this->getWorkingCopy()->getProjectRoot();
// Determine build engine.
if (Filesystem::binaryExists("msbuild")) {
$this->buildEngine = "msbuild";
} else if (Filesystem::binaryExists("xbuild")) {
$this->buildEngine = "xbuild";
} else {
throw new Exception("Unable to find msbuild or xbuild in PATH!");
}
// Determine runtime engine (.NET or Mono).
if (phutil_is_windows()) {
$this->runtimeEngine = "";
} else if (Filesystem::binaryExists("mono")) {
$this->runtimeEngine = Filesystem::resolveBinary("mono");
} else {
throw new Exception("Unable to find Mono and you are not on Windows!");
}
// Determine xUnit test runner path.
if ($this->xunitHintPath === null) {
- $this->xunitHintPath = $this->getWorkingCopy()->getConfig(
- 'unit.xunit.binary');
+ $this->xunitHintPath =
+ $this->getConfigurationManager()->getConfigFromAnySource(
+ 'unit.xunit.binary');
}
if ($this->xunitHintPath === null) {
}
$xunit = $this->projectRoot."/".$this->xunitHintPath;
if (file_exists($xunit)) {
$this->testEngine = Filesystem::resolvePath($xunit);
} else if (Filesystem::binaryExists("xunit.console.clr4.exe")) {
$this->testEngine = "xunit.console.clr4.exe";
} else {
throw new Exception(
"Unable to locate xUnit console runner. Configure ".
"it with the `$config_item' option in .arcconfig");
}
}
/**
* Returns all available tests and related projects. Recurses into
* Protobuild submodules if they are present.
*
* @return array Mappings of test project to project being tested.
*/
public function getAllAvailableTestsAndRelatedProjects($path = null) {
if ($path == null) {
$path = $this->projectRoot;
}
$entries = Filesystem::listDirectory($path);
$mappings = array();
foreach ($entries as $entry) {
if (substr($entry, -6) === ".Tests") {
if (is_dir($path."/".$entry)) {
$mappings[$path."/".$entry] = $path."/".
substr($entry, 0, strlen($entry) - 6);
}
} elseif (is_dir($path."/".$entry."/Build")) {
if (file_exists($path."/".$entry."/Build/Module.xml")) {
// The entry is a Protobuild submodule, which we should
// also recurse into.
$submappings =
$this->getAllAvailableTestsAndRelatedProjects($path."/".$entry);
foreach ($submappings as $key => $value) {
$mappings[$key] = $value;
}
}
}
}
return $mappings;
}
/**
* Main entry point for the test engine. Determines what assemblies to
* build and test based on the files that have changed.
*
* @return array Array of test results.
*/
public function run() {
$this->loadEnvironment();
$affected_tests = array();
if ($this->getRunAllTests()) {
echo "Loading tests..."."\n";
$entries = $this->getAllAvailableTestsAndRelatedProjects();
foreach ($entries as $key => $value) {
echo "Test: ".substr($key, strlen($this->projectRoot) + 1)."\n";
$affected_tests[] = substr($key, strlen($this->projectRoot) + 1);
}
} else {
$paths = $this->getPaths();
foreach ($paths as $path) {
if (substr($path, -4) == ".dll" ||
substr($path, -4) == ".mdb") {
continue;
}
if (substr_count($path, "/") > 0) {
$components = explode("/", $path);
$affected_assembly = $components[0];
// If the change is made inside an assembly that has a `.Tests`
// extension, then the developer has changed the actual tests.
if (substr($affected_assembly, -6) === ".Tests") {
$affected_assembly_path = Filesystem::resolvePath(
$affected_assembly);
$test_assembly = $affected_assembly;
} else {
$affected_assembly_path = Filesystem::resolvePath(
$affected_assembly.".Tests");
$test_assembly = $affected_assembly.".Tests";
}
if (is_dir($affected_assembly_path) &&
!in_array($test_assembly, $affected_tests)) {
$affected_tests[] = $test_assembly;
}
}
}
}
return $this->runAllTests($affected_tests);
}
/**
* Builds and runs the specified test assemblies.
*
* @param array Array of test assemblies.
* @return array Array of test results.
*/
public function runAllTests(array $test_assemblies) {
if (empty($test_assemblies)) {
return array();
}
$results = array();
$results[] = $this->generateProjects();
if ($this->resultsContainFailures($results)) {
return array_mergev($results);
}
$results[] = $this->buildProjects($test_assemblies);
if ($this->resultsContainFailures($results)) {
return array_mergev($results);
}
$results[] = $this->testAssemblies($test_assemblies);
return array_mergev($results);
}
/**
* Determine whether or not a current set of results contains any failures.
* This is needed since we build the assemblies as part of the unit tests, but
* we can't run any of the unit tests if the build fails.
*
* @param array Array of results to check.
* @return bool If there are any failures in the results.
*/
private function resultsContainFailures(array $results) {
$results = array_mergev($results);
foreach ($results as $result) {
if ($result->getResult() != ArcanistUnitTestResult::RESULT_PASS) {
return true;
}
}
return false;
}
/**
* If the `Build` directory exists, we assume that this is a multi-platform
* project that requires generation of C# project files. Because we want to
* test that the generation and subsequent build is whole, we need to
* regenerate any projects in case the developer has added files through an
* IDE and then forgotten to add them to the respective `.definitions` file.
* By regenerating the projects we ensure that any missing definition entries
* will cause the build to fail.
*
* @return array Array of test results.
*/
private function generateProjects() {
// No "Build" directory; so skip generation of projects.
if (!is_dir(Filesystem::resolvePath($this->projectRoot."/Build"))) {
return array();
}
// Work out what platform the user is building for already.
$platform = phutil_is_windows() ? "Windows" : "Linux";
$files = Filesystem::listDirectory($this->projectRoot);
foreach ($files as $file) {
if (strtolower(substr($file, -4)) == ".sln") {
$parts = explode(".", $file);
$platform = $parts[count($parts) - 2];
break;
}
}
$regenerate_start = microtime(true);
$regenerate_future = new ExecFuture(
"%C Protobuild.exe --resync %s",
$this->runtimeEngine,
$platform);
$regenerate_future->setCWD(Filesystem::resolvePath(
$this->projectRoot));
$results = array();
$result = new ArcanistUnitTestResult();
$result->setName("(regenerate projects for $platform)");
try {
$regenerate_future->resolvex();
$result->setResult(ArcanistUnitTestResult::RESULT_PASS);
} catch(CommandException $exc) {
if ($exc->getError() > 1) {
throw $exc;
}
$result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
$result->setUserdata($exc->getStdout());
}
$result->setDuration(microtime(true) - $regenerate_start);
$results[] = $result;
return $results;
}
/**
* Build the projects relevant for the specified test assemblies and return
* the results of the builds as test results. This build also passes the
* "SkipTestsOnBuild" parameter when building the projects, so that MSBuild
* conditionals can be used to prevent any tests running as part of the
* build itself (since the unit tester is about to run each of the tests
* individually).
*
* @param array Array of test assemblies.
* @return array Array of test results.
*/
private function buildProjects(array $test_assemblies) {
$build_futures = array();
$build_failed = false;
$build_start = microtime(true);
$results = array();
foreach ($test_assemblies as $test_assembly) {
$build_future = new ExecFuture(
"%C %s",
$this->buildEngine,
"/p:SkipTestsOnBuild=True");
$build_future->setCWD(Filesystem::resolvePath(
$this->projectRoot."/".$test_assembly));
$build_futures[$test_assembly] = $build_future;
}
$iterator = Futures($build_futures)->limit(1);
foreach ($iterator as $test_assembly => $future) {
$result = new ArcanistUnitTestResult();
$result->setName("(build) ".$test_assembly);
try {
$future->resolvex();
$result->setResult(ArcanistUnitTestResult::RESULT_PASS);
} catch(CommandException $exc) {
if ($exc->getError() > 1) {
throw $exc;
}
$result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
$result->setUserdata($exc->getStdout());
$build_failed = true;
}
$result->setDuration(microtime(true) - $build_start);
$results[] = $result;
}
return $results;
}
/**
* Build the future for running a unit test. This can be
* overridden to enable support for code coverage via
* another tool
*
* @param string Name of the test assembly.
* @return array The future, output filename and coverage filename
* stored in an array.
*/
protected function buildTestFuture($test_assembly) {
// FIXME: Can't use TempFile here as xUnit doesn't like
// UNIX-style full paths. It sees the leading / as the
// start of an option flag, even when quoted.
$xunit_temp = $test_assembly.".results.xml";
if (file_exists($xunit_temp)) {
unlink($xunit_temp);
}
$future = new ExecFuture(
"%C %s /xml %s /silent",
trim($this->runtimeEngine." ".$this->testEngine),
$test_assembly."/bin/Debug/".$test_assembly.".dll",
$xunit_temp);
$future->setCWD(Filesystem::resolvePath($this->projectRoot));
return array($future, $xunit_temp, null);
}
/**
* Run the xUnit test runner on each of the assemblies and parse the
* resulting XML.
*
* @param array Array of test assemblies.
* @return array Array of test results.
*/
private function testAssemblies(array $test_assemblies) {
$results = array();
// Build the futures for running the tests.
$futures = array();
$outputs = array();
$coverages = array();
foreach ($test_assemblies as $test_assembly) {
list($future, $xunit_temp, $coverage) =
$this->buildTestFuture($test_assembly);
$futures[$test_assembly] = $future;
$outputs[$test_assembly] = $xunit_temp;
$coverages[$test_assembly] = $coverage;
}
// Run all of the tests.
foreach (Futures($futures) as $test_assembly => $future) {
$future->resolve();
if (file_exists($outputs[$test_assembly])) {
$result = $this->parseTestResult(
$outputs[$test_assembly],
$coverages[$test_assembly]);
$results[] = $result;
unlink($outputs[$test_assembly]);
} else {
$result = new ArcanistUnitTestResult();
$result->setName("(execute) ".$test_assembly);
$result->setResult(ArcanistUnitTestResult::RESULT_BROKEN);
$result->setUserData($outputs[$test_assembly]." not found on disk.");
$results[] = array($result);
}
}
return array_mergev($results);
}
/**
* Returns null for this implementation as xUnit does not support code
* coverage directly. Override this method in another class to provide
* code coverage information (also see `CSharpToolsUnitEngine`).
*
* @param string The name of the coverage file if one was provided by
* `buildTestFuture`.
* @return array Code coverage results, or null.
*/
protected function parseCoverageResult($coverage) {
return null;
}
/**
* Parses the test results from xUnit.
*
* @param string The name of the xUnit results file.
* @param string The name of the coverage file if one was provided by
* `buildTestFuture`. This is passed through to
* `parseCoverageResult`.
* @return array Test results.
*/
private function parseTestResult($xunit_tmp, $coverage) {
$xunit_dom = new DOMDocument();
$xunit_dom->loadXML(Filesystem::readFile($xunit_tmp));
$results = array();
$tests = $xunit_dom->getElementsByTagName("test");
foreach ($tests as $test) {
$name = $test->getAttribute("name");
$time = $test->getAttribute("time");
$status = ArcanistUnitTestResult::RESULT_UNSOUND;
switch ($test->getAttribute("result")) {
case "Pass":
$status = ArcanistUnitTestResult::RESULT_PASS;
break;
case "Fail":
$status = ArcanistUnitTestResult::RESULT_FAIL;
break;
case "Skip":
$status = ArcanistUnitTestResult::RESULT_SKIP;
break;
}
$userdata = "";
$reason = $test->getElementsByTagName("reason");
$failure = $test->getElementsByTagName("failure");
if ($reason->length > 0 || $failure->length > 0) {
$node = ($reason->length > 0) ? $reason : $failure;
$message = $node->item(0)->getElementsByTagName("message");
if ($message->length > 0) {
$userdata = $message->item(0)->nodeValue;
}
$stacktrace = $node->item(0)->getElementsByTagName("stack-trace");
if ($stacktrace->length > 0) {
$userdata .= "\n".$stacktrace->item(0)->nodeValue;
}
}
$result = new ArcanistUnitTestResult();
$result->setName($name);
$result->setResult($status);
$result->setDuration($time);
$result->setUserData($userdata);
if ($coverage != null) {
$result->setCoverage($this->parseCoverageResult($coverage));
}
$results[] = $result;
}
return $results;
}
}
diff --git a/src/workflow/ArcanistUnitWorkflow.php b/src/workflow/ArcanistUnitWorkflow.php
index 64e7fc19..cbf99ce4 100644
--- a/src/workflow/ArcanistUnitWorkflow.php
+++ b/src/workflow/ArcanistUnitWorkflow.php
@@ -1,356 +1,357 @@
<?php
/**
* Runs unit tests which cover your changes.
*
* @group workflow
*/
final class ArcanistUnitWorkflow extends ArcanistBaseWorkflow {
const RESULT_OKAY = 0;
const RESULT_UNSOUND = 1;
const RESULT_FAIL = 2;
const RESULT_SKIP = 3;
const RESULT_POSTPONED = 4;
private $unresolvedTests;
private $testResults;
private $engine;
public function getWorkflowName() {
return 'unit';
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**unit** [__options__] [__paths__]
**unit** [__options__] --rev [__rev__]
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: git, svn, hg
Run unit tests that cover specified paths. If no paths are specified,
unit tests covering all modified files will be run.
EOTEXT
);
}
public function getArguments() {
return array(
'rev' => array(
'param' => 'revision',
'help' => "Run unit tests covering changes since a specific revision.",
'supports' => array(
'git',
'hg',
),
'nosupport' => array(
'svn' => "Arc unit does not currently support --rev in SVN.",
),
),
'engine' => array(
'param' => 'classname',
'help' =>
"Override configured unit engine for this project."
),
'coverage' => array(
'help' => 'Always enable coverage information.',
'conflicts' => array(
'no-coverage' => null,
),
),
'no-coverage' => array(
'help' => 'Always disable coverage information.',
),
'detailed-coverage' => array(
'help' => "Show a detailed coverage report on the CLI. Implies ".
"--coverage.",
),
'json' => array(
'help' => 'Report results in JSON format.',
),
'output' => array(
'param' => 'format',
'help' =>
"With 'full', show full pretty report (Default). ".
"With 'json', report results in JSON format. ".
"With 'ugly', use uglier (but more efficient) JSON formatting. ".
"With 'none', don't print results. ",
'conflicts' => array(
'json' => 'Only one output format allowed',
'ugly' => 'Only one output format allowed',
)
),
'everything' => array(
'help' => 'Run every test.',
'conflicts' => array(
'rev' => '--everything runs all tests.',
),
),
'ugly' => array(
'help' => 'With --json, use uglier (but more efficient) formatting.',
),
'*' => 'paths',
);
}
public function requiresWorkingCopy() {
return true;
}
public function requiresRepositoryAPI() {
return true;
}
public function getEngine() {
return $this->engine;
}
public function run() {
$working_copy = $this->getWorkingCopy();
$engine_class = $this->getArgument(
'engine',
$this->getConfigurationManager()->getConfigFromAnySource('unit.engine'));
if (!$engine_class) {
throw new ArcanistNoEngineException(
"No unit test engine is configured for this project. Edit .arcconfig ".
"to specify a unit test engine.");
}
$paths = $this->getArgument('paths');
$rev = $this->getArgument('rev');
$everything = $this->getArgument('everything');
if ($everything && $paths) {
throw new ArcanistUsageException(
"You can not specify paths with --everything. The --everything ".
"flag runs every test.");
}
$paths = $this->selectPathsForWorkflow($paths, $rev);
if (!class_exists($engine_class) ||
!is_subclass_of($engine_class, 'ArcanistBaseUnitTestEngine')) {
throw new ArcanistUsageException(
"Configured unit test engine '{$engine_class}' is not a subclass of ".
"'ArcanistBaseUnitTestEngine'.");
}
$this->engine = newv($engine_class, array());
$this->engine->setWorkingCopy($working_copy);
+ $this->engine->setConfigurationManager($this->getConfigurationManager());
if ($everything) {
$this->engine->setRunAllTests(true);
} else {
$this->engine->setPaths($paths);
}
$this->engine->setArguments($this->getPassthruArgumentsAsMap('unit'));
$renderer = new ArcanistUnitConsoleRenderer();
$this->engine->setRenderer($renderer);
$enable_coverage = null; // Means "default".
if ($this->getArgument('coverage') ||
$this->getArgument('detailed-coverage')) {
$enable_coverage = true;
} else if ($this->getArgument('no-coverage')) {
$enable_coverage = false;
}
$this->engine->setEnableCoverage($enable_coverage);
// Enable possible async tests only for 'arc diff' not 'arc unit'
if ($this->getParentWorkflow()) {
$this->engine->setEnableAsyncTests(true);
} else {
$this->engine->setEnableAsyncTests(false);
}
$results = $this->engine->run();
$this->testResults = $results;
$console = PhutilConsole::getConsole();
$output_format = $this->getOutputFormat();
if ($output_format !== 'full') {
$console->disableOut();
}
$unresolved = array();
$coverage = array();
$postponed_count = 0;
foreach ($results as $result) {
$result_code = $result->getResult();
if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED) {
$postponed_count++;
$unresolved[] = $result;
} else {
if ($this->engine->shouldEchoTestResults()) {
$console->writeOut('%s', $renderer->renderUnitResult($result));
}
if ($result_code != ArcanistUnitTestResult::RESULT_PASS) {
$unresolved[] = $result;
}
}
if ($result->getCoverage()) {
foreach ($result->getCoverage() as $file => $report) {
$coverage[$file][] = $report;
}
}
}
if ($postponed_count) {
$console->writeOut(
'%s',
$renderer->renderPostponedResult($postponed_count));
}
if ($coverage) {
$file_coverage = array_fill_keys(
$paths,
0);
$file_reports = array();
foreach ($coverage as $file => $reports) {
$report = ArcanistUnitTestResult::mergeCoverage($reports);
$cov = substr_count($report, 'C');
$uncov = substr_count($report, 'U');
if ($cov + $uncov) {
$coverage = $cov / ($cov + $uncov);
} else {
$coverage = 0;
}
$file_coverage[$file] = $coverage;
$file_reports[$file] = $report;
}
$console->writeOut("\n__COVERAGE REPORT__\n");
asort($file_coverage);
foreach ($file_coverage as $file => $coverage) {
$console->writeOut(
" **%s%%** %s\n",
sprintf('% 3d', (int)(100 * $coverage)),
$file);
$full_path = $working_copy->getProjectRoot().'/'.$file;
if ($this->getArgument('detailed-coverage') &&
Filesystem::pathExists($full_path) &&
is_file($full_path) &&
array_key_exists($file, $file_reports)) {
$console->writeOut(
'%s',
$this->renderDetailedCoverageReport(
Filesystem::readFile($full_path),
$file_reports[$file]));
}
}
}
$this->unresolvedTests = $unresolved;
$overall_result = self::RESULT_OKAY;
foreach ($results as $result) {
$result_code = $result->getResult();
if ($result_code == ArcanistUnitTestResult::RESULT_FAIL ||
$result_code == ArcanistUnitTestResult::RESULT_BROKEN) {
$overall_result = self::RESULT_FAIL;
break;
} else if ($result_code == ArcanistUnitTestResult::RESULT_UNSOUND) {
$overall_result = self::RESULT_UNSOUND;
} else if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED &&
$overall_result != self::RESULT_UNSOUND) {
$overall_result = self::RESULT_POSTPONED;
}
}
if ($output_format !== 'full') {
$console->enableOut();
}
$data = array_values(mpull($results, 'toDictionary'));
switch ($output_format) {
case 'ugly':
$console->writeOut('%s', json_encode($data));
break;
case 'json':
$json = new PhutilJSON();
$console->writeOut('%s', $json->encodeFormatted($data));
break;
case 'full':
// already printed
break;
case 'none':
// do nothing
break;
}
return $overall_result;
}
public function getUnresolvedTests() {
return $this->unresolvedTests;
}
public function getTestResults() {
return $this->testResults;
}
private function renderDetailedCoverageReport($data, $report) {
$data = explode("\n", $data);
$out = '';
$n = 0;
foreach ($data as $line) {
$out .= sprintf('% 5d ', $n + 1);
$line = str_pad($line, 80, ' ');
if (empty($report[$n])) {
$c = 'N';
} else {
$c = $report[$n];
}
switch ($c) {
case 'C':
$out .= phutil_console_format(
'<bg:green> %s </bg>',
$line);
break;
case 'U':
$out .= phutil_console_format(
'<bg:red> %s </bg>',
$line);
break;
case 'X':
$out .= phutil_console_format(
'<bg:magenta> %s </bg>',
$line);
break;
default:
$out .= ' '.$line.' ';
break;
}
$out .= "\n";
$n++;
}
return $out;
}
private function getOutputFormat() {
if ($this->getArgument('ugly')) {
return 'ugly';
}
if ($this->getArgument('json')) {
return 'json';
}
$format = $this->getArgument('output');
$known_formats = array(
'none' => 'none',
'json' => 'json',
'ugly' => 'ugly',
'full' => 'full',
);
return idx($known_formats, $format, 'full');
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Dec 24, 08:03 (1 d, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557050
Default Alt Text
(46 KB)
Attached To
Mode
R118 Arcanist - fork
Attached
Detach File
Event Timeline
Log In to Comment