Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / ryu / .eggs / pbr-5.3.1-py2.7.egg / pbr / testr_command.py
1 # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Copyright (c) 2013 Testrepository Contributors
17 #
18 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
19 # license at the users choice. A copy of both licenses are available in the
20 # project source as Apache-2.0 and BSD. You may not use this file except in
21 # compliance with one of these two licences.
22 #
23 # Unless required by applicable law or agreed to in writing, software
24 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
25 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
26 # license you chose for the specific language governing permissions and
27 # limitations under that license.
28
29 """setuptools/distutils command to run testr via setup.py
30
31 PBR will hook in the Testr class to provide "setup.py test" when
32 .testr.conf is present in the repository (see pbr/hooks/commands.py).
33
34 If we are activated but testrepository is not installed, we provide a
35 sensible error.
36
37 You can pass --coverage which will also export PYTHON='coverage run
38 --source <your package>' and automatically combine the coverage from
39 each testr backend test runner after the run completes.
40
41 """
42
43 from distutils import cmd
44 import distutils.errors
45 import logging
46 import os
47 import sys
48 import warnings
49
50 logger = logging.getLogger(__name__)
51
52
53 class TestrReal(cmd.Command):
54
55     description = "DEPRECATED: Run unit tests using testr"
56
57     user_options = [
58         ('coverage', None, "Replace PYTHON with coverage and merge coverage "
59          "from each testr worker."),
60         ('testr-args=', 't', "Run 'testr' with these args"),
61         ('omit=', 'o', "Files to omit from coverage calculations"),
62         ('coverage-package-name=', None, "Use this name to select packages "
63                                          "for coverage (one or more, "
64                                          "comma-separated)"),
65         ('slowest', None, "Show slowest test times after tests complete."),
66         ('no-parallel', None, "Run testr serially"),
67         ('log-level=', 'l', "Log level (default: info)"),
68     ]
69
70     boolean_options = ['coverage', 'slowest', 'no_parallel']
71
72     def _run_testr(self, *args):
73         logger.debug("_run_testr called with args = %r", args)
74         return commands.run_argv([sys.argv[0]] + list(args),
75                                  sys.stdin, sys.stdout, sys.stderr)
76
77     def initialize_options(self):
78         self.testr_args = None
79         self.coverage = None
80         self.omit = ""
81         self.slowest = None
82         self.coverage_package_name = None
83         self.no_parallel = None
84         self.log_level = 'info'
85
86     def finalize_options(self):
87         self.log_level = getattr(
88             logging,
89             self.log_level.upper(),
90             logging.INFO)
91         logging.basicConfig(level=self.log_level)
92         logger.debug("finalize_options called")
93         if self.testr_args is None:
94             self.testr_args = []
95         else:
96             self.testr_args = self.testr_args.split()
97         if self.omit:
98             self.omit = "--omit=%s" % self.omit
99         logger.debug("finalize_options: self.__dict__ = %r", self.__dict__)
100
101     def run(self):
102         """Set up testr repo, then run testr."""
103         logger.debug("run called")
104
105         warnings.warn('testr integration in pbr is deprecated. Please use '
106                       'the \'testr\' setup command or call testr directly',
107                       DeprecationWarning)
108
109         if not os.path.isdir(".testrepository"):
110             self._run_testr("init")
111
112         if self.coverage:
113             self._coverage_before()
114         if not self.no_parallel:
115             testr_ret = self._run_testr("run", "--parallel", *self.testr_args)
116         else:
117             testr_ret = self._run_testr("run", *self.testr_args)
118         if testr_ret:
119             raise distutils.errors.DistutilsError(
120                 "testr failed (%d)" % testr_ret)
121         if self.slowest:
122             print("Slowest Tests")
123             self._run_testr("slowest")
124         if self.coverage:
125             self._coverage_after()
126
127     def _coverage_before(self):
128         logger.debug("_coverage_before called")
129         package = self.distribution.get_name()
130         if package.startswith('python-'):
131             package = package[7:]
132
133         # Use this as coverage package name
134         if self.coverage_package_name:
135             package = self.coverage_package_name
136         options = "--source %s --parallel-mode" % package
137         os.environ['PYTHON'] = ("coverage run %s" % options)
138         logger.debug("os.environ['PYTHON'] = %r", os.environ['PYTHON'])
139
140     def _coverage_after(self):
141         logger.debug("_coverage_after called")
142         os.system("coverage combine")
143         os.system("coverage html -d ./cover %s" % self.omit)
144         os.system("coverage xml -o ./cover/coverage.xml %s" % self.omit)
145
146
147 class TestrFake(cmd.Command):
148     description = "Run unit tests using testr"
149     user_options = []
150
151     def initialize_options(self):
152         pass
153
154     def finalize_options(self):
155         pass
156
157     def run(self):
158         print("Install testrepository to run 'testr' command properly.")
159
160
161 try:
162     from testrepository import commands
163     have_testr = True
164     Testr = TestrReal
165 except ImportError:
166     have_testr = False
167     Testr = TestrFake