summaryrefslogtreecommitdiff
path: root/Source/UnitTests/run-unittests.py
blob: 29033943b2e2101bf0c39d7e06985cb626c6ee1a (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
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""
This is a simple script to invoke nunit-console.exe
on Windows or Linux on all UnitTest libraries.
"""
import argparse
import logging
import os
import subprocess
import sys

class ReturnCode:
  SUCCESS = 0
  CONFIG_ERROR = 1
  RUN_ERROR = 2


def main(args):
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument('BuildType', choices=['Release', 'Debug'],
                      help='Build type of unit test libraries')
  parser.add_argument('-l','--log-level',
                      choices=['critical', 'error', 'warning', 'info', 'debug'],
                      default='info',
                      dest='log_level',
                      help='Logging level. Default %(default)s')
  parser.add_argument('--nunit-console-path', default='',
                      help='Path to nunit-console tool. If not set the script will try to guess its location',
                      dest='nunit_console_path'
                     )
  parser.add_argument('-e', '--nunit-console-extra-args', action='append',
                      help='Pass extra arguments to nunit-console. This can be used multiple times',
                      default=[],
                      dest='nunit_console_extra_args'
                     )
  parser.add_argument('--no-progress',
                      action='store_true',
                      dest='no_progress',
                      help='Suppress nunit-console progress information')


  parsedArgs = parser.parse_args(args)
  logging.basicConfig(level=getattr(logging, parsedArgs.log_level.upper()))
  buildType=parsedArgs.BuildType

  logging.info('Build type is {}'.format(buildType))

  if parsedArgs.no_progress:
    parsedArgs.nunit_console_extra_args.append('-nodots')

  logging.info('Extra arguments to nunit-console "{}"'.format(parsedArgs.nunit_console_extra_args))

  UnitTestDirRoot = os.path.dirname(os.path.abspath(__file__))

  # Find nunit-console.exe
  if parsedArgs.nunit_console_path == "":
    # Try to guess where the tool is
    nunitConsolePath = os.path.join(os.path.join(UnitTestDirRoot,'..'),
                                    "packages",
                                    "NUnit.Runners.2.6.3",
                                    "tools",
                                    "nunit-console.exe"
                                   )
    # Mono needs the path to be absolute
    nunitConsolePath = os.path.abspath(nunitConsolePath)
  else:
    nunitConsolePath = parsedArgs.nunit_console_path

  logging.info('Looking for NUnit console at "{}"'.format(nunitConsolePath))
  if not os.path.exists(nunitConsolePath):
    logging.error('Could not find NUnit console tool')
    return ReturnCode.CONFIG_ERROR

  # Find unit test libraries
  unitTestLibraries = [ ]
  logging.info('Searching for Unit test libraries in "{}"'.format(UnitTestDirRoot))
  for (dirpath, dirnames, filenames) in os.walk(UnitTestDirRoot):

    # Search current directory for dll with correct suffix
    for filename in filenames:
      if filename.endswith('Tests.dll'):
        logging.debug('Found "{}" checking build type'.format(filename))

        pathComponents = dirpath.split(os.path.sep)
        fullPath = os.path.join(dirpath, filename)
        try:
          buildTypeIndex = pathComponents.index(buildType)
          if buildTypeIndex == 0 or pathComponents[buildTypeIndex -1] != 'bin':
            continue

          logging.info('Using "{}"'.format(fullPath))
          unitTestLibraries.append(fullPath)
        except ValueError:
          pass

  # Run Unit tests
  if len(unitTestLibraries) == 0:
    logging.error('No Unit test libraries were found')
    return ReturnCode.CONFIG_ERROR

  cmd = []

  if os.name == 'posix':
    cmd.append('mono')

  cmd.extend([nunitConsolePath, '-nologo'])

  if len(parsedArgs.nunit_console_extra_args) > 0:
    cmd.extend(parsedArgs.nunit_console_extra_args)

  cmd.extend(unitTestLibraries)
  logging.info('Running "{}"'.format(cmd))
  exitCode = subprocess.call(cmd)
  if exitCode != 0:
    logging.error('FAILED!')
    return ReturnCode.RUN_ERROR
  else:
    logging.info('Succeeded')

  return ReturnCode.SUCCESS


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