Commit 979fa780 authored by Edward Lemur's avatar Edward Lemur Committed by Commit Bot

depot_tools: Make gclient_scm Python 3 compatible.

Bug: 984182
Change-Id: Ib9c5d2762546f29eaca5eae89b0428431067da4a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1753029
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: 's avatarRobbie Iannucci <iannucci@chromium.org>
parent 4ec0fe31
...@@ -448,7 +448,7 @@ class GitWrapper(SCMWrapper): ...@@ -448,7 +448,7 @@ class GitWrapper(SCMWrapper):
target_rev, patch_rev, base_rev)) target_rev, patch_rev, base_rev))
self.Print('Current dir is %r' % self.checkout_path) self.Print('Current dir is %r' % self.checkout_path)
self.Print('git returned non-zero exit status %s:\n%s' % ( self.Print('git returned non-zero exit status %s:\n%s' % (
e.returncode, e.stderr)) e.returncode, e.stderr.decode('utf-8')))
# Print the current status so that developers know what changes caused # Print the current status so that developers know what changes caused
# the patch failure, since git cherry-pick doesn't show that # the patch failure, since git cherry-pick doesn't show that
# information. # information.
...@@ -553,7 +553,7 @@ class GitWrapper(SCMWrapper): ...@@ -553,7 +553,7 @@ class GitWrapper(SCMWrapper):
files = self._Capture( files = self._Capture(
['-c', 'core.quotePath=false', 'ls-files']).splitlines() ['-c', 'core.quotePath=false', 'ls-files']).splitlines()
file_list.extend( file_list.extend(
[os.path.join(self.checkout_path, f.decode()) for f in files]) [os.path.join(self.checkout_path, f) for f in files])
if mirror: if mirror:
self._Capture( self._Capture(
['remote', 'set-url', '--push', 'origin', mirror.url]) ['remote', 'set-url', '--push', 'origin', mirror.url])
...@@ -761,7 +761,8 @@ class GitWrapper(SCMWrapper): ...@@ -761,7 +761,8 @@ class GitWrapper(SCMWrapper):
merge_output = self._Capture(merge_args) merge_output = self._Capture(merge_args)
except subprocess2.CalledProcessError as e: except subprocess2.CalledProcessError as e:
rebase_files = [] rebase_files = []
if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr): if re.match(b'fatal: Not possible to fast-forward, aborting.',
e.stderr):
if not printed_path: if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision), self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False) timestamp=False)
...@@ -791,19 +792,19 @@ class GitWrapper(SCMWrapper): ...@@ -791,19 +792,19 @@ class GitWrapper(SCMWrapper):
return return
else: else:
self.Print('Input not recognized') self.Print('Input not recognized')
elif re.match("error: Your local changes to '.*' would be " elif re.match(b"error: Your local changes to '.*' would be "
"overwritten by merge. Aborting.\nPlease, commit your " b"overwritten by merge. Aborting.\nPlease, commit your "
"changes or stash them before you can merge.\n", b"changes or stash them before you can merge.\n",
e.stderr): e.stderr):
if not printed_path: if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision), self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False) timestamp=False)
printed_path = True printed_path = True
raise gclient_utils.Error(e.stderr) raise gclient_utils.Error(e.stderr.decode('utf-8'))
else: else:
# Some other problem happened with the merge # Some other problem happened with the merge
logging.error("Error during fast-forward merge in %s!" % self.relpath) logging.error("Error during fast-forward merge in %s!" % self.relpath)
self.Print(e.stderr) self.Print(e.stderr.decode('utf-8'))
raise raise
else: else:
# Fast-forward merge was successful # Fast-forward merge was successful
...@@ -1117,8 +1118,8 @@ class GitWrapper(SCMWrapper): ...@@ -1117,8 +1118,8 @@ class GitWrapper(SCMWrapper):
try: try:
rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path) rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
except subprocess2.CalledProcessError as e: except subprocess2.CalledProcessError as e:
if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or if (re.match(br'cannot rebase: you have unstaged changes', e.stderr) or
re.match(r'cannot rebase: your index contains uncommitted changes', re.match(br'cannot rebase: your index contains uncommitted changes',
e.stderr)): e.stderr)):
while True: while True:
rebase_action = self._AskForData( rebase_action = self._AskForData(
...@@ -1136,18 +1137,19 @@ class GitWrapper(SCMWrapper): ...@@ -1136,18 +1137,19 @@ class GitWrapper(SCMWrapper):
"cd %s && git " % self.checkout_path "cd %s && git " % self.checkout_path
+ "%s" % ' '.join(rebase_cmd)) + "%s" % ' '.join(rebase_cmd))
elif re.match(r'show|s', rebase_action, re.I): elif re.match(r'show|s', rebase_action, re.I):
self.Print('%s' % e.stderr.strip()) self.Print('%s' % e.stderr.decode('utf-8').strip())
continue continue
else: else:
gclient_utils.Error("Input not recognized") gclient_utils.Error("Input not recognized")
continue continue
elif re.search(r'^CONFLICT', e.stdout, re.M): elif re.search(br'^CONFLICT', e.stdout, re.M):
raise gclient_utils.Error("Conflict while rebasing this branch.\n" raise gclient_utils.Error("Conflict while rebasing this branch.\n"
"Fix the conflict and run gclient again.\n" "Fix the conflict and run gclient again.\n"
"See 'man git-rebase' for details.\n") "See 'man git-rebase' for details.\n")
else: else:
self.Print(e.stdout.strip()) self.Print(e.stdout.decode('utf-8').strip())
self.Print('Rebase produced error output:\n%s' % e.stderr.strip()) self.Print('Rebase produced error output:\n%s' %
e.stderr.decode('utf-8').strip())
raise gclient_utils.Error("Unrecognized error, please merge or rebase " raise gclient_utils.Error("Unrecognized error, please merge or rebase "
"manually.\ncd %s && git " % "manually.\ncd %s && git " %
self.checkout_path self.checkout_path
...@@ -1177,7 +1179,7 @@ class GitWrapper(SCMWrapper): ...@@ -1177,7 +1179,7 @@ class GitWrapper(SCMWrapper):
try: try:
self._Capture(['rev-list', '-n', '1', 'HEAD']) self._Capture(['rev-list', '-n', '1', 'HEAD'])
except subprocess2.CalledProcessError as e: except subprocess2.CalledProcessError as e:
if ('fatal: bad object HEAD' in e.stderr if (b'fatal: bad object HEAD' in e.stderr
and self.cache_dir and self.cache_dir in url): and self.cache_dir and self.cache_dir in url):
self.Print(( self.Print((
'Likely due to DEPS change with git cache_dir, ' 'Likely due to DEPS change with git cache_dir, '
......
...@@ -980,7 +980,7 @@ class FakeReposTestBase(trial_dir.TestCase): ...@@ -980,7 +980,7 @@ class FakeReposTestBase(trial_dir.TestCase):
def gitrevparse(self, repo): def gitrevparse(self, repo):
"""Returns the actual revision for a given repo.""" """Returns the actual revision for a given repo."""
return self.FAKE_REPOS._git_rev_parse(repo) return self.FAKE_REPOS._git_rev_parse(repo).decode('utf-8')
def main(argv): def main(argv):
......
...@@ -79,9 +79,6 @@ class TestCaseUtils(object): ...@@ -79,9 +79,6 @@ class TestCaseUtils(object):
self.args = self.Args() self.args = self.Args()
self.relpath = self.String(200) self.relpath = self.String(200)
def tearDown(self):
pass
class StdoutCheck(object): class StdoutCheck(object):
def setUp(self): def setUp(self):
...@@ -130,7 +127,6 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): ...@@ -130,7 +127,6 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase):
def tearDown(self): def tearDown(self):
StdoutCheck.tearDown(self) StdoutCheck.tearDown(self)
TestCaseUtils.tearDown(self)
mox.MoxTestBase.tearDown(self) mox.MoxTestBase.tearDown(self)
def MockList(self, parent, items_to_mock): def MockList(self, parent, items_to_mock):
......
...@@ -19,10 +19,15 @@ import sys ...@@ -19,10 +19,15 @@ import sys
import tempfile import tempfile
import unittest import unittest
if sys.version_info.major == 2:
from cStringIO import StringIO
else:
from io import StringIO
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 third_party import mock
from testing_support import fake_repos from testing_support import fake_repos
from testing_support.super_mox import mox, StdoutCheck, SuperMoxTestBase
from testing_support.super_mox import TestCaseUtils from testing_support.super_mox import TestCaseUtils
import gclient_scm import gclient_scm
...@@ -38,73 +43,37 @@ join = gclient_scm.os.path.join ...@@ -38,73 +43,37 @@ join = gclient_scm.os.path.join
TIMESTAMP_RE = re.compile('\[[0-9]{1,2}:[0-9]{2}:[0-9]{2}\] (.*)', re.DOTALL) TIMESTAMP_RE = re.compile('\[[0-9]{1,2}:[0-9]{2}:[0-9]{2}\] (.*)', re.DOTALL)
def strip_timestamps(value): def strip_timestamps(value):
lines = value.splitlines(True) lines = value.splitlines(True)
for i in xrange(len(lines)): for i in range(len(lines)):
m = TIMESTAMP_RE.match(lines[i]) m = TIMESTAMP_RE.match(lines[i])
if m: if m:
lines[i] = m.group(1) lines[i] = m.group(1)
return ''.join(lines) return ''.join(lines)
class GCBaseTestCase(object): class BasicTests(unittest.TestCase):
def assertRaisesError(self, msg, fn, *args, **kwargs): @mock.patch('gclient_scm.scm.GIT.Capture')
"""Like unittest's assertRaises() but checks for Gclient.Error.""" def testGetFirstRemoteUrl(self, mockCapture):
try:
fn(*args, **kwargs)
except gclient_scm.gclient_utils.Error as e:
self.assertEquals(e.args[0], msg)
else:
self.fail('%s not raised' % msg)
class BaseTestCase(GCBaseTestCase, SuperMoxTestBase):
def setUp(self):
SuperMoxTestBase.setUp(self)
self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter')
self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead')
self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite')
self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'rmtree')
self.mox.StubOutWithMock(subprocess2, 'communicate')
self.mox.StubOutWithMock(subprocess2, 'Popen')
self._scm_wrapper = gclient_scm.GitWrapper
self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists
gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True)
# Absolute path of the fake checkout directory.
self.base_path = join(self.root_dir, self.relpath)
def tearDown(self):
SuperMoxTestBase.tearDown(self)
gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists
class BasicTests(SuperMoxTestBase):
def setUp(self):
SuperMoxTestBase.setUp(self)
def testGetFirstRemoteUrl(self):
REMOTE_STRINGS = [('remote.origin.url E:\\foo\\bar', 'E:\\foo\\bar'), REMOTE_STRINGS = [('remote.origin.url E:\\foo\\bar', 'E:\\foo\\bar'),
('remote.origin.url /b/foo/bar', '/b/foo/bar'), ('remote.origin.url /b/foo/bar', '/b/foo/bar'),
('remote.origin.url https://foo/bar', 'https://foo/bar'), ('remote.origin.url https://foo/bar', 'https://foo/bar'),
('remote.origin.url E:\\Fo Bar\\bax', 'E:\\Fo Bar\\bax'), ('remote.origin.url E:\\Fo Bar\\bax', 'E:\\Fo Bar\\bax'),
('remote.origin.url git://what/"do', 'git://what/"do')] ('remote.origin.url git://what/"do', 'git://what/"do')]
FAKE_PATH = '/fake/path' FAKE_PATH = '/fake/path'
self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'Capture') mockCapture.side_effect = [question for question, _ in REMOTE_STRINGS]
for question, _ in REMOTE_STRINGS:
gclient_scm.scm.GIT.Capture(
['config', '--local', '--get-regexp', r'remote.*.url'],
cwd=FAKE_PATH).AndReturn(question)
self.mox.ReplayAll()
for _, answer in REMOTE_STRINGS: for _, answer in REMOTE_STRINGS:
self.assertEquals(gclient_scm.SCMWrapper._get_first_remote_url(FAKE_PATH), self.assertEqual(
answer) gclient_scm.SCMWrapper._get_first_remote_url(FAKE_PATH), answer)
def tearDown(self): expected_calls = [
SuperMoxTestBase.tearDown(self) mock.call(['config', '--local', '--get-regexp', r'remote.*.url'],
cwd=FAKE_PATH)
for _ in REMOTE_STRINGS
]
self.assertEqual(mockCapture.mock_calls, expected_calls)
class BaseGitWrapperTestCase(GCBaseTestCase, StdoutCheck, TestCaseUtils, class BaseGitWrapperTestCase(unittest.TestCase, TestCaseUtils):
unittest.TestCase):
"""This class doesn't use pymox.""" """This class doesn't use pymox."""
class OptionsObject(object): class OptionsObject(object):
def __init__(self, verbose=False, revision=None): def __init__(self, verbose=False, revision=None):
...@@ -193,7 +162,7 @@ from :3 ...@@ -193,7 +162,7 @@ from :3
value = sys.stdout.getvalue() value = sys.stdout.getvalue()
sys.stdout.close() sys.stdout.close()
# pylint: disable=no-member # pylint: disable=no-member
self.assertEquals(expected, strip_timestamps(value)) self.assertEqual(expected, strip_timestamps(value))
@staticmethod @staticmethod
def CreateGitRepo(git_import, path): def CreateGitRepo(git_import, path):
...@@ -205,7 +174,7 @@ from :3 ...@@ -205,7 +174,7 @@ from :3
# git is not available, skip this test. # git is not available, skip this test.
return False return False
Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE, Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE,
stderr=STDOUT, cwd=path).communicate(input=git_import) stderr=STDOUT, cwd=path).communicate(input=git_import.encode())
Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT, Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT,
cwd=path).communicate() cwd=path).communicate()
Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE, Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
...@@ -224,13 +193,13 @@ from :3 ...@@ -224,13 +193,13 @@ from :3
def _GetAskForDataCallback(self, expected_prompt, return_value): def _GetAskForDataCallback(self, expected_prompt, return_value):
def AskForData(prompt, options): def AskForData(prompt, options):
self.assertEquals(prompt, expected_prompt) self.assertEqual(prompt, expected_prompt)
return return_value return return_value
return AskForData return AskForData
def setUp(self): def setUp(self):
TestCaseUtils.setUp(self)
unittest.TestCase.setUp(self) unittest.TestCase.setUp(self)
TestCaseUtils.setUp(self)
self.url = 'git://foo' self.url = 'git://foo'
# The .git suffix allows gclient_scm to recognize the dir as a git repo # The .git suffix allows gclient_scm to recognize the dir as a git repo
# when cloning it locally # when cloning it locally
...@@ -238,19 +207,12 @@ from :3 ...@@ -238,19 +207,12 @@ from :3
self.relpath = '.' self.relpath = '.'
self.base_path = join(self.root_dir, self.relpath) self.base_path = join(self.root_dir, self.relpath)
self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
StdoutCheck.setUp(self)
self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists
gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) mock.patch('gclient_scm.GitWrapper.BinaryExists',
staticmethod(lambda : True)).start()
def tearDown(self): mock.patch('sys.stdout', StringIO()).start()
try: self.addCleanup(mock.patch.stopall)
rmtree(self.root_dir) self.addCleanup(lambda: rmtree(self.root_dir))
StdoutCheck.tearDown(self)
TestCaseUtils.tearDown(self)
unittest.TestCase.tearDown(self)
finally:
# TODO(maruel): Use auto_stub.TestCase.
gclient_scm.GitWrapper.BinaryExists = self._original_GitBinaryExists
class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
...@@ -267,10 +229,10 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -267,10 +229,10 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
gclient_scm.os.remove(file_path) gclient_scm.os.remove(file_path)
file_list = [] file_list = []
scm.revert(options, self.args, file_list) scm.revert(options, self.args, file_list)
self.assertEquals(file_list, [file_path]) self.assertEqual(file_list, [file_path])
file_list = [] file_list = []
scm.diff(options, self.args, file_list) scm.diff(options, self.args, file_list)
self.assertEquals(file_list, []) self.assertEqual(file_list, [])
sys.stdout.close() sys.stdout.close()
def testRevertNone(self): def testRevertNone(self):
...@@ -283,9 +245,9 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -283,9 +245,9 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
scm.update(options, None, file_list) scm.update(options, None, file_list)
file_list = [] file_list = []
scm.revert(options, self.args, file_list) scm.revert(options, self.args, file_list)
self.assertEquals(file_list, []) self.assertEqual(file_list, [])
self.assertEquals(scm.revinfo(options, self.args, None), self.assertEqual(scm.revinfo(options, self.args, None),
'a7142dc9f0009350b96a11f372b6ea658592aa95') 'a7142dc9f0009350b96a11f372b6ea658592aa95')
sys.stdout.close() sys.stdout.close()
def testRevertModified(self): def testRevertModified(self):
...@@ -297,14 +259,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -297,14 +259,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
file_list = [] file_list = []
scm.update(options, None, file_list) scm.update(options, None, file_list)
file_path = join(self.base_path, 'a') file_path = join(self.base_path, 'a')
open(file_path, 'a').writelines('touched\n') with open(file_path, 'a') as f:
f.writelines('touched\n')
file_list = [] file_list = []
scm.revert(options, self.args, file_list) scm.revert(options, self.args, file_list)
self.assertEquals(file_list, [file_path]) self.assertEqual(file_list, [file_path])
file_list = [] file_list = []
scm.diff(options, self.args, file_list) scm.diff(options, self.args, file_list)
self.assertEquals(file_list, []) self.assertEqual(file_list, [])
self.assertEquals(scm.revinfo(options, self.args, None), self.assertEqual(scm.revinfo(options, self.args, None),
'a7142dc9f0009350b96a11f372b6ea658592aa95') 'a7142dc9f0009350b96a11f372b6ea658592aa95')
sys.stdout.close() sys.stdout.close()
...@@ -317,19 +280,18 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -317,19 +280,18 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
file_list = [] file_list = []
scm.update(options, None, file_list) scm.update(options, None, file_list)
file_path = join(self.base_path, 'c') file_path = join(self.base_path, 'c')
f = open(file_path, 'w') with open(file_path, 'w') as f:
f.writelines('new\n') f.writelines('new\n')
f.close()
Popen(['git', 'add', 'c'], stdout=PIPE, Popen(['git', 'add', 'c'], stdout=PIPE,
stderr=STDOUT, cwd=self.base_path).communicate() stderr=STDOUT, cwd=self.base_path).communicate()
file_list = [] file_list = []
scm.revert(options, self.args, file_list) scm.revert(options, self.args, file_list)
self.assertEquals(file_list, [file_path]) self.assertEqual(file_list, [file_path])
file_list = [] file_list = []
scm.diff(options, self.args, file_list) scm.diff(options, self.args, file_list)
self.assertEquals(file_list, []) self.assertEqual(file_list, [])
self.assertEquals(scm.revinfo(options, self.args, None), self.assertEqual(scm.revinfo(options, self.args, None),
'a7142dc9f0009350b96a11f372b6ea658592aa95') 'a7142dc9f0009350b96a11f372b6ea658592aa95')
sys.stdout.close() sys.stdout.close()
def testStatusNew(self): def testStatusNew(self):
...@@ -337,12 +299,13 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -337,12 +299,13 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
return return
options = self.Options() options = self.Options()
file_path = join(self.base_path, 'a') file_path = join(self.base_path, 'a')
open(file_path, 'a').writelines('touched\n') with open(file_path, 'a') as f:
f.writelines('touched\n')
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
file_list = [] file_list = []
scm.status(options, self.args, file_list) scm.status(options, self.args, file_list)
self.assertEquals(file_list, [file_path]) self.assertEqual(file_list, [file_path])
self.checkstdout( self.checkstdout(
('\n________ running \'git -c core.quotePath=false diff --name-status ' ('\n________ running \'git -c core.quotePath=false diff --name-status '
'069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\n') % '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\n') %
...@@ -355,14 +318,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -355,14 +318,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
expected_file_list = [] expected_file_list = []
for f in ['a', 'b']: for f in ['a', 'b']:
file_path = join(self.base_path, f) file_path = join(self.base_path, f)
open(file_path, 'a').writelines('touched\n') with open(file_path, 'a') as f:
f.writelines('touched\n')
expected_file_list.extend([file_path]) expected_file_list.extend([file_path])
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
file_list = [] file_list = []
scm.status(options, self.args, file_list) scm.status(options, self.args, file_list)
expected_file_list = [join(self.base_path, x) for x in ['a', 'b']] expected_file_list = [join(self.base_path, x) for x in ['a', 'b']]
self.assertEquals(sorted(file_list), expected_file_list) self.assertEqual(sorted(file_list), expected_file_list)
self.checkstdout( self.checkstdout(
('\n________ running \'git -c core.quotePath=false diff --name-status ' ('\n________ running \'git -c core.quotePath=false diff --name-status '
'069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\nM\tb\n') '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\nM\tb\n')
...@@ -377,8 +341,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -377,8 +341,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
self.relpath) self.relpath)
file_list = [] file_list = []
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'a7142dc9f0009350b96a11f372b6ea658592aa95') 'a7142dc9f0009350b96a11f372b6ea658592aa95')
sys.stdout.close() sys.stdout.close()
...@@ -393,15 +357,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -393,15 +357,15 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
rev = scm.revinfo(options, (), None) rev = scm.revinfo(options, (), None)
file_list = [] file_list = []
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, [join(self.base_path, x) self.assertEqual(file_list, [join(self.base_path, x)
for x in ['a', 'b', 'c']]) for x in ['a', 'b', 'c']])
# The actual commit that is created is unstable, so we verify its tree and # The actual commit that is created is unstable, so we verify its tree and
# parents instead. # parents instead.
self.assertEquals(scm._Capture(['rev-parse', 'HEAD:']), self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
'd2e35c10ac24d6c621e14a1fcadceb533155627d') 'd2e35c10ac24d6c621e14a1fcadceb533155627d')
self.assertEquals(scm._Capture(['rev-parse', 'HEAD^1']), rev) self.assertEqual(scm._Capture(['rev-parse', 'HEAD^1']), rev)
self.assertEquals(scm._Capture(['rev-parse', 'HEAD^2']), self.assertEqual(scm._Capture(['rev-parse', 'HEAD^2']),
scm._Capture(['rev-parse', 'origin/master'])) scm._Capture(['rev-parse', 'origin/master']))
sys.stdout.close() sys.stdout.close()
def testUpdateRebase(self): def testUpdateRebase(self):
...@@ -417,14 +381,14 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -417,14 +381,14 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
'Cannot fast-forward merge, attempt to rebase? ' 'Cannot fast-forward merge, attempt to rebase? '
'(y)es / (q)uit / (s)kip : ', 'y') '(y)es / (q)uit / (s)kip : ', 'y')
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, [join(self.base_path, x) self.assertEqual(file_list, [join(self.base_path, x)
for x in ['a', 'b', 'c']]) for x in ['a', 'b', 'c']])
# The actual commit that is created is unstable, so we verify its tree and # The actual commit that is created is unstable, so we verify its tree and
# parent instead. # parent instead.
self.assertEquals(scm._Capture(['rev-parse', 'HEAD:']), self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
'd2e35c10ac24d6c621e14a1fcadceb533155627d') 'd2e35c10ac24d6c621e14a1fcadceb533155627d')
self.assertEquals(scm._Capture(['rev-parse', 'HEAD^']), self.assertEqual(scm._Capture(['rev-parse', 'HEAD^']),
scm._Capture(['rev-parse', 'origin/master'])) scm._Capture(['rev-parse', 'origin/master']))
sys.stdout.close() sys.stdout.close()
def testUpdateReset(self): def testUpdateReset(self):
...@@ -435,10 +399,12 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -435,10 +399,12 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
dir_path = join(self.base_path, 'c') dir_path = join(self.base_path, 'c')
os.mkdir(dir_path) os.mkdir(dir_path)
open(join(dir_path, 'nested'), 'w').writelines('new\n') with open(join(dir_path, 'nested'), 'w') as f:
f.writelines('new\n')
file_path = join(self.base_path, 'file') file_path = join(self.base_path, 'file')
open(file_path, 'w').writelines('new\n') with open(file_path, 'w') as f:
f.writelines('new\n')
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
...@@ -461,8 +427,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -461,8 +427,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
file_list = [] file_list = []
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'069c602044c5388d2d15c3f875b057c852003458') '069c602044c5388d2d15c3f875b057c852003458')
sys.stdout.close() sys.stdout.close()
def testUpdateResetDeleteUnversionedTrees(self): def testUpdateResetDeleteUnversionedTrees(self):
...@@ -474,10 +440,12 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -474,10 +440,12 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
dir_path = join(self.base_path, 'dir') dir_path = join(self.base_path, 'dir')
os.mkdir(dir_path) os.mkdir(dir_path)
open(join(dir_path, 'nested'), 'w').writelines('new\n') with open(join(dir_path, 'nested'), 'w') as f:
f.writelines('new\n')
file_path = join(self.base_path, 'file') file_path = join(self.base_path, 'file')
open(file_path, 'w').writelines('new\n') with open(file_path, 'w') as f:
f.writelines('new\n')
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
...@@ -494,7 +462,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -494,7 +462,8 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
file_path = join(self.base_path, 'b') file_path = join(self.base_path, 'b')
open(file_path, 'w').writelines('conflict\n') with open(file_path, 'w') as f:
f.writelines('conflict\n')
try: try:
scm.update(options, (), []) scm.update(options, (), [])
self.fail() self.fail()
...@@ -544,19 +513,29 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -544,19 +513,29 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
file_path = join(self.base_path, 'b') file_path = join(self.base_path, 'b')
open(file_path, 'w').writelines('conflict\n') with open(file_path, 'w') as f:
f.writelines('conflict\n')
scm._Run(['commit', '-am', 'test'], options) scm._Run(['commit', '-am', 'test'], options)
scm._AskForData = self._GetAskForDataCallback( scm._AskForData = self._GetAskForDataCallback(
'Cannot fast-forward merge, attempt to rebase? ' 'Cannot fast-forward merge, attempt to rebase? '
'(y)es / (q)uit / (s)kip : ', 'y') '(y)es / (q)uit / (s)kip : ', 'y')
exception = ('Conflict while rebasing this branch.\n'
'Fix the conflict and run gclient again.\n' with self.assertRaises(gclient_scm.gclient_utils.Error) as e:
'See \'man git-rebase\' for details.\n') scm.update(options, (), [])
self.assertRaisesError(exception, scm.update, options, (), []) self.assertEqual(
exception = ('\n____ . at refs/remotes/origin/master\n' e.exception.args[0],
'\tYou have unstaged changes.\n' 'Conflict while rebasing this branch.\n'
'\tPlease commit, stash, or reset.\n') 'Fix the conflict and run gclient again.\n'
self.assertRaisesError(exception, scm.update, options, (), []) 'See \'man git-rebase\' for details.\n')
with self.assertRaises(gclient_scm.gclient_utils.Error) as e:
scm.update(options, (), [])
self.assertEqual(
e.exception.args[0],
'\n____ . at refs/remotes/origin/master\n'
'\tYou have unstaged changes.\n'
'\tPlease commit, stash, or reset.\n')
sys.stdout.close() sys.stdout.close()
def testRevinfo(self): def testRevinfo(self):
...@@ -566,7 +545,7 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -566,7 +545,7 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
scm = gclient_scm.GitWrapper(self.url, self.root_dir, scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
rev_info = scm.revinfo(options, (), None) rev_info = scm.revinfo(options, (), None)
self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') self.assertEqual(rev_info, '069c602044c5388d2d15c3f875b057c852003458')
def testMirrorPushUrl(self): def testMirrorPushUrl(self):
if not self.enabled: if not self.enabled:
...@@ -600,11 +579,11 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -600,11 +579,11 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
' fetch_url: %s' % fetch_url, ' fetch_url: %s' % fetch_url,
' mirror: %s' % mirror])) ' mirror: %s' % mirror]))
push_url = scm._Capture(['remote', 'get-url', '--push', 'origin']) push_url = scm._Capture(['remote', 'get-url', '--push', 'origin'])
self.assertEquals(push_url, self.url) self.assertEqual(push_url, self.url)
sys.stdout.close() sys.stdout.close()
class ManagedGitWrapperTestCaseMox(BaseTestCase): class ManagedGitWrapperTestCaseMock(unittest.TestCase):
class OptionsObject(object): class OptionsObject(object):
def __init__(self, verbose=False, revision=None, force=False): def __init__(self, verbose=False, revision=None, force=False):
self.verbose = verbose self.verbose = verbose
...@@ -627,10 +606,9 @@ class ManagedGitWrapperTestCaseMox(BaseTestCase): ...@@ -627,10 +606,9 @@ class ManagedGitWrapperTestCaseMox(BaseTestCase):
value = sys.stdout.getvalue() value = sys.stdout.getvalue()
sys.stdout.close() sys.stdout.close()
# pylint: disable=no-member # pylint: disable=no-member
self.assertEquals(expected, strip_timestamps(value)) self.assertEqual(expected, strip_timestamps(value))
def setUp(self): def setUp(self):
BaseTestCase.setUp(self)
self.fake_hash_1 = 't0ta11yf4k3' self.fake_hash_1 = 't0ta11yf4k3'
self.fake_hash_2 = '3v3nf4k3r' self.fake_hash_2 = '3v3nf4k3r'
self.url = 'git://foo' self.url = 'git://foo'
...@@ -639,107 +617,94 @@ class ManagedGitWrapperTestCaseMox(BaseTestCase): ...@@ -639,107 +617,94 @@ class ManagedGitWrapperTestCaseMox(BaseTestCase):
self.base_path = os.path.join(self.root_dir, self.relpath) self.base_path = os.path.join(self.root_dir, self.relpath)
self.backup_base_path = os.path.join(self.root_dir, self.backup_base_path = os.path.join(self.root_dir,
'old_%s.git' % self.relpath) 'old_%s.git' % self.relpath)
mock.patch('gclient_scm.scm.GIT.ApplyEnvVars').start()
def tearDown(self): mock.patch('gclient_scm.GitWrapper._CheckMinVersion').start()
BaseTestCase.tearDown(self) mock.patch('gclient_scm.GitWrapper._Fetch').start()
mock.patch('gclient_scm.GitWrapper._DeleteOrMove').start()
def testGetUsableRevGit(self): mock.patch('sys.stdout', StringIO()).start()
self.addCleanup(mock.patch.stopall)
@mock.patch('scm.GIT.IsValidRevision')
@mock.patch('os.path.isdir', lambda _: True)
def testGetUsableRevGit(self, mockIsValidRevision):
# pylint: disable=no-member # pylint: disable=no-member
options = self.Options(verbose=True) options = self.Options(verbose=True)
self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) mockIsValidRevision.side_effect = lambda cwd, rev: rev != '1'
gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1
).AndReturn(True)
gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev='1'
).AndReturn(False)
gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev='1'
).AndReturn(False)
self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Fetch', True)
# pylint: disable=no-value-for-parameter
gclient_scm.GitWrapper._Fetch(options).AndReturn(None)
gclient_scm.scm.os.path.isdir(self.base_path).AndReturn(True)
gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
self.mox.ReplayAll()
git_scm = gclient_scm.GitWrapper(self.url, self.root_dir, git_scm = gclient_scm.GitWrapper(self.url, self.root_dir,
self.relpath) self.relpath)
# A [fake] git sha1 with a git repo should work (this is in the case that # A [fake] git sha1 with a git repo should work (this is in the case that
# the LKGR gets flipped to git sha1's some day). # the LKGR gets flipped to git sha1's some day).
self.assertEquals(git_scm.GetUsableRev(self.fake_hash_1, options), self.assertEqual(git_scm.GetUsableRev(self.fake_hash_1, options),
self.fake_hash_1) self.fake_hash_1)
# An SVN rev with an existing purely git repo should raise an exception. # An SVN rev with an existing purely git repo should raise an exception.
self.assertRaises(gclient_scm.gclient_utils.Error, self.assertRaises(gclient_scm.gclient_utils.Error,
git_scm.GetUsableRev, '1', options) git_scm.GetUsableRev, '1', options)
def testUpdateNoDotGit(self): @mock.patch('gclient_scm.GitWrapper._Clone')
options = self.Options() @mock.patch('os.path.isdir')
@mock.patch('os.path.exists')
@mock.patch('subprocess2.check_output')
def testUpdateNoDotGit(
self, mockCheckOutput, mockExists, mockIsdir, mockClone):
mockIsdir.side_effect = lambda path: path == self.base_path
mockExists.side_effect = lambda path: path == self.base_path
mockCheckOutput.return_value = b''
gclient_scm.os.path.isdir( options = self.Options()
os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) scm = gclient_scm.GitWrapper(
gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False) self.url, self.root_dir, self.relpath)
gclient_scm.os.path.exists(self.base_path).AndReturn(True)
gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
gclient_scm.os.path.exists(os.path.join(self.base_path, '.git')
).AndReturn(False)
self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Clone', True)
# pylint: disable=no-value-for-parameter
gclient_scm.GitWrapper._Clone('refs/remotes/origin/master', self.url,
options)
self.mox.StubOutWithMock(gclient_scm.subprocess2, 'check_output', True)
gclient_scm.subprocess2.check_output(
['git', '-c', 'core.quotePath=false', 'ls-files'], cwd=self.base_path,
env=gclient_scm.scm.GIT.ApplyEnvVars({}), stderr=-1,).AndReturn('')
gclient_scm.subprocess2.check_output(
['git', 'rev-parse', '--verify', 'HEAD'],
cwd=self.base_path,
env=gclient_scm.scm.GIT.ApplyEnvVars({}),
stderr=-1,
).AndReturn('')
self.mox.ReplayAll()
scm = self._scm_wrapper(self.url, self.root_dir,
self.relpath)
scm.update(options, None, []) scm.update(options, None, [])
env = gclient_scm.scm.GIT.ApplyEnvVars({})
self.assertEqual(
mockCheckOutput.mock_calls,
[
mock.call(
['git', '-c', 'core.quotePath=false', 'ls-files'],
cwd=self.base_path, env=env, stderr=-1),
mock.call(
['git', 'rev-parse', '--verify', 'HEAD'],
cwd=self.base_path, env=env, stderr=-1),
])
mockClone.assert_called_with(
'refs/remotes/origin/master', self.url, options)
self.checkstdout('\n') self.checkstdout('\n')
def testUpdateConflict(self): @mock.patch('gclient_scm.GitWrapper._Clone')
options = self.Options() @mock.patch('os.path.isdir')
@mock.patch('os.path.exists')
@mock.patch('subprocess2.check_output')
def testUpdateConflict(
self, mockCheckOutput, mockExists, mockIsdir, mockClone):
mockIsdir.side_effect = lambda path: path == self.base_path
mockExists.side_effect = lambda path: path == self.base_path
mockCheckOutput.return_value = b''
mockClone.side_effect = [
gclient_scm.subprocess2.CalledProcessError(
None, None, None, None, None),
None,
]
gclient_scm.os.path.isdir( options = self.Options()
os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) scm = gclient_scm.GitWrapper(self.url, self.root_dir,
gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False)
gclient_scm.os.path.exists(self.base_path).AndReturn(True)
gclient_scm.os.path.isdir(self.base_path).AndReturn(True)
gclient_scm.os.path.exists(os.path.join(self.base_path, '.git')
).AndReturn(False)
self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Clone', True)
# pylint: disable=no-value-for-parameter
gclient_scm.GitWrapper._Clone(
'refs/remotes/origin/master', self.url, options
).AndRaise(gclient_scm.subprocess2.CalledProcessError(None, None, None,
None, None))
self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_DeleteOrMove', True)
gclient_scm.GitWrapper._DeleteOrMove(False)
gclient_scm.GitWrapper._Clone('refs/remotes/origin/master', self.url,
options)
self.mox.StubOutWithMock(gclient_scm.subprocess2, 'check_output', True)
gclient_scm.subprocess2.check_output(
['git', '-c', 'core.quotePath=false', 'ls-files'], cwd=self.base_path,
env=gclient_scm.scm.GIT.ApplyEnvVars({}), stderr=-1,).AndReturn('')
gclient_scm.subprocess2.check_output(
['git', 'rev-parse', '--verify', 'HEAD'],
cwd=self.base_path,
env=gclient_scm.scm.GIT.ApplyEnvVars({}),
stderr=-1,
).AndReturn('')
self.mox.ReplayAll()
scm = self._scm_wrapper(self.url, self.root_dir,
self.relpath) self.relpath)
scm.update(options, None, []) scm.update(options, None, [])
env = gclient_scm.scm.GIT.ApplyEnvVars({})
self.assertEqual(
mockCheckOutput.mock_calls,
[
mock.call(
['git', '-c', 'core.quotePath=false', 'ls-files'],
cwd=self.base_path, env=env, stderr=-1),
mock.call(
['git', 'rev-parse', '--verify', 'HEAD'],
cwd=self.base_path, env=env, stderr=-1),
])
mockClone.assert_called_with(
'refs/remotes/origin/master', self.url, options)
self.checkstdout('\n') self.checkstdout('\n')
...@@ -784,11 +749,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -784,11 +749,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'069c602044c5388d2d15c3f875b057c852003458') '069c602044c5388d2d15c3f875b057c852003458')
# indicates detached HEAD # indicates detached HEAD
self.assertEquals(self.getCurrentBranch(), None) self.assertEqual(self.getCurrentBranch(), None)
self.checkInStdout( self.checkInStdout(
'Checked out refs/remotes/origin/master to a detached HEAD') 'Checked out refs/remotes/origin/master to a detached HEAD')
...@@ -816,11 +781,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -816,11 +781,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'a7142dc9f0009350b96a11f372b6ea658592aa95') 'a7142dc9f0009350b96a11f372b6ea658592aa95')
# indicates detached HEAD # indicates detached HEAD
self.assertEquals(self.getCurrentBranch(), None) self.assertEqual(self.getCurrentBranch(), None)
self.checkInStdout( self.checkInStdout(
'Checked out a7142dc9f0009350b96a11f372b6ea658592aa95 to a detached HEAD') 'Checked out a7142dc9f0009350b96a11f372b6ea658592aa95 to a detached HEAD')
...@@ -848,11 +813,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -848,11 +813,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'9a51244740b25fa2ded5252ca00a3178d3f665a9') '9a51244740b25fa2ded5252ca00a3178d3f665a9')
# indicates detached HEAD # indicates detached HEAD
self.assertEquals(self.getCurrentBranch(), None) self.assertEqual(self.getCurrentBranch(), None)
self.checkInStdout( self.checkInStdout(
'Checked out 9a51244740b25fa2ded5252ca00a3178d3f665a9 ' 'Checked out 9a51244740b25fa2ded5252ca00a3178d3f665a9 '
'to a detached HEAD') 'to a detached HEAD')
...@@ -881,11 +846,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -881,11 +846,11 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'9a51244740b25fa2ded5252ca00a3178d3f665a9') '9a51244740b25fa2ded5252ca00a3178d3f665a9')
# indicates detached HEAD # indicates detached HEAD
self.assertEquals(self.getCurrentBranch(), None) self.assertEqual(self.getCurrentBranch(), None)
self.checkInStdout( self.checkInStdout(
'Checked out refs/remotes/origin/feature to a detached HEAD') 'Checked out refs/remotes/origin/feature to a detached HEAD')
...@@ -913,9 +878,9 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -913,9 +878,9 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'9a51244740b25fa2ded5252ca00a3178d3f665a9') '9a51244740b25fa2ded5252ca00a3178d3f665a9')
# @refs/heads/feature is AKA @refs/remotes/origin/feature in the clone, so # @refs/heads/feature is AKA @refs/remotes/origin/feature in the clone, so
# should be treated as such by gclient. # should be treated as such by gclient.
# TODO(mmoss): Though really, we should only allow DEPS to specify branches # TODO(mmoss): Though really, we should only allow DEPS to specify branches
...@@ -924,7 +889,7 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -924,7 +889,7 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
# defaults at some point). But that will take more work to stop using # defaults at some point). But that will take more work to stop using
# refs/remotes/ everywhere that we do (and to stop assuming a DEPS ref will # refs/remotes/ everywhere that we do (and to stop assuming a DEPS ref will
# always resolve locally, like when passing them to show-ref or rev-list). # always resolve locally, like when passing them to show-ref or rev-list).
self.assertEquals(self.getCurrentBranch(), None) self.assertEqual(self.getCurrentBranch(), None)
self.checkInStdout( self.checkInStdout(
'Checked out refs/remotes/origin/feature to a detached HEAD') 'Checked out refs/remotes/origin/feature to a detached HEAD')
...@@ -940,19 +905,18 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): ...@@ -940,19 +905,18 @@ class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
file_list = [] file_list = []
options.revision = 'unmanaged' options.revision = 'unmanaged'
scm.update(options, (), file_list) scm.update(options, (), file_list)
self.assertEquals(file_list, expected_file_list) self.assertEqual(file_list, expected_file_list)
self.assertEquals(scm.revinfo(options, (), None), self.assertEqual(scm.revinfo(options, (), None),
'069c602044c5388d2d15c3f875b057c852003458') '069c602044c5388d2d15c3f875b057c852003458')
self.checkstdout('________ unmanaged solution; skipping .\n') self.checkstdout('________ unmanaged solution; skipping .\n')
class CipdWrapperTestCase(BaseTestCase): class CipdWrapperTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
# Create this before setting up mocks. # Create this before setting up mocks.
self._cipd_root_dir = tempfile.mkdtemp() self._cipd_root_dir = tempfile.mkdtemp()
self._workdir = tempfile.mkdtemp() self._workdir = tempfile.mkdtemp()
BaseTestCase.setUp(self)
self._cipd_instance_url = 'https://chrome-infra-packages.appspot.com' self._cipd_instance_url = 'https://chrome-infra-packages.appspot.com'
self._cipd_root = gclient_scm.CipdRoot( self._cipd_root = gclient_scm.CipdRoot(
...@@ -963,12 +927,13 @@ class CipdWrapperTestCase(BaseTestCase): ...@@ -963,12 +927,13 @@ class CipdWrapperTestCase(BaseTestCase):
self._cipd_root.add_package('b', 'bar_package', 'bar_version'), self._cipd_root.add_package('b', 'bar_package', 'bar_version'),
self._cipd_root.add_package('b', 'baz_package', 'baz_version'), self._cipd_root.add_package('b', 'baz_package', 'baz_version'),
] ]
self.mox.StubOutWithMock(self._cipd_root, 'add_package') mock.patch('tempfile.mkdtemp', lambda: self._workdir).start()
self.mox.StubOutWithMock(self._cipd_root, 'clobber') mock.patch('gclient_scm.CipdRoot.add_package').start()
self.mox.StubOutWithMock(self._cipd_root, 'ensure') mock.patch('gclient_scm.CipdRoot.clobber').start()
mock.patch('gclient_scm.CipdRoot.ensure').start()
self.addCleanup(mock.patch.stopall)
def tearDown(self): def tearDown(self):
BaseTestCase.tearDown(self)
rmtree(self._cipd_root_dir) rmtree(self._cipd_root_dir)
rmtree(self._workdir) rmtree(self._workdir)
...@@ -989,14 +954,12 @@ class CipdWrapperTestCase(BaseTestCase): ...@@ -989,14 +954,12 @@ class CipdWrapperTestCase(BaseTestCase):
def testRevert(self): def testRevert(self):
"""Checks that revert does nothing.""" """Checks that revert does nothing."""
scm = self.createScmWithPackageThatSatisfies( scm = self.createScmWithPackageThatSatisfies(lambda _: True)
lambda _: True)
self.mox.ReplayAll()
scm.revert(None, (), []) scm.revert(None, (), [])
def testRevinfo(self): @mock.patch('gclient_scm.gclient_utils.CheckCallAndFilter')
@mock.patch('gclient_scm.gclient_utils.rmtree')
def testRevinfo(self, mockRmtree, mockCheckCallAndFilter):
"""Checks that revinfo uses the JSON from cipd describe.""" """Checks that revinfo uses the JSON from cipd describe."""
scm = self.createScmWithPackageThatSatisfies(lambda _: True) scm = self.createScmWithPackageThatSatisfies(lambda _: True)
...@@ -1012,31 +975,20 @@ class CipdWrapperTestCase(BaseTestCase): ...@@ -1012,31 +975,20 @@ class CipdWrapperTestCase(BaseTestCase):
with open(describe_json_path, 'w') as describe_json: with open(describe_json_path, 'w') as describe_json:
json.dump(json_contents, describe_json) json.dump(json_contents, describe_json)
cmd = [ revinfo = scm.revinfo(None, (), [])
self.assertEqual(revinfo, expected_revinfo)
mockRmtree.assert_called_with(self._workdir)
mockCheckCallAndFilter.assert_called_with([
'cipd', 'describe', 'foo_package', 'cipd', 'describe', 'foo_package',
'-log-level', 'error', '-log-level', 'error',
'-version', 'foo_version', '-version', 'foo_version',
'-json-output', describe_json_path, '-json-output', describe_json_path,
] ])
self.mox.StubOutWithMock(tempfile, 'mkdtemp')
tempfile.mkdtemp().AndReturn(self._workdir)
gclient_scm.gclient_utils.CheckCallAndFilter(cmd)
gclient_scm.gclient_utils.rmtree(self._workdir)
self.mox.ReplayAll()
revinfo = scm.revinfo(None, (), [])
self.assertEquals(revinfo, expected_revinfo)
def testUpdate(self): def testUpdate(self):
"""Checks that update does nothing.""" """Checks that update does nothing."""
scm = self.createScmWithPackageThatSatisfies( scm = self.createScmWithPackageThatSatisfies(lambda _: True)
lambda _: True)
self.mox.ReplayAll()
scm.update(None, (), []) scm.update(None, (), [])
...@@ -1327,7 +1279,7 @@ class GerritChangesTest(fake_repos.FakeReposTestBase): ...@@ -1327,7 +1279,7 @@ class GerritChangesTest(fake_repos.FakeReposTestBase):
self.url, 'refs/changes/36/1236/1', 'refs/heads/master', self.options, self.url, 'refs/changes/36/1236/1', 'refs/heads/master', self.options,
file_list) file_list)
self.assertEqual(cm.exception.cmd[:2], ['git', 'cherry-pick']) self.assertEqual(cm.exception.cmd[:2], ['git', 'cherry-pick'])
self.assertIn('error: could not apply', cm.exception.stderr) self.assertIn(b'error: could not apply', cm.exception.stderr)
# Try to apply 'refs/changes/35/1235/1', which doesn't have a merge # Try to apply 'refs/changes/35/1235/1', which doesn't have a merge
# conflict. # conflict.
......
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