| 1 |
|
| 2 |
|
| 3 |
import re |
| 4 |
import sys |
| 5 |
import getopt |
| 6 |
from subprocess import Popen, PIPE |
| 7 |
from pprint import pprint as ppr |
| 8 |
import os |
| 9 |
|
| 10 |
_python3 = sys.version_info.major == 3 |
| 11 |
|
| 12 |
|
| 13 |
def Usage(s): |
| 14 |
print('Usage: {} -t <cstest_path> [-f <file_name.cs>] [-d <directory>]'.format(s)) |
| 15 |
sys.exit(-1) |
| 16 |
|
| 17 |
def get_report_file(toolpath, filepath, getDetails, cmt_out): |
| 18 |
cmd = [toolpath, '-f', filepath] |
| 19 |
process = Popen(cmd, stdout=PIPE, stderr=PIPE) |
| 20 |
stdout, stderr = process.communicate() |
| 21 |
|
| 22 |
|
| 23 |
failed_tests = [] |
| 24 |
if _python3: |
| 25 |
stdout = bytes.decode(stdout) |
| 26 |
stderr = bytes.decode(stderr) |
| 27 |
|
| 28 |
|
| 29 |
matches = re.finditer(r'\[\s+RUN\s+\]\s+(.*)\n\[\s+FAILED\s+\]', stdout) |
| 30 |
for match in matches: |
| 31 |
failed_tests.append(match.group(1)) |
| 32 |
|
| 33 |
counter = 0 |
| 34 |
details = [] |
| 35 |
for line in stderr.split('\n'): |
| 36 |
if '[ PASSED ] 0 test(s).' in line: |
| 37 |
break |
| 38 |
elif 'LINE' in line: |
| 39 |
continue |
| 40 |
elif 'ERROR' in line and ' --- ' in line: |
| 41 |
parts = line.split(' --- ') |
| 42 |
try: |
| 43 |
details.append((parts[1], failed_tests[counter], parts[2])) |
| 44 |
except IndexError: |
| 45 |
details.append(('', 'Unknown test', line.split(' --- ')[1])) |
| 46 |
counter += 1 |
| 47 |
else: |
| 48 |
continue |
| 49 |
print('\n[-] There are/is {} failed test(s)'.format(len(details))) |
| 50 |
if len(details) > 0 and getDetails: |
| 51 |
print('[-] Detailed report for {}:\n'.format(filepath)) |
| 52 |
for c, f, d in details: |
| 53 |
print('\t[+] {}: {}\n\t\t{}\n'.format(f, c, d)) |
| 54 |
print('\n') |
| 55 |
return 0 |
| 56 |
elif len(details) > 0: |
| 57 |
for c, f, d in details: |
| 58 |
if len(f) > 0 and cmt_out is True: |
| 59 |
tmp_cmd = ['sed', '-E', '-i.bak', 's/({})(.*)/\/\/ \\1\\2/g'.format(c), filepath] |
| 60 |
sed_proc = Popen(tmp_cmd, stdout=PIPE, stderr=PIPE) |
| 61 |
sed_proc.communicate() |
| 62 |
tmp_cmd2 = ['rm', '-f', filepath + '.bak'] |
| 63 |
rm_proc = Popen(tmp_cmd2, stdout=PIPE, stderr=PIPE) |
| 64 |
rm_proc.communicate() |
| 65 |
|
| 66 |
return 0 |
| 67 |
return 1 |
| 68 |
|
| 69 |
def get_report_folder(toolpath, folderpath, details, cmt_out): |
| 70 |
result = 1 |
| 71 |
for root, dirs, files in os.walk(folderpath): |
| 72 |
path = root.split(os.sep) |
| 73 |
for f in files: |
| 74 |
if f.split('.')[-1] == 'cs': |
| 75 |
print('[-] Target:', f,) |
| 76 |
result *= get_report_file(toolpath, os.sep.join(x for x in path) + os.sep + f, details, cmt_out) |
| 77 |
|
| 78 |
sys.exit(result ^ 1) |
| 79 |
|
| 80 |
if __name__ == '__main__': |
| 81 |
Done = False |
| 82 |
details = False |
| 83 |
toolpath = '' |
| 84 |
cmt_out = False |
| 85 |
try: |
| 86 |
opts, args = getopt.getopt(sys.argv[1:], "ct:f:d:D") |
| 87 |
for opt, arg in opts: |
| 88 |
if opt == '-f': |
| 89 |
result = get_report_file(toolpath, arg, details, cmt_out) |
| 90 |
if result == 0: |
| 91 |
sys.exit(1) |
| 92 |
Done = True |
| 93 |
elif opt == '-d': |
| 94 |
get_report_folder(toolpath, arg, details, cmt_out) |
| 95 |
Done = True |
| 96 |
elif opt == '-t': |
| 97 |
toolpath = arg |
| 98 |
elif opt == '-D': |
| 99 |
details = True |
| 100 |
elif opt == '-c': |
| 101 |
cmt_out = True |
| 102 |
|
| 103 |
except getopt.GetoptError: |
| 104 |
Usage(sys.argv[0]) |
| 105 |
|
| 106 |
if Done is False: |
| 107 |
Usage(sys.argv[0]) |