mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
test: Make mail_diff support comparing multipart messages
In upcoming patches we will want to compare mime-multipart messages, so this patch extends the mail_diff test helper to support it.
This commit is contained in:
@@ -2,31 +2,10 @@
|
|||||||
|
|
||||||
import difflib
|
import difflib
|
||||||
import email.parser
|
import email.parser
|
||||||
|
import itertools
|
||||||
import mailbox
|
import mailbox
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
f1, f2 = sys.argv[1:3]
|
|
||||||
|
|
||||||
expected = email.parser.Parser().parse(open(f1))
|
|
||||||
|
|
||||||
mbox = mailbox.mbox(f2, create=False)
|
|
||||||
msg = mbox[0]
|
|
||||||
|
|
||||||
diff = False
|
|
||||||
|
|
||||||
for h, val in expected.items():
|
|
||||||
if h not in msg:
|
|
||||||
print("Header missing: %r" % h)
|
|
||||||
diff = True
|
|
||||||
continue
|
|
||||||
|
|
||||||
if expected[h] == '*':
|
|
||||||
continue
|
|
||||||
|
|
||||||
if msg[h] != val:
|
|
||||||
print("Header %r differs: %r != %r" % (h, val, msg[h]))
|
|
||||||
diff = True
|
|
||||||
|
|
||||||
|
|
||||||
def flexible_eq(expected, got):
|
def flexible_eq(expected, got):
|
||||||
"""Compare two strings, supporting wildcards.
|
"""Compare two strings, supporting wildcards.
|
||||||
@@ -48,7 +27,7 @@ def flexible_eq(expected, got):
|
|||||||
posG += 1
|
posG += 1
|
||||||
continue
|
continue
|
||||||
if c == '*':
|
if c == '*':
|
||||||
while got[posG] != '\n':
|
while posG < len(got) and got[posG] != '\n':
|
||||||
posG += 1
|
posG += 1
|
||||||
continue
|
continue
|
||||||
continue
|
continue
|
||||||
@@ -58,20 +37,61 @@ def flexible_eq(expected, got):
|
|||||||
|
|
||||||
posG += 1
|
posG += 1
|
||||||
|
|
||||||
|
if posG != len(got):
|
||||||
|
# We got more than we expected.
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
if not flexible_eq(expected.get_payload(), msg.get_payload()):
|
def msg_equals(expected, msg):
|
||||||
|
"""Compare two messages recursively, using flexible_eq()."""
|
||||||
|
diff = False
|
||||||
|
for h, val in expected.items():
|
||||||
|
if h not in msg:
|
||||||
|
print("Header missing: %r" % h)
|
||||||
diff = True
|
diff = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
if expected[h] == '*':
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not flexible_eq(val, msg[h]):
|
||||||
|
print("Header %r differs:" % h)
|
||||||
|
print("Exp: %r" % val)
|
||||||
|
print("Got: %r" % msg[h])
|
||||||
|
diff = True
|
||||||
|
|
||||||
|
if diff:
|
||||||
|
return False
|
||||||
|
|
||||||
if expected.is_multipart() != msg.is_multipart():
|
if expected.is_multipart() != msg.is_multipart():
|
||||||
print("Multipart differs, expected %s, got %s" % (
|
print("Multipart differs, expected %s, got %s" % (
|
||||||
expected.is_multipart(), msg.is_multipart()))
|
expected.is_multipart(), msg.is_multipart()))
|
||||||
elif not msg.is_multipart():
|
return False
|
||||||
|
|
||||||
|
if expected.is_multipart():
|
||||||
|
for exp, got in itertools.izip_longest(expected.get_payload(), msg.get_payload()):
|
||||||
|
if not msg_equals(exp, got):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if not flexible_eq(expected.get_payload(), msg.get_payload()):
|
||||||
exp = expected.get_payload().splitlines()
|
exp = expected.get_payload().splitlines()
|
||||||
got = msg.get_payload().splitlines()
|
got = msg.get_payload().splitlines()
|
||||||
print("Payload differs:")
|
print("Payload differs:")
|
||||||
for l in difflib.ndiff(exp, got):
|
for l in difflib.ndiff(exp, got):
|
||||||
print(l)
|
print(l)
|
||||||
|
return False
|
||||||
|
|
||||||
sys.exit(0 if not diff else 1)
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
f1, f2 = sys.argv[1:3]
|
||||||
|
|
||||||
|
expected = email.parser.Parser().parse(open(f1))
|
||||||
|
|
||||||
|
mbox = mailbox.mbox(f2, create=False)
|
||||||
|
msg = mbox[0]
|
||||||
|
|
||||||
|
sys.exit(0 if msg_equals(expected, msg) else 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user