~ubuntu-branches/ubuntu/wily/phabricator/wily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

/**
 * Go Test Result Parsing utility
 *
 * (To generate test output, run something like: `go test -v`)
 */
final class GoTestResultParser extends ArcanistTestResultParser {

  /**
   * Parse test results from Go test report
   * (e.g. `go test -v`)
   *
   * @param string $path Path to test
   * @param string $test_results String containing Go test output
   *
   * @return array
   */
  public function parseTestResults($path, $test_results) {
    $test_results = explode("\n", $test_results);

    $results = array();
    // We'll get our full test case name at the end and add it back in
    $test_case_name = '';

    // Temp store for test case results (in case we run multiple test cases)
    $test_case_results = array();
    foreach ($test_results as $i => $line) {

      if (strncmp($line, '--- PASS', 8) === 0) {
        // We have a passing test
        $meta = array();
        preg_match(
          '/^--- PASS: (?P<test_name>.+) \((?P<time>.+) seconds\).*/',
          $line,
          $meta);

        $result = new ArcanistUnitTestResult();
        // For now set name without test case, we'll add it later
        $result->setName($meta['test_name']);
        $result->setResult(ArcanistUnitTestResult::RESULT_PASS);
        $result->setDuration($meta['time']);

        $test_case_results[] = $result;

        continue;
      }

      if (strncmp($line, '--- FAIL', 8) === 0) {
        // We have a failing test
        $reason = trim($test_results[$i + 1]);
        $meta = array();
        preg_match(
          '/^--- FAIL: (?P<test_name>.+) \((?P<time>.+) seconds\).*/',
          $line,
          $meta);

        $result = new ArcanistUnitTestResult();
        $result->setName($meta['test_name']);
        $result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
        $result->setDuration($meta['time']);
        $result->setUserData($reason."\n");

        $test_case_results[] = $result;

        continue;
      }

      if (strncmp($line, 'ok', 2) === 0) {
        $meta = array();
        preg_match(
          '/^ok[\s\t]+(?P<test_name>\w.*)[\s\t]+(?P<time>.*)s.*/',
          $line,
          $meta);

        $test_case_name = str_replace('/', '::', $meta['test_name']);

        // Our test case passed
        // check to make sure we were in verbose (-v) mode
        if (empty($test_case_results)) {
          // We weren't in verbose mode
          // create one successful result for the whole test case
          $test_name = 'Go::TestCase::'.$test_case_name;

          $result = new ArcanistUnitTestResult();
          $result->setName($test_name);
          $result->setResult(ArcanistUnitTestResult::RESULT_PASS);
          $result->setDuration($meta['time']);

          $results[] = $result;
        } else {
          $test_case_results = $this->fixNames(
            $test_case_results,
            $test_case_name);
          $results = array_merge($results, $test_case_results);
          $test_case_results = array();
        }

        continue;
      }

      if (strncmp($line, "FAIL\t", 5) === 0) {
        $meta = array();
        preg_match(
          '/^FAIL[\s\t]+(?P<test_name>\w.*)[\s\t]+.*/',
          $line,
          $meta);

        $test_case_name = str_replace('/', '::', $meta['test_name']);

        $test_case_results = $this->fixNames(
          $test_case_results,
          $test_case_name);
        $results = array_merge($results, $test_case_results);
        $test_case_results = array();

        continue;
      }
    }

    return $results;
  }

  private function fixNames($test_case_results, $test_case_name) {

    foreach ($test_case_results as &$result) {
      $test_name = $result->getName();
      $result->setName('Go::Test::'.$test_case_name.'::'.$test_name);
    }

    return $test_case_results;
  }

}