| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright 2023 Google Inc. |
| 3 |
# Use of this source code is governed by a BSD-style license that can be |
| 4 |
# found in the LICENSE file. |
| 5 |
|
| 6 |
import unittest |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
import PRESUBMIT |
| 10 |
|
| 11 |
from PRESUBMIT_test_mocks import MockFile, MockAffectedFile |
| 12 |
from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
| 13 |
|
| 14 |
|
| 15 |
class ReleaseNotesTest(unittest.TestCase): |
| 16 |
def testNoEditTopReleaseNotesNoWarning(self): |
| 17 |
mock_input_api = MockInputApi() |
| 18 |
mock_input_api.files = [ |
| 19 |
MockFile('README.chromium', ''), |
| 20 |
] |
| 21 |
|
| 22 |
mock_output_api = MockOutputApi() |
| 23 |
results = PRESUBMIT._CheckTopReleaseNotesChanged( |
| 24 |
mock_input_api, mock_output_api) |
| 25 |
|
| 26 |
self.assertEqual(0, len(results)) |
| 27 |
|
| 28 |
def testUpdateTopReleaseNotesIssuesWarning(self): |
| 29 |
mock_input_api = MockInputApi() |
| 30 |
mock_input_api.files = [ |
| 31 |
MockFile('RELEASE_NOTES.md', ''), |
| 32 |
] |
| 33 |
|
| 34 |
mock_output_api = MockOutputApi() |
| 35 |
results = PRESUBMIT._CheckTopReleaseNotesChanged( |
| 36 |
mock_input_api, mock_output_api) |
| 37 |
|
| 38 |
self.assertEqual(1, len(results)) |
| 39 |
self.assertIsInstance( |
| 40 |
results[0], mock_output_api.PresubmitPromptWarning, 'Not a warning') |
| 41 |
self.assertTrue(results[0].message.startswith( |
| 42 |
'Do not edit RELEASE_NOTES.md')) |
| 43 |
|
| 44 |
def testUpdateTopReleaseNotesNoWarning(self): |
| 45 |
mock_input_api = MockInputApi() |
| 46 |
mock_input_api.files = [ |
| 47 |
MockFile('RELEASE_NOTES.md', ''), |
| 48 |
MockFile('relnotes/deleted_note.md', ''), |
| 49 |
] |
| 50 |
|
| 51 |
mock_output_api = MockOutputApi() |
| 52 |
results = PRESUBMIT._CheckTopReleaseNotesChanged( |
| 53 |
mock_input_api, mock_output_api) |
| 54 |
|
| 55 |
self.assertEqual(0, len(results)) |
| 56 |
|
| 57 |
def testUpdatePublicHeaderAndNoReleaseNoteGeneratesWarning(self): |
| 58 |
mock_input_api = MockInputApi() |
| 59 |
mock_input_api.files = [ |
| 60 |
MockFile('include/core/SkDrawable.h', ''), |
| 61 |
] |
| 62 |
|
| 63 |
mock_output_api = MockOutputApi() |
| 64 |
results = PRESUBMIT._CheckReleaseNotesForPublicAPI( |
| 65 |
mock_input_api, mock_output_api) |
| 66 |
|
| 67 |
self.assertEqual(1, len(results)) |
| 68 |
self.assertIsInstance( |
| 69 |
results[0], mock_output_api.PresubmitPromptWarning, 'Not a warning') |
| 70 |
|
| 71 |
def testUpdatePublicHeaderAndReleaseNoteGeneratesNoWarning(self): |
| 72 |
mock_input_api = MockInputApi() |
| 73 |
mock_input_api.files = [ |
| 74 |
MockFile('include/core/SkDrawable.h', ''), |
| 75 |
MockFile('relnotes/new_note.md', ''), |
| 76 |
] |
| 77 |
|
| 78 |
mock_output_api = MockOutputApi() |
| 79 |
results = PRESUBMIT._CheckReleaseNotesForPublicAPI( |
| 80 |
mock_input_api, mock_output_api) |
| 81 |
|
| 82 |
self.assertEqual(0, len(results)) |
| 83 |
|
| 84 |
|
| 85 |
class RunCommandAndCheckDiffTest(unittest.TestCase): |
| 86 |
def setUp(self): |
| 87 |
self.foo_file = MockAffectedFile('foo.txt', new_contents=['foo']) |
| 88 |
self.bar_file = MockAffectedFile('bar.txt', new_contents=['bar']) |
| 89 |
|
| 90 |
self.mock_input_api = MockInputApi() |
| 91 |
self.mock_input_api.files = [self.foo_file, self.bar_file] |
| 92 |
self.mock_output_api = MockOutputApi() |
| 93 |
|
| 94 |
def setContents(self, file, contents): |
| 95 |
file._new_contents = contents |
| 96 |
|
| 97 |
@mock.patch('subprocess.check_output') |
| 98 |
def testNoChangesReturnsNoResults(self, mock_subprocess): |
| 99 |
results = PRESUBMIT._RunCommandAndCheckDiff(self.mock_output_api, [], []) |
| 100 |
self.assertEqual(results, []) |
| 101 |
|
| 102 |
@mock.patch('subprocess.check_output') |
| 103 |
def testChangingIrrelevantFilesReturnsNoResults(self, mock_subprocess): |
| 104 |
mock_subprocess.side_effect = lambda *args, **kwargs: self.setContents(self.bar_file, ['foo']) |
| 105 |
results = PRESUBMIT._RunCommandAndCheckDiff( |
| 106 |
self.mock_output_api, ['cmd'], [self.foo_file], |
| 107 |
) |
| 108 |
self.assertEqual(results, []) |
| 109 |
|
| 110 |
@mock.patch('subprocess.check_output') |
| 111 |
def testChangingRelevantFilesReturnsDiff(self, mock_subprocess): |
| 112 |
mock_subprocess.side_effect = lambda *args, **kwargs: self.setContents(self.foo_file, ['bar']) |
| 113 |
results = PRESUBMIT._RunCommandAndCheckDiff( |
| 114 |
self.mock_output_api, ['cmd'], [self.foo_file], |
| 115 |
) |
| 116 |
self.assertEqual(len(results), 1) |
| 117 |
self.assertEqual(results[0].message, |
| 118 |
"""Diffs found after running "cmd": |
| 119 |
|
| 120 |
--- foo.txt |
| 121 |
+++ foo.txt |
| 122 |
@@ -1 +1 @@ |
| 123 |
-foo |
| 124 |
+bar |
| 125 |
|
| 126 |
Please commit or discard the above changes.""") |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == '__main__': |
| 130 |
unittest.main() |