Approval tests now use Python std lib instead of call to diff

This needed to change to let it run on Windows as well as on the Unices
This commit is contained in:
Martin Hořeňovský 2017-01-19 22:48:23 +01:00
parent c4b5057094
commit 9acc6b9673

View File

@ -1,9 +1,12 @@
#!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
import os import os
import sys import sys
import subprocess import subprocess
import re import re
import difflib
import scriptCommon import scriptCommon
from scriptCommon import catchPath from scriptCommon import catchPath
@ -26,6 +29,19 @@ else:
overallResult = 0 overallResult = 0
def diffFiles(fileA, fileB):
with open(fileA, 'r') as file:
aLines = file.readlines()
with open(fileB, 'r') as file:
bLines = file.readlines()
shortenedFilenameA = fileA.rsplit(os.sep, 1)[-1]
shortenedFilenameB = fileB.rsplit(os.sep, 1)[-1]
diff = difflib.unified_diff(aLines, bLines, fromfile=shortenedFilenameA, tofile=shortenedFilenameB, n=0)
return [line for line in diff if line[0] in ('+', '-')]
def filterLine(line): def filterLine(line):
# make paths relative to Catch root # make paths relative to Catch root
line = line.replace(catchPath + '/', '') line = line.replace(catchPath + '/', '')
@ -82,14 +98,15 @@ def approve(baseName, args):
print() print()
print(baseName + ":") print(baseName + ":")
if os.path.exists(baselinesPath): if os.path.exists(baselinesPath):
diffResult = subprocess.call(["diff", baselinesPath, filteredResultsPath]) diffResult = diffFiles(baselinesPath, filteredResultsPath)
if diffResult == 0: if diffResult:
print(''.join(diffResult))
print(" \n****************************\n \033[91mResults differed")
if len(diffResult) > overallResult:
overallResult = len(diffResult)
else:
os.remove(filteredResultsPath) os.remove(filteredResultsPath)
print(" \033[92mResults matched") print(" \033[92mResults matched")
else:
print(" \n****************************\n \033[91mResults differed")
if diffResult > overallResult:
overallResult = diffResult
print("\033[0m") print("\033[0m")
else: else:
print(" first approval") print(" first approval")