|
14 | 14 | # License for the specific language governing permissions and limitations |
15 | 15 | # under the License. |
16 | 16 |
|
17 | | -from setuptools import setup, Command |
18 | | - |
19 | | -import os |
20 | | -import sys |
| 17 | +from setuptools import setup |
21 | 18 |
|
22 | 19 | import splunklib |
23 | 20 |
|
24 | | -failed = False |
25 | | - |
26 | | -def run_test_suite(): |
27 | | - import unittest |
28 | | - |
29 | | - def mark_failed(): |
30 | | - global failed |
31 | | - failed = True |
32 | | - |
33 | | - class _TrackingTextTestResult(unittest._TextTestResult): |
34 | | - def addError(self, test, err): |
35 | | - unittest._TextTestResult.addError(self, test, err) |
36 | | - mark_failed() |
37 | | - |
38 | | - def addFailure(self, test, err): |
39 | | - unittest._TextTestResult.addFailure(self, test, err) |
40 | | - mark_failed() |
41 | | - |
42 | | - class TrackingTextTestRunner(unittest.TextTestRunner): |
43 | | - def _makeResult(self): |
44 | | - return _TrackingTextTestResult( |
45 | | - self.stream, self.descriptions, self.verbosity) |
46 | | - |
47 | | - original_cwd = os.path.abspath(os.getcwd()) |
48 | | - os.chdir('tests') |
49 | | - suite = unittest.defaultTestLoader.discover('.') |
50 | | - runner = TrackingTextTestRunner(verbosity=2) |
51 | | - runner.run(suite) |
52 | | - os.chdir(original_cwd) |
53 | | - |
54 | | - return failed |
55 | | - |
56 | | - |
57 | | -def run_test_suite_with_junit_output(): |
58 | | - try: |
59 | | - import unittest2 as unittest |
60 | | - except ImportError: |
61 | | - import unittest |
62 | | - import xmlrunner |
63 | | - original_cwd = os.path.abspath(os.getcwd()) |
64 | | - os.chdir('tests') |
65 | | - suite = unittest.defaultTestLoader.discover('.') |
66 | | - xmlrunner.XMLTestRunner(output='../test-reports').run(suite) |
67 | | - os.chdir(original_cwd) |
68 | | - |
69 | | - |
70 | | -class CoverageCommand(Command): |
71 | | - """setup.py command to run code coverage of the test suite.""" |
72 | | - description = "Create an HTML coverage report from running the full test suite." |
73 | | - user_options = [] |
74 | | - |
75 | | - def initialize_options(self): |
76 | | - pass |
77 | | - |
78 | | - def finalize_options(self): |
79 | | - pass |
80 | | - |
81 | | - def run(self): |
82 | | - try: |
83 | | - import coverage |
84 | | - except ImportError: |
85 | | - print("Could not import coverage. Please install it and try again.") |
86 | | - exit(1) |
87 | | - cov = coverage.coverage(source=['splunklib']) |
88 | | - cov.start() |
89 | | - run_test_suite() |
90 | | - cov.stop() |
91 | | - cov.html_report(directory='coverage_report') |
92 | | - |
93 | | - |
94 | | -class TestCommand(Command): |
95 | | - """setup.py command to run the whole test suite.""" |
96 | | - description = "Run test full test suite." |
97 | | - user_options = [] |
98 | | - |
99 | | - def initialize_options(self): |
100 | | - pass |
101 | | - |
102 | | - def finalize_options(self): |
103 | | - pass |
104 | | - |
105 | | - def run(self): |
106 | | - failed = run_test_suite() |
107 | | - if failed: |
108 | | - sys.exit(1) |
109 | | - |
110 | | - |
111 | | -class JunitXmlTestCommand(Command): |
112 | | - """setup.py command to run the whole test suite.""" |
113 | | - description = "Run test full test suite with JUnit-formatted output." |
114 | | - user_options = [] |
115 | | - |
116 | | - def initialize_options(self): |
117 | | - pass |
118 | | - |
119 | | - def finalize_options(self): |
120 | | - pass |
121 | | - |
122 | | - def run(self): |
123 | | - run_test_suite_with_junit_output() |
124 | | - |
125 | | - |
126 | 21 | setup( |
127 | 22 | author="Splunk, Inc.", |
128 | 23 |
|
129 | 24 | author_email="devinfo@splunk.com", |
130 | 25 |
|
131 | | - cmdclass={'coverage': CoverageCommand, |
132 | | - 'test': TestCommand, |
133 | | - 'testjunit': JunitXmlTestCommand}, |
134 | | - |
135 | 26 | description="The Splunk Software Development Kit for Python.", |
136 | 27 |
|
137 | 28 | license="http://www.apache.org/licenses/LICENSE-2.0", |
|
0 commit comments