summaryrefslogtreecommitdiffstats
path: root/mojo/tools/test_runner.py
blob: b0bb4d9f94b2801ff0642d22bb10e00d4ca967e1 (plain)
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
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""A "smart" test runner for gtest unit tests (that caches successes)."""

import logging
import os
import subprocess
import sys

_logging = logging.getLogger()

_script_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(_script_dir, "pylib"))

from transitive_hash import transitive_hash

def main(argv):
  logging.basicConfig()
  # Uncomment to debug:
  # _logging.setLevel(logging.DEBUG)

  if len(argv) < 3 or len(argv) > 4:
    print "Usage: %s gtest_list_file root_dir [successes_cache_file]" % \
        os.path.basename(argv[0])
    return 0 if len(argv) < 2 else 1

  _logging.debug("Test list file: %s", argv[1])
  with open(argv[1], 'rb') as f:
    gtest_list = [y for y in [x.strip() for x in f.readlines()] \
                      if y and y[0] != '#']
  _logging.debug("Test list: %s" % gtest_list)

  print "Running tests in directory: %s" % argv[2]
  os.chdir(argv[2])

  if len(argv) == 4 and argv[3]:
    successes_cache_filename = argv[3]
    print "Successes cache file: %s" % successes_cache_filename
  else:
    successes_cache_filename = None
    print "No successes cache file (will run all tests unconditionally)"

  if successes_cache_filename:
    # This file simply contains a list of transitive hashes of tests that
    # succeeded.
    try:
      _logging.debug("Trying to read successes cache file: %s",
                     successes_cache_filename)
      with open(argv[3], 'rb') as f:
        successes = set([x.strip() for x in f.readlines()])
      _logging.debug("Successes: %s", successes)
    except:
      # Just assume that it didn't exist, or whatever.
      print "Failed to read successes cache file %s (will create)" % argv[3]
      successes = set()

  # Run gtests with color if we're on a TTY (and we're not being told explicitly
  # what to do).
  if sys.stdout.isatty() and 'GTEST_COLOR' not in os.environ:
    _logging.debug("Setting GTEST_COLOR=yes")
    os.environ['GTEST_COLOR'] = 'yes'

  # TODO(vtl): We may not close this file on failure.
  successes_cache_file = open(successes_cache_filename, 'ab') \
      if successes_cache_filename else None
  for gtest in gtest_list:
    if gtest[0] == '*':
      gtest = gtest[1:]
      _logging.debug("%s is marked as non-cacheable" % gtest)
      cacheable = False
    else:
      cacheable = True

    if successes_cache_file and cacheable:
      _logging.debug("Getting transitive hash for %s ... " % gtest)
      try:
        gtest_hash = transitive_hash(gtest)
      except:
        print "Failed to get transitive hash for %s" % gtest
        return 1
      _logging.debug("  Transitive hash: %s" % gtest_hash)

      if gtest_hash in successes:
        print "Skipping %s (previously succeeded)" % gtest
        continue

    print "Running %s...." % gtest,
    sys.stdout.flush()
    try:
      subprocess.check_output(["./" + gtest], stderr=subprocess.STDOUT)
      print "Succeeded"
      # Record success.
      if successes_cache_filename and cacheable:
        successes.add(gtest_hash)
        successes_cache_file.write(gtest_hash + '\n')
        successes_cache_file.flush()
    except subprocess.CalledProcessError as e:
      print "Failed with exit code %d and output:" % e.returncode
      print 72 * '-'
      print e.output
      print 72 * '-'
      return 1
    except OSError as e:
      print "  Failed to start test"
      return 1
  print "All tests succeeded"
  if successes_cache_file:
    successes_cache_file.close()

  return 0

if __name__ == '__main__':
  sys.exit(main(sys.argv))