← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-unittest-bytes-text into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-unittest-bytes-text into launchpad:master.

Commit message:
Fix various simple bytes/text issues in unit tests

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/398879
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-unittest-bytes-text into launchpad:master.
diff --git a/lib/lp/archivepublisher/tests/test_pool.py b/lib/lp/archivepublisher/tests/test_pool.py
index 7c60fd9..e56ef02 100644
--- a/lib/lp/archivepublisher/tests/test_pool.py
+++ b/lib/lp/archivepublisher/tests/test_pool.py
@@ -44,7 +44,7 @@ class PoolTestingFile:
         self.pool = pool
         self.sourcename = sourcename
         self.filename = filename
-        self.contents = sourcename
+        self.contents = sourcename.encode("UTF-8")
 
     def addToPool(self, component):
         return self.pool.addFile(
diff --git a/lib/lp/archiveuploader/tests/test_ocirecipeupload.py b/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
index 13b577b..b36d2c6 100644
--- a/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
+++ b/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
@@ -67,7 +67,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
         write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
         write_file(os.path.join(upload_dir, "layer_2.tar.gz"), b"layer_2")
         write_file(
-            os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
+            os.path.join(upload_dir, "digests.json"),
+            json.dumps(self.digests).encode("UTF-8"))
         write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
         handler = UploadHandler.forProcessor(
             self.uploadprocessor, self.incoming_folder, "test", self.build)
@@ -101,7 +102,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
         write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
         write_file(os.path.join(upload_dir, "layer_2.tar.gz"), b"layer_2")
         write_file(
-            os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
+            os.path.join(upload_dir, "digests.json"),
+            json.dumps(self.digests).encode("UTF-8"))
         write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
         handler = UploadHandler.forProcessor(
             self.uploadprocessor, self.incoming_folder, "test", build)
@@ -139,7 +141,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
             self.incoming_folder, "test", str(self.build.id), "ubuntu")
         write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
         write_file(
-            os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
+            os.path.join(upload_dir, "digests.json"),
+            json.dumps(self.digests).encode("UTF-8"))
         handler = UploadHandler.forProcessor(
             self.uploadprocessor, self.incoming_folder, "test", self.build)
         result = handler.processOCIRecipe(self.log)
@@ -160,7 +163,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
         write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
         write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
         write_file(
-            os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
+            os.path.join(upload_dir, "digests.json"),
+            json.dumps(self.digests).encode("UTF-8"))
 
         # create the existing file
         self.switchToAdmin()
diff --git a/lib/lp/codehosting/vfs/tests/test_filesystem.py b/lib/lp/codehosting/vfs/tests/test_filesystem.py
index abf5197..3f955d6 100644
--- a/lib/lp/codehosting/vfs/tests/test_filesystem.py
+++ b/lib/lp/codehosting/vfs/tests/test_filesystem.py
@@ -143,7 +143,8 @@ class TestFilesystem(TestCaseWithTransport):
             % (self.requester.name, product.name))
         stacked_on = IBranchTarget(product).default_stacked_on_branch
         self.assertEqual(
-            'default_stack_on = %s' % branch_id_alias(stacked_on),
+            ('default_stack_on = %s' % branch_id_alias(stacked_on)).encode(
+                'UTF-8'),
             control_file.strip())
 
     def test_can_open_product_control_dir(self):
diff --git a/lib/lp/coop/answersbugs/visibility.py b/lib/lp/coop/answersbugs/visibility.py
index 87709f0..02cbb89 100644
--- a/lib/lp/coop/answersbugs/visibility.py
+++ b/lib/lp/coop/answersbugs/visibility.py
@@ -10,6 +10,7 @@ __all__ = [
     'TestMessageVisibilityMixin',
     ]
 
+import six
 
 from lp.services.webapp.escaping import html_escape
 from lp.testing.pages import find_tag_by_id
@@ -18,7 +19,7 @@ from lp.testing.pages import find_tag_by_id
 class TestMessageVisibilityMixin:
 
     comment_text = "You can't see me."
