regression_test_helpers.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Support functions for some other test files.
  3. FIXME: This should probably be integrated into ../../tests/gvtest.py
  4. """
  5. import difflib
  6. from pathlib import Path
  7. def compare_graphs(name, output_type):
  8. """
  9. Compare a given graph in the given output format against its reference.
  10. """
  11. filename = Path(f"{name}.{output_type}")
  12. filename_reference = Path("reference") / filename
  13. filename_output = Path("output") / filename
  14. if not filename_reference.is_file():
  15. print(f"Failure: {filename} - No reference file present.")
  16. return False
  17. with open(filename_reference, "rt", encoding="utf-8") as reference_file:
  18. with open(filename_output, "rt", encoding="utf-8") as output_file:
  19. reference = reference_file.readlines()
  20. output = output_file.readlines()
  21. diff_generator = difflib.context_diff(
  22. output, reference, str(filename_output), str(filename_reference)
  23. )
  24. # if diff contains at least one line, the files are different
  25. diff = []
  26. for line in diff_generator:
  27. diff.append(line)
  28. if len(diff) == 0:
  29. print(f"Success: {filename}")
  30. return True
  31. difference = Path("difference")
  32. if not difference.exists():
  33. difference.mkdir(parents=True)
  34. # Write diff to console
  35. for line in diff:
  36. print(line)
  37. # Store diff in file
  38. with open(
  39. f"difference/{str(filename)}", "wt", encoding="utf-8"
  40. ) as diff_file:
  41. diff_file.writelines(diff)
  42. print(
  43. f"Failure: {filename} - Generated file does not match reference file."
  44. )
  45. return False