File: TestRunner.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (310 lines) | stat: -rwxr-xr-x 9,353 bytes parent folder | download | duplicates (2)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#
#  Copyright (c) 2003-2010 Greg Landrum and Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#

import os
import sys
import time

from rdkit import RDConfig

try:
  import subprocess
except ImportError:
  subprocess = None

TEST_FAILED = -1
TEST_PASSED = 0

BUILD_TYPE_ENVVAR = 'RDKIT_BUILD_TYPE'


def isDebugBuild():
  try:
    return os.environ[BUILD_TYPE_ENVVAR] == 'DEBUG'
  except KeyError:
    return False


def RunTest(exeName, args, extras):
  if exeName == "python":
    exeName = RDConfig.pythonExe
  args = args.split(' ')

  startDir = os.getcwd()
  if 'dir' in extras:
    os.chdir(extras['dir'])
  expectedReturn = extras.get('returns', 0)
  if not subprocess:
    raise NotImplementedError('cannot run tests if the subprocess module is not available.')
  else:
    try:
      retVal = subprocess.call([exeName] + list(args))
    except OSError:
      print("Could not find executable: %s." % exeName, file=sys.stderr)
      return TEST_FAILED
  if 'dir' in extras:
    os.chdir(startDir)
  if retVal != expectedReturn:
    return TEST_FAILED
  else:
    return TEST_PASSED


def RunScript(script, doLongTests, verbose):
  # support python 2.7 style -f argument for failfast
  if sys.argv[-1] == '-f':
    # setting environment allows this setting to recursively pass to all child
    # processes
    os.environ['PYTHON_TEST_FAILFAST'] = '1'
  if len(sys.argv) >= 3 and sys.argv[1] == '--testDir':
    os.chdir(sys.argv[2])
  if len(sys.argv) >= 5 and sys.argv[3] == '--buildType':
    os.environ[BUILD_TYPE_ENVVAR] = sys.argv[4].upper()
  # -------------------------------------------------------
  # this is pretty funny.  Whatever directory we started python in
  # will be in the search path, and if we've changed to another
  # directory, that'll be there too.  HOWEVER... the starting
  # directory will be searched first (at least in python2.2), so we
  # need to make sure that '.' is at the front of the search path
  if sys.path[0] != '.':
    sys.path = ['.'] + sys.path
  script = script.split('.py')[0]
  mod = __import__(script)
  try:
    tests = mod.tests
  except AttributeError:
    return [], 0

  longTests = []
  if doLongTests:
    try:
      longTests = mod.longTests
    except AttributeError:
      pass

  failed = []
  for i, entry in enumerate(tests):
    try:
      exeName, args, extras = entry
    except ValueError:
      print('bad entry:', entry)
      sys.exit(-1)
    try:
      res = RunTest(exeName, args, extras)
    except Exception:
      import traceback
      traceback.print_exc()
      res = TEST_FAILED
    if res != TEST_PASSED:
      failed.append((exeName, args, extras))
      # check failfast setting
      if os.environ.get('PYTHON_TEST_FAILFAST', '') == '1':
        # return immediately
        sys.stderr.write("Exiting from %s\n" % str([exeName] + list(args)))
        return failed, i + 1
  for i, (exeName, args, extras) in enumerate(longTests):
    res = RunTest(exeName, args, extras)
    if res != TEST_PASSED:
      failed.append((exeName, args, extras))
      if os.environ.get('PYTHON_TEST_FAILFAST', '') == '1':
        # return immediately
        sys.stderr.write("Exitng from %s\n" % str([exeName] + list(args)))
        return failed, len(tests) + i + 1

  nTests = len(tests) + len(longTests)
  del sys.modules[script]
  if verbose and failed:
    for exeName, args, extras in failed:
      print("!!! TEST FAILURE: ", exeName, args, extras, file=sys.stderr)
  return failed, nTests


def ReportResults(script, failedTests, nTests, runTime, verbose, dest):
  if not nTests:
    dest.write('!-!-!-!-!-!-!-!-!-!-!\n')
    dest.write('\tScript: %s.  No tests run!\n' % (script))
  elif not len(failedTests):
    dest.write('-----------------\n')
    dest.write('\tScript: %s.  Passed %d tests in %.2f seconds\n' % (script, nTests, runTime))
  else:
    dest.write('!-!-!-!-!-!-!-!-!-!-!\n')
    dest.write('\tScript: %s.  Failed %d (of %d) tests in %.2f seconds\n' %
               (script, len(failedTests), nTests, runTime))
    if verbose:
      for exeName, args, extras in failedTests:
        dirName = extras.get('dir', '.')
        dirName = os.path.abspath(dirName)
        dest.write('\t\t(%s): %s %s\n' % (dirName, exeName, args))


