Index: tests/presubmit_unittest.py |
diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py |
index c11283e3785c30b6cce5f8aa94df502864ada81c..3ed655fadf663994a5bf49fdffd6becb84d4a245 100755 |
--- a/tests/presubmit_unittest.py |
+++ b/tests/presubmit_unittest.py |
@@ -1069,6 +1069,7 @@ class CannedChecksUnittest(PresubmitTestsBase): |
'CheckChangeHasTestField', |
'CheckChangeLintsClean', |
'CheckChangeSvnEolStyle', |
+ 'CheckLicense', |
'CheckSvnModifiedDirectories', |
'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
'CheckDoNotSubmit', |
@@ -1282,6 +1283,66 @@ class CannedChecksUnittest(PresubmitTestsBase): |
'svn:eol-style', 'LF', '', False, |
presubmit.OutputApi.PresubmitNotifyResult, True) |
+ def _LicenseCheck(self, text, license, committing, expected_result): |
+ change = self.mox.CreateMock(presubmit.SvnChange) |
+ change.scm = 'svn' |
+ input_api = self.MockInputApi(change, committing) |
+ affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
+ input_api.AffectedSourceFiles(42).AndReturn([affected_file]) |
+ input_api.ReadFile(affected_file, 'rb').AndReturn(text) |
+ if expected_result: |
+ affected_file.LocalPath().AndReturn('bleh') |
+ |
+ self.mox.ReplayAll() |
+ result = presubmit_canned_checks.CheckLicense( |
+ input_api, presubmit.OutputApi, license, 42) |
+ if expected_result: |
+ self.assertEqual(len(result), 1) |
+ self.assertEqual(result[0].__class__, expected_result) |
+ else: |
+ self.assertEqual(result, []) |
+ |
+ def testCheckLicenseSuccess(self): |
+ text = ( |
+ "#!/bin/python\n" |
+ "# Copyright (c) 2037 Nobody.\n" |
+ "# All Rights Reserved.\n" |
+ "print 'foo'\n" |
+ ) |
+ license = ( |
+ r".*? Copyright \(c\) 2037 Nobody." "\n" |
+ r".*? All Rights Reserved\." "\n" |
+ ) |
+ self._LicenseCheck(text, license, True, None) |
+ |
+ def testCheckLicenseFailCommit(self): |
+ text = ( |
+ "#!/bin/python\n" |
+ "# Copyright (c) 2037 Nobody.\n" |
+ "# All Rights Reserved.\n" |
+ "print 'foo'\n" |
+ ) |
+ license = ( |
+ r".*? Copyright \(c\) 0007 Nobody." "\n" |
+ r".*? All Rights Reserved\." "\n" |
+ ) |
+ self._LicenseCheck(text, license, True, |
+ presubmit.OutputApi.PresubmitPromptWarning) |
+ |
+ def testCheckLicenseFailUpload(self): |
+ text = ( |
+ "#!/bin/python\n" |
+ "# Copyright (c) 2037 Nobody.\n" |
+ "# All Rights Reserved.\n" |
+ "print 'foo'\n" |
+ ) |
+ license = ( |
+ r".*? Copyright \(c\) 0007 Nobody." "\n" |
+ r".*? All Rights Reserved\." "\n" |
+ ) |
+ self._LicenseCheck(text, license, False, |
+ presubmit.OutputApi.PresubmitNotifyResult) |
+ |
def testCannedCheckSvnAccidentalSubmission(self): |
modified_dir_file = 'foo/' |
accidental_submssion_file = 'foo/bar.cc' |