← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:matcher-str into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:matcher-str into launchpad:master.

Commit message:
Implement __str__ for all Matcher subclasses

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/429363

Matchers must implement `__str__` as well as `match`, but the base `Matcher.__str__` raises `NotImplementedError`.  Implement it properly.  This avoids unhelpful exceptions in some cases when matchers fail to match.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:matcher-str into launchpad:master.
diff --git a/lib/lp/app/browser/tests/test_stringformatter.py b/lib/lp/app/browser/tests/test_stringformatter.py
index 2976ab3..78622ea 100644
--- a/lib/lp/app/browser/tests/test_stringformatter.py
+++ b/lib/lp/app/browser/tests/test_stringformatter.py
@@ -821,6 +821,9 @@ class MarksDownAs(Matcher):
     def __init__(self, expected_html):
         self.expected_html = expected_html
 
+    def __str__(self):
+        return "MarksDownAs({!r})".format(self.expected_html)
+
     def match(self, input_string):
         return Equals(self.expected_html).match(
             FormattersAPI(input_string).markdown()
diff --git a/lib/lp/archivepublisher/tests/test_publisher.py b/lib/lp/archivepublisher/tests/test_publisher.py
index 084f53a..e52d5a3 100644
--- a/lib/lp/archivepublisher/tests/test_publisher.py
+++ b/lib/lp/archivepublisher/tests/test_publisher.py
@@ -523,6 +523,9 @@ class ByHashHasContents(Matcher):
             ]
         )
 
+    def __str__(self):
+        return "ByHashHasContents({})".format(self.contents)
+
     def match(self, by_hash_path):
         mismatch = DirContains(self.expected_hashes.keys()).match(by_hash_path)
         if mismatch is not None:
@@ -560,6 +563,9 @@ class ByHashesHaveContents(Matcher):
     def __init__(self, path_contents):
         self.path_contents = path_contents
 
+    def __str__(self):
+        return "ByHashesHaveContents({})".format(self.path_contents)
+
     def match(self, root):
         children = set()
         for dirpath, dirnames, _ in os.walk(root):
diff --git a/lib/lp/archivepublisher/tests/test_signing.py b/lib/lp/archivepublisher/tests/test_signing.py
index 7804a43..ae4b7ea 100644
--- a/lib/lp/archivepublisher/tests/test_signing.py
+++ b/lib/lp/archivepublisher/tests/test_signing.py
@@ -67,6 +67,9 @@ class SignedMatches(Matcher):
     def __init__(self, expected):
         self.expected = expected
 
+    def __str__(self):
+        return "SignedMatches({})".format(self.expected)
+
     def match(self, base):
         content = []
         for root, dirs, files in os.walk(base):
diff --git a/lib/lp/charms/tests/test_charmhubclient.py b/lib/lp/charms/tests/test_charmhubclient.py
index 771941f..b951718 100644
--- a/lib/lp/charms/tests/test_charmhubclient.py
+++ b/lib/lp/charms/tests/test_charmhubclient.py
@@ -58,6 +58,9 @@ class MacaroonVerifies(Matcher):
     def __init__(self, key):
         self.key = key
 
+    def __str__(self):
+        return "MacaroonVerifies({!r})".format(self.key)
+
     def match(self, macaroon_raw):
         macaroon = Macaroon.deserialize(macaroon_raw)
         try:
diff --git a/lib/lp/codehosting/scripts/tests/test_sync_branches.py b/lib/lp/codehosting/scripts/tests/test_sync_branches.py
index 3e35444..a6dcc4b 100644
--- a/lib/lp/codehosting/scripts/tests/test_sync_branches.py
+++ b/lib/lp/codehosting/scripts/tests/test_sync_branches.py
@@ -20,6 +20,9 @@ from lp.testing.layers import ZopelessDatabaseLayer
 
 
 class BranchDirectoryCreated(Matcher):
+    def __str__(self):
+        return "BranchDirectoryCreated()"
+
     def match(self, branch):
         return DirExists().match(
             os.path.join(
diff --git a/lib/lp/registry/tests/test_sourcepackagename_vocabulary.py b/lib/lp/registry/tests/test_sourcepackagename_vocabulary.py
index 11d34df..bb800c8 100644
--- a/lib/lp/registry/tests/test_sourcepackagename_vocabulary.py
+++ b/lib/lp/registry/tests/test_sourcepackagename_vocabulary.py
@@ -24,6 +24,11 @@ class MatchesSourcePackageNameTerms(Matcher):
         self.names = names
         self.ordered = ordered
 
+    def __str__(self):
+        return "MatchesSourcePackageNameTerms({}, ordered={})".format(
+            self.names, self.ordered
+        )
+
     def match(self, terms):
         matchers = [
             MatchesStructure.byEquality(
diff --git a/lib/lp/services/macaroons/testing.py b/lib/lp/services/macaroons/testing.py
index 30e5e8e..1c7d43e 100644
--- a/lib/lp/services/macaroons/testing.py
+++ b/lib/lp/services/macaroons/testing.py
@@ -35,6 +35,11 @@ class MacaroonVerifies(Matcher):
         self.matcher = matcher
         self.verify_kwargs = verify_kwargs
 
+    def __str__(self):
+        return "MacaroonVerifies({!r}, {!r}, matcher={}, **{})".format(
+            self.issuer_name, self.context, self.matcher, self.verify_kwargs
+        )
+
     def match(self, macaroon_raw):
         issuer = getUtility(IMacaroonIssuer, self.issuer_name)
         macaroon = Macaroon.deserialize(macaroon_raw)
diff --git a/lib/lp/services/webhooks/testing.py b/lib/lp/services/webhooks/testing.py
index adeb55c..d6d120a 100644
--- a/lib/lp/services/webhooks/testing.py
+++ b/lib/lp/services/webhooks/testing.py
@@ -23,6 +23,11 @@ class LogsOneScheduledWebhook(Matcher):
         self.event_type = event_type
         self.payload_matcher = payload_matcher
 
+    def __str__(self):
+        return "LogsOneScheduledWebhook({!r}, {!r}, {})".format(
+            self.webhook, self.event_type, self.payload_matcher
+        )
+
     def match(self, line):
         prefix = (
             "Scheduled <WebhookDeliveryJob for webhook %d on %r> (%s): "
diff --git a/lib/lp/snappy/tests/test_snapstoreclient.py b/lib/lp/snappy/tests/test_snapstoreclient.py
index 9734c0b..960bafa 100644
--- a/lib/lp/snappy/tests/test_snapstoreclient.py
+++ b/lib/lp/snappy/tests/test_snapstoreclient.py
@@ -62,6 +62,9 @@ class MacaroonsVerify(Matcher):
     def __init__(self, key):
         self.key = key
 
+    def __str__(self):
+        return "MacaroonsVerify({!r})".format(self.key)
+
     def match(self, macaroons):
         mismatch = Contains("root").match(macaroons)
         if mismatch is not None:
@@ -194,6 +197,18 @@ class RequestMatches(Matcher):
         self.form_data = form_data
         self.kwargs = kwargs
 
+    def __str__(self):
+        return (
+            "RequestMatches({!r}, auth={}, json_data={}, form_data={}, "
+            "**{})".format(
+                self.url,
+                self.auth,
+                self.json_data,
+                self.form_data,
+                self.kwargs,
+            )
+        )
+
     def match(self, request):
         mismatch = MatchesStructure(url=self.url, **self.kwargs).match(request)
         if mismatch is not None: