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

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 1d8fa00fb0ef81538687ce7fd5ada1fbbe4e9594
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-unittest-bytes-text
Merge into: launchpad:master
Diff against target: 327 lines (+44/-32)
16 files modified
lib/lp/archivepublisher/tests/test_pool.py (+1/-1)
lib/lp/archiveuploader/tests/test_ocirecipeupload.py (+8/-4)
lib/lp/codehosting/vfs/tests/test_filesystem.py (+2/-1)
lib/lp/coop/answersbugs/visibility.py (+2/-1)
lib/lp/oci/tests/test_ocirecipe.py (+2/-1)
lib/lp/oci/tests/test_ocirecipebuildbehaviour.py (+2/-1)
lib/lp/registry/tests/test_mailinglist.py (+2/-1)
lib/lp/registry/tests/test_product.py (+1/-1)
lib/lp/scripts/tests/test_garbo.py (+1/-1)
lib/lp/services/tests/test_osutils.py (+2/-2)
lib/lp/snappy/tests/test_snap.py (+2/-1)
lib/lp/snappy/tests/test_snapbuildbehaviour.py (+2/-2)
lib/lp/soyuz/browser/tests/test_archive_webservice.py (+2/-2)
lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py (+4/-3)
lib/lp/testing/tests/test_layers_functional.py (+9/-8)
lib/lp/translations/scripts/tests/test_translations_to_branch.py (+2/-2)
Reviewer Review Type Date Requested Status
Cristian Gonzalez (community) Approve
Review via email: mp+398879@code.launchpad.net

Commit message

Fix various simple bytes/text issues in unit tests

