2011-01-10 00:22:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
Unify arguments for 'arc lint', 'arc unit'
Summary: See T645. These commands take inconsistent and overly-magical arguments
right now. Instead, make them behave consistently and allow them both to operate
on "arc <workflow> path path2 path3 ...", which is a generally useful workflow.
Test Plan: Ran "arc lint <path>", "arc unit <path>", "arc lint --rev
HEAD^^^^^^", "arc unit --rev HEAD^^^^^^^^^^^^", etc. Ran "arc diff --trace" and
verified --rev argument to child workflows.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, epriestley, btrahan
Maniphest Tasks: T645
Differential Revision: https://secure.phabricator.com/D1348
2012-01-09 21:40:50 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-10 00:22:25 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-02-19 20:36:08 +01:00
|
|
|
/**
|
|
|
|
* Runs unit tests which cover your changes.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class ArcanistUnitWorkflow extends ArcanistBaseWorkflow {
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2011-06-09 01:36:17 +02:00
|
|
|
const RESULT_OKAY = 0;
|
|
|
|
const RESULT_UNSOUND = 1;
|
|
|
|
const RESULT_FAIL = 2;
|
|
|
|
const RESULT_SKIP = 3;
|
|
|
|
const RESULT_POSTPONED = 4;
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2011-01-12 07:13:31 +01:00
|
|
|
private $unresolvedTests;
|
2012-01-31 21:07:19 +01:00
|
|
|
private $testResults;
|
2011-05-28 01:15:43 +02:00
|
|
|
private $engine;
|
2011-01-12 07:13:31 +01:00
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
Unify arguments for 'arc lint', 'arc unit'
Summary: See T645. These commands take inconsistent and overly-magical arguments
right now. Instead, make them behave consistently and allow them both to operate
on "arc <workflow> path path2 path3 ...", which is a generally useful workflow.
Test Plan: Ran "arc lint <path>", "arc unit <path>", "arc lint --rev
HEAD^^^^^^", "arc unit --rev HEAD^^^^^^^^^^^^", etc. Ran "arc diff --trace" and
verified --rev argument to child workflows.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, epriestley, btrahan
Maniphest Tasks: T645
Differential Revision: https://secure.phabricator.com/D1348
2012-01-09 21:40:50 +01:00
|
|
|
**unit** [__options__] [__paths__]
|
|
|
|
**unit** [__options__] --rev [__rev__]
|
|
|
|
Supports: git, svn, hg
|
2011-11-30 21:56:18 +01:00
|
|
|
Run unit tests that cover specified paths. If no paths are specified,
|
|
|
|
unit tests covering all modified files will be run.
|
2011-01-10 00:22:25 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArguments() {
|
|
|
|
return array(
|
Unify arguments for 'arc lint', 'arc unit'
Summary: See T645. These commands take inconsistent and overly-magical arguments
right now. Instead, make them behave consistently and allow them both to operate
on "arc <workflow> path path2 path3 ...", which is a generally useful workflow.
Test Plan: Ran "arc lint <path>", "arc unit <path>", "arc lint --rev
HEAD^^^^^^", "arc unit --rev HEAD^^^^^^^^^^^^", etc. Ran "arc diff --trace" and
verified --rev argument to child workflows.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, epriestley, btrahan
Maniphest Tasks: T645
Differential Revision: https://secure.phabricator.com/D1348
2012-01-09 21:40:50 +01:00
|
|
|
'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.",
|
|
|
|
),
|
|
|
|
),
|
2011-02-16 20:53:13 +01:00
|
|
|
'engine' => array(
|
|
|
|
'param' => 'classname',
|
|
|
|
'help' =>
|
|
|
|
"Override configured unit engine for this project."
|
|
|
|
),
|
2012-01-31 21:07:19 +01:00
|
|
|
'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.",
|
|
|
|
),
|
2011-01-10 00:22:25 +01:00
|
|
|
'*' => 'paths',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresWorkingCopy() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresRepositoryAPI() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-09 02:36:20 +02:00
|
|
|
public function getEngine() {
|
|
|
|
return $this->engine;
|
|
|
|
}
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
public function run() {
|
|
|
|
|
|
|
|
$working_copy = $this->getWorkingCopy();
|
|
|
|
|
|
|
|
$engine_class = $this->getArgument(
|
|
|
|
'engine',
|
|
|
|
$working_copy->getConfig('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.");
|
|
|
|
}
|
|
|
|
|
Unify arguments for 'arc lint', 'arc unit'
Summary: See T645. These commands take inconsistent and overly-magical arguments
right now. Instead, make them behave consistently and allow them both to operate
on "arc <workflow> path path2 path3 ...", which is a generally useful workflow.
Test Plan: Ran "arc lint <path>", "arc unit <path>", "arc lint --rev
HEAD^^^^^^", "arc unit --rev HEAD^^^^^^^^^^^^", etc. Ran "arc diff --trace" and
verified --rev argument to child workflows.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, epriestley, btrahan
Maniphest Tasks: T645
Differential Revision: https://secure.phabricator.com/D1348
2012-01-09 21:40:50 +01:00
|
|
|
$paths = $this->getArgument('paths');
|
|
|
|
$rev = $this->getArgument('rev');
|
2011-02-19 02:08:10 +01:00
|
|
|
|
Unify arguments for 'arc lint', 'arc unit'
Summary: See T645. These commands take inconsistent and overly-magical arguments
right now. Instead, make them behave consistently and allow them both to operate
on "arc <workflow> path path2 path3 ...", which is a generally useful workflow.
Test Plan: Ran "arc lint <path>", "arc unit <path>", "arc lint --rev
HEAD^^^^^^", "arc unit --rev HEAD^^^^^^^^^^^^", etc. Ran "arc diff --trace" and
verified --rev argument to child workflows.
Reviewers: btrahan, jungejason
Reviewed By: btrahan
CC: aran, epriestley, btrahan
Maniphest Tasks: T645
Differential Revision: https://secure.phabricator.com/D1348
2012-01-09 21:40:50 +01:00
|
|
|
$paths = $this->selectPathsForWorkflow($paths, $rev);
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2011-01-13 00:45:17 +01:00
|
|
|
PhutilSymbolLoader::loadClass($engine_class);
|
2011-12-21 17:51:34 +01:00
|
|
|
if (!is_subclass_of($engine_class, 'ArcanistBaseUnitTestEngine')) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"Configured unit test engine '{$engine_class}' is not a subclass of ".
|
|
|
|
"'ArcanistBaseUnitTestEngine'.");
|
|
|
|
}
|
|
|
|
|
2011-05-28 01:15:43 +02:00
|
|
|
$this->engine = newv($engine_class, array());
|
|
|
|
$this->engine->setWorkingCopy($working_copy);
|
|
|
|
$this->engine->setPaths($paths);
|
|
|
|
$this->engine->setArguments($this->getPassthruArgumentsAsMap('unit'));
|
2011-01-10 00:22:25 +01:00
|
|
|
|
2012-01-31 21:07:19 +01:00
|
|
|
$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);
|
|
|
|
|
2011-06-10 21:40:01 +02:00
|
|
|
// Enable possible async tests only for 'arc diff' not 'arc unit'
|
|
|
|
if ($this->getParentWorkflow()) {
|
|
|
|
$this->engine->setEnableAsyncTests(true);
|
|
|
|
} else {
|
|
|
|
$this->engine->setEnableAsyncTests(false);
|
|
|
|
}
|
|
|
|
|
2011-05-28 01:15:43 +02:00
|
|
|
$results = $this->engine->run();
|
2012-01-31 21:07:19 +01:00
|
|
|
$this->testResults = $results;
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
$status_codes = array(
|
|
|
|
ArcanistUnitTestResult::RESULT_PASS => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:green>** PASS **</bg>'),
|
2011-01-10 00:22:25 +01:00
|
|
|
ArcanistUnitTestResult::RESULT_FAIL => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:red>** FAIL **</bg>'),
|
2011-01-10 00:22:25 +01:00
|
|
|
ArcanistUnitTestResult::RESULT_SKIP => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:yellow>** SKIP **</bg>'),
|
2011-01-10 00:22:25 +01:00
|
|
|
ArcanistUnitTestResult::RESULT_BROKEN => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:red>** BROKEN **</bg>'),
|
2011-01-10 00:22:25 +01:00
|
|
|
ArcanistUnitTestResult::RESULT_UNSOUND => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:yellow>** UNSOUND **</bg>'),
|
2011-03-22 01:29:12 +01:00
|
|
|
ArcanistUnitTestResult::RESULT_POSTPONED => phutil_console_format(
|
2011-06-07 19:48:16 +02:00
|
|
|
'<bg:yellow>** POSTPONED **</bg>'),
|
2011-01-10 00:22:25 +01:00
|
|
|
);
|
|
|
|
|
2011-01-12 07:13:31 +01:00
|
|
|
$unresolved = array();
|
2012-01-31 21:07:19 +01:00
|
|
|
$coverage = array();
|
2011-04-28 20:46:20 +02:00
|
|
|
$postponed_count = 0;
|
2011-01-10 00:22:25 +01:00
|
|
|
foreach ($results as $result) {
|
|
|
|
$result_code = $result->getResult();
|
2011-04-28 20:46:20 +02:00
|
|
|
if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED) {
|
|
|
|
$postponed_count++;
|
2011-06-09 01:36:17 +02:00
|
|
|
$unresolved[] = $result;
|
2011-04-28 20:46:20 +02:00
|
|
|
} else {
|
2011-06-07 01:43:36 +02:00
|
|
|
if ($this->engine->shouldEchoTestResults()) {
|
2011-07-04 08:31:14 +02:00
|
|
|
echo ' '.$status_codes[$result_code];
|
|
|
|
if ($result_code == ArcanistUnitTestResult::RESULT_PASS) {
|
|
|
|
echo ' '.self::formatTestDuration($result->getDuration());
|
|
|
|
}
|
|
|
|
echo ' '.$result->getName()."\n";
|
2011-06-07 01:43:36 +02:00
|
|
|
}
|
2011-04-28 20:46:20 +02:00
|
|
|
if ($result_code != ArcanistUnitTestResult::RESULT_PASS) {
|
2011-06-07 01:43:36 +02:00
|
|
|
if ($this->engine->shouldEchoTestResults()) {
|
|
|
|
echo $result->getUserData()."\n";
|
|
|
|
}
|
2011-04-28 20:46:20 +02:00
|
|
|
$unresolved[] = $result;
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
2012-01-31 21:07:19 +01:00
|
|
|
if ($result->getCoverage()) {
|
|
|
|
foreach ($result->getCoverage() as $file => $report) {
|
|
|
|
$coverage[$file][] = $report;
|
|
|
|
}
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
2011-04-28 20:46:20 +02:00
|
|
|
if ($postponed_count) {
|
|
|
|
echo sprintf("%s %d %s\n",
|
|
|
|
$status_codes[ArcanistUnitTestResult::RESULT_POSTPONED],
|
|
|
|
$postponed_count,
|
|
|
|
($postponed_count > 1)?'tests':'test');
|
|
|
|
}
|
|
|
|
|
2012-01-31 21:07:19 +01:00
|
|
|
if ($coverage) {
|
2012-02-02 02:36:25 +01:00
|
|
|
$file_coverage = array_fill_keys(
|
|
|
|
$paths,
|
|
|
|
0);
|
2012-01-31 21:07:19 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
echo phutil_console_format('__COVERAGE REPORT__');
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
asort($file_coverage);
|
|
|
|
foreach ($file_coverage as $file => $coverage) {
|
|
|
|
echo phutil_console_format(
|
|
|
|
" **%s%%** %s\n",
|
|
|
|
sprintf('% 3d', (int)(100 * $coverage)),
|
|
|
|
$file);
|
|
|
|
|
2012-02-02 02:36:25 +01:00
|
|
|
$full_path = $working_copy->getProjectRoot().'/'.$file;
|
|
|
|
if ($this->getArgument('detailed-coverage') &&
|
|
|
|
Filesystem::pathExists($full_path) &&
|
|
|
|
is_file($full_path)) {
|
2012-01-31 21:07:19 +01:00
|
|
|
echo $this->renderDetailedCoverageReport(
|
2012-02-02 02:36:25 +01:00
|
|
|
Filesystem::readFile($full_path),
|
2012-01-31 21:07:19 +01:00
|
|
|
$file_reports[$file]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 07:13:31 +01:00
|
|
|
$this->unresolvedTests = $unresolved;
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
$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;
|
2011-01-12 07:13:31 +01:00
|
|
|
} else if ($result_code == ArcanistUnitTestResult::RESULT_UNSOUND) {
|
2011-01-10 00:22:25 +01:00
|
|
|
$overall_result = self::RESULT_UNSOUND;
|
2011-06-09 01:36:17 +02:00
|
|
|
} else if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED &&
|
|
|
|
$overall_result != self::RESULT_UNSOUND) {
|
|
|
|
$overall_result = self::RESULT_POSTPONED;
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $overall_result;
|
|
|
|
}
|
2011-01-12 07:13:31 +01:00
|
|
|
|
|
|
|
public function getUnresolvedTests() {
|
|
|
|
return $this->unresolvedTests;
|
|
|
|
}
|
|
|
|
|
2012-01-31 21:07:19 +01:00
|
|
|
public function getTestResults() {
|
|
|
|
return $this->testResults;
|
|
|
|
}
|
|
|
|
|
2011-05-28 01:15:43 +02:00
|
|
|
public function setDifferentialDiffID($id) {
|
|
|
|
if ($this->engine) {
|
|
|
|
$this->engine->setDifferentialDiffID($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 08:31:14 +02:00
|
|
|
private static function formatTestDuration($seconds) {
|
|
|
|
// Very carefully define inclusive upper bounds on acceptable unit test
|
|
|
|
// durations. Times are in milliseconds and are in increasing order.
|
|
|
|
$acceptableness = array(
|
|
|
|
50 => "<fg:green>%s</fg><fg:yellow>\xE2\x98\x85</fg> ",
|
|
|
|
200 => '<fg:green>%s</fg> ',
|
|
|
|
500 => '<fg:yellow>%s</fg> ',
|
|
|
|
INF => '<fg:red>%s</fg> ',
|
|
|
|
);
|
|
|
|
|
|
|
|
$milliseconds = $seconds * 1000;
|
|
|
|
$duration = self::formatTime($seconds);
|
|
|
|
foreach ($acceptableness as $upper_bound => $formatting) {
|
|
|
|
if ($milliseconds <= $upper_bound) {
|
|
|
|
return phutil_console_format($formatting, $duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return phutil_console_format(end($acceptableness), $duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function formatTime($seconds) {
|
|
|
|
if ($seconds >= 60) {
|
|
|
|
$minutes = floor($seconds / 60);
|
|
|
|
return sprintf('%dm%02ds', $minutes, round($seconds % 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($seconds >= 1) {
|
|
|
|
return sprintf('%4.1fs', $seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
$milliseconds = $seconds * 1000;
|
|
|
|
if ($milliseconds >= 1) {
|
|
|
|
return sprintf('%3dms', round($milliseconds));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ' <1ms';
|
|
|
|
}
|
2012-01-31 21:07:19 +01:00
|
|
|
|
2012-02-02 02:36:25 +01:00
|
|
|
private function renderDetailedCoverageReport($data, $report) {
|
2012-01-31 21:07:19 +01:00
|
|
|
$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;
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|