if __name__ == '__main__':
  import getopt
  args, extras = getopt.getopt(sys.argv[1:], 'lv')
  doLongTests = 0
  verbose = 1
  for arg, val in args:
    if arg == '-l':
      doLongTests = 1
    elif arg == '-v':
      verbose = 0

  pwd = os.getcwd()
  totNumFailed = 0
  totNumRun = 0
  failures = []
  timeAccum = 0.0
  for script in extras:
    try:
      open(script, 'r')
    except IOError:
      sys.stderr.write('ERROR: Test script %s could not be opened.\n' % (script))
    else:
      dirName = os.path.dirname(script)
      scriptBase = os.path.basename(script)
      if dirName:
        os.chdir(dirName)
      try:
        t1 = time.time()
        failed, nTests = RunScript(scriptBase, doLongTests, verbose)
        t2 = time.time()
      except ImportError:
        import traceback
        traceback.print_exc()
        sys.stderr.write('ERROR: Could not import test script %s\n' % (script))
      else:
        runTime = t2 - t1
        ReportResults(script, failed, nTests, runTime, verbose, sys.stderr)
        timeAccum += runTime
      if dirName:
        os.chdir(pwd)
      if len(extras) > 1:
        totNumFailed += len(failed)
        totNumRun += nTests
        if len(failed):
          failures.append(script)

  if totNumRun > 1:
    sys.stderr.write('\n\n-*-*-*-*-*-*- Test Results Summary -*-*-*-*-*-*-\n')
    sys.stderr.write('\t\tTotal run time: %.2f seconds\n' % (timeAccum))
    if totNumFailed:
      sys.stderr.write('!!!!!---  %d Failures in %d tests  ---!!!!!\n' % (totNumFailed, totNumRun))
      sys.stderr.write('\tModules with failures:\n')
      for failure in failures:
        sys.stderr.write('\t\t%s\n' % failure)
    else:
      sys.stderr.write('  All %d tests (in %d modules) passed\n' % (totNumRun, len(extras)))
  sys.exit(totNumFailed)


class _RedirectStream:
  _stream = None

  def __init__(self, new_target):
    self._new_target = new_target
    # We use a list of old targets to make this CM re-entrant
    self._old_targets = []

  def __enter__(self):
    self._old_targets.append(getattr(sys, self._stream))
    setattr(sys, self._stream, self._new_target)
    return self._new_target

  def __exit__(self, exctype, excinst, exctb):
    setattr(sys, self._stream, self._old_targets.pop())


class redirect_stdout(_RedirectStream):
  """Context manager for temporarily redirecting stdout to another file.

        # How to send help() to stderr
        with redirect_stdout(sys.stderr):
            help(dir)

        # How to write help() to a file
        with open('help.txt', 'w') as f:
            with redirect_stdout(f):
                help(pow)
    """
  _stream = "stdout"


class redirect_stderr(_RedirectStream):
  """Context manager for temporarily redirecting stderr to another file."""
  _stream = "stderr"


class OutputRedirectC:
  """Context manager which uses low-level file descriptors to suppress
  output to stdout/stderr, optionally redirecting to the named file(s).

  Suppress all output
  with Silence():
    <code>

  Redirect stdout to file
  with OutputRedirectC(stdout='output.txt', mode='w'):
    <code>

  Redirect stderr to file
  with OutputRedirectC(stderr='output.txt', mode='a'):
    <code>
  http://code.activestate.com/recipes/577564-context-manager-for-low-level-redirection-of-stdou/
  >>>

  """

  def __init__(self, stdout=os.devnull, stderr=os.devnull, mode='wb'):
    self.outfiles = stdout, stderr
    self.combine = (stdout == stderr)
    self.mode = mode
    self.saved_streams = None
    self.fds = None
    self.saved_fds = None
    self.null_fds = None
    self.null_streams = None

  def __enter__(self):
    # save previous stdout/stderr
    self.saved_streams = saved_streams = sys.__stdout__, sys.__stderr__
    self.fds = fds = [s.fileno() for s in saved_streams]
    self.saved_fds = [os.dup(fd) for fd in fds]
    # flush any pending output
    for s in saved_streams:
      s.flush()

    # open surrogate files
    if self.combine:
      null_streams = [open(self.outfiles[0], self.mode, 0)] * 2
      if self.outfiles[0] != os.devnull:
        # disable buffering so output is merged immediately
        sys.stdout, sys.stderr = [os.fdopen(fd, 'wb', 0) for fd in fds]
    else:
      null_streams = [open(f, self.mode, 0) for f in self.outfiles]
    self.null_fds = null_fds = [s.fileno() for s in null_streams]
    self.null_streams = null_streams

    # overwrite file objects and low-level file descriptors
    for null_fd, fd in zip(null_fds, fds):
      os.dup2(null_fd, fd)

  def __exit__(self, *args):
    # flush any pending output
    for s in self.saved_streams:
      s.flush()
    # restore original streams and file descriptors
    for saved_fd, fd in zip(self.saved_fds, self.fds):
      os.dup2(saved_fd, fd)
    sys.stdout, sys.stderr = self.saved_streams
    # clean up
    for s in self.null_streams:
      s.close()
    for fd in self.saved_fds:
      os.close(fd)
    return False