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