Commit dd218e59 authored by maruel@chromium.org's avatar maruel@chromium.org

Make codereview.setting syntax more liberal and add more unit tests.

Improve gclient_utils to test output lacking trailing LF

BUG=none
TEST=new unit tests

Review URL: http://codereview.chromium.org/3140023

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@57047 0039d316-1c4b-4281-b951-d872f2087c98
parent a488c7d7
...@@ -191,8 +191,8 @@ def GetCodeReviewSetting(key): ...@@ -191,8 +191,8 @@ def GetCodeReviewSetting(key):
raise gclient_utils.Error( raise gclient_utils.Error(
'%s is invalid, please fix. It\'s content:\n\n%s' % '%s is invalid, please fix. It\'s content:\n\n%s' %
(CODEREVIEW_SETTINGS_FILE, settings_file)) (CODEREVIEW_SETTINGS_FILE, settings_file))
k, v = line.split(': ', 1) k, v = line.split(':', 1)
CODEREVIEW_SETTINGS[k] = v CODEREVIEW_SETTINGS[k.strip()] = v.strip()
CODEREVIEW_SETTINGS.setdefault('__just_initialized', None) CODEREVIEW_SETTINGS.setdefault('__just_initialized', None)
return CODEREVIEW_SETTINGS.get(key, "") return CODEREVIEW_SETTINGS.get(key, "")
......
...@@ -28,6 +28,9 @@ class GclTestsBase(SuperMoxTestBase): ...@@ -28,6 +28,9 @@ class GclTestsBase(SuperMoxTestBase):
class GclUnittest(GclTestsBase): class GclUnittest(GclTestsBase):
"""General gcl.py tests.""" """General gcl.py tests."""
def tearDown(self):
gcl.CODEREVIEW_SETTINGS = {}
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll() self.mox.ReplayAll()
members = [ members = [
...@@ -73,6 +76,39 @@ class GclUnittest(GclTestsBase): ...@@ -73,6 +76,39 @@ class GclUnittest(GclTestsBase):
# TODO(maruel): TEST ME # TODO(maruel): TEST ME
pass pass
def testDefaultSettings(self):
self.assertEquals({}, gcl.CODEREVIEW_SETTINGS)
def testGetCodeReviewSettingOk(self):
self.mox.StubOutWithMock(gcl, 'GetCachedFile')
gcl.GetCachedFile(gcl.CODEREVIEW_SETTINGS_FILE).AndReturn(
'foo:bar\n'
'# comment\n'
' c : d \n\r'
'e: f')
self.mox.ReplayAll()
self.assertEquals('bar', gcl.GetCodeReviewSetting('foo'))
self.assertEquals('d', gcl.GetCodeReviewSetting('c'))
self.assertEquals('f', gcl.GetCodeReviewSetting('e'))
self.assertEquals('', gcl.GetCodeReviewSetting('other'))
self.assertEquals(
{'foo': 'bar', 'c': 'd', 'e': 'f', '__just_initialized': None},
gcl.CODEREVIEW_SETTINGS)
def testGetCodeReviewSettingFail(self):
self.mox.StubOutWithMock(gcl, 'GetCachedFile')
gcl.GetCachedFile(gcl.CODEREVIEW_SETTINGS_FILE).AndReturn(
'aaa\n'
' c : d \n\r'
'e: f')
self.mox.ReplayAll()
try:
gcl.GetCodeReviewSetting('c')
self.fail()
except gcl.gclient_utils.Error:
pass
self.assertEquals({}, gcl.CODEREVIEW_SETTINGS)
def testGetRepositoryRootNone(self): def testGetRepositoryRootNone(self):
gcl.os.getcwd().AndReturn(self.fake_root_dir) gcl.os.getcwd().AndReturn(self.fake_root_dir)
gcl.SVN.GetCheckoutRoot(self.fake_root_dir).AndReturn(None) gcl.SVN.GetCheckoutRoot(self.fake_root_dir).AndReturn(None)
......
...@@ -78,30 +78,30 @@ class CheckCallTestCase(GclientUtilBase): ...@@ -78,30 +78,30 @@ class CheckCallTestCase(GclientUtilBase):
class SubprocessCallAndFilterTestCase(GclientUtilBase): class SubprocessCallAndFilterTestCase(GclientUtilBase):
def testSubprocessCallAndFilter(self): class ProcessIdMock(object):
command = ['boo', 'foo', 'bar'] def __init__(self, test_string):
self.stdout = StringIO.StringIO(test_string)
def wait(self):
pass
def _inner(self, command, test_string):
in_directory = 'bleh' in_directory = 'bleh'
fail_status = None
pattern = 'a(.*)b'
test_string = 'ahah\naccb\nallo\naddb\n'
env = gclient_utils.os.environ.copy() env = gclient_utils.os.environ.copy()
env['LANGUAGE'] = 'en' env['LANGUAGE'] = 'en'
class Mock(object):
stdout = StringIO.StringIO(test_string)
def wait(self):
pass
kid = Mock()
print("\n________ running 'boo foo bar' in 'bleh'") print("\n________ running 'boo foo bar' in 'bleh'")
for i in test_string: for i in test_string:
gclient_utils.sys.stdout.write(i) gclient_utils.sys.stdout.write(i)
gclient_utils.subprocess.Popen( gclient_utils.subprocess.Popen(
command, bufsize=0, cwd=in_directory, command,
cwd=in_directory,
shell=(gclient_utils.sys.platform == 'win32'), shell=(gclient_utils.sys.platform == 'win32'),
env=env, env=env,
stdout=gclient_utils.subprocess.PIPE, stdout=gclient_utils.subprocess.PIPE,
stderr=gclient_utils.subprocess.STDOUT).AndReturn(kid) stderr=gclient_utils.subprocess.STDOUT,
bufsize=0).AndReturn(self.ProcessIdMock(test_string))
self.mox.ReplayAll() self.mox.ReplayAll()
compiled_pattern = gclient_utils.re.compile(pattern) compiled_pattern = gclient_utils.re.compile(r'a(.*)b')
line_list = [] line_list = []
capture_list = [] capture_list = []
def FilterLines(line): def FilterLines(line):
...@@ -110,12 +110,21 @@ class SubprocessCallAndFilterTestCase(GclientUtilBase): ...@@ -110,12 +110,21 @@ class SubprocessCallAndFilterTestCase(GclientUtilBase):
if match: if match:
capture_list.append(match.group(1)) capture_list.append(match.group(1))
gclient_utils.SubprocessCallAndFilter( gclient_utils.SubprocessCallAndFilter(
command, in_directory, command, in_directory, True, True, None, FilterLines)
True, True,
fail_status, FilterLines)
self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb'])
self.assertEquals(capture_list, ['cc', 'dd']) self.assertEquals(capture_list, ['cc', 'dd'])
def testSubprocessCallAndFilter(self):
command = ['boo', 'foo', 'bar']
test_string = 'ahah\naccb\nallo\naddb\n'
self._inner(command, test_string)
def testNoLF(self):
# Exactly as testSubprocessCallAndFilter without trailing \n
command = ['boo', 'foo', 'bar']
test_string = 'ahah\naccb\nallo\naddb'
self._inner(command, test_string)
class SplitUrlRevisionTestCase(GclientUtilBase): class SplitUrlRevisionTestCase(GclientUtilBase):
def testSSHUrl(self): def testSSHUrl(self):
......
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