public function run()

in src/workflow/ArcanistUnitWorkflow.php [130:282]


  public function run() {
    $working_copy = $this->getWorkingCopy();

    $paths = $this->getArgument('paths');
    $rev = $this->getArgument('rev');
    $everything = $this->getArgument('everything');
    if ($everything && $paths) {
      throw new ArcanistUsageException(
        pht(
          'You can not specify paths with %s. The %s flag runs every test '.
          'associated with a tracked file in the working copy.',
          '--everything',
          '--everything'));
    }

    if ($everything) {
      $paths = iterator_to_array($this->getRepositoryAPI()->getAllFiles());
    } else {
      $paths = $this->selectPathsForWorkflow($paths, $rev);
    }

    $this->engine = $this->newUnitTestEngine($this->getArgument('engine'));
    if ($everything) {
      $this->engine->setRunAllTests(true);
    } else {
      $this->engine->setPaths($paths);
    }

    $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);

    $results = $this->engine->run();

    $this->validateUnitEngineResults($this->engine, $results);

    $this->testResults = $results;

    $console = PhutilConsole::getConsole();

    $output_format = $this->getOutputFormat();

    if ($output_format !== 'full') {
      $console->disableOut();
    }

    $unresolved = array();
    $coverage = array();
    foreach ($results as $result) {
      $result_code = $result->getResult();
      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 ($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__%s__\n", pht('COVERAGE REPORT'));

      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;
      }
    }

    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;
    }


    $target_phid = $this->getArgument('target');
    if ($target_phid) {
      $this->uploadTestResults($target_phid, $overall_result, $results);
    }

    return $overall_result;
  }