forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRESUBMIT_test.py
executable file
·169 lines (127 loc) · 5.62 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# Copyright (c) 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import shutil
import subprocess
import sys
import unittest
import PRESUBMIT
class MockInputApi(object):
"""A minimal mock InputApi for our checks."""
def __init__(self):
self.affected_paths = []
self.os_path = os.path
self.python_executable = sys.executable
self.subprocess = subprocess
self.is_windows = sys.platform == 'win32'
self.environ = os.environ
self.logging = PrintLogger()
self.change = MockChange()
def AbsoluteLocalPaths(self):
return self.affected_paths
def PresubmitLocalPath(self):
return os.path.abspath(os.path.dirname(__file__))
def AffectedSourceFiles(self, filter_func):
all_files = [MockFile(self.PresubmitLocalPath(), path) for path in self.affected_paths]
return filter(lambda f: filter_func(f), all_files)
class MockChange(object):
"""A minimal mock Change for our checks."""
def RepositoryRoot(self):
here = os.path.dirname(__file__)
return os.path.abspath(os.path.join(here, '..', '..', '..', '..'))
class PrintLogger(object):
"""A simple logger that just prints log messages."""
def debug(self, message):
print(message)
class MockPresubmitError(object):
"""A minimal mock of an error class for our checks."""
def __init__(self, message, items, long_text):
self.message = message
self.items = items
self.long_text = long_text
def __repr__(self):
return self.message + "\n" + self.long_text
class MockPresubmitWarning(object):
"""A minimal mock of an warning class for our checks."""
def __init__(self, message, items, long_text):
self.message = message
self.items = items
self.long_text = long_text
class MockOutputApi(object):
"""A minimal mock OutputApi for our checks."""
def PresubmitError(self, message, items=None, long_text=''):
return MockPresubmitError(message, items, long_text)
def PresubmitPromptWarning(self, message, items=None, long_text=''):
return MockPresubmitWarning(message, items, long_text)
class MockFile(object):
"""A minimal mock File for our checks."""
def __init__(self, presubmit_abs_path, file_abs_path):
self.abs_path = file_abs_path
self.local_path = os.path.relpath(file_abs_path, presubmit_abs_path)
def AbsoluteLocalPath(self):
return self.abs_path
def LocalPath(self):
return self.local_path
class LintWPTTest(unittest.TestCase):
def setUp(self):
self._test_file = os.path.join(os.path.dirname(__file__), 'wpt', '_DO_NOT_SUBMIT_.html')
self._ignored_directory = os.path.join(os.path.dirname(__file__), 'wpt', 'css', '_DNS_')
def tearDown(self):
if os.path.exists(self._test_file):
os.remove(self._test_file)
if os.path.exists(self._ignored_directory):
shutil.rmtree(self._ignored_directory)
def testWPTLintSuccess(self):
with open(self._test_file, 'w') as f:
f.write('<body>Hello, world!</body>')
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = [os.path.abspath(self._test_file)]
errors = PRESUBMIT._LintWPT(mock_input, mock_output)
self.assertEqual(errors, [])
def testWPTLintErrors(self):
# Private LayoutTests APIs are not allowed.
with open(self._test_file, 'w') as f:
f.write('<script>testRunner.notifyDone()</script>')
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = [os.path.abspath(self._test_file)]
errors = PRESUBMIT._LintWPT(mock_input, mock_output)
self.assertEqual(len(errors), 1)
self.assertTrue(isinstance(errors[0], MockPresubmitError))
def testWPTLintIgnore(self):
os.mkdir(self._ignored_directory)
files = []
for f in ['DIR_METADATA', 'OWNERS', 'test-expected.txt']:
path = os.path.abspath(os.path.join(self._ignored_directory, f))
files.append(path)
open(path, 'w').close()
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = files
errors = PRESUBMIT._LintWPT(mock_input, mock_output)
self.assertEqual(errors, [])
class DontModifyIDLFilesTest(unittest.TestCase):
def testModifiesIDL(self):
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = [os.path.join(mock_input.PresubmitLocalPath(), 'wpt', 'interfaces', 'test.idl')]
errors = PRESUBMIT._DontModifyIDLFiles(mock_input, mock_output)
self.assertEqual(len(errors), 1)
self.assertTrue(isinstance(errors[0], MockPresubmitWarning))
def testModifiesNonIDLFiles(self):
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = [os.path.join(mock_input.PresubmitLocalPath(), 'wpt', 'css', 'foo.html')]
errors = PRESUBMIT._DontModifyIDLFiles(mock_input, mock_output)
self.assertEqual(errors, [])
def testModifiesInterfaceDirOutsideOfWPT(self):
mock_input = MockInputApi()
mock_output = MockOutputApi()
mock_input.affected_paths = [os.path.join(mock_input.PresubmitLocalPath(), 'other', 'interfaces', 'test.idl')]
errors = PRESUBMIT._DontModifyIDLFiles(mock_input, mock_output)
self.assertEqual(errors, [])
if __name__ == '__main__':
unittest.main()