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 @@
from __future__ import print_function
import argparse
import cPickle
import functools
import logging
import os
......@@ -19,6 +18,10 @@ import tempfile
import git_common
if sys.version_info.major == 2:
import cPickle
else:
import pickle as cPickle
class Error(Exception):
pass
......@@ -67,6 +70,12 @@ else:
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):
def __init__(self, branch, revision, parent_repo, dry_run, verbose):
......@@ -183,7 +192,7 @@ class _Drover(object):
result = ''
while result not in ('y', 'n'):
try:
result = raw_input('%s Continue (y/n)? ' % message)
result = _raw_input('%s Continue (y/n)? ' % message)
except EOFError:
result = 'n'
return result == 'y'
......@@ -268,9 +277,9 @@ class _Drover(object):
['-c', 'core.quotePath=false', 'status', '--porcelain']).splitlines()
extra_files = [f[3:] for f in repo_status if f[:2] == ' D']
if extra_files:
stdin = '\n'.join(extra_files) + '\n'
self._run_git_command_with_stdin(
['update-index', '--skip-worktree', '--stdin'],
stdin='\n'.join(extra_files) + '\n')
['update-index', '--skip-worktree', '--stdin'], stdin=stdin.encode())
def _upload_and_land(self):
if self._dry_run:
......
#!/usr/bin/env python
#!/usr/bin/env vpython3
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
......@@ -13,14 +13,15 @@ import unittest
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
class GitDroverTest(auto_stub.TestCase):
class GitDroverTest(unittest.TestCase):
def setUp(self):
super(GitDroverTest, self).setUp()
self.maxDiff = None
self._temp_directory = tempfile.mkdtemp()
self._parent_repo = os.path.join(self._temp_directory, 'parent_repo')
self._target_repo = os.path.join(self._temp_directory, 'drover_branch_123')
......@@ -33,12 +34,14 @@ class GitDroverTest(auto_stub.TestCase):
with open(
os.path.join(self._parent_repo, '.git', 'info', 'refs'), 'w') as f:
f.write('refs')
self.mock(tempfile, 'mkdtemp', self._mkdtemp)
self.mock(__builtins__, 'raw_input', self._get_input)
self.mock(subprocess, 'check_call', self._check_call)
self.mock(subprocess, 'check_output', self._check_call)
mock.patch('tempfile.mkdtemp', self._mkdtemp).start()
mock.patch('git_drover._raw_input', self._get_input).start()
mock.patch('subprocess.check_call', self._check_call).start()
mock.patch('subprocess.check_output', self._check_call).start()
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._input = []
self._fail_on_command = None
......@@ -131,7 +134,7 @@ class GitDroverTest(auto_stub.TestCase):
def __init__(self, *args, **kwargs):
self.returncode = -999
def communicate(self, stdin):
if stdin == 'foo\nbar\n':
if stdin == b'foo\nbar\n':
self.returncode = 0
else:
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