| 1 |
|
| 2 |
|
| 3 |
import io |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import subprocess |
| 7 |
import re |
| 8 |
import difflib |
| 9 |
import shutil |
| 10 |
|
| 11 |
import scriptCommon |
| 12 |
from scriptCommon import catchPath |
| 13 |
|
| 14 |
if os.name == 'nt': |
| 15 |
|
| 16 |
os.system('') |
| 17 |
|
| 18 |
rootPath = os.path.join(catchPath, 'tests/SelfTest/Baselines') |
| 19 |
|
| 20 |
outputDirPath = None |
| 21 |
|
| 22 |
if len(sys.argv) == 3: |
| 23 |
cmdPath = sys.argv[1] |
| 24 |
outputDirBasePath = sys.argv[2] |
| 25 |
outputDirPath = os.path.join(outputDirBasePath, 'ApprovalTests') |
| 26 |
if not os.path.isdir(outputDirPath): |
| 27 |
os.mkdir(outputDirPath) |
| 28 |
else: |
| 29 |
print('Usage: {} path-to-SelfTest-executable path-to-temp-output-dir'.format(sys.argv[0])) |
| 30 |
exit(1) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
def get_rawResultsPath(baseName): |
| 35 |
return os.path.join(outputDirPath, '_{0}.tmp'.format(baseName)) |
| 36 |
|
| 37 |
def get_baselinesPath(baseName): |
| 38 |
return os.path.join(rootPath, '{0}.approved.txt'.format(baseName)) |
| 39 |
|
| 40 |
def _get_unapprovedPath(path, baseName): |
| 41 |
return os.path.join(path, '{0}.unapproved.txt'.format(baseName)) |
| 42 |
|
| 43 |
def get_filteredResultsPath(baseName): |
| 44 |
return _get_unapprovedPath(outputDirPath, baseName) |
| 45 |
|
| 46 |
def get_unapprovedResultsPath(baseName): |
| 47 |
return _get_unapprovedPath(rootPath, baseName) |
| 48 |
|
| 49 |
langFilenameParser = re.compile(r'(.+\.[ch]pp)') |
| 50 |
filelocParser = re.compile(r''' |
| 51 |
(?P<path_prefix>tests/SelfTest/(?:\w+/)*) # We separate prefix and fname, so that |
| 52 |
(?P<filename>\w+\.tests\.[ch]pp) # we can keep only filename |
| 53 |
(?::|\() # Linux has : as separator between fname and line number, Windows uses ( |
| 54 |
(\d*) # line number |
| 55 |
\)? # Windows also uses an ending separator, ) |
| 56 |
''', re.VERBOSE) |
| 57 |
lineNumberParser = re.compile(r' line="[0-9]*"') |
| 58 |
hexParser = re.compile(r'\b(0[xX][0-9a-fA-F]+)\b') |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
junitDurationsParser = re.compile(r' time="[0-9]+\.[0-9]{3}"') |
| 63 |
durationParser = re.compile(r''' duration=['"][0-9]+['"]''') |
| 64 |
timestampsParser = re.compile(r'\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}Z') |
| 65 |
versionParser = re.compile(r'[0-9]+\.[0-9]+\.[0-9]+(-\w*\.[0-9]+)?') |
| 66 |
nullParser = re.compile(r'\b(__null|nullptr)\b') |
| 67 |
exeNameParser = re.compile(r''' |
| 68 |
\b |
| 69 |
SelfTest # Expected executable name |
| 70 |
(?:.exe)? # Executable name contains .exe on Windows. |
| 71 |
\b |
| 72 |
''', re.VERBOSE) |
| 73 |
|
| 74 |
specialCaseParser = re.compile(r'file\((\d+)\)') |
| 75 |
|
| 76 |
sinceEpochParser = re.compile(r'\d+ .+ since epoch') |
| 77 |
|
| 78 |
|
| 79 |
tapTestNumParser = re.compile(r'^((?:not ok)|(?:ok)|(?:warning)|(?:info)) (\d+) -') |
| 80 |
|
| 81 |
overallResult = 0 |
| 82 |
|
| 83 |
def diffFiles(fileA, fileB): |
| 84 |
with io.open(fileA, 'r', encoding='utf-8', errors='surrogateescape') as file: |
| 85 |
aLines = [line.rstrip() for line in file.readlines()] |
| 86 |
with io.open(fileB, 'r', encoding='utf-8', errors='surrogateescape') as file: |
| 87 |
bLines = [line.rstrip() for line in file.readlines()] |
| 88 |
|
| 89 |
shortenedFilenameA = fileA.rsplit(os.sep, 1)[-1] |
| 90 |
shortenedFilenameB = fileB.rsplit(os.sep, 1)[-1] |
| 91 |
|
| 92 |
diff = difflib.unified_diff(aLines, bLines, fromfile=shortenedFilenameA, tofile=shortenedFilenameB, n=0) |
| 93 |
return [line for line in diff if line[0] in ('+', '-')] |
| 94 |
|
| 95 |
|
| 96 |
def normalizeFilepath(line): |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
forwardSlashPath = catchPath.replace('\\', '/') |
| 101 |
if forwardSlashPath in line: |
| 102 |
line = line.replace(forwardSlashPath + '/', '') |
| 103 |
backwardSlashPath = catchPath.replace('/', '\\') |
| 104 |
if backwardSlashPath in line: |
| 105 |
line = line.replace(backwardSlashPath + '\\', '') |
| 106 |
|
| 107 |
m = langFilenameParser.match(line) |
| 108 |
if m: |
| 109 |
filepath = m.group(0) |
| 110 |
|
| 111 |
filepath = filepath.replace('\\', '/') |
| 112 |
|
| 113 |
filepath = filepath.replace('../', '') |
| 114 |
line = line[:m.start()] + filepath + line[m.end():] |
| 115 |
|
| 116 |
return line |
| 117 |
|
| 118 |
def filterLine(line, isCompact): |
| 119 |
line = normalizeFilepath(line) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
line = filelocParser.sub('\g<filename>:<line number>', line) |
| 125 |
|
| 126 |
line = lineNumberParser.sub(" ", line) |
| 127 |
|
| 128 |
if isCompact: |
| 129 |
line = line.replace(': FAILED', ': failed') |
| 130 |
line = line.replace(': PASSED', ': passed') |
| 131 |
|
| 132 |
|
| 133 |
line = tapTestNumParser.sub("\g<1> {test-number} -", line) |
| 134 |
|
| 135 |
|
| 136 |
line = versionParser.sub("<version>", line) |
| 137 |
|
| 138 |
|
| 139 |
line = nullParser.sub("0", line) |
| 140 |
|
| 141 |
|
| 142 |
line = exeNameParser.sub("<exe-name>", line) |
| 143 |
|
| 144 |
|
| 145 |
line = hexParser.sub("0x<hex digits>", line) |
| 146 |
|
| 147 |
|
| 148 |
line = junitDurationsParser.sub(' time="{duration}"', line) |
| 149 |
line = durationParser.sub(' duration="{duration}"', line) |
| 150 |
line = timestampsParser.sub('{iso8601-timestamp}', line) |
| 151 |
line = specialCaseParser.sub('file:\g<1>', line) |
| 152 |
line = sinceEpochParser.sub('{since-epoch-report}', line) |
| 153 |
return line |
| 154 |
|
| 155 |
|
| 156 |
def run_test(baseName, args): |
| 157 |
args[0:0] = [cmdPath] |
| 158 |
if not os.path.exists(cmdPath): |
| 159 |
raise Exception("Executable doesn't exist at " + cmdPath) |
| 160 |
|
| 161 |
print(args) |
| 162 |
rawResultsPath = get_rawResultsPath(baseName) |
| 163 |
f = open(rawResultsPath, 'w') |
| 164 |
subprocess.call(args, stdout=f, stderr=f) |
| 165 |
f.close() |
| 166 |
|
| 167 |
|
| 168 |
def check_outputs(baseName): |
| 169 |
global overallResult |
| 170 |
rawResultsPath = get_rawResultsPath(baseName) |
| 171 |
baselinesPath = get_baselinesPath(baseName) |
| 172 |
filteredResultsPath = get_filteredResultsPath(baseName) |
| 173 |
|
| 174 |
rawFile = io.open(rawResultsPath, 'r', encoding='utf-8', errors='surrogateescape') |
| 175 |
filteredFile = io.open(filteredResultsPath, 'w', encoding='utf-8', errors='surrogateescape') |
| 176 |
for line in rawFile: |
| 177 |
filteredFile.write(filterLine(line, 'compact' in baseName).rstrip() + "\n") |
| 178 |
filteredFile.close() |
| 179 |
rawFile.close() |
| 180 |
|
| 181 |
os.remove(rawResultsPath) |
| 182 |
print() |
| 183 |
print(baseName + ":") |
| 184 |
if not os.path.exists(baselinesPath): |
| 185 |
print( 'first approval') |
| 186 |
overallResult += 1 |
| 187 |
return |
| 188 |
|
| 189 |
diffResult = diffFiles(baselinesPath, filteredResultsPath) |
| 190 |
if diffResult: |
| 191 |
print('\n'.join(diffResult)) |
| 192 |
print(" \n****************************\n \033[91mResults differed\033[0m") |
| 193 |
overallResult += 1 |
| 194 |
shutil.move(filteredResultsPath, get_unapprovedResultsPath(baseName)) |
| 195 |
else: |
| 196 |
os.remove(filteredResultsPath) |
| 197 |
print(" \033[92mResults matched\033[0m") |
| 198 |
|
| 199 |
|
| 200 |
def approve(baseName, args): |
| 201 |
run_test(baseName, args) |
| 202 |
check_outputs(baseName) |
| 203 |
|
| 204 |
|
| 205 |
print("Running approvals against executable:") |
| 206 |
print(" " + cmdPath) |
| 207 |
|
| 208 |
|
| 209 |
base_args = ["--order", "lex", "--rng-seed", "1", "--colour-mode", "none"] |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
approve("console.std", ["~[!nonportable]~[!benchmark]~[approvals] *"] + base_args) |
| 214 |
|
| 215 |
|
| 216 |
approve("console.swa4", ["~[!nonportable]~[!benchmark]~[approvals] *", "-s", "-w", "NoAssertions", "-x", "4"] + base_args) |
| 217 |
|
| 218 |
|
| 219 |
reporters = ('console', 'junit', 'xml', 'compact', 'sonarqube', 'tap', 'teamcity', 'automake') |
| 220 |
for reporter in reporters: |
| 221 |
filename = '{}.sw'.format(reporter) |
| 222 |
common_args = ["~[!nonportable]~[!benchmark]~[approvals] *", "-s", "-w", "NoAssertions"] + base_args |
| 223 |
reporter_args = ['-r', reporter] |
| 224 |
approve(filename, common_args + reporter_args) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
common_args = ["~[!nonportable]~[!benchmark]~[approvals] *", "-s", "-w", "NoAssertions"] + base_args |
| 229 |
filenames = ['{}.sw.multi'.format(reporter) for reporter in reporters] |
| 230 |
reporter_args = [] |
| 231 |
for reporter, filename in zip(reporters, filenames): |
| 232 |
reporter_args += ['-r', '{}::out={}'.format(reporter, get_rawResultsPath(filename))] |
| 233 |
|
| 234 |
run_test("default.sw.multi", common_args + reporter_args) |
| 235 |
|
| 236 |
check_outputs("default.sw.multi") |
| 237 |
for reporter, filename in zip(reporters, filenames): |
| 238 |
check_outputs(filename) |
| 239 |
|
| 240 |
|
| 241 |
if overallResult != 0: |
| 242 |
print("If these differences are expected, run approve.py to approve new baselines.") |
| 243 |
exit(2) |