Skip to content

Commit

Permalink
Fixed unittests.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoracqio committed Sep 30, 2020
1 parent 34a4c96 commit 79352cc
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions tools/arc/unit/engine/BazelTestEngine.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
// Heavily inspired by:
// https://github.com/google/kythe/blob/master/tools/arc/unit/engine/BazelTestEngine.php
// With proper support for event file.
final class BazelTestEngine extends ArcanistUnitTestEngine {
private static $omit_tags = ["manual", "broken", "arc-ignore", "docker"];
private $debug;
Expand Down Expand Up @@ -49,14 +50,16 @@ private function getTargets() {
// Quote each file to make it safe in case it has special characters in it.
$files = array_map(function($s) { return '"'.$s.'"'; }, $files);
$files = join($files, " ");
$this->debugPrint("files: " . $files);

$cmd = $this->bazelCommand("query", ["%s"]);
$cmd = $this->bazelCommand("query", ["-k", "%s"]);
$tag_filter = join("|", self::$omit_tags);
$query = 'rdeps(//..., set('.$files.')) except attr(tags, "'.$tag_filter.'", //...)';
$this->debugPrint($query);
$query = 'rdeps(//..., set('.$files.')) intersect tests(//...) except attr(tags, "'.$tag_filter.'", //...)';

$future = new ExecFuture($cmd, $query);
$this->debugPrint($future->getCommand());
$future->setCWD($this->project_root);

print("Querying affected files...\n");
$status = $future->resolve();
if ($status[0] != 3 && $status[0] != 0) {
throw new Exception("Bazel query error (".$status[0]."): ".$status[2]);
Expand All @@ -71,14 +74,19 @@ private function getTargets() {
}

private function getFileTargets() {
$this->debugPrint("getFileTargets()");

if (empty($this->getPaths())) {
return array();
}
$files = join(
" ", array_map(array('BazelTestEngine', 'fileToTarget'), $this->getPaths()));

$future = new ExecFuture($this->bazelCommand("query", ["-k", "%s"]), 'set('.$files.')');
$this->debugPrint($future->getCommand());
$future->setCWD($this->project_root);

print("Discovering affected test targets...\n");
$status = $future->resolve();
if ($status[0] != 3 && $status[0] != 0) {
throw new Exception("Bazel query error (".$status[0]."): ".$status[2]);
Expand All @@ -96,11 +104,11 @@ private static function fileToTarget($file) {
if (dirname($file) == ".") {
return '//:' . $file;
}
return "'" . $file . "'";
return "\"" . $file . "\"";
}

private function runTests($targets) {
$this->debugPrint("runTests(" . join($targets, ", ") . ")");
$this->debugPrint("runTests()");

if (!file_exists($this->testlogs_root)) {
mkdir($this->testlogs_root, 0777, true);
Expand All @@ -121,14 +129,25 @@ private function runTests($targets) {
"--build_event_json_file=$events_file",
""],
$targets)));
$this->debugPrint($future->getCommand());
$future->setCWD($this->project_root);

$future->start();
do {
$done = $future->resolve();
usleep(200*1000);
list($stdout, $stderr) = $future->read();
print($stderr);
$future->discardBuffers();
} while ($done === null);
} while (!$future->isReady());

$status = $future->resolve();
$code = $status[0];
if ($code == 4) {
print("No tests affected...\n");
return [];
} else if ($code == 1) {
throw new Exception($output . "\n" . $status[2]);
}

return $this->parseEventFile($targets, $events_file);
}
Expand Down Expand Up @@ -207,7 +226,8 @@ private function parseResultEvent($targets, $obj) {
}

private function bazelInfo($name) {
$future = new ExecFuture($this->bazelCommand("info", [$name]));
$future = new ExecFuture($this->bazelCommand("info", ["--fetch", $name]));
$this->debugPrint($future->getCommand());
$future->setCWD($this->project_root);
$future->resolve();
list($stdout) = $future->read();
Expand All @@ -224,7 +244,6 @@ private function bazelCommand($subcommand, $args) {
}
$cmd = $cmd . $subcommand . " --tool_tag=arcanist ";
$cmd = $cmd . join(" ", $args);
$this->debugPrint($cmd);
return $cmd;
}

Expand Down

0 comments on commit 79352cc

Please sign in to comment.