Created
September 11, 2018 21:30
-
-
Save felipecrv/c58c09e2f10fcda2c46fd3c13c5ffc3a to your computer and use it in GitHub Desktop.
sbt Test Engine for Arcanist/Phabricator #scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class SBTTestEngine extends ArcanistUnitTestEngine { | |
const SBT_ROOT = 'services'; | |
public function getEngineConfigurationName() { | |
return 'sbt'; | |
} | |
protected function supportsRunAllTests() { | |
return true; | |
} | |
private function getAllPackages() { | |
return array(''); | |
} | |
private function getPackagesForPaths() { | |
$root = $this | |
->getWorkingCopy() | |
->getProjectRoot(); | |
$library_root = $root.'/'.self::SBT_ROOT; | |
$src_main_scala = $library_root.'/src/main/scala'; | |
$src_test_scala = $library_root.'/src/test/scala'; | |
$packages = array(); | |
foreach ($this->getPaths() as $path) { | |
$path = Filesystem::resolvePath($path, $root); | |
if (!Filesystem::isDescendant($path, $library_root)) { | |
continue; | |
} | |
if (Filesystem::isDescendant($path, $src_main_scala)) { | |
$package_path = dirname(substr($path, strlen($src_main_scala) + 1)); | |
$package = str_replace(DIRECTORY_SEPARATOR, '.', $package_path); | |
$packages[] = $package; | |
continue; | |
} | |
if (Filesystem::isDescendant($path, $src_test_scala)) { | |
$package_path = dirname(substr($path, strlen($src_test_scala) + 1)); | |
$package = str_replace(DIRECTORY_SEPARATOR, '.', $package_path); | |
$packages[] = $package; | |
continue; | |
} | |
} | |
return $packages; | |
} | |
public function run() { | |
if ($this->getRunAllTests()) { | |
$packages = $this->getAllPackages(); | |
} else { | |
$packages = $this->getPackagesForPaths(); | |
} | |
if (!$packages) { | |
throw new ArcanistNoEffectException(pht('No tests to run.')); | |
} | |
$root = $this | |
->getWorkingCopy() | |
->getProjectRoot(); | |
$report_dirs[] = $root.'/'.self::SBT_ROOT.'/target/test-reports'; | |
$patterns = array(); | |
foreach ($packages as $package) { | |
if ($package === '') { | |
$patterns[] = '**'; | |
} else { | |
$patterns[] = $package.'.**'; | |
} | |
} | |
$sbt_command = 'testOnly '.implode($patterns, ' '); | |
echo $sbt_command; | |
$future = new ExecFuture( | |
'cd %s; sbt -Dorg.slf4j.simpleLogger.defaultLogLevel=error %s', | |
$root.'/'.self::SBT_ROOT, | |
$sbt_command); | |
do { | |
list($stdout, $stderr) = $future->read(); | |
echo $stdout; | |
fwrite(STDERR, $stderr); | |
sleep(0.5); | |
} while (!$future->isReady()); | |
list($error, $stdout, $stderr) = $future->resolve(); | |
$xml_parser = new ArcanistXUnitTestResultParser(); | |
$results = array(); | |
$working_copy = $this->getWorkingCopy(); | |
foreach ($report_dirs as $dir) { | |
foreach (Filesystem::listDirectory($dir) as $filename) { | |
if (!preg_match('|.*\.xml$|', $filename)) { | |
continue; | |
} | |
if (!$this->fromAnyPackage($filename, $packages)) { | |
continue; | |
} | |
$filepath = $dir.DIRECTORY_SEPARATOR.$filename; | |
$result = $xml_parser->parseTestResults(Filesystem::readFile($filepath)); | |
$results[] = $result; | |
} | |
} | |
return array_mergev($results); | |
} | |
private function fromAnyPackage($filename, array $packages) { | |
foreach ($packages as $p) { | |
if ($p === '') { | |
return true; | |
} | |
if (strpos($filename, $p) === 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment