Commit 6f661161 authored by Edward Lemur's avatar Edward Lemur Committed by Commit Bot

git-drover: Fix tests to run on Python 3.

Bug: 1009809
Change-Id: I5f453c7e019376e502716ab2e9891a97663d1f28
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1838254
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
parent ab51754c
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import cPickle
import functools import functools
import logging import logging
import os import os
...@@ -19,6 +18,10 @@ import tempfile ...@@ -19,6 +18,10 @@ import tempfile
import git_common import git_common
if sys.version_info.major == 2:
import cPickle
else:
import pickle as cPickle
class Error(Exception): class Error(Exception):
pass pass
...@@ -67,6 +70,12 @@ else: ...@@ -67,6 +70,12 @@ else:
mk_symlink = os.symlink mk_symlink = os.symlink
def _raw_input(message):
# Use this so that it can be mocked in tests on Python 2 and 3.
if sys.version_info.major == 2:
return raw_input(message)
return input(message)
class _Drover(object): class _Drover(object):
def __init__(self, branch, revision, parent_repo, dry_run, verbose): def __init__(self, branch, revision, parent_repo, dry_run, verbose):
...@@ -183,7 +192,7 @@ class _Drover(object): ...@@ -183,7 +192,7 @@ class _Drover(object):
result = '' result = ''
while result not in ('y', 'n'): while result not in ('y', 'n'):
try: try:
result = raw_input('%s Continue (y/n)? ' % message) result = _raw_input('%s Continue (y/n)? ' % message)
except EOFError: except EOFError:
result = 'n' result = 'n'
return result == 'y' return result == 'y'
...@@ -268,9 +277,9 @@ class _Drover(object): ...@@ -268,9 +277,9 @@ class _Drover(object):
['-c', 'core.quotePath=false', 'status', '--porcelain']).splitlines() ['-c', 'core.quotePath=false', 'status', '--porcelain']).splitlines()
extra_files = [f[3:] for f in repo_status if f[:2] == ' D'] extra_files = [f[3:] for f in repo_status if f[:2] == ' D']
if extra_files: if extra_files:
stdin = '\n'.join(extra_files) + '\n'
self._run_git_command_with_stdin( self._run_git_command_with_stdin(
['update-index', '--skip-worktree', '--stdin'], ['update-index', '--skip-worktree', '--stdin'], stdin=stdin.encode())
stdin='\n'.join(extra_files) + '\n')
def _upload_and_land(self): def _upload_and_land(self):
if self._dry_run: if self._dry_run:
......
#!/usr/bin/env python #!/usr/bin/env vpython3
# Copyright 2015 The Chromium Authors. All rights reserved. # Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
...@@ -13,14 +13,15 @@ import unittest ...@@ -13,14 +13,15 @@ import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from testing_support import auto_stub from third_party import mock
import git_drover import git_drover
class GitDroverTest(auto_stub.TestCase): class GitDroverTest(unittest.TestCase):
def setUp(self): def setUp(self):
super(GitDroverTest, self).setUp() super(GitDroverTest, self).setUp()
self.maxDiff = None
self._temp_directory = tempfile.mkdtemp() self._temp_directory = tempfile.mkdtemp()
self._parent_repo = os.path.join(self._temp_directory, 'parent_repo') self._parent_repo = os.path.join(self._temp_directory, 'parent_repo')
self._target_repo = os.path.join(self._temp_directory, 'drover_branch_123') self._target_repo = os.path.join(self._temp_directory, 'drover_branch_123')
...@@ -33,12 +34,14 @@ class GitDroverTest(auto_stub.TestCase): ...@@ -33,12 +34,14 @@ class GitDroverTest(auto_stub.TestCase):
with open( with open(
os.path.join(self._parent_repo, '.git', 'info', 'refs'), 'w') as f: os.path.join(self._parent_repo, '.git', 'info', 'refs'), 'w') as f:
f.write('refs') f.write('refs')
self.mock(tempfile, 'mkdtemp', self._mkdtemp) mock.patch('tempfile.mkdtemp', self._mkdtemp).start()
self.mock(__builtins__, 'raw_input', self._get_input) mock.patch('git_drover._raw_input', self._get_input).start()
self.mock(subprocess, 'check_call', self._check_call) mock.patch('subprocess.check_call', self._check_call).start()
self.mock(subprocess, 'check_output', self._check_call) mock.patch('subprocess.check_output', self._check_call).start()
self.real_popen = subprocess.Popen self.real_popen = subprocess.Popen
self.mock(subprocess, 'Popen', self._Popen) mock.patch('subprocess.Popen', self._Popen).start()
self.addCleanup(mock.patch.stopall)
self._commands = [] self._commands = []
self._input = [] self._input = []
self._fail_on_command = None self._fail_on_command = None
...@@ -131,7 +134,7 @@ class GitDroverTest(auto_stub.TestCase): ...@@ -131,7 +134,7 @@ class GitDroverTest(auto_stub.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.returncode = -999 self.returncode = -999
def communicate(self, stdin): def communicate(self, stdin):
if stdin == 'foo\nbar\n': if stdin == b'foo\nbar\n':
self.returncode = 0 self.returncode = 0
else: else:
self.returncode = 1 self.returncode = 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment