← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-branch-rewrite-tests into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-branch-rewrite-tests into launchpad:master.

Commit message:
Fix branch-rewrite tests for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/395902
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-branch-rewrite-tests into launchpad:master.
diff --git a/lib/lp/codehosting/tests/test_rewrite.py b/lib/lp/codehosting/tests/test_rewrite.py
index 16be709..9daa088 100644
--- a/lib/lp/codehosting/tests/test_rewrite.py
+++ b/lib/lp/codehosting/tests/test_rewrite.py
@@ -284,12 +284,13 @@ class TestBranchRewriterScript(TestCaseWithFactory):
             config.root, 'scripts', 'branch-rewrite.py')
         proc = subprocess.Popen(
             [script_file], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
-            stderr=subprocess.PIPE, bufsize=0)
+            stderr=subprocess.PIPE, bufsize=0, universal_newlines=True)
         output_lines = []
         # For each complete line of input, the script should, without
         # buffering, write a complete line of output.
         for input_line in input_lines:
             proc.stdin.write(input_line + '\n')
+            proc.stdin.flush()
             output_lines.append(
                 nonblocking_readline(proc.stdout, 60).rstrip('\n'))
         # If we create a new branch after the branch-rewrite.py script has
@@ -305,6 +306,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
             'file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README'
             % branch_id_to_path(new_branch.id))
         proc.stdin.write(new_branch_input + '\n')
+        proc.stdin.flush()
         output_lines.append(
             nonblocking_readline(proc.stdout, 60).rstrip('\n'))
 
@@ -313,6 +315,7 @@ class TestBranchRewriterScript(TestCaseWithFactory):
             'file:///var/tmp/bazaar.launchpad.test/mirrors/%s/.bzr/README'
             % branch_id_to_path(edited_branch.id))
         proc.stdin.write(edited_branch_input + '\n')
+        proc.stdin.flush()
         output_lines.append(
             nonblocking_readline(proc.stdout, 60).rstrip('\n'))
 
@@ -333,7 +336,7 @@ class TestBranchRewriterScriptHandlesDisconnects(TestCase):
 
         self.rewriter_proc = subprocess.Popen(
             [script_file], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
-            stderr=subprocess.PIPE, bufsize=0)
+            stderr=subprocess.PIPE, bufsize=0, universal_newlines=True)
 
         self.addCleanup(self.rewriter_proc.terminate)
 
diff --git a/lib/lp/testing/__init__.py b/lib/lp/testing/__init__.py
index 55759e8..b63632a 100644
--- a/lib/lp/testing/__init__.py
+++ b/lib/lp/testing/__init__.py
@@ -62,6 +62,7 @@ from datetime import (
     )
 from fnmatch import fnmatchcase
 from functools import partial
+import io
 import logging
 import os
 import re
@@ -1491,20 +1492,20 @@ def nonblocking_readline(instream, timeout):
     Files must provide a valid fileno() method. This is a test helper
     as it is inefficient and unlikely useful for production code.
     """
-    result = six.StringIO()
+    result = io.BytesIO()
     start = now = time.time()
     deadline = start + timeout
-    while (now < deadline and not result.getvalue().endswith('\n')):
+    while (now < deadline and not result.getvalue().endswith(b'\n')):
         rlist = select([instream], [], [], deadline - now)
         if rlist:
             # Reading 1 character at a time is inefficient, but means
             # we don't need to implement put-back.
             next_char = os.read(instream.fileno(), 1)
-            if next_char == "":
+            if next_char == b"":
                 break  # EOF
             result.write(next_char)
         now = time.time()
-    return result.getvalue()
+    return six.ensure_str(result.getvalue())
 
 
 class FakeLaunchpadRequest(FakeRequest):
diff --git a/scripts/branch-rewrite.py b/scripts/branch-rewrite.py
index 2583d0c..1e00e69 100755
--- a/scripts/branch-rewrite.py
+++ b/scripts/branch-rewrite.py
@@ -60,6 +60,7 @@ class BranchRewriteScript(LaunchpadScript):
                 # Mod-rewrite always gives us a newline terminated string.
                 if line:
                     print(rewriter.rewriteLine(line.strip()))
+                    sys.stdout.flush()
                 else:
                     # Standard input has been closed, so die.
                     return
@@ -68,6 +69,7 @@ class BranchRewriteScript(LaunchpadScript):
             except Exception:
                 self.logger.exception('Exception occurred:')
                 print("NULL")
+                sys.stdout.flush()
                 # The exception might have been a DisconnectionError or
                 # similar. Cleanup such as database reconnection will
                 # not happen until the transaction is rolled back.