2011-01-10 00:22:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-31 21:07:05 +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
|
|
|
/**
|
|
|
|
* Very basic unit test engine which runs libphutil tests.
|
|
|
|
*
|
|
|
|
* @group unitrun
|
|
|
|
*/
|
2012-01-31 21:07:05 +01:00
|
|
|
final class PhutilUnitTestEngine extends ArcanistBaseUnitTestEngine {
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
public function run() {
|
|
|
|
|
2011-01-13 00:45:17 +01:00
|
|
|
$bootloader = PhutilBootloader::getInstance();
|
2012-06-06 06:08:52 +02:00
|
|
|
$project_root = $this->getWorkingCopy()->getProjectRoot();
|
2011-01-13 00:45:17 +01:00
|
|
|
|
2012-05-31 19:40:35 +02:00
|
|
|
$look_here = array();
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
foreach ($this->getPaths() as $path) {
|
|
|
|
$library_root = phutil_get_library_root_for_path($path);
|
|
|
|
if (!$library_root) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$library_name = phutil_get_library_name_for_root($library_root);
|
|
|
|
|
2012-03-05 19:03:13 +01:00
|
|
|
if (!$library_name) {
|
|
|
|
throw new Exception(
|
|
|
|
"Attempting to run unit tests on a libphutil library which has not ".
|
|
|
|
"been loaded, at:\n\n".
|
|
|
|
" {$library_root}\n\n".
|
|
|
|
"This probably means one of two things:\n\n".
|
|
|
|
" - You may need to add this library to .arcconfig.\n".
|
|
|
|
" - You may be running tests on a copy of libphutil or arcanist\n".
|
|
|
|
" using a different copy of libphutil or arcanist. This\n".
|
|
|
|
" operation is not supported.");
|
|
|
|
}
|
|
|
|
|
2012-06-06 06:08:52 +02:00
|
|
|
$path = Filesystem::resolvePath($path, $project_root);
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
$path = dirname($path);
|
|
|
|
}
|
|
|
|
|
2011-12-01 01:10:27 +01:00
|
|
|
if ($path == $library_root) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-26 01:13:04 +02:00
|
|
|
if (!Filesystem::isDescendant($path, $library_root)) {
|
|
|
|
// We have encountered some kind of symlink maze -- for instance, $path
|
|
|
|
// is some symlink living outside the library that links into some file
|
|
|
|
// inside the library. Just ignore these cases, since the affected file
|
|
|
|
// does not actually lie within the library.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
$library_path = Filesystem::readablePath($path, $library_root);
|
2011-11-18 20:27:55 +01:00
|
|
|
do {
|
2012-05-31 19:40:35 +02:00
|
|
|
$look_here[$library_name.':'.$library_path] = array(
|
|
|
|
'library' => $library_name,
|
|
|
|
'path' => $library_path,
|
2011-11-18 20:27:55 +01:00
|
|
|
);
|
|
|
|
$library_path = dirname($library_path);
|
|
|
|
} while ($library_path != '.');
|
|
|
|
}
|
|
|
|
|
2012-05-31 19:40:35 +02:00
|
|
|
// Look for any class that extends ArcanistPhutilTestCase inside a
|
|
|
|
// __tests__ directory in any parent directory of every affected file.
|
|
|
|
//
|
|
|
|
// The idea is that "infrastructure/__tests__/" tests defines general tests
|
|
|
|
// for all of "infrastructure/", and those tests run for any change in
|
|
|
|
// "infrastructure/". However, "infrastructure/concrete/rebar/__tests__/"
|
|
|
|
// defines more specific tests that run only when rebar/ (or some
|
|
|
|
// subdirectory) changes.
|
2011-01-10 00:22:25 +01:00
|
|
|
|
|
|
|
$run_tests = array();
|
2012-05-31 19:40:35 +02:00
|
|
|
foreach ($look_here as $path_info) {
|
|
|
|
$library = $path_info['library'];
|
|
|
|
$path = $path_info['path'];
|
|
|
|
|
2011-01-13 00:45:17 +01:00
|
|
|
$symbols = id(new PhutilSymbolLoader())
|
|
|
|
->setType('class')
|
2012-05-31 19:40:35 +02:00
|
|
|
->setLibrary($library)
|
|
|
|
->setPathPrefix($path.'/__tests__/')
|
2011-01-13 00:45:17 +01:00
|
|
|
->setAncestorClass('ArcanistPhutilTestCase')
|
2012-04-22 22:06:32 +02:00
|
|
|
->setConcreteOnly(true)
|
2011-01-13 00:45:17 +01:00
|
|
|
->selectAndLoadSymbols();
|
2012-05-31 19:40:35 +02:00
|
|
|
|
2011-01-13 00:45:17 +01:00
|
|
|
foreach ($symbols as $symbol) {
|
|
|
|
$run_tests[$symbol['name']] = true;
|
|
|
|
}
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
|
|
|
$run_tests = array_keys($run_tests);
|
|
|
|
|
|
|
|
if (!$run_tests) {
|
2012-05-31 19:40:35 +02:00
|
|
|
throw new ArcanistNoEffectException("No tests to run.");
|
2011-01-10 00:22:25 +01:00
|
|
|
}
|
|
|
|
|
2012-01-31 21:07:19 +01:00
|
|
|
$enable_coverage = $this->getEnableCoverage();
|
|
|
|
if ($enable_coverage !== false) {
|
|
|
|
if (!function_exists('xdebug_start_code_coverage')) {
|
|
|
|
if ($enable_coverage === true) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
"You specified --coverage but xdebug is not available, so ".
|
|
|
|
"coverage can not be enabled for PhutilUnitTestEngine.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$enable_coverage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
$results = array();
|
|
|
|
foreach ($run_tests as $test_class) {
|
|
|
|
$test_case = newv($test_class, array());
|
2012-01-31 21:07:19 +01:00
|
|
|
$test_case->setEnableCoverage($enable_coverage);
|
2012-06-06 06:08:52 +02:00
|
|
|
$test_case->setProjectRoot($project_root);
|
2012-01-31 21:07:19 +01:00
|
|
|
$test_case->setPaths($this->getPaths());
|
2011-01-10 00:22:25 +01:00
|
|
|
$results[] = $test_case->run();
|
|
|
|
}
|
2012-01-31 21:07:19 +01:00
|
|
|
|
|
|
|
|
2011-01-10 00:22:25 +01:00
|
|
|
if ($results) {
|
|
|
|
$results = call_user_func_array('array_merge', $results);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|