Commit e0426257 authored by davileen's avatar davileen Committed by Commit bot

Add PRESUBMIT tests for _CheckUserActionUpdate function.

Presubmit does not contain any tests for the _CheckUserActionUpdate
function. This commit adds a test to check against an action in
actions.xml and one that is not found there. To do this we have to
improve the MockInputApi and MockFile classes.

BUG=462814

Review URL: https://codereview.chromium.org/966703003

Cr-Commit-Position: refs/heads/master@{#318759}
parent 0d149cf3
......@@ -775,5 +775,39 @@ class TryServerMasterTest(unittest.TestCase):
bot, master, PRESUBMIT.GetTryServerMasterForBot(bot)))
class UserMetricsActionTest(unittest.TestCase):
def testUserMetricsActionInActions(self):
input_api = MockInputApi()
file_with_user_action = 'file_with_user_action.cc'
contents_with_user_action = [
'base::UserMetricsAction("AboutChrome")'
]
input_api.files = [MockFile(file_with_user_action,
contents_with_user_action)]
self.assertEqual(
[], PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()))
def testUserMetricsActionNotAddedToActions(self):
input_api = MockInputApi()
file_with_user_action = 'file_with_user_action.cc'
contents_with_user_action = [
'base::UserMetricsAction("NotInActionsXml")'
]
input_api.files = [MockFile(file_with_user_action,
contents_with_user_action)]
output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi())
self.assertEqual(
('File %s line %d: %s is missing in '
'tools/metrics/actions/actions.xml. Please run '
'tools/metrics/actions/extract_actions.py to update.'
% (file_with_user_action, 1, 'NotInActionsXml')),
output[0].message)
if __name__ == '__main__':
unittest.main()
......@@ -32,6 +32,9 @@ class MockInputApi(object):
def AffectedSourceFiles(self, file_filter=None):
return self.files
def LocalPaths(self):
return self.files
def PresubmitLocalPath(self):
return os.path.dirname(__file__)
......@@ -62,22 +65,22 @@ class MockOutputApi(object):
return self.message
class PresubmitError(PresubmitResult):
def __init__(self, message, items, long_text=''):
def __init__(self, message, items=None, long_text=''):
MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
self.type = 'error'
class PresubmitPromptWarning(PresubmitResult):
def __init__(self, message, items, long_text=''):
def __init__(self, message, items=None, long_text=''):
MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
self.type = 'warning'
class PresubmitNotifyResult(PresubmitResult):
def __init__(self, message, items, long_text=''):
def __init__(self, message, items=None, long_text=''):
MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
self.type = 'notify'
class PresubmitPromptOrNotify(PresubmitResult):
def __init__(self, message, items, long_text=''):
def __init__(self, message, items=None, long_text=''):
MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
self.type = 'promptOrNotify'
......@@ -103,6 +106,14 @@ class MockFile(object):
def LocalPath(self):
return self._local_path
def rfind(self, p):
"""os.path.basename is called on MockFile so we need an rfind method."""
return self._local_path.rfind(p)
def __getitem__(self, i):
"""os.path.basename is called on MockFile so we need a get method."""
return self._local_path[i]
class MockAffectedFile(MockFile):
def AbsoluteLocalPath(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