Revert 15861 because of

File "D:\b\depot_tools\bootstrap\\..\gclient.py", line 446, in SubprocessCallAndCapture
   out.write(in_byte)

TBR=nsylvain
Review URL: http://codereview.chromium.org/115244

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@15862 0039d316-1c4b-4281-b951-d872f2087c98
experimental/szager/collated-output
maruel@chromium.org 16 years ago
parent e77173e573
commit 96b9171280

@ -64,7 +64,7 @@ Hooks
""" """
__author__ = "darinf@gmail.com (Darin Fisher)" __author__ = "darinf@gmail.com (Darin Fisher)"
__version__ = "0.3.2" __version__ = "0.3.1"
import errno import errno
import optparse import optparse
@ -397,7 +397,7 @@ def RemoveDirectory(*path):
os.rmdir(file_path) os.rmdir(file_path)
def SubprocessCall(command, in_directory, fail_status=None): def SubprocessCall(command, in_directory, out, fail_status=None):
"""Runs command, a list, in directory in_directory. """Runs command, a list, in directory in_directory.
This function wraps SubprocessCallAndCapture, but does not perform the This function wraps SubprocessCallAndCapture, but does not perform the
@ -405,10 +405,10 @@ def SubprocessCall(command, in_directory, fail_status=None):
description. description.
""" """
# Call subprocess and capture nothing: # Call subprocess and capture nothing:
SubprocessCallAndCapture(command, in_directory, fail_status) SubprocessCallAndCapture(command, in_directory, out, fail_status)
def SubprocessCallAndCapture(command, in_directory, fail_status=None, def SubprocessCallAndCapture(command, in_directory, out, fail_status=None,
pattern=None, capture_list=None): pattern=None, capture_list=None):
"""Runs command, a list, in directory in_directory. """Runs command, a list, in directory in_directory.
@ -423,7 +423,7 @@ def SubprocessCallAndCapture(command, in_directory, fail_status=None,
default), gclient will raise an Error exception. default), gclient will raise an Error exception.
""" """
print("\n________ running \'%s\' in \'%s\'" print >> out, ("\n________ running \'%s\' in \'%s\'"
% (' '.join(command), in_directory)) % (' '.join(command), in_directory))
# *Sigh*: Windows needs shell=True, or else it won't search %PATH% for the # *Sigh*: Windows needs shell=True, or else it won't search %PATH% for the
@ -475,7 +475,7 @@ def IsUsingGit(root, paths):
# SVN utils: # SVN utils:
def RunSVN(args, in_directory): def RunSVN(options, args, in_directory):
"""Runs svn, sending output to stdout. """Runs svn, sending output to stdout.
Args: Args:
@ -488,10 +488,10 @@ def RunSVN(args, in_directory):
c = [SVN_COMMAND] c = [SVN_COMMAND]
c.extend(args) c.extend(args)
SubprocessCall(c, in_directory) SubprocessCall(c, in_directory, options.stdout)
def CaptureSVN(args, in_directory): def CaptureSVN(options, args, in_directory):
"""Runs svn, capturing output sent to stdout as a string. """Runs svn, capturing output sent to stdout as a string.
Args: Args:
@ -508,13 +508,11 @@ def CaptureSVN(args, in_directory):
# the svn.exe executable, but shell=True makes subprocess on Linux fail # the svn.exe executable, but shell=True makes subprocess on Linux fail
# when it's called with a list because it only tries to execute the # when it's called with a list because it only tries to execute the
# first string ("svn"). # first string ("svn").
return subprocess.Popen(c, return subprocess.Popen(c, cwd=in_directory, shell=(sys.platform == 'win32'),
cwd=in_directory,
shell=(sys.platform == 'win32'),
stdout=subprocess.PIPE).communicate()[0] stdout=subprocess.PIPE).communicate()[0]
def RunSVNAndGetFileList(args, in_directory, file_list): def RunSVNAndGetFileList(options, args, in_directory, file_list):
"""Runs svn checkout, update, or status, output to stdout. """Runs svn checkout, update, or status, output to stdout.
The first item in args must be either "checkout", "update", or "status". The first item in args must be either "checkout", "update", or "status".
@ -554,13 +552,11 @@ def RunSVNAndGetFileList(args, in_directory, file_list):
'update': update_pattern, 'update': update_pattern,
}[args[0]] }[args[0]]
SubprocessCallAndCapture(command, SubprocessCallAndCapture(command, in_directory, options.stdout,
in_directory, pattern=pattern, capture_list=file_list)
pattern=pattern,
capture_list=file_list)
def CaptureSVNInfo(relpath, in_directory=None): def CaptureSVNInfo(options, relpath, in_directory):
"""Returns a dictionary from the svn info output for the given file. """Returns a dictionary from the svn info output for the given file.
Args: Args:
@ -568,8 +564,7 @@ def CaptureSVNInfo(relpath, in_directory=None):
the directory given by in_directory. the directory given by in_directory.
in_directory: The directory where svn is to be run. in_directory: The directory where svn is to be run.
""" """
output = CaptureSVN(["info", "--xml", relpath], in_directory) dom = ParseXML(CaptureSVN(options, ["info", "--xml", relpath], in_directory))
dom = ParseXML(output)
result = {} result = {}
if dom: if dom:
def C(item, f): def C(item, f):
@ -597,13 +592,13 @@ def CaptureSVNInfo(relpath, in_directory=None):
return result return result
def CaptureSVNHeadRevision(url): def CaptureSVNHeadRevision(options, url):
"""Get the head revision of a SVN repository. """Get the head revision of a SVN repository.
Returns: Returns:
Int head revision Int head revision
""" """
info = CaptureSVN(["info", "--xml", url], os.getcwd()) info = CaptureSVN(options, ["info", "--xml", url], os.getcwd())
dom = xml.dom.minidom.parseString(info) dom = xml.dom.minidom.parseString(info)
return int(dom.getElementsByTagName('entry')[0].getAttribute('revision')) return int(dom.getElementsByTagName('entry')[0].getAttribute('revision'))
@ -622,7 +617,7 @@ class FileStatus:
self.path) self.path)
def CaptureSVNStatus(path): def CaptureSVNStatus(options, path):
"""Runs 'svn status' on an existing path. """Runs 'svn status' on an existing path.
Args: Args:
@ -631,7 +626,7 @@ def CaptureSVNStatus(path):
Returns: Returns:
An array of FileStatus corresponding to the emulated output of 'svn status' An array of FileStatus corresponding to the emulated output of 'svn status'
version 1.5.""" version 1.5."""
dom = ParseXML(CaptureSVN(["status", "--xml"], path)) dom = ParseXML(CaptureSVN(options, ["status", "--xml"], path))
results = [] results = []
if dom: if dom:
# /status/target/entry/(wc-status|commit|author|date) # /status/target/entry/(wc-status|commit|author|date)
@ -733,13 +728,13 @@ class SCMWrapper(object):
"""Cleanup working copy.""" """Cleanup working copy."""
command = ['cleanup'] command = ['cleanup']
command.extend(args) command.extend(args)
RunSVN(command, os.path.join(self._root_dir, self.relpath)) RunSVN(options, command, os.path.join(self._root_dir, self.relpath))
def diff(self, options, args, file_list): def diff(self, options, args, file_list):
# NOTE: This function does not currently modify file_list. # NOTE: This function does not currently modify file_list.
command = ['diff'] command = ['diff']
command.extend(args) command.extend(args)
RunSVN(command, os.path.join(self._root_dir, self.relpath)) RunSVN(options, command, os.path.join(self._root_dir, self.relpath))
def update(self, options, args, file_list): def update(self, options, args, file_list):
"""Runs SCM to update or transparently checkout the working copy. """Runs SCM to update or transparently checkout the working copy.
@ -752,7 +747,8 @@ class SCMWrapper(object):
# Only update if git is not controlling the directory. # Only update if git is not controlling the directory.
git_path = os.path.join(self._root_dir, self.relpath, '.git') git_path = os.path.join(self._root_dir, self.relpath, '.git')
if options.path_exists(git_path): if options.path_exists(git_path):
print("________ found .git directory; skipping %s" % self.relpath) print >> options.stdout, (
"________ found .git directory; skipping %s" % self.relpath)
return return
if args: if args:
@ -780,22 +776,23 @@ class SCMWrapper(object):
command = ['checkout', url, os.path.join(self._root_dir, self.relpath)] command = ['checkout', url, os.path.join(self._root_dir, self.relpath)]
if revision: if revision:
command.extend(['--revision', str(revision)]) command.extend(['--revision', str(revision)])
RunSVNAndGetFileList(command, self._root_dir, file_list) RunSVNAndGetFileList(options, command, self._root_dir, file_list)
return return
# Get the existing scm url and the revision number of the current checkout. # Get the existing scm url and the revision number of the current checkout.
from_info = CaptureSVNInfo(os.path.join(self._root_dir, self.relpath, '.'), from_info = CaptureSVNInfo(options,
os.path.join(self._root_dir, self.relpath, '.'),
'.') '.')
if options.manually_grab_svn_rev: if options.manually_grab_svn_rev:
# Retrieve the current HEAD version because svn is slow at null updates. # Retrieve the current HEAD version because svn is slow at null updates.
if not revision: if not revision:
from_info_live = CaptureSVNInfo(from_info['URL'], '.') from_info_live = CaptureSVNInfo(options, from_info['URL'], '.')
revision = int(from_info_live['Revision']) revision = int(from_info_live['Revision'])
rev_str = ' at %d' % revision rev_str = ' at %d' % revision
if from_info['URL'] != components[0]: if from_info['URL'] != components[0]:
to_info = CaptureSVNInfo(url, '.') to_info = CaptureSVNInfo(options, url, '.')
if from_info['Repository Root'] != to_info['Repository Root']: if from_info['Repository Root'] != to_info['Repository Root']:
# We have different roots, so check if we can switch --relocate. # We have different roots, so check if we can switch --relocate.
# Subversion only permits this if the repository UUIDs match. # Subversion only permits this if the repository UUIDs match.
@ -817,7 +814,7 @@ class SCMWrapper(object):
from_info['Repository Root'], from_info['Repository Root'],
to_info['Repository Root'], to_info['Repository Root'],
self.relpath] self.relpath]
RunSVN(command, self._root_dir) RunSVN(options, command, self._root_dir)
from_info['URL'] = from_info['URL'].replace( from_info['URL'] = from_info['URL'].replace(
from_info['Repository Root'], from_info['Repository Root'],
to_info['Repository Root']) to_info['Repository Root'])
@ -826,13 +823,14 @@ class SCMWrapper(object):
# number of the existing directory, then we don't need to bother updating. # number of the existing directory, then we don't need to bother updating.
if not options.force and from_info['Revision'] == revision: if not options.force and from_info['Revision'] == revision:
if options.verbose or not forced_revision: if options.verbose or not forced_revision:
print("\n_____ %s%s" % (self.relpath, rev_str)) print >>options.stdout, ("\n_____ %s%s" % (
self.relpath, rev_str))
return return
command = ["update", os.path.join(self._root_dir, self.relpath)] command = ["update", os.path.join(self._root_dir, self.relpath)]
if revision: if revision:
command.extend(['--revision', str(revision)]) command.extend(['--revision', str(revision)])
RunSVNAndGetFileList(command, self._root_dir, file_list) RunSVNAndGetFileList(options, command, self._root_dir, file_list)
def revert(self, options, args, file_list): def revert(self, options, args, file_list):
"""Reverts local modifications. Subversion specific. """Reverts local modifications. Subversion specific.
@ -844,16 +842,17 @@ class SCMWrapper(object):
if not os.path.isdir(path): if not os.path.isdir(path):
# svn revert won't work if the directory doesn't exist. It needs to # svn revert won't work if the directory doesn't exist. It needs to
# checkout instead. # checkout instead.
print("\n_____ %s is missing, synching instead" % self.relpath) print >>options.stdout, ("\n_____ %s is missing, synching instead" %
self.relpath)
# Don't reuse the args. # Don't reuse the args.
return self.update(options, [], file_list) return self.update(options, [], file_list)
files = CaptureSVNStatus(path) files = CaptureSVNStatus(options, path)
# Batch the command. # Batch the command.
files_to_revert = [] files_to_revert = []
for file in files: for file in files:
file_path = os.path.join(path, file.path) file_path = os.path.join(path, file.path)
print(file_path) print >>options.stdout, file_path
# Unversioned file or unexpected unversioned file. # Unversioned file or unexpected unversioned file.
if file.text_status in ('?', '~'): if file.text_status in ('?', '~'):
# Remove extraneous file. Also remove unexpected unversioned # Remove extraneous file. Also remove unexpected unversioned
@ -877,7 +876,7 @@ class SCMWrapper(object):
for p in files_to_revert: for p in files_to_revert:
# Some shell have issues with command lines too long. # Some shell have issues with command lines too long.
if accumulated_length and accumulated_length + len(p) > 3072: if accumulated_length and accumulated_length + len(p) > 3072:
RunSVN(command + accumulated_paths, RunSVN(options, command + accumulated_paths,
os.path.join(self._root_dir, self.relpath)) os.path.join(self._root_dir, self.relpath))
accumulated_paths = [] accumulated_paths = []
accumulated_length = 0 accumulated_length = 0
@ -885,7 +884,7 @@ class SCMWrapper(object):
accumulated_paths.append(p) accumulated_paths.append(p)
accumulated_length += len(p) accumulated_length += len(p)
if accumulated_paths: if accumulated_paths:
RunSVN(command + accumulated_paths, RunSVN(options, command + accumulated_paths,
os.path.join(self._root_dir, self.relpath)) os.path.join(self._root_dir, self.relpath))
def status(self, options, args, file_list): def status(self, options, args, file_list):
@ -895,12 +894,13 @@ class SCMWrapper(object):
command.extend(args) command.extend(args)
if not os.path.isdir(path): if not os.path.isdir(path):
# svn status won't work if the directory doesn't exist. # svn status won't work if the directory doesn't exist.
print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory " print >> options.stdout, (
"\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
"does not exist." "does not exist."
% (' '.join(command), path)) % (' '.join(command), path))
# There's no file list to retrieve. # There's no file list to retrieve.
else: else:
RunSVNAndGetFileList(command, path, file_list) RunSVNAndGetFileList(options, command, path, file_list)
## GClient implementation. ## GClient implementation.
@ -1181,7 +1181,8 @@ class GClient(object):
# Use a discrete exit status code of 2 to indicate that a hook action # Use a discrete exit status code of 2 to indicate that a hook action
# failed. Users of this script may wish to treat hook action failures # failed. Users of this script may wish to treat hook action failures
# differently from VC failures. # differently from VC failures.
SubprocessCall(command, self._root_dir, fail_status=2) SubprocessCall(command, self._root_dir, self._options.stdout,
fail_status=2)
def _RunHooks(self, command, file_list, is_using_git): def _RunHooks(self, command, file_list, is_using_git):
"""Evaluates all hooks, running actions as needed. """Evaluates all hooks, running actions as needed.
@ -1324,14 +1325,15 @@ class GClient(object):
for entry in prev_entries: for entry in prev_entries:
e_dir = os.path.join(self._root_dir, entry) e_dir = os.path.join(self._root_dir, entry)
if entry not in entries and self._options.path_exists(e_dir): if entry not in entries and self._options.path_exists(e_dir):
if CaptureSVNStatus(e_dir): if CaptureSVNStatus(self._options, e_dir):
# There are modified files in this entry # There are modified files in this entry
entries[entry] = None # Keep warning until removed. entries[entry] = None # Keep warning until removed.
print("\nWARNING: \"%s\" is no longer part of this client. " print >> self._options.stdout, (
"\nWARNING: \"%s\" is no longer part of this client. "
"It is recommended that you manually remove it.\n") % entry "It is recommended that you manually remove it.\n") % entry
else: else:
# Delete the entry # Delete the entry
print("\n________ deleting \'%s\' " + print >> self._options.stdout, ("\n________ deleting \'%s\' " +
"in \'%s\'") % (entry, self._root_dir) "in \'%s\'") % (entry, self._root_dir)
RemoveDirectory(e_dir) RemoveDirectory(e_dir)
# record the current list of entries for next time # record the current list of entries for next time
@ -1381,7 +1383,8 @@ class GClient(object):
return (original_url, int(revision_overrides[name])) return (original_url, int(revision_overrides[name]))
else: else:
# TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset) # TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset)
return (original_url, CaptureSVNHeadRevision(original_url)) return (original_url, CaptureSVNHeadRevision(self._options,
original_url))
else: else:
url_components = original_url.split("@") url_components = original_url.split("@")
if revision_overrides.has_key(name): if revision_overrides.has_key(name):
@ -1398,6 +1401,7 @@ class GClient(object):
entries[name] = "%s@%d" % (url, rev) entries[name] = "%s@%d" % (url, rev)
# TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset) # TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset)
entries_deps_content[name] = CaptureSVN( entries_deps_content[name] = CaptureSVN(
self._options,
["cat", ["cat",
"%s/%s@%d" % (url, "%s/%s@%d" % (url,
self._options.deps_file, self._options.deps_file,
@ -1425,6 +1429,7 @@ class GClient(object):
deps_parent_url_components = deps_parent_url.split("@") deps_parent_url_components = deps_parent_url.split("@")
# TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset) # TODO(aharper): SVN/SCMWrapper cleanup (non-local commandset)
deps_parent_content = CaptureSVN( deps_parent_content = CaptureSVN(
self._options,
["cat", ["cat",
"%s/%s@%s" % (deps_parent_url_components[0], "%s/%s@%s" % (deps_parent_url_components[0],
self._options.deps_file, self._options.deps_file,
@ -1439,7 +1444,7 @@ class GClient(object):
(url, rev) = GetURLAndRev(d, sub_deps[d]) (url, rev) = GetURLAndRev(d, sub_deps[d])
entries[d] = "%s@%d" % (url, rev) entries[d] = "%s@%d" % (url, rev)
print(";".join(["%s,%s" % (x, entries[x]) for x in sorted(entries.keys())])) print ";".join(["%s,%s" % (x, entries[x]) for x in sorted(entries.keys())])
## gclient commands. ## gclient commands.
@ -1457,7 +1462,7 @@ def DoCleanup(options, args):
if options.verbose: if options.verbose:
# Print out the .gclient file. This is longer than if we just printed the # Print out the .gclient file. This is longer than if we just printed the
# client dict, but more legible, and it might contain helpful comments. # client dict, but more legible, and it might contain helpful comments.
print(client.ConfigContent()) print >>options.stdout, client.ConfigContent()
options.verbose = True options.verbose = True
return client.RunOnDeps('cleanup', args) return client.RunOnDeps('cleanup', args)
@ -1500,7 +1505,7 @@ def DoHelp(options, args):
Error: if the command is unknown. Error: if the command is unknown.
""" """
if len(args) == 1 and args[0] in COMMAND_USAGE_TEXT: if len(args) == 1 and args[0] in COMMAND_USAGE_TEXT:
print(COMMAND_USAGE_TEXT[args[0]]) print >>options.stdout, COMMAND_USAGE_TEXT[args[0]]
else: else:
raise Error("unknown subcommand '%s'; see 'gclient help'" % args[0]) raise Error("unknown subcommand '%s'; see 'gclient help'" % args[0])
@ -1517,7 +1522,7 @@ def DoStatus(options, args):
if options.verbose: if options.verbose:
# Print out the .gclient file. This is longer than if we just printed the # Print out the .gclient file. This is longer than if we just printed the
# client dict, but more legible, and it might contain helpful comments. # client dict, but more legible, and it might contain helpful comments.
print(client.ConfigContent()) print >>options.stdout, client.ConfigContent()
options.verbose = True options.verbose = True
return client.RunOnDeps('status', args) return client.RunOnDeps('status', args)
@ -1556,7 +1561,7 @@ def DoUpdate(options, args):
if options.verbose: if options.verbose:
# Print out the .gclient file. This is longer than if we just printed the # Print out the .gclient file. This is longer than if we just printed the
# client dict, but more legible, and it might contain helpful comments. # client dict, but more legible, and it might contain helpful comments.
print(client.ConfigContent()) print >>options.stdout, client.ConfigContent()
return client.RunOnDeps('update', args) return client.RunOnDeps('update', args)
@ -1572,7 +1577,7 @@ def DoDiff(options, args):
if options.verbose: if options.verbose:
# Print out the .gclient file. This is longer than if we just printed the # Print out the .gclient file. This is longer than if we just printed the
# client dict, but more legible, and it might contain helpful comments. # client dict, but more legible, and it might contain helpful comments.
print(client.ConfigContent()) print >>options.stdout, client.ConfigContent()
options.verbose = True options.verbose = True
return client.RunOnDeps('diff', args) return client.RunOnDeps('diff', args)
@ -1601,7 +1606,7 @@ def DoRunHooks(options, args):
if options.verbose: if options.verbose:
# Print out the .gclient file. This is longer than if we just printed the # Print out the .gclient file. This is longer than if we just printed the
# client dict, but more legible, and it might contain helpful comments. # client dict, but more legible, and it might contain helpful comments.
print(client.ConfigContent()) print >>options.stdout, client.ConfigContent()
return client.RunOnDeps('runhooks', args) return client.RunOnDeps('runhooks', args)
@ -1700,6 +1705,8 @@ def Main(argv):
options.entries_filename = ".gclient_entries" options.entries_filename = ".gclient_entries"
options.deps_file = "DEPS" options.deps_file = "DEPS"
# These are overridded when testing. They are not externally visible.
options.stdout = sys.stdout
options.path_exists = os.path.exists options.path_exists = os.path.exists
options.gclient = GClient options.gclient = GClient
options.scm_wrapper = SCMWrapper options.scm_wrapper = SCMWrapper
@ -1711,7 +1718,7 @@ if "__main__" == __name__:
try: try:
result = Main(sys.argv) result = Main(sys.argv)
except Error, e: except Error, e:
print >> sys.stderr, "Error: %s" % str(e) print "Error: %s" % str(e)
result = 1 result = 1
sys.exit(result) sys.exit(result)

@ -77,21 +77,6 @@ class BaseTestCase(unittest.TestCase):
else: else:
self.fail('%s not raised' % msg) self.fail('%s not raised' % msg)
def compareMembers(self, object, members):
"""If you add a member, be sure to add the relevant test!"""
# Skip over members starting with '_' since they are usually not meant to
# be for public use.
actual_members = [x for x in sorted(dir(object))
if not x.startswith('_')]
expected_members = sorted(members)
if actual_members != expected_members:
diff = ([i for i in actual_members if i not in expected_members] +
[i for i in expected_members if i not in actual_members])
print diff
self.assertEqual(actual_members, expected_members)
class GClientBaseTestCase(BaseTestCase):
def Options(self, *args, **kwargs): def Options(self, *args, **kwargs):
return self.OptionsObject(self, *args, **kwargs) return self.OptionsObject(self, *args, **kwargs)
@ -114,8 +99,11 @@ class GClientBaseTestCase(BaseTestCase):
gclient.RunSVN = self.mox.CreateMockAnything() gclient.RunSVN = self.mox.CreateMockAnything()
self._RunSVNAndGetFileList = gclient.RunSVNAndGetFileList self._RunSVNAndGetFileList = gclient.RunSVNAndGetFileList
gclient.RunSVNAndGetFileList = self.mox.CreateMockAnything() gclient.RunSVNAndGetFileList = self.mox.CreateMockAnything()
self._sys_stdout = gclient.sys.stdout # Doesn't seem to work very well:
gclient.sys.stdout = self.mox.CreateMock(self._sys_stdout) self._os = gclient.os
gclient.os = self.mox.CreateMock(os)
self._sys = gclient.sys
gclient.sys = self.mox.CreateMock(sys)
self._subprocess = gclient.subprocess self._subprocess = gclient.subprocess
gclient.subprocess = self.mox.CreateMock(subprocess) gclient.subprocess = self.mox.CreateMock(subprocess)
@ -128,11 +116,13 @@ class GClientBaseTestCase(BaseTestCase):
gclient.RemoveDirectory = self._RemoveDirectory gclient.RemoveDirectory = self._RemoveDirectory
gclient.RunSVN = self._RunSVN gclient.RunSVN = self._RunSVN
gclient.RunSVNAndGetFileList = self._RunSVNAndGetFileList gclient.RunSVNAndGetFileList = self._RunSVNAndGetFileList
gclient.sys.stdout = self._sys_stdout # Doesn't seem to work very well:
gclient.os = self._os
gclient.sys = self._sys
gclient.subprocess = self._subprocess gclient.subprocess = self._subprocess
class GclientTestCase(GClientBaseTestCase): class GclientTestCase(BaseTestCase):
class OptionsObject(object): class OptionsObject(object):
def __init__(self, test_case, verbose=False, spec=None, def __init__(self, test_case, verbose=False, spec=None,
config_filename='a_file_name', config_filename='a_file_name',
@ -150,16 +140,19 @@ class GclientTestCase(GClientBaseTestCase):
self.head = False self.head = False
# Mox # Mox
self.stdout = test_case.stdout
self.path_exists = test_case.path_exists self.path_exists = test_case.path_exists
self.platform = test_case.platform self.platform = test_case.platform
self.gclient = test_case.gclient self.gclient = test_case.gclient
self.scm_wrapper = test_case.scm_wrapper self.scm_wrapper = test_case.scm_wrapper
def setUp(self): def setUp(self):
GClientBaseTestCase.setUp(self) BaseTestCase.setUp(self)
self.stdout = self.mox.CreateMock(sys.stdout)
#self.subprocess = self.mox.CreateMock(subprocess) #self.subprocess = self.mox.CreateMock(subprocess)
# Stub os.path.exists. # Stub os.path.exists.
self.path_exists = self.mox.CreateMockAnything() self.path_exists = self.mox.CreateMockAnything()
self.sys = self.mox.CreateMock(sys)
self.platform = 'darwin' self.platform = 'darwin'
self.gclient = self.mox.CreateMock(gclient.GClient) self.gclient = self.mox.CreateMock(gclient.GClient)
@ -170,7 +163,7 @@ class GclientTestCase(GClientBaseTestCase):
self.url = Url() self.url = Url()
class GClientCommandsTestCase(GClientBaseTestCase): class GClientCommandsTestCase(BaseTestCase):
def testCommands(self): def testCommands(self):
known_commands = [gclient.DoCleanup, gclient.DoConfig, gclient.DoDiff, known_commands = [gclient.DoCleanup, gclient.DoConfig, gclient.DoDiff,
gclient.DoHelp, gclient.DoStatus, gclient.DoUpdate, gclient.DoHelp, gclient.DoStatus, gclient.DoUpdate,
@ -231,23 +224,24 @@ class TestDoConfig(GclientTestCase):
class TestDoHelp(GclientTestCase): class TestDoHelp(GclientTestCase):
def testGetUsage(self): def testGetUsage(self):
print(gclient.COMMAND_USAGE_TEXT['config'])
self.mox.ReplayAll()
options = self.Options() options = self.Options()
print >> options.stdout, gclient.COMMAND_USAGE_TEXT['config']
self.mox.ReplayAll()
gclient.DoHelp(options, ('config',)) gclient.DoHelp(options, ('config',))
self.mox.VerifyAll() self.mox.VerifyAll()
def testTooManyArgs(self): def testTooManyArgs(self):
self.mox.ReplayAll()
options = self.Options() options = self.Options()
self.mox.ReplayAll()
self.assertRaisesError("unknown subcommand 'config'; see 'gclient help'", self.assertRaisesError("unknown subcommand 'config'; see 'gclient help'",
gclient.DoHelp, options, ('config', gclient.DoHelp, options, ('config',
'another argument')) 'another argument'))
self.mox.VerifyAll() self.mox.VerifyAll()
def testUnknownSubcommand(self): def testUnknownSubcommand(self):
self.mox.ReplayAll()
options = self.Options() options = self.Options()
self.mox.ReplayAll()
self.assertRaisesError("unknown subcommand 'xyzzy'; see 'gclient help'", self.assertRaisesError("unknown subcommand 'xyzzy'; see 'gclient help'",
gclient.DoHelp, options, ('xyzzy',)) gclient.DoHelp, options, ('xyzzy',))
self.mox.VerifyAll() self.mox.VerifyAll()
@ -279,7 +273,7 @@ class GenericCommandTestCase(GclientTestCase):
self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient) self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient)
text = "# Dummy content\nclient = 'my client'" text = "# Dummy content\nclient = 'my client'"
self.gclient.ConfigContent().AndReturn(text) self.gclient.ConfigContent().AndReturn(text)
print(text) print >>self.stdout, text
self.gclient.RunOnDeps(command, self.args).AndReturn(0) self.gclient.RunOnDeps(command, self.args).AndReturn(0)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -337,7 +331,7 @@ class TestDoUpdate(GenericCommandTestCase):
self.gclient.GetVar("solutions") self.gclient.GetVar("solutions")
text = "# Dummy content\nclient = 'my client'" text = "# Dummy content\nclient = 'my client'"
self.gclient.ConfigContent().AndReturn(text) self.gclient.ConfigContent().AndReturn(text)
print(text) print >>self.stdout, text
self.gclient.RunOnDeps(command, self.args).AndReturn(0) self.gclient.RunOnDeps(command, self.args).AndReturn(0)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -383,14 +377,18 @@ class TestDoRevert(GenericCommandTestCase):
class GClientClassTestCase(GclientTestCase): class GClientClassTestCase(GclientTestCase):
def testDir(self): def testDir(self):
members = [ members = ['ConfigContent', 'FromImpl', '_VarImpl', '_ParseAllDeps',
'ConfigContent', 'FromImpl', 'GetVar', 'LoadCurrentConfig', '_ParseSolutionDeps', 'GetVar', '_LoadConfig', 'LoadCurrentConfig',
'RunOnDeps', 'SaveConfig', 'SetConfig', 'SetDefaultConfig', '_ReadEntries', '_RunHookAction', '_RunHooks', 'RunOnDeps', 'SaveConfig',
'supported_commands', 'PrintRevInfo', '_SaveEntries', 'SetConfig', 'SetDefaultConfig', 'supported_commands',
] 'PrintRevInfo']
# If you add a member, be sure to add the relevant test! # If you add a member, be sure to add the relevant test!
self.compareMembers(gclient.GClient('root_dir', 'options'), members) actual_members = [x for x in sorted(dir(gclient.GClient))
if not x.startswith('__')]
self.assertEqual(actual_members, sorted(members))
self.mox.ReplayAll()
self.mox.VerifyAll()
def testSetConfig_ConfigContent_GetVar_SaveConfig_SetDefaultConfig(self): def testSetConfig_ConfigContent_GetVar_SaveConfig_SetDefaultConfig(self):
options = self.Options() options = self.Options()
@ -733,11 +731,11 @@ class GClientClassTestCase(GclientTestCase):
def testRunOnDepsRevisions(self): def testRunOnDepsRevisions(self):
def OptIsRev(options, rev): def OptIsRev(options, rev):
if not options.revision == str(rev): if not options.revision == str(rev):
print("options.revision = %s" % options.revision) print "options.revision = %s" % options.revision
return options.revision == str(rev) return options.revision == str(rev)
def OptIsRevNone(options): def OptIsRevNone(options):
if options.revision: if options.revision:
print("options.revision = %s" % options.revision) print "options.revision = %s" % options.revision
return options.revision == None return options.revision == None
def OptIsRev42(options): def OptIsRev42(options):
return OptIsRev(options, 42) return OptIsRev(options, 42)
@ -1060,7 +1058,7 @@ deps = {
pass pass
class SCMWrapperTestCase(GClientBaseTestCase): class SCMWrapperTestCase(BaseTestCase):
class OptionsObject(object): class OptionsObject(object):
def __init__(self, test_case, verbose=False, revision=None): def __init__(self, test_case, verbose=False, revision=None):
self.verbose = verbose self.verbose = verbose
@ -1070,25 +1068,29 @@ class SCMWrapperTestCase(GClientBaseTestCase):
self.force = False self.force = False
# Mox # Mox
self.stdout = test_case.stdout
self.path_exists = test_case.path_exists self.path_exists = test_case.path_exists
def setUp(self): def setUp(self):
GClientBaseTestCase.setUp(self) BaseTestCase.setUp(self)
self.root_dir = Dir() self.root_dir = Dir()
self.args = Args() self.args = Args()
self.url = Url() self.url = Url()
self.relpath = 'asf' self.relpath = 'asf'
self.stdout = self.mox.CreateMock(sys.stdout)
# Stub os.path.exists. # Stub os.path.exists.
self.path_exists = self.mox.CreateMockAnything() self.path_exists = self.mox.CreateMockAnything()
def testDir(self): def testDir(self):
members = [ members = ['FullUrlForRelativeUrl', 'RunCommand',
'FullUrlForRelativeUrl', 'RunCommand', 'cleanup', 'diff', 'relpath', 'cleanup', 'diff', 'revert', 'status', 'update']
'revert', 'scm_name', 'status', 'update', 'url',
]
# If you add a member, be sure to add the relevant test! # If you add a member, be sure to add the relevant test!
self.compareMembers(gclient.SCMWrapper(), members) actual_members = [x for x in sorted(dir(gclient.SCMWrapper))
if not x.startswith('__')]
self.assertEqual(actual_members, sorted(members))
self.mox.ReplayAll()
self.mox.VerifyAll()
def testFullUrlForRelativeUrl(self): def testFullUrlForRelativeUrl(self):
self.url = 'svn://a/b/c/d' self.url = 'svn://a/b/c/d'
@ -1123,11 +1125,12 @@ class SCMWrapperTestCase(GClientBaseTestCase):
gclient.os.path.isdir(base_path).AndReturn(False) gclient.os.path.isdir(base_path).AndReturn(False)
# It'll to a checkout instead. # It'll to a checkout instead.
options.path_exists(os.path.join(base_path, '.git')).AndReturn(False) options.path_exists(os.path.join(base_path, '.git')).AndReturn(False)
print("\n_____ %s is missing, synching instead" % self.relpath) print >>options.stdout, ("\n_____ %s is missing, synching instead" %
self.relpath)
# Checkout. # Checkout.
options.path_exists(base_path).AndReturn(False) options.path_exists(base_path).AndReturn(False)
files_list = self.mox.CreateMockAnything() files_list = self.mox.CreateMockAnything()
gclient.RunSVNAndGetFileList(['checkout', self.url, base_path], gclient.RunSVNAndGetFileList(options, ['checkout', self.url, base_path],
self.root_dir, files_list) self.root_dir, files_list)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -1142,7 +1145,7 @@ class SCMWrapperTestCase(GClientBaseTestCase):
base_path = os.path.join(self.root_dir, self.relpath) base_path = os.path.join(self.root_dir, self.relpath)
gclient.os.path.isdir = self.mox.CreateMockAnything() gclient.os.path.isdir = self.mox.CreateMockAnything()
gclient.os.path.isdir(base_path).AndReturn(True) gclient.os.path.isdir(base_path).AndReturn(True)
gclient.CaptureSVNStatus(base_path).AndReturn([]) gclient.CaptureSVNStatus(options, base_path).AndReturn([])
self.mox.ReplayAll() self.mox.ReplayAll()
scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir,
@ -1161,11 +1164,11 @@ class SCMWrapperTestCase(GClientBaseTestCase):
gclient.FileStatus('a', 'M', ' ', ' ', ' '), gclient.FileStatus('a', 'M', ' ', ' ', ' '),
gclient.FileStatus('b', 'A', ' ', ' ', ' '), gclient.FileStatus('b', 'A', ' ', ' ', ' '),
] ]
gclient.CaptureSVNStatus(base_path).AndReturn(items) gclient.CaptureSVNStatus(options, base_path).AndReturn(items)
print(os.path.join(base_path, 'a')) print >>options.stdout, os.path.join(base_path, 'a')
print(os.path.join(base_path, 'b')) print >>options.stdout, os.path.join(base_path, 'b')
gclient.RunSVN(['revert', 'a', 'b'], base_path) gclient.RunSVN(options, ['revert', 'a', 'b'], base_path)
self.mox.ReplayAll() self.mox.ReplayAll()
scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir,
@ -1180,7 +1183,7 @@ class SCMWrapperTestCase(GClientBaseTestCase):
base_path = os.path.join(self.root_dir, self.relpath) base_path = os.path.join(self.root_dir, self.relpath)
gclient.os.path.isdir = self.mox.CreateMockAnything() gclient.os.path.isdir = self.mox.CreateMockAnything()
gclient.os.path.isdir(base_path).AndReturn(True) gclient.os.path.isdir(base_path).AndReturn(True)
gclient.RunSVNAndGetFileList(['status'] + self.args, base_path, gclient.RunSVNAndGetFileList(options, ['status'] + self.args, base_path,
[]).AndReturn(None) []).AndReturn(None)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -1205,7 +1208,7 @@ class SCMWrapperTestCase(GClientBaseTestCase):
# Checkout. # Checkout.
options.path_exists(base_path).AndReturn(False) options.path_exists(base_path).AndReturn(False)
files_list = self.mox.CreateMockAnything() files_list = self.mox.CreateMockAnything()
gclient.RunSVNAndGetFileList(['checkout', self.url, base_path], gclient.RunSVNAndGetFileList(options, ['checkout', self.url, base_path],
self.root_dir, files_list) self.root_dir, files_list)
self.mox.ReplayAll() self.mox.ReplayAll()
scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir,
@ -1226,15 +1229,15 @@ class SCMWrapperTestCase(GClientBaseTestCase):
options.path_exists(os.path.join(base_path, '.git')).AndReturn(False) options.path_exists(os.path.join(base_path, '.git')).AndReturn(False)
# Checkout or update. # Checkout or update.
options.path_exists(base_path).AndReturn(True) options.path_exists(base_path).AndReturn(True)
gclient.CaptureSVNInfo(os.path.join(base_path, "."), '.' gclient.CaptureSVNInfo(options, os.path.join(base_path, "."), '.'
).AndReturn(file_info) ).AndReturn(file_info)
# Cheat a bit here. # Cheat a bit here.
gclient.CaptureSVNInfo(file_info['URL'], '.').AndReturn(file_info) gclient.CaptureSVNInfo(options, file_info['URL'], '.').AndReturn(file_info)
additional_args = [] additional_args = []
if options.manually_grab_svn_rev: if options.manually_grab_svn_rev:
additional_args = ['--revision', str(file_info['Revision'])] additional_args = ['--revision', str(file_info['Revision'])]
files_list = [] files_list = []
gclient.RunSVNAndGetFileList(['update', base_path] + additional_args, gclient.RunSVNAndGetFileList(options, ['update', base_path] + additional_args,
self.root_dir, files_list) self.root_dir, files_list)
self.mox.ReplayAll() self.mox.ReplayAll()
@ -1247,7 +1250,8 @@ class SCMWrapperTestCase(GClientBaseTestCase):
options = self.Options(verbose=True) options = self.Options(verbose=True)
options.path_exists(os.path.join(self.root_dir, self.relpath, '.git') options.path_exists(os.path.join(self.root_dir, self.relpath, '.git')
).AndReturn(True) ).AndReturn(True)
print("________ found .git directory; skipping %s" % self.relpath) print >> options.stdout, (
"________ found .git directory; skipping %s" % self.relpath)
self.mox.ReplayAll() self.mox.ReplayAll()
scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir,
@ -1272,7 +1276,9 @@ class SCMWrapperTestCase(GClientBaseTestCase):
</entry> </entry>
</info> </info>
""" % self.url """ % self.url
gclient.CaptureSVN(['info', '--xml', self.url], '.').AndReturn(xml_text) options = self.Options(verbose=True)
gclient.CaptureSVN(options, ['info', '--xml', self.url],
'.').AndReturn(xml_text)
expected = { expected = {
'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d',
'UUID': None, 'UUID': None,
@ -1285,7 +1291,7 @@ class SCMWrapperTestCase(GClientBaseTestCase):
'Node Kind': 'file', 'Node Kind': 'file',
} }
self.mox.ReplayAll() self.mox.ReplayAll()
file_info = self._CaptureSVNInfo(self.url, '.') file_info = self._CaptureSVNInfo(options, self.url, '.')
self.assertEquals(sorted(file_info.items()), sorted(expected.items())) self.assertEquals(sorted(file_info.items()), sorted(expected.items()))
self.mox.VerifyAll() self.mox.VerifyAll()
@ -1313,9 +1319,11 @@ class SCMWrapperTestCase(GClientBaseTestCase):
</entry> </entry>
</info> </info>
""" % (self.url, self.root_dir) """ % (self.url, self.root_dir)
gclient.CaptureSVN(['info', '--xml', self.url], '.').AndReturn(xml_text) options = self.Options(verbose=True)
gclient.CaptureSVN(options, ['info', '--xml', self.url],
'.').AndReturn(xml_text)
self.mox.ReplayAll() self.mox.ReplayAll()
file_info = self._CaptureSVNInfo(self.url, '.') file_info = self._CaptureSVNInfo(options, self.url, '.')
expected = { expected = {
'URL': self.url, 'URL': self.url,
'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb',
@ -1331,23 +1339,6 @@ class SCMWrapperTestCase(GClientBaseTestCase):
self.mox.VerifyAll() self.mox.VerifyAll()
class RunSVNTestCase(BaseTestCase):
def setUp(self):
self.mox = mox.Mox()
self._OldSubprocessCall = gclient.SubprocessCall
gclient.SubprocessCall = self.mox.CreateMockAnything()
def tearDown(self):
gclient.SubprocessCall = self._OldSubprocessCall
def testRunSVN(self):
param2 = 'bleh'
gclient.SubprocessCall(['svn', 'foo', 'bar'], param2).AndReturn(None)
self.mox.ReplayAll()
gclient.RunSVN(['foo', 'bar'], param2)
self.mox.VerifyAll()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save