forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresubmit_support_test.py
executable file
·147 lines (129 loc) · 6.06 KB
/
presubmit_support_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os.path
from pathlib import Path
import unittest
from unittest.mock import patch
import sys
from presubmit_support import _CheckGM3Counterpart, _CheckNoDirectLitImport
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile
class GM3CounterpartPresubmit(unittest.TestCase):
def setUp(self):
self.mock_input_api = MockInputApi()
self.mock_input_api.change.RepositoryRoot = lambda: os.path.join(
os.path.dirname(__file__), '..', '..', '..')
self.mock_output_api = MockOutputApi()
def testWarningWithGM3CounterpartNotChanged(self):
"""
If a CSS file foo.css is changed, and there's a corresponding
foo_gm3.css existed but not changed, show warning.
"""
foo_css = MockFile(os.path.join('some', 'path', 'foo.css'), '')
self.mock_input_api.files.append(foo_css)
# Mock Path.is_file call to make sure foo_gm3.css is existed.
with patch.object(Path, 'is_file') as mock_is_file:
mock_is_file.return_value = True
errors = _CheckGM3Counterpart(self.mock_input_api,
self.mock_output_api)
self.assertEqual(1, len(errors))
self.assertEqual(1, len(errors[0].items))
self.assertTrue('foo.css' in errors[0].items[0])
self.assertTrue('foo_gm3.css' in errors[0].items[0])
def testNoWarningWithGM3CounterpartChanged(self):
"""
If a CSS file foo.css is changed, and there's a corresponding
foo_gm3.css existed also changed, no warnings.
"""
foo_css = MockFile(os.path.join('some', 'path', 'foo.css'), '')
foo_gm3_css = MockFile(os.path.join('some', 'path', 'foo_gm3.css'), '')
self.mock_input_api.files.append(foo_css)
self.mock_input_api.files.append(foo_gm3_css)
# Mock Path.is_file call to make sure foo_gm3.css is existed.
with patch.object(Path, 'is_file') as mock_is_file:
mock_is_file.return_value = True
errors = _CheckGM3Counterpart(self.mock_input_api,
self.mock_output_api)
self.assertEqual([], errors)
def testNoWarningWithoutGM3Counterpart(self):
"""
If a CSS file foo.css is changed, and the corresponding foo_gm3.css
does not existed, no warnings.
"""
foo_css = MockFile(os.path.join('some', 'path', 'foo.css'), '')
self.mock_input_api.files.append(foo_css)
# Mock Path.is_file call to make sure foo_gm3.css is not existed.
with patch.object(Path, 'is_file') as mock_is_file:
mock_is_file.return_value = False
errors = _CheckGM3Counterpart(self.mock_input_api,
self.mock_output_api)
self.assertEqual([], errors)
def testNoWarningForNonCSSChange(self):
"""
If the patch doesn't have any CSS files, no warnings.
"""
foo_js = MockFile(os.path.join('some', 'path', 'foo.js'), '')
foo_cpp = MockFile(os.path.join('some', 'path', 'foo.cc'), '')
self.mock_input_api.files.append(foo_js)
self.mock_input_api.files.append(foo_cpp)
errors = _CheckGM3Counterpart(self.mock_input_api,
self.mock_output_api)
self.assertEqual([], errors)
class NoDirectLitImportPresubmit(unittest.TestCase):
def setUp(self):
self.mock_input_api = MockInputApi()
self.mock_input_api.change.RepositoryRoot = lambda: os.path.join(
os.path.dirname(__file__), '..', '..', '..')
self.mock_output_api = MockOutputApi()
def testWarningWithDirectLitImport(self):
"""
If a TS file foo.ts or a JS file foo.js is changed, and there's direct
Lit import in the file, show warnings.
"""
lines = [
"import {aaa} from 'a.js';"
"import {css} from 'chrome://resources/mwc/lit/index.js';"
]
ts_path = os.path.join('some', 'path', 'foo.ts')
js_path = os.path.join('some', 'path', 'foo.js')
foo_ts = MockFile(ts_path, lines)
foo_js = MockFile(js_path, lines)
self.mock_input_api.files.append(foo_ts)
self.mock_input_api.files.append(foo_js)
errors = _CheckNoDirectLitImport(self.mock_input_api,
self.mock_output_api)
self.assertEqual(2, len(errors))
self.assertTrue(ts_path + ':1' in errors[0].message)
self.assertTrue(js_path + ':1' in errors[1].message)
def testNoWarningWithDirectLitImportInXfBase(self):
"""
If ui/file_manager/file_manager/widgets/xf_base.ts is changed, and
there's direct lit import in the file, no warnings.
"""
lines = [
"import {aaa} from 'a.js';"
"import {css} from 'chrome://resources/mwc/lit/index.js';"
]
xf_base_ts = MockFile(
os.path.join('ui', 'file_manager', 'file_manager', 'widgets',
'xf_base.ts'), lines)
self.mock_input_api.files.append(xf_base_ts)
errors = _CheckNoDirectLitImport(self.mock_input_api,
self.mock_output_api)
self.assertEqual([], errors)
def testNoWarningWithoutDirectLitImport(self):
"""
If a TS file foo.ts is changed, and there is no direct Lit import
in the file, no warnings.
"""
foo_ts = MockFile(os.path.join('some', 'path', 'foo.ts'), '')
foo_js = MockFile(os.path.join('some', 'path', 'foo.js'), '')
self.mock_input_api.files.append(foo_ts)
self.mock_input_api.files.append(foo_js)
errors = _CheckGM3Counterpart(self.mock_input_api,
self.mock_output_api)
self.assertEqual([], errors)
if __name__ == '__main__':
unittest.main()