Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(687)

Unified Diff: tests/presubmit_unittest.py

Issue 118370: Add InputApi.ReadFile() and presubmit_canned_checks.CheckChangeHasNoCR(). (Closed)
Patch Set: Use mode 'rb' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« presubmit_support.py ('K') | « presubmit_support.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/presubmit_unittest.py
diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py
index 8135e5f03e120f3d0e6701d6b534aed185306f08..cc5825f421862b5eb6b28a78edee459514de1c25 100755
--- a/tests/presubmit_unittest.py
+++ b/tests/presubmit_unittest.py
@@ -94,6 +94,12 @@ def CheckChangeOnUpload(input_api, output_api):
if not x.startswith('_')]
self.assertEqual(actual_members, sorted(members))
+ def MakeBasicChange(self, name, description, root=None):
+ ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None)
+ if root is None:
+ root = self.fake_root_dir
+ return presubmit.GclChange(ci, root)
+
class PresubmitUnittest(PresubmitTestsBase):
"""General presubmit_support.py tests (excluding InputApi and OutputApi)."""
@@ -519,7 +525,7 @@ class InputApiUnittest(PresubmitTestsBase):
members = [
'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles',
'DepotToLocalPath', 'LocalPaths', 'LocalToDepotPath',
- 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths',
+ 'PresubmitLocalPath', 'RightHandSideLines', 'ReadFile', 'ServerPaths',
'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change',
'is_committing', 'marshal', 'os_path', 'pickle', 'platform',
're', 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2',
@@ -672,6 +678,34 @@ class InputApiUnittest(PresubmitTestsBase):
api = presubmit.InputApi(change, 'foo/PRESUBMIT.py', True)
api.AffectedTextFiles(include_deletes=False)
+ def testReadFileStringDenied(self):
+ self.mox.ReplayAll()
+ input_api = presubmit.InputApi(None, './p', False)
+ input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA')
+ self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x')
+
+ def testReadFileStringAccepted(self):
+ presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None)
+ self.mox.ReplayAll()
+ input_api = presubmit.InputApi(None, './p', False)
+ input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA')
+ input_api.ReadFile('/AA/boo', 'x')
+
+ def testReadFileAffectedFileDenied(self):
+ file = presubmit.AffectedFile('boo', 'M')
+ self.mox.ReplayAll()
+ input_api = presubmit.InputApi(None, './p', False)
+ input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA')
+ self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x')
+
+ def testReadFileAffectedFileAccepted(self):
+ file = presubmit.AffectedFile('/AA/boo', 'M')
+ presubmit.gcl.ReadFile('/AA/boo', 'x').AndReturn(None)
+ self.mox.ReplayAll()
+ input_api = presubmit.InputApi(None, './p', False)
+ input_api.change = self.MakeBasicChange('foo', 'Foo\n', '/AA')
+ input_api.ReadFile('/AA/boo', 'x')
+
class OuputApiUnittest(PresubmitTestsBase):
"""Tests presubmit.OutputApi."""
@@ -836,14 +870,10 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api.unittest = unittest
return input_api
- def MakeBasicChange(self, name, description):
- ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None)
- return presubmit.GclChange(ci, self.fake_root_dir)
-
def testMembersChanged(self):
self.mox.ReplayAll()
members = [
- 'CheckChangeHasBugField', 'CheckChangeHasNoTabs',
+ 'CheckChangeHasBugField', 'CheckChangeHasNoCR', 'CheckChangeHasNoTabs',
'CheckChangeHasQaField', 'CheckChangeHasTestedField',
'CheckChangeHasTestField', 'CheckDoNotSubmit',
'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles',
@@ -922,6 +952,31 @@ class CannedChecksUnittest(PresubmitTestsBase):
'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT',
presubmit.OutputApi.PresubmitError)
+ def testCheckChangeHasNoCR(self):
+ input_api1 = self.MockInputApi()
+ self.mox.StubOutWithMock(input_api1, 'ReadFile')
+ input_api1.change = self.MakeBasicChange('foo', 'Foo\n')
+ affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile)
+ input_api1.AffectedTextFiles().AndReturn([affected_file1])
+ input_api1.ReadFile(affected_file1, 'rb').AndReturn("Hey!\nHo!\n")
+ input_api2 = self.MockInputApi()
+ self.mox.StubOutWithMock(input_api2, 'ReadFile')
+ input_api2.change = self.MakeBasicChange('foo', 'Foo\n')
+ affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile)
+ input_api2.AffectedTextFiles().AndReturn([affected_file2])
+ input_api2.ReadFile(affected_file2, 'rb').AndReturn("Hey!\r\nHo!\r\n")
+ affected_file2.LocalPath().AndReturn('bar.cc')
+ self.mox.ReplayAll()
+
+ results = presubmit_canned_checks.CheckChangeHasNoCR(
+ input_api1, presubmit.OutputApi)
+ self.assertEquals(results, [])
+ results2 = presubmit_canned_checks.CheckChangeHasNoCR(
+ input_api2, presubmit.OutputApi)
+ self.assertEquals(len(results2), 1)
+ self.assertEquals(results2[0].__class__,
+ presubmit.OutputApi.PresubmitPromptWarning)
+
def testCannedCheckChangeHasNoTabs(self):
self.TestContent(presubmit_canned_checks.CheckChangeHasNoTabs,
'blah blah', 'blah\tblah',
« presubmit_support.py ('K') | « presubmit_support.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698