This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-patch.py
executable file
·195 lines (167 loc) · 6.94 KB
/
test-patch.py
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
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import fnmatch
import logging
import os
import sys
import re
import xml.etree.ElementTree as ET
from subprocess import Popen
TARGET_DIR = "./build/"
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT)
LOG = logging.getLogger('testpatch')
LOG.setLevel(logging.INFO)
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
yield os.path.join(root, basename)
def tee(proc, f):
for line in iter(proc.stdout.readline, ''):
sys.stdout.write(line)
f.write(line)
class TestPatchRunner:
def __init__(self, out):
self.compilationWarnings = 0
self.failedTests = []
self.findbugsWarnings = 0
self.out = out
def validate(self):
LOG.info('Validating dependency and licenses')
proc = Popen([
'/bin/bash', '-c',
'mvn clean enforcer:enforce apache-rat:check --batch-mode --fail-at-end|tee ' + TARGET_DIR + '/validation.txt;'
+ '[ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS'], stdout=sys.stdout, stderr=sys.stderr)
return proc.wait()
def checkstyle(self):
LOG.info('Validating checkstyles')
proc = Popen([
'/bin/bash', '-c',
'mvn clean checkstyle:check --batch-mode --fail-at-end|tee -a ' + TARGET_DIR + '/validation.txt;'
+ '[ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS'], stdout=sys.stdout, stderr=sys.stderr)
return proc.wait()
def compile(self):
LOG.info('Compiling the project')
proc = Popen([
'/bin/bash', '-c',
'mvn compile test-compile install'
+ ' -DskipTests=true --update-snapshots --batch-mode --fail-at-end|tee ' + TARGET_DIR + '/buildWarnings.txt;'
+ '[ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS'], stdout=sys.stdout, stderr=sys.stderr)
ret = proc.wait()
if ret != 0:
LOG.warning('The compilation has failed.')
else:
with open(TARGET_DIR + '/buildWarnings.txt') as f:
for line in f:
if line.startswith('[WARNING]') and line.find('/target/generated-sources/') == -1:
self.compilationWarnings += 1
return ret
def run_test(self):
LOG.info('Running tests')
proc = Popen([
'/bin/bash', '-c',
'mvn test integration-test --batch-mode --fail-at-end|tee ' + TARGET_DIR + '/testResults.txt;'
+ '[ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS'], stdout=sys.stdout, stderr=sys.stderr)
return proc.wait()
def run_spotbugs(self):
LOG.info('Running spotbugs')
proc = Popen([
'/bin/bash', '-c',
'mvn spotbugs:spotbugs --batch-mode --fail-at-end|tee ' + TARGET_DIR + '/spotbugsResults.txt;'
+ '[ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS'], stdout=sys.stdout, stderr=sys.stderr)
return proc.wait()
def parse_spotbugs(self):
for f in find_files('.', 'spotbugsXml.xml'):
try:
tree = ET.parse(f)
root = tree.getroot()
bugs = len(root.findall('.//BugInstance'))
if bugs > 0:
name = root.find('./Project').attrib['projectName']
yield (name, bugs)
except Exception as e:
LOG.warning('[test-patch] Failed to parse the spotbugs results from %s, reason: %s' % (f, e))
def parse_failed_test(self):
for f in find_files('.', 'TEST*.xml'):
try:
tree = ET.parse(f)
root = tree.getroot()
failures = int(root.attrib['failures'])
errors = int(root.attrib['errors'])
name = root.attrib['name']
if failures > 0 or errors > 0:
yield name
except Exception as e:
LOG.warning('[test-patch] Failed to parse the result %s, reason: %s' % (f, e))
continue
def run(self):
# -- Begin mvn enforcer and rat check --
ret = self.validate()
if ret != 0:
self.out.write('The validation of dependency or licenses have failed.\n')
return ret
# -- Begin mvn compile check --
ret = self.compile()
if ret != 0:
self.out.write('The compilation has failed.\n')
return ret
if self.compilationWarnings > 0:
self.out.write('The compiler has generated %d warnings.\n\n' % self.compilationWarnings)
return -1
# -- Begin mvn checkstyle check --
ret = self.checkstyle()
if ret != 0:
self.out.write('The validation of checkstyles have failed.\n')
return ret
# -- Begin mvn test check --
if self.run_test() != 0:
self.out.write('There are failures when running the tests.\n')
self.failedTests = list(self.parse_failed_test())
if len(self.failedTests) == 0:
self.out.write('There are no test failures.\n')
else:
self.out.write('The following tests have failed:\n')
for test in self.failedTests:
self.out.write(' %s\n' % test)
return -1
# -- Begin spotbugs check --
if self.run_spotbugs() != 0:
self.out.write('There are failures when running spotbugs.\n')
else:
self.spotbugsWarnings = list(self.parse_spotbugs())
if len(self.spotbugsWarnings) == 0:
self.out.write('No spotbugs warnings has been found.\n')
else:
self.out.write('spotbugs has failed.\n')
for name, bugs in self.spotbugsWarnings:
self.out.write(' Project %s has %s spotbugs warnings.\n' % (name, bugs))
return -1
self.out.write('====================================================\n')
self.out.write('TEST PATCH PASSED!\n')
self.out.write('====================================================\n')
return 0
try:
os.mkdir(TARGET_DIR)
except:
pass
runner = TestPatchRunner(sys.stdout)
ret = runner.run()
sys.exit(ret)