-    html_comment_text = html_escape(comment_text).encode('utf-8')
+    html_comment_text = six.ensure_str(html_escape(comment_text))
 
     def makeHiddenMessage(self):
         """To be overwridden by subclasses.
diff --git a/lib/lp/oci/tests/test_ocirecipe.py b/lib/lp/oci/tests/test_ocirecipe.py
index 5335baa..2879b37 100644
--- a/lib/lp/oci/tests/test_ocirecipe.py
+++ b/lib/lp/oci/tests/test_ocirecipe.py
@@ -8,6 +8,7 @@ from __future__ import absolute_import, print_function, unicode_literals
 import json
 
 from fixtures import FakeLogger
+import six
 from six import string_types
 from storm.exceptions import LostObjectError
 from storm.store import Store
@@ -738,7 +739,7 @@ class TestOCIRecipe(OCIConfigHelperMixin, TestCaseWithFactory):
         # Makes sure we only store one level of key=pair, flattening to
         # string every value.
         args = {
-            "VAR1": {b"something": [1, 2, 3]},
+            "VAR1": {six.ensure_str("something"): [1, 2, 3]},
             "VAR2": 123,
             "VAR3": "A string",
         }
diff --git a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
index 5cf9d08..29a18e1 100644
--- a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
+++ b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
@@ -20,6 +20,7 @@ import fixtures
 from fixtures import MockPatch
 from pymacaroons import Macaroon
 import pytz
+import six
 from six.moves.urllib_parse import urlsplit
 from testtools import ExpectedException
 from testtools.matchers import (
@@ -662,7 +663,7 @@ class TestHandleStatusForOCIRecipeBuild(MakeOCIBuildMixin,
     def _createTestFile(self, name, content, hash):
         path = os.path.join(self.test_files_dir, name)
         with open(path, 'wb') as fp:
-            fp.write(content)
+            fp.write(six.ensure_binary(content))
         self.slave.valid_files[hash] = path
 
     def setUp(self):
diff --git a/lib/lp/registry/tests/test_mailinglist.py b/lib/lp/registry/tests/test_mailinglist.py
index a5ce917..f29d036 100644
--- a/lib/lp/registry/tests/test_mailinglist.py
+++ b/lib/lp/registry/tests/test_mailinglist.py
@@ -8,6 +8,7 @@ __all__ = []
 
 from textwrap import dedent
 
+import six
 from testtools.matchers import Equals
 import transaction
 from zope.component import getUtility
@@ -828,7 +829,7 @@ class MessageApprovalTestCase(MailingListMessageTestCase):
         finally:
             held_message.posted_message.close()
         self.assertTextMatchesExpressionIgnoreWhitespace(
-            '.*Message-ID: <first-post>.*', text)
+            '.*Message-ID: <first-post>.*', six.ensure_text(text))
 
     def test_approve(self):
         test_objects = self.makeMailingListAndHeldMessage()
diff --git a/lib/lp/registry/tests/test_product.py b/lib/lp/registry/tests/test_product.py
index bc4f5fd..bd40296 100644
--- a/lib/lp/registry/tests/test_product.py
+++ b/lib/lp/registry/tests/test_product.py
@@ -1419,7 +1419,7 @@ class TestProductFiles(TestCase):
     def test_adddownloadfile_nonascii_filename(self):
         """Test uploading a file with a non-ascii char in the filename."""
         firefox_owner = setupBrowser(auth='Basic mark@xxxxxxxxxxx:test')
-        filename = u'foo\xa5.txt'.encode('utf-8')
+        filename = u'foo\xa5.txt'
         firefox_owner.open(
             'http://launchpad.test/firefox/1.0/1.0.0/+adddownloadfile')
         foo_file = BytesIO(b'Foo installer package...')
diff --git a/lib/lp/scripts/tests/test_garbo.py b/lib/lp/scripts/tests/test_garbo.py
index f7a995f..03ccdd2 100644
--- a/lib/lp/scripts/tests/test_garbo.py
+++ b/lib/lp/scripts/tests/test_garbo.py
@@ -1308,7 +1308,7 @@ class TestGarbo(FakeAdapterMixin, TestCaseWithFactory):
         naked_bug.heat_last_updated = old_update
         IMasterStore(FeatureFlag).add(FeatureFlag(
             'default', 0, 'bugs.heat_updates.cutoff',
-            cutoff.isoformat().decode('ascii')))
+            six.ensure_text(cutoff.isoformat())))
         transaction.commit()
         self.assertEqual(old_update, naked_bug.heat_last_updated)
         self.runHourly()
diff --git a/lib/lp/services/tests/test_osutils.py b/lib/lp/services/tests/test_osutils.py
index 8429c33..bbf4300 100644
--- a/lib/lp/services/tests/test_osutils.py
+++ b/lib/lp/services/tests/test_osutils.py
@@ -106,8 +106,8 @@ class TestWriteFile(TestCase):
     def test_write_file(self):
         directory = self.makeTemporaryDirectory()
         filename = os.path.join(directory, 'filename')
-        content = self.getUniqueString()
-        write_file(filename, content)
+        content = self.factory.getUniqueUnicode()
+        write_file(filename, content.encode('UTF-8'))
         self.assertThat(filename, FileContains(content))
 
 
diff --git a/lib/lp/snappy/tests/test_snap.py b/lib/lp/snappy/tests/test_snap.py
index cfb995c..feb0238 100644
--- a/lib/lp/snappy/tests/test_snap.py
+++ b/lib/lp/snappy/tests/test_snap.py
@@ -3460,7 +3460,8 @@ class TestSnapWebservice(TestCaseWithFactory):
             self.assertThat(snap.store_secrets, MatchesDict({
                 "root": Equals(root_macaroon.serialize()),
                 "discharge_encrypted": AfterPreprocessing(
-                    container.decrypt, Equals(discharge_macaroon.serialize())),
+                    lambda data: container.decrypt(data).decode("UTF-8"),
+                    Equals(discharge_macaroon.serialize())),
                 }))
 
     def makeBuildableDistroArchSeries(self, **kwargs):
diff --git a/lib/lp/snappy/tests/test_snapbuildbehaviour.py b/lib/lp/snappy/tests/test_snapbuildbehaviour.py
index 54af8f3..dceaa04 100644
--- a/lib/lp/snappy/tests/test_snapbuildbehaviour.py
+++ b/lib/lp/snappy/tests/test_snapbuildbehaviour.py
@@ -137,10 +137,10 @@ class InProcessAuthServerFixture(fixtures.Fixture, xmlrpc.XMLRPC):
     def _setUp(self):
         listener = reactor.listenTCP(0, server.Site(InProcessAuthServer()))
         self.addCleanup(listener.stopListening)
-        config.push("in-process-auth-server-fixture", (dedent("""
+        config.push("in-process-auth-server-fixture", dedent("""
             [builddmaster]
             authentication_endpoint: http://localhost:%d/
-            """) % listener.getHost().port).encode("UTF-8"))
+            """) % listener.getHost().port)
         self.addCleanup(config.pop, "in-process-auth-server-fixture")
 
 
diff --git a/lib/lp/soyuz/browser/tests/test_archive_webservice.py b/lib/lp/soyuz/browser/tests/test_archive_webservice.py
index 68e7d64..a0511d9 100644
--- a/lib/lp/soyuz/browser/tests/test_archive_webservice.py
+++ b/lib/lp/soyuz/browser/tests/test_archive_webservice.py
@@ -177,7 +177,7 @@ class TestSigningKey(TestCaseWithFactory):
         self.assertEqual(fingerprint, ws_archive["signing_key_fingerprint"])
         response = ws.named_get(archive_url, "getSigningKeyData")
         self.assertEqual(200, response.status)
-        self.assertEqual(public_key_data, response.jsonBody())
+        self.assertEqual(public_key_data.decode("ASCII"), response.jsonBody())
 
     @responses.activate
     def test_signing_key_private_subscriber(self):
@@ -195,7 +195,7 @@ class TestSigningKey(TestCaseWithFactory):
         self.assertEqual(fingerprint, ws_archive["signing_key_fingerprint"])
         response = ws.named_get(archive_url, "getSigningKeyData")
         self.assertEqual(200, response.status)
-        self.assertEqual(public_key_data, response.jsonBody())
+        self.assertEqual(public_key_data.decode("ASCII"), response.jsonBody())
 
     @responses.activate
     def test_signing_key_private_non_subscriber(self):
diff --git a/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py b/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
index 176449b..2a89f35 100644
--- a/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
+++ b/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
@@ -337,12 +337,13 @@ class TestDistroArchSeriesWebservice(TestCaseWithFactory):
         e = self.assertRaises(
             BadRequest, ws_das.setSourceFilter,
             packageset=packageset_url, sense="Include")
-        self.assertEqual(
+        expected_error = (
             "The requested package set is for %s and cannot be set as a "
             "filter for %s %s." % (
                 packageset.distroseries.fullseriesname,
-                das.distroseries.fullseriesname, das.architecturetag),
-            e.content)
+                das.distroseries.fullseriesname,
+                das.architecturetag))
+        self.assertEqual(expected_error.encode("UTF-8"), e.content)
 
     def test_setSourceFilter_removeSourceFilter(self):
         das = self.factory.makeDistroArchSeries()
diff --git a/lib/lp/testing/tests/test_layers_functional.py b/lib/lp/testing/tests/test_layers_functional.py
index 987a935..02f5bc5 100644
--- a/lib/lp/testing/tests/test_layers_functional.py
+++ b/lib/lp/testing/tests/test_layers_functional.py
@@ -22,6 +22,7 @@ from fixtures import (
     Fixture,
     TestWithFixtures,
     )
+import six
 from six.moves.urllib.error import HTTPError
 from six.moves.urllib.request import urlopen
 from zope.component import (
@@ -343,12 +344,12 @@ class LibrarianResetTestCase(TestCase):
         # loaded to work.
         client = LibrarianClient()
         LibrarianTestCase.url = client.remoteAddFile(
-                self.sample_data, len(self.sample_data),
-                io.BytesIO(self.sample_data), 'text/plain'
-                )
+            six.ensure_str(self.sample_data), len(self.sample_data),
+            io.BytesIO(self.sample_data), 'text/plain'
+            )
         self.assertEqual(
-                urlopen(LibrarianTestCase.url).read(), self.sample_data
-                )
+            urlopen(LibrarianTestCase.url).read(), self.sample_data
+            )
         # Perform the librarian specific between-test code:
         LibrarianLayer.testTearDown()
         LibrarianLayer.testSetUp()
@@ -485,9 +486,9 @@ class LayerProcessControllerInvariantsTestCase(BaseTestCase):
     def testAppServerIsAvailable(self):
         # Test that the app server is up and running.
         mainsite = LayerProcessController.appserver_config.vhost.mainsite
-        home_page = urlopen(mainsite.rooturl).read()
-        self.assertTrue(
-            'Is your project registered yet?' in home_page,
+        home_page = six.ensure_text(urlopen(mainsite.rooturl).read())
+        self.assertIn(
+            'Is your project registered yet?', home_page,
             "Home page couldn't be retrieved:\n%s" % home_page)
 
     def testStartingAppServerTwiceRaisesInvariantError(self):
diff --git a/lib/lp/translations/scripts/tests/test_translations_to_branch.py b/lib/lp/translations/scripts/tests/test_translations_to_branch.py
index b0f9b45..492b505 100644
--- a/lib/lp/translations/scripts/tests/test_translations_to_branch.py
+++ b/lib/lp/translations/scripts/tests/test_translations_to_branch.py
@@ -127,8 +127,8 @@ class TestExportTranslationsToBranch(TestCaseWithFactory):
         self.assertEqual(set(), missing_filenames)
 
         for filename, expected in six.iteritems(expected_contents):
-            contents = branch_contents[filename].lstrip('\n')
-            pattern = dedent(expected.lstrip('\n'))
+            contents = branch_contents[filename].lstrip(b'\n')
+            pattern = dedent(expected.lstrip('\n')).encode('UTF-8')
             if not re.match(pattern, contents, re.MULTILINE):
                 self.assertEqual(pattern, contents)