Skip to content

Commit

Permalink
20 More informative diff test
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 26, 2024
1 parent 8811842 commit f759432
Showing 1 changed file with 17 additions and 37 deletions.
54 changes: 17 additions & 37 deletions tests/test_shapes.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
import os
import subprocess
import unittest
from difflib import context_diff
from glob import glob
from typing import List


def get_file_lines(file_path: str) -> List[str]:
"""
Load a file into a list of lines
Parameters
----------
file_path : str
The path to the file to load
Returns
-------
List[str]
A list of lines read from the file, with excess whitespace and empty lines removed
"""

with open(file_path, "r") as in_file:
# remove excess whitespace
lines = [line.rstrip().lstrip() for line in in_file]
# remove empty lines
lines = [line for line in lines if line]

return lines


def compare_files(file_path_a: str, file_path_b: str) -> bool:
"""
Check if two files have the same content
def file_diff(file_a: str, file_b: str) -> bool:
"""Check if two files have the same content.
Parameters
__________
file_path_a: str
file_a: str
The path to the first file
file_path_b: str
file__b: str
The path to the second file
Returns
__________
bool
True if the files have the same content
str
A diff of the two files
"""
# Get file lines with whitespace and empty lines stripped
file_lines_a = get_file_lines(file_path_a)
file_lines_b = get_file_lines(file_path_b)
# Read files and remove excess whitespace
with open(file_a, "r") as fa:
a = [line.strip() for line in fa]
a = [line for line in a if line]

with open(file_b, "r") as fb:
b = [line.strip() for line in fb]
b = [line for line in b if line]

return file_lines_a == file_lines_b
return "\n".join(context_diff(a, b))


class TestShapes(unittest.TestCase):
Expand Down Expand Up @@ -104,7 +84,7 @@ def test_wrapper_generation(self) -> None:

self.assertTrue(os.path.isfile(file_ref))
self.assertTrue(os.path.isfile(file_gen))
self.assertTrue(compare_files(file_gen, file_ref))
self.assertEqual(file_diff(file_gen, file_ref), "")


if __name__ == "__main__":
Expand Down

0 comments on commit f759432

Please sign in to comment.