To post a comment you must log in.
Revision history for this message
Cristian Gonzalez (cristiangsp) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/archivepublisher/tests/test_pool.py b/lib/lp/archivepublisher/tests/test_pool.py
2index 7c60fd9..e56ef02 100644
3--- a/lib/lp/archivepublisher/tests/test_pool.py
4+++ b/lib/lp/archivepublisher/tests/test_pool.py
5@@ -44,7 +44,7 @@ class PoolTestingFile:
6 self.pool = pool
7 self.sourcename = sourcename
8 self.filename = filename
9- self.contents = sourcename
10+ self.contents = sourcename.encode("UTF-8")
11
12 def addToPool(self, component):
13 return self.pool.addFile(
14diff --git a/lib/lp/archiveuploader/tests/test_ocirecipeupload.py b/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
15index 13b577b..b36d2c6 100644
16--- a/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
17+++ b/lib/lp/archiveuploader/tests/test_ocirecipeupload.py
18@@ -67,7 +67,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
19 write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
20 write_file(os.path.join(upload_dir, "layer_2.tar.gz"), b"layer_2")
21 write_file(
22- os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
23+ os.path.join(upload_dir, "digests.json"),
24+ json.dumps(self.digests).encode("UTF-8"))
25 write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
26 handler = UploadHandler.forProcessor(
27 self.uploadprocessor, self.incoming_folder, "test", self.build)
28@@ -101,7 +102,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
29 write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
30 write_file(os.path.join(upload_dir, "layer_2.tar.gz"), b"layer_2")
31 write_file(
32- os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
33+ os.path.join(upload_dir, "digests.json"),
34+ json.dumps(self.digests).encode("UTF-8"))
35 write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
36 handler = UploadHandler.forProcessor(
37 self.uploadprocessor, self.incoming_folder, "test", build)
38@@ -139,7 +141,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
39 self.incoming_folder, "test", str(self.build.id), "ubuntu")
40 write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
41 write_file(
42- os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
43+ os.path.join(upload_dir, "digests.json"),
44+ json.dumps(self.digests).encode("UTF-8"))
45 handler = UploadHandler.forProcessor(
46 self.uploadprocessor, self.incoming_folder, "test", self.build)
47 result = handler.processOCIRecipe(self.log)
48@@ -160,7 +163,8 @@ class TestOCIRecipeUploads(OCIConfigHelperMixin, TestUploadProcessorBase):
49 write_file(os.path.join(upload_dir, "layer_1.tar.gz"), b"layer_1")
50 write_file(os.path.join(upload_dir, "manifest.json"), b"manifest")
51 write_file(
52- os.path.join(upload_dir, "digests.json"), json.dumps(self.digests))
53+ os.path.join(upload_dir, "digests.json"),
54+ json.dumps(self.digests).encode("UTF-8"))
55
56 # create the existing file
57 self.switchToAdmin()
58diff --git a/lib/lp/codehosting/vfs/tests/test_filesystem.py b/lib/lp/codehosting/vfs/tests/test_filesystem.py
59index abf5197..3f955d6 100644
60--- a/lib/lp/codehosting/vfs/tests/test_filesystem.py
61+++ b/lib/lp/codehosting/vfs/tests/test_filesystem.py
62@@ -143,7 +143,8 @@ class TestFilesystem(TestCaseWithTransport):
63 % (self.requester.name, product.name))
64 stacked_on = IBranchTarget(product).default_stacked_on_branch
65 self.assertEqual(
66- 'default_stack_on = %s' % branch_id_alias(stacked_on),
67+ ('default_stack_on = %s' % branch_id_alias(stacked_on)).encode(
68+ 'UTF-8'),
69 control_file.strip())
70
71 def test_can_open_product_control_dir(self):
72diff --git a/lib/lp/coop/answersbugs/visibility.py b/lib/lp/coop/answersbugs/visibility.py
73index 87709f0..02cbb89 100644
74--- a/lib/lp/coop/answersbugs/visibility.py
75+++ b/lib/lp/coop/answersbugs/visibility.py
76@@ -10,6 +10,7 @@ __all__ = [
77 'TestMessageVisibilityMixin',
78 ]
79
80+import six
81
82 from lp.services.webapp.escaping import html_escape
83 from lp.testing.pages import find_tag_by_id
84@@ -18,7 +19,7 @@ from lp.testing.pages import find_tag_by_id
85 class TestMessageVisibilityMixin:
86
87 comment_text = "You can't see me."
88- html_comment_text = html_escape(comment_text).encode('utf-8')
89+ html_comment_text = six.ensure_str(html_escape(comment_text))
90
91 def makeHiddenMessage(self):
92 """To be overwridden by subclasses.
93diff --git a/lib/lp/oci/tests/test_ocirecipe.py b/lib/lp/oci/tests/test_ocirecipe.py
94index 5335baa..2879b37 100644
95--- a/lib/lp/oci/tests/test_ocirecipe.py
96+++ b/lib/lp/oci/tests/test_ocirecipe.py
97@@ -8,6 +8,7 @@ from __future__ import absolute_import, print_function, unicode_literals
98 import json
99
100 from fixtures import FakeLogger
101+import six
102 from six import string_types
103 from storm.exceptions import LostObjectError
104 from storm.store import Store
105@@ -738,7 +739,7 @@ class TestOCIRecipe(OCIConfigHelperMixin, TestCaseWithFactory):
106 # Makes sure we only store one level of key=pair, flattening to
107 # string every value.
108 args = {
109- "VAR1": {b"something": [1, 2, 3]},
110+ "VAR1": {six.ensure_str("something"): [1, 2, 3]},
111 "VAR2": 123,
112 "VAR3": "A string",
113 }
114diff --git a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
115index 5cf9d08..29a18e1 100644
116--- a/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
117+++ b/lib/lp/oci/tests/test_ocirecipebuildbehaviour.py
118@@ -20,6 +20,7 @@ import fixtures
119 from fixtures import MockPatch
120 from pymacaroons import Macaroon
121 import pytz
122+import six
123 from six.moves.urllib_parse import urlsplit
124 from testtools import ExpectedException
125 from testtools.matchers import (
126@@ -662,7 +663,7 @@ class TestHandleStatusForOCIRecipeBuild(MakeOCIBuildMixin,
127 def _createTestFile(self, name, content, hash):
128 path = os.path.join(self.test_files_dir, name)
129 with open(path, 'wb') as fp:
130- fp.write(content)
131+ fp.write(six.ensure_binary(content))
132 self.slave.valid_files[hash] = path
133
134 def setUp(self):
135diff --git a/lib/lp/registry/tests/test_mailinglist.py b/lib/lp/registry/tests/test_mailinglist.py
136index a5ce917..f29d036 100644
137--- a/lib/lp/registry/tests/test_mailinglist.py
138+++ b/lib/lp/registry/tests/test_mailinglist.py
139@@ -8,6 +8,7 @@ __all__ = []
140
141 from textwrap import dedent
142
143+import six
144 from testtools.matchers import Equals
145 import transaction
146 from zope.component import getUtility
147@@ -828,7 +829,7 @@ class MessageApprovalTestCase(MailingListMessageTestCase):
148 finally:
149 held_message.posted_message.close()
150 self.assertTextMatchesExpressionIgnoreWhitespace(
151- '.*Message-ID: <first-post>.*', text)
152+ '.*Message-ID: <first-post>.*', six.ensure_text(text))
153
154 def test_approve(self):
155 test_objects = self.makeMailingListAndHeldMessage()
156diff --git a/lib/lp/registry/tests/test_product.py b/lib/lp/registry/tests/test_product.py
157index bc4f5fd..bd40296 100644
158--- a/lib/lp/registry/tests/test_product.py
159+++ b/lib/lp/registry/tests/test_product.py
160@@ -1419,7 +1419,7 @@ class TestProductFiles(TestCase):
161 def test_adddownloadfile_nonascii_filename(self):
162 """Test uploading a file with a non-ascii char in the filename."""
163 firefox_owner = setupBrowser(auth='Basic mark@example.com:test')
164- filename = u'foo\xa5.txt'.encode('utf-8')
165+ filename = u'foo\xa5.txt'
166 firefox_owner.open(
167 'http://launchpad.test/firefox/1.0/1.0.0/+adddownloadfile')
168 foo_file = BytesIO(b'Foo installer package...')
169diff --git a/lib/lp/scripts/tests/test_garbo.py b/lib/lp/scripts/tests/test_garbo.py
170index f7a995f..03ccdd2 100644
171--- a/lib/lp/scripts/tests/test_garbo.py
172+++ b/lib/lp/scripts/tests/test_garbo.py
173@@ -1308,7 +1308,7 @@ class TestGarbo(FakeAdapterMixin, TestCaseWithFactory):
174 naked_bug.heat_last_updated = old_update
175 IMasterStore(FeatureFlag).add(FeatureFlag(
176 'default', 0, 'bugs.heat_updates.cutoff',
177- cutoff.isoformat().decode('ascii')))
178+ six.ensure_text(cutoff.isoformat())))
179 transaction.commit()
180 self.assertEqual(old_update, naked_bug.heat_last_updated)
181 self.runHourly()
182diff --git a/lib/lp/services/tests/test_osutils.py b/lib/lp/services/tests/test_osutils.py
183index 8429c33..bbf4300 100644
184--- a/lib/lp/services/tests/test_osutils.py
185+++ b/lib/lp/services/tests/test_osutils.py
186@@ -106,8 +106,8 @@ class TestWriteFile(TestCase):
187 def test_write_file(self):
188 directory = self.makeTemporaryDirectory()
189 filename = os.path.join(directory, 'filename')
190- content = self.getUniqueString()
191- write_file(filename, content)
192+ content = self.factory.getUniqueUnicode()
193+ write_file(filename, content.encode('UTF-8'))
194 self.assertThat(filename, FileContains(content))
195
196
197diff --git a/lib/lp/snappy/tests/test_snap.py b/lib/lp/snappy/tests/test_snap.py
198index cfb995c..feb0238 100644
199--- a/lib/lp/snappy/tests/test_snap.py
200+++ b/lib/lp/snappy/tests/test_snap.py
201@@ -3460,7 +3460,8 @@ class TestSnapWebservice(TestCaseWithFactory):
202 self.assertThat(snap.store_secrets, MatchesDict({
203 "root": Equals(root_macaroon.serialize()),
204 "discharge_encrypted": AfterPreprocessing(
205- container.decrypt, Equals(discharge_macaroon.serialize())),
206+ lambda data: container.decrypt(data).decode("UTF-8"),
207+ Equals(discharge_macaroon.serialize())),
208 }))
209
210 def makeBuildableDistroArchSeries(self, **kwargs):
211diff --git a/lib/lp/snappy/tests/test_snapbuildbehaviour.py b/lib/lp/snappy/tests/test_snapbuildbehaviour.py
212index 54af8f3..dceaa04 100644
213--- a/lib/lp/snappy/tests/test_snapbuildbehaviour.py
214+++ b/lib/lp/snappy/tests/test_snapbuildbehaviour.py
215@@ -137,10 +137,10 @@ class InProcessAuthServerFixture(fixtures.Fixture, xmlrpc.XMLRPC):
216 def _setUp(self):
217 listener = reactor.listenTCP(0, server.Site(InProcessAuthServer()))
218 self.addCleanup(listener.stopListening)
219- config.push("in-process-auth-server-fixture", (dedent("""
220+ config.push("in-process-auth-server-fixture", dedent("""
221 [builddmaster]
222 authentication_endpoint: http://localhost:%d/
223- """) % listener.getHost().port).encode("UTF-8"))
224+ """) % listener.getHost().port)
225 self.addCleanup(config.pop, "in-process-auth-server-fixture")
226
227
228diff --git a/lib/lp/soyuz/browser/tests/test_archive_webservice.py b/lib/lp/soyuz/browser/tests/test_archive_webservice.py
229index 68e7d64..a0511d9 100644
230--- a/lib/lp/soyuz/browser/tests/test_archive_webservice.py
231+++ b/lib/lp/soyuz/browser/tests/test_archive_webservice.py
232@@ -177,7 +177,7 @@ class TestSigningKey(TestCaseWithFactory):
233 self.assertEqual(fingerprint, ws_archive["signing_key_fingerprint"])
234 response = ws.named_get(archive_url, "getSigningKeyData")
235 self.assertEqual(200, response.status)
236- self.assertEqual(public_key_data, response.jsonBody())
237+ self.assertEqual(public_key_data.decode("ASCII"), response.jsonBody())
238
239 @responses.activate
240 def test_signing_key_private_subscriber(self):
241@@ -195,7 +195,7 @@ class TestSigningKey(TestCaseWithFactory):
242 self.assertEqual(fingerprint, ws_archive["signing_key_fingerprint"])
243 response = ws.named_get(archive_url, "getSigningKeyData")
244 self.assertEqual(200, response.status)
245- self.assertEqual(public_key_data, response.jsonBody())
246+ self.assertEqual(public_key_data.decode("ASCII"), response.jsonBody())
247
248 @responses.activate
249 def test_signing_key_private_non_subscriber(self):
250diff --git a/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py b/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
251index 176449b..2a89f35 100644
252--- a/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
253+++ b/lib/lp/soyuz/browser/tests/test_distroarchseries_webservice.py
254@@ -337,12 +337,13 @@ class TestDistroArchSeriesWebservice(TestCaseWithFactory):
255 e = self.assertRaises(
256 BadRequest, ws_das.setSourceFilter,
257 packageset=packageset_url, sense="Include")
258- self.assertEqual(
259+ expected_error = (
260 "The requested package set is for %s and cannot be set as a "
261 "filter for %s %s." % (
262 packageset.distroseries.fullseriesname,
263- das.distroseries.fullseriesname, das.architecturetag),
264- e.content)
265+ das.distroseries.fullseriesname,
266+ das.architecturetag))
267+ self.assertEqual(expected_error.encode("UTF-8"), e.content)
268
269 def test_setSourceFilter_removeSourceFilter(self):
270 das = self.factory.makeDistroArchSeries()
271diff --git a/lib/lp/testing/tests/test_layers_functional.py b/lib/lp/testing/tests/test_layers_functional.py
272index 987a935..02f5bc5 100644
273--- a/lib/lp/testing/tests/test_layers_functional.py
274+++ b/lib/lp/testing/tests/test_layers_functional.py
275@@ -22,6 +22,7 @@ from fixtures import (
276 Fixture,
277 TestWithFixtures,
278 )
279+import six
280 from six.moves.urllib.error import HTTPError
281 from six.moves.urllib.request import urlopen
282 from zope.component import (
283@@ -343,12 +344,12 @@ class LibrarianResetTestCase(TestCase):
284 # loaded to work.
285 client = LibrarianClient()
286 LibrarianTestCase.url = client.remoteAddFile(
287- self.sample_data, len(self.sample_data),
288- io.BytesIO(self.sample_data), 'text/plain'
289- )
290+ six.ensure_str(self.sample_data), len(self.sample_data),
291+ io.BytesIO(self.sample_data), 'text/plain'
292+ )
293 self.assertEqual(
294- urlopen(LibrarianTestCase.url).read(), self.sample_data
295- )
296+ urlopen(LibrarianTestCase.url).read(), self.sample_data
297+ )
298 # Perform the librarian specific between-test code:
299 LibrarianLayer.testTearDown()
300 LibrarianLayer.testSetUp()
301@@ -485,9 +486,9 @@ class LayerProcessControllerInvariantsTestCase(BaseTestCase):
302 def testAppServerIsAvailable(self):
303 # Test that the app server is up and running.
304 mainsite = LayerProcessController.appserver_config.vhost.mainsite
305- home_page = urlopen(mainsite.rooturl).read()
306- self.assertTrue(
307- 'Is your project registered yet?' in home_page,
308+ home_page = six.ensure_text(urlopen(mainsite.rooturl).read())
309+ self.assertIn(
310+ 'Is your project registered yet?', home_page,
311 "Home page couldn't be retrieved:\n%s" % home_page)
312
313 def testStartingAppServerTwiceRaisesInvariantError(self):
314diff --git a/lib/lp/translations/scripts/tests/test_translations_to_branch.py b/lib/lp/translations/scripts/tests/test_translations_to_branch.py
315index b0f9b45..492b505 100644
316--- a/lib/lp/translations/scripts/tests/test_translations_to_branch.py
317+++ b/lib/lp/translations/scripts/tests/test_translations_to_branch.py
318@@ -127,8 +127,8 @@ class TestExportTranslationsToBranch(TestCaseWithFactory):
319 self.assertEqual(set(), missing_filenames)
320
321 for filename, expected in six.iteritems(expected_contents):
322- contents = branch_contents[filename].lstrip('\n')
323- pattern = dedent(expected.lstrip('\n'))
324+ contents = branch_contents[filename].lstrip(b'\n')
325+ pattern = dedent(expected.lstrip('\n')).encode('UTF-8')
326 if not re.match(pattern, contents, re.MULTILINE):
327 self.assertEqual(pattern, contents)
328

Subscribers

People subscribed via source and target branches

to status/vote changes: