cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
wi_tests_manage.py
Go to the documentation of this file.
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3 # @COPYRIGHT_begin
4 #
5 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @COPYRIGHT_end
20 
21 ##
22 # @package src.wi.tests_manage
23 #
24 # Control Interface for running CC1 Project's tests
25 #
26 # Usage:
27 # manage.py runtest wi [--testserver=HOSTNAME] [--testfiles=TEST_FILES]
28 # manage.py --help
29 #
30 # Options:
31 # --testserver=HOSTNAME test server hostname ([host]:[port]). If not set local develompment server is started.
32 # --testfiles=TEST_FILES which tests to run. Only test files that match the pattern will be loaded. (Using shell style pattern matching.)
33 #
34 # @author Piotr Wójcik
35 # @author Krzysztof Danielowski
36 # @date 25.10.2013
37 #
38 
39 import datetime
40 import docopt
41 import os
42 import sys
43 
44 
45 BASEDIR = os.path.dirname(os.path.abspath(__file__))
46 sys.path.append(BASEDIR)
47 
48 
49 ##
50 # Manager that stores implementation of commands as Python functions.
51 class Commands(object):
52 
53  COMMANDS = {}
54 
55  @classmethod
56  def get_command(self, cmds):
57  func = self.COMMANDS.get(tuple(sorted(list(cmds))))
58  if not func:
59  raise Exception("Unknown command '%s'" % cmds)
60  return func
61 
62  @classmethod
63  def set_command(self, func, *cmds):
64  self.COMMANDS[tuple(sorted(list(cmds)))] = func
65 
66 
67 def runtest_wi(args):
68  if args['--testserver'] is None:
69  from django.core.management import execute_from_command_line
70  from threading import Thread
71  import time
72 
73  # load settings
74  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wi.settings")
75  from django.conf import settings
76 
77  # edit wi settings to disable CAPTCHA
78  settings.CAPTCHA = False
79 
80  # run local webserver
81  server = Thread(target=execute_from_command_line, args=([[__file__, 'runserver', '--noreload']]))
82  server.start()
83  time.sleep(1)
84 
85  else:
86  # set test server host
87  from wi import tests
88  tests.TEST_SERVER = args['--testserver']
89 
90  # load tests
91  from unittest import TestLoader, TextTestRunner
92  if args['--testfiles'] is None:
93  print('Test file not specified.')
94  test_files = '*test.py'
95  else:
96  test_files = args['--testfiles']
97 
98  print('Running tests: ' + test_files)
99  tests = TestLoader().discover(os.path.join(BASEDIR, 'wi', 'tests'), test_files)
100 
101  # run and save result to file
102  with open(os.path.join(os.path.dirname(__file__), 'wi', 'tests.log').replace('\\', '/'), 'w') as f:
103  f.write('Test run ' + str(datetime.datetime.now()) + '\n\n')
104  testResults = TextTestRunner(stream=f, verbosity=2).run(tests)
105  print('Tests finished. Errors: ' + str(len(testResults.errors) + len(testResults.failures)) + '.')
106 
107 Commands.set_command(runtest_wi, 'runtest', 'wi')
108 
109 
110 if __name__ == '__main__':
111  options = docopt.docopt(__doc__, help=True)
112  command, arguments = [], {}
113  for arg, val in options.iteritems():
114  if arg[0] == '-' or arg[0].isupper():
115  arguments[arg] = val
116  elif val:
117  command.append(arg)
118  func = Commands.get_command(command)
119  func(arguments)
120  exit(0)
121