~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to arcanist/src/unit/engine/GoTestResultParser.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Go Test Result Parsing utility
 
5
 *
 
6
 * (To generate test output, run something like: `go test -v`)
 
7
 */
 
8
final class GoTestResultParser extends ArcanistTestResultParser {
 
9
 
 
10
  /**
 
11
   * Parse test results from Go test report
 
12
   * (e.g. `go test -v`)
 
13
   *
 
14
   * @param string $path Path to test
 
15
   * @param string $test_results String containing Go test output
 
16
   *
 
17
   * @return array
 
18
   */
 
19
  public function parseTestResults($path, $test_results) {
 
20
    $test_results = explode("\n", $test_results);
 
21
 
 
22
    $results = array();
 
23
    // We'll get our full test case name at the end and add it back in
 
24
    $test_case_name = '';
 
25
 
 
26
    // Temp store for test case results (in case we run multiple test cases)
 
27
    $test_case_results = array();
 
28
    foreach ($test_results as $i => $line) {
 
29
 
 
30
      if (strncmp($line, '--- PASS', 8) === 0) {
 
31
        // We have a passing test
 
32
        $meta = array();
 
33
        preg_match(
 
34
          '/^--- PASS: (?P<test_name>.+) \((?P<time>.+) seconds\).*/',
 
35
          $line,
 
36
          $meta);
 
37
 
 
38
        $result = new ArcanistUnitTestResult();
 
39
        // For now set name without test case, we'll add it later
 
40
        $result->setName($meta['test_name']);
 
41
        $result->setResult(ArcanistUnitTestResult::RESULT_PASS);
 
42
        $result->setDuration($meta['time']);
 
43
 
 
44
        $test_case_results[] = $result;
 
45
 
 
46
        continue;
 
47
      }
 
48
 
 
49
      if (strncmp($line, '--- FAIL', 8) === 0) {
 
50
        // We have a failing test
 
51
        $reason = trim($test_results[$i + 1]);
 
52
        $meta = array();
 
53
        preg_match(
 
54
          '/^--- FAIL: (?P<test_name>.+) \((?P<time>.+) seconds\).*/',
 
55
          $line,
 
56
          $meta);
 
57
 
 
58
        $result = new ArcanistUnitTestResult();
 
59
        $result->setName($meta['test_name']);
 
60
        $result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
 
61
        $result->setDuration($meta['time']);
 
62
        $result->setUserData($reason."\n");
 
63
 
 
64
        $test_case_results[] = $result;
 
65
 
 
66
        continue;
 
67
      }
 
68
 
 
69
      if (strncmp($line, 'ok', 2) === 0) {
 
70
        $meta = array();
 
71
        preg_match(
 
72
          '/^ok[\s\t]+(?P<test_name>\w.*)[\s\t]+(?P<time>.*)s.*/',
 
73
          $line,
 
74
          $meta);
 
75
 
 
76
        $test_case_name = str_replace('/', '::', $meta['test_name']);
 
77
 
 
78
        // Our test case passed
 
79
        // check to make sure we were in verbose (-v) mode
 
80
        if (empty($test_case_results)) {
 
81
          // We weren't in verbose mode
 
82
          // create one successful result for the whole test case
 
83
          $test_name = 'Go::TestCase::'.$test_case_name;
 
84
 
 
85
          $result = new ArcanistUnitTestResult();
 
86
          $result->setName($test_name);
 
87
          $result->setResult(ArcanistUnitTestResult::RESULT_PASS);
 
88
          $result->setDuration($meta['time']);
 
89
 
 
90
          $results[] = $result;
 
91
        } else {
 
92
          $test_case_results = $this->fixNames(
 
93
            $test_case_results,
 
94
            $test_case_name);
 
95
          $results = array_merge($results, $test_case_results);
 
96
          $test_case_results = array();
 
97
        }
 
98
 
 
99
        continue;
 
100
      }
 
101
 
 
102
      if (strncmp($line, "FAIL\t", 5) === 0) {
 
103
        $meta = array();
 
104
        preg_match(
 
105
          '/^FAIL[\s\t]+(?P<test_name>\w.*)[\s\t]+.*/',
 
106
          $line,
 
107
          $meta);
 
108
 
 
109
        $test_case_name = str_replace('/', '::', $meta['test_name']);
 
110
 
 
111
        $test_case_results = $this->fixNames(
 
112
          $test_case_results,
 
113
          $test_case_name);
 
114
        $results = array_merge($results, $test_case_results);
 
115
        $test_case_results = array();
 
116
 
 
117
        continue;
 
118
      }
 
119
    }
 
120
 
 
121
    return $results;
 
122
  }
 
123
 
 
124
  private function fixNames($test_case_results, $test_case_name) {
 
125
 
 
126
    foreach ($test_case_results as &$result) {
 
127
      $test_name = $result->getName();
 
128
      $result->setName('Go::Test::'.$test_case_name.'::'.$test_name);
 
129
    }
 
130
 
 
131
    return $test_case_results;
 
132
  }
 
133
 
 
134
}