From 9acc6b96739f2a1f30def5833dc0546472f44698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 19 Jan 2017 22:48:23 +0100 Subject: [PATCH] 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 --- scripts/approvalTests.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/scripts/approvalTests.py b/scripts/approvalTests.py index 4a77a510..e09c5354 100644 --- a/scripts/approvalTests.py +++ b/scripts/approvalTests.py @@ -1,9 +1,12 @@ +#!/usr/bin/env python + from __future__ import print_function import os import sys import subprocess import re +import difflib import scriptCommon from scriptCommon import catchPath @@ -26,6 +29,19 @@ else: 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): # make paths relative to Catch root line = line.replace(catchPath + '/', '') @@ -82,14 +98,15 @@ def approve(baseName, args): print() print(baseName + ":") if os.path.exists(baselinesPath): - diffResult = subprocess.call(["diff", baselinesPath, filteredResultsPath]) - if diffResult == 0: + diffResult = diffFiles(baselinesPath, filteredResultsPath) + if diffResult: + print(''.join(diffResult)) + print(" \n****************************\n \033[91mResults differed") + if len(diffResult) > overallResult: + overallResult = len(diffResult) + else: os.remove(filteredResultsPath) print(" \033[92mResults matched") - else: - print(" \n****************************\n \033[91mResults differed") - if diffResult > overallResult: - overallResult = diffResult print("\033[0m") else: print(" first approval")