Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

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
Commit
9acc6b96739f2a1f30def5833dc0546472f44698
Author
Martin Hořeňovský ·
Parents
c4b50570948f

Files · Download .tar.gz

Changes

 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")