Commit 9185e449 authored by Edward Lemur's avatar Edward Lemur Committed by Commit Bot

depot_tools: Make scm Python 3 compatible.

Bug: 984182
Change-Id: I6165cf889ebd4e074f1f72b29e8d1216337fb04f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1752480
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: 's avatarRobbie Iannucci <iannucci@chromium.org>
parent 979fa780
......@@ -30,7 +30,7 @@ import subprocess2
def write(path, content):
f = open(path, 'wb')
f.write(content)
f.write(content.encode())
f.close()
......@@ -52,12 +52,12 @@ def read_tree(tree_root):
def dict_diff(dict1, dict2):
diff = {}
for k, v in dict1.iteritems():
for k, v in dict1.items():
if k not in dict2:
diff[k] = v
elif v != dict2[k]:
diff[k] = (v, dict2[k])
for k, v in dict2.iteritems():
for k, v in dict2.items():
if k not in dict1:
diff[k] = v
return diff
......@@ -68,7 +68,8 @@ def commit_git(repo):
subprocess2.check_call(['git', 'add', '-A', '-f'], cwd=repo)
subprocess2.check_call(['git', 'commit', '-q', '--message', 'foo'], cwd=repo)
rev = subprocess2.check_output(
['git', 'show-ref', '--head', 'HEAD'], cwd=repo).split(' ', 1)[0]
['git', 'show-ref', '--head', 'HEAD'], cwd=repo).split(b' ', 1)[0]
rev = rev.decode('utf-8')
logging.debug('At revision %s' % rev)
return rev
......@@ -221,7 +222,7 @@ class FakeReposBase(object):
"""For a dictionary of file contents, generate a filesystem."""
if not os.path.isdir(root):
os.makedirs(root)
for (k, v) in tree_dict.iteritems():
for (k, v) in tree_dict.items():
k_os = k.replace('/', os.sep)
k_arr = k_os.split(os.sep)
if len(k_arr) > 1:
......@@ -301,7 +302,7 @@ class FakeReposBase(object):
repo_root = join(self.git_root, repo)
logging.debug('%s: fast-import %s', repo, data)
subprocess2.check_call(
['git', 'fast-import', '--quiet'], cwd=repo_root, stdin=data)
['git', 'fast-import', '--quiet'], cwd=repo_root, stdin=data.encode())
def check_port_is_free(self, port):
sock = socket.socket()
......@@ -966,7 +967,7 @@ class FakeReposTestBase(trial_dir.TestCase):
for item, new_root in args:
repo, rev = item.split('@', 1)
tree = self.gittree(repo, rev)
for k, v in tree.iteritems():
for k, v in tree.items():
result[join(new_root, k)] = v
return result
......
......@@ -9,8 +9,7 @@ import logging
import os
import sys
import tempfile
from testing_support import auto_stub
import unittest
import gclient_utils
......@@ -80,15 +79,15 @@ class TrialDirMixIn(object):
return self.trial.root_dir
class TestCase(auto_stub.TestCase, TrialDirMixIn):
class TestCase(unittest.TestCase, TrialDirMixIn):
"""Base unittest class that cleans off a trial directory in tearDown()."""
def setUp(self):
auto_stub.TestCase.setUp(self)
unittest.TestCase.setUp(self)
TrialDirMixIn.setUp(self)
def tearDown(self):
TrialDirMixIn.tearDown(self)
auto_stub.TestCase.tearDown(self)
unittest.TestCase.tearDown(self)
if '-l' in sys.argv:
......
......@@ -12,100 +12,23 @@ import unittest
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.super_mox import SuperMoxTestBase
import scm
import subprocess2
# Access to a protected member XXX of a client class
# pylint: disable=protected-access
class BaseTestCase(SuperMoxTestBase):
# Like unittest's assertRaises, but checks for Gclient.Error.
def assertRaisesError(self, msg, fn, *args, **kwargs):
try:
fn(*args, **kwargs)
except scm.gclient_utils.Error as e:
self.assertEquals(e.args[0], msg)
else:
self.fail('%s not raised' % msg)
class BaseSCMTestCase(BaseTestCase):
class GitWrapperTestCase(unittest.TestCase):
def setUp(self):
BaseTestCase.setUp(self)
self.mox.StubOutWithMock(subprocess2, 'Popen')
self.mox.StubOutWithMock(subprocess2, 'communicate')
class RootTestCase(BaseSCMTestCase):
def testMembersChanged(self):
self.mox.ReplayAll()
members = [
'determine_scm',
'ElementTree',
'gclient_utils',
'GenFakeDiff',
'GetCasedPath',
'GIT',
'glob',
'io',
'logging',
'only_int',
'os',
'platform',
're',
'subprocess2',
'sys',
'tempfile',
'time',
'ValidateEmail',
]
# If this test fails, you should add the relevant test.
self.compareMembers(scm, members)
class GitWrapperTestCase(BaseSCMTestCase):
def testMembersChanged(self):
members = [
'ApplyEnvVars',
'AssertVersion',
'Capture',
'CaptureStatus',
'CleanupDir',
'current_version',
'FetchUpstreamTuple',
'GenerateDiff',
'GetBranch',
'GetBranchRef',
'GetCheckoutRoot',
'GetDifferentFiles',
'GetEmail',
'GetGitDir',
'GetOldContents',
'GetPatchName',
'GetUpstreamBranch',
'IsAncestor',
'IsDirectoryVersioned',
'IsInsideWorkTree',
'IsValidRevision',
'IsWorkTreeDirty',
'RefToRemoteRef',
'RemoteRefToRef',
'ShortBranchName',
]
# If this test fails, you should add the relevant test.
self.compareMembers(scm.GIT, members)
def testGetEmail(self):
self.mox.StubOutWithMock(scm.GIT, 'Capture')
scm.GIT.Capture(['config', 'user.email'], cwd=self.root_dir
).AndReturn('mini@me.com')
self.mox.ReplayAll()
super(GitWrapperTestCase, self).setUp()
self.root_dir = '/foo/bar'
@mock.patch('scm.GIT.Capture')
def testGetEmail(self, mockCapture):
mockCapture.return_value = 'mini@me.com'
self.assertEqual(scm.GIT.GetEmail(self.root_dir), 'mini@me.com')
mockCapture.assert_called_with(['config', 'user.email'], cwd=self.root_dir)
def testRefToRemoteRef(self):
remote = 'origin'
......
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