aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Skultety <eskultet@redhat.com>2023-03-29 14:36:52 +0200
committerDaniel P. Berrangé <berrange@redhat.com>2023-03-30 08:00:42 +0000
commit65466bf2ffe5012e585d8b0f234d54360ecab772 (patch)
tree7c121a277728b9eea5b2d5e95e3121699c9bc1ee
parente35d7a415a256053bb999848ad11680f691fd708 (diff)
downloadlibvirt-ci-65466bf2ffe5012e585d8b0f234d54360ecab772.zip
libvirt-ci-65466bf2ffe5012e585d8b0f234d54360ecab772.tar.gz
libvirt-ci-65466bf2ffe5012e585d8b0f234d54360ecab772.tar.bz2
tests: utils: Introduce a class representing a diff operand
The idea is to be able to compare all datatypes we produce in a generic manner interchangeably. In other words, we need to be able to compare lists, dicts, strings, files or even strings with files transparently to the caller. How are we going to do that? Since our native serialization format is YAML, we'll serialize everything apart from raw strings to YAML and compare those the same way as if two files were compared using diff. To implement the idea, difflib, which is part of the standard library, is used. The interface to compare the actual and expected outputs was borrowed from Python sets and so follows the "A.diff(B)" mantra where A is the actual produced output and B is the expected one. Both need to be of type DiffOperand (introduced by this patch) which takes care of any conversion needed. Unlike sets though, we can't use this the other way around because then we'd get difflib's uninified_diff confused on which is which. Not that it wouldn't work, it's just the final diff might be confusing. Sadly, there's no way to enforce this usage though. The nice thing about this patch is that we'll now get something like this if the outputs don't match: --- actual +++ expected @@ -48,7 +48,6 @@ - firewalld-filesystem - flake8 - flex -- foo - fuse - fuse3 - fusermount @@ -317,7 +316,6 @@ - xsltproc - xz - yajl -- zfoo - zip - zlib - zlib-static Signed-off-by: Erik Skultety <eskultet@redhat.com>
-rw-r--r--tests/test_utils/utils.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_utils/utils.py b/tests/test_utils/utils.py
index f22946b..1afa152 100644
--- a/tests/test_utils/utils.py
+++ b/tests/test_utils/utils.py
@@ -4,6 +4,7 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
+import difflib
import pytest
import yaml
@@ -75,6 +76,38 @@ class Diff:
return not bool(str(self))
+class DiffOperand:
+ def __init__(self, obj):
+ self._obj = obj
+ self._data = self._stringify(obj)
+
+ def __repr__(self):
+ return f"obj: {repr(self._obj)}, str: {self._data})"
+
+ def __str__(self):
+ return self._data
+
+ @staticmethod
+ def _stringify(obj):
+ if isinstance(obj, str):
+ return obj
+
+ if isinstance(obj, Path):
+ with open(obj, 'r') as f:
+ return f.read()
+
+ return yaml.safe_dump(obj)
+
+ def diff(self, other):
+ if not isinstance(other, self.__class__):
+ other = self._stringify(other)
+
+ return Diff(difflib.unified_diff(str(self).splitlines(keepends=True),
+ str(other).splitlines(keepends=True),
+ fromfile="actual",
+ tofile="expected",))
+
+
def assert_equal(actual, expected, indices):
if not isinstance(actual, type(expected)):
raise AssertionError(format_err_msg(indices, f"expected {type(expected)}, got {type(actual)}"))