xmlutil: add a diff() helper

And centralize all the scattered difflib calls we have

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-09-05 08:29:04 -04:00
parent 7f1f5e343e
commit 4a47d32b09
5 changed files with 20 additions and 23 deletions

View File

@ -3,7 +3,6 @@
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
import difflib
import os
import sys
import unittest
@ -11,8 +10,9 @@ import unittest
import libvirt
import virtinst
from virtinst import cli
import virtinst.uri
from virtinst import cli
from virtinst import xmlutil
# pylint: disable=protected-access
@ -231,10 +231,8 @@ def diff_compare(actual_out, filename=None, expect_out=None):
open(filename, "w").write(actual_out)
expect_out = open(filename).read()
diff = "".join(difflib.unified_diff(expect_out.splitlines(1),
actual_out.splitlines(1),
fromfile=filename or '',
tofile="Generated Output"))
diff = xmlutil.diff(expect_out, actual_out,
filename or '', "Generated output")
if diff:
raise AssertionError("Conversion outputs did not match.\n%s" % diff)

View File

@ -5,6 +5,7 @@
# See the COPYING file in the top-level directory.
from virtinst import log
from virtinst import xmlutil
from ..baseclass import vmmGObject
@ -43,11 +44,7 @@ class vmmLibvirtObject(vmmGObject):
obj)
return
import difflib
diff = "".join(difflib.unified_diff(origxml.splitlines(1),
newxml.splitlines(1),
fromfile="Original XML",
tofile="New XML"))
diff = xmlutil.diff(origxml, newxml, "Original XML", "New XML")
log.debug("Redefining %s with XML diff:\n%s", obj, diff)
@staticmethod

View File

@ -9,11 +9,10 @@ import re
import urllib.parse
from .logger import log
from . import xmlutil
def sanitize_xml_for_test_define(xml):
import difflib
orig = xml
xml = re.sub("arch=\".*\"", "arch=\"i686\"", xml)
xml = re.sub("domain type=\".*\"", "domain type=\"test\"", xml)
@ -21,8 +20,7 @@ def sanitize_xml_for_test_define(xml):
xml = re.sub(">exe<", ">hvm<", xml)
xml = re.sub(">linux<", ">xen<", xml)
diff = "\n".join(difflib.unified_diff(orig.split("\n"),
xml.split("\n")))
diff = xmlutil.diff(orig, xml)
if diff:
log.debug("virtinst test sanitizing diff\n:%s", diff)
return xml

View File

@ -5,7 +5,6 @@
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
import difflib
import sys
import libvirt
@ -37,16 +36,13 @@ def prompt_yes_or_no(msg):
def get_diff(origxml, newxml):
ret = "".join(difflib.unified_diff(origxml.splitlines(1),
newxml.splitlines(1),
fromfile="Original XML",
tofile="Altered XML"))
diff = xmlutil.diff(origxml, newxml, "Original XML", "Altered XML")
if ret:
log.debug("XML diff:\n%s", ret)
if diff:
log.debug("XML diff:\n%s", diff)
else:
log.debug("No XML diff, didn't generate any change.")
return ret
return diff
def set_os_variant(options, guest):

View File

@ -68,3 +68,11 @@ def set_prop_path(obj, prop_path, value):
def in_testsuite():
return "VIRTINST_TEST_SUITE" in os.environ
def diff(origstr, newstr, fromfile="Original", tofile="New"):
import difflib
dlist = difflib.unified_diff(
origstr.splitlines(1), newstr.splitlines(1),
fromfile=fromfile, tofile=tofile)
return "".join(dlist)