OLD | NEW |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 import json |
5 import os | 6 import os |
6 import sys | 7 import sys |
7 import unittest | 8 import unittest |
8 | 9 |
9 import checkteamtags | 10 import checkteamtags |
10 | 11 |
11 SRC = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) | 12 SRC = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) |
12 sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) | 13 sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) |
13 | 14 |
14 import mock | 15 import mock |
15 | 16 |
16 | 17 |
17 def mock_file(lines): | 18 def mock_file(lines): |
18 inner_mock = mock.MagicMock() | 19 inner_mock = mock.MagicMock() |
19 inner_attrs = {'readlines.return_value': lines} | 20 inner_attrs = {'readlines.return_value': lines, |
| 21 '__iter__.return_value': lines} |
20 inner_mock.configure_mock(**inner_attrs) | 22 inner_mock.configure_mock(**inner_attrs) |
21 | 23 |
22 return_val = mock.MagicMock() | 24 return_val = mock.MagicMock() |
23 attrs = {'__enter__.return_value': inner_mock} | 25 attrs = {'__enter__.return_value': inner_mock} |
24 return_val.configure_mock(**attrs) | 26 return_val.configure_mock(**attrs) |
25 return return_val | 27 return return_val |
26 | 28 |
| 29 |
| 30 DEFAULT_MAPPING = { |
| 31 'dir-to-component': {}, |
| 32 'component-to-team': {}, |
| 33 } |
| 34 |
| 35 def mock_url_open(data=None): |
| 36 """Simulate the result of fetching the cloud location of the mapping. |
| 37 |
| 38 i.e. https://storage.googleapis.com/chromium-owners/component_map.json |
| 39 """ |
| 40 if data is None: |
| 41 data = DEFAULT_MAPPING |
| 42 |
| 43 class _MockJsonResponse(object): |
| 44 def __init__(self, data): |
| 45 self.data = data |
| 46 |
| 47 def read(self): |
| 48 return json.dumps(self.data) |
| 49 |
| 50 def inner(url): |
| 51 if url.endswith('.json'): |
| 52 return _MockJsonResponse(data) |
| 53 return inner |
| 54 |
| 55 |
27 NO_TAGS = """ | 56 NO_TAGS = """ |
28 mock@chromium.org | 57 mock@chromium.org |
29 """.splitlines() | 58 """.splitlines() |
30 | 59 |
31 MULTIPLE_COMPONENT_TAGS = """ | 60 MULTIPLE_COMPONENT_TAGS = """ |
32 mock@chromium.org | 61 mock@chromium.org |
33 | 62 |
34 # COMPONENT: Blink>mock_component | 63 # COMPONENT: Blink>mock_component |
35 # COMPONENT: V8>mock_component | 64 # COMPONENT: V8>mock_component |
36 """.splitlines() | 65 """.splitlines() |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 97 |
69 BASIC = """ | 98 BASIC = """ |
70 mock@chromium.org | 99 mock@chromium.org |
71 | 100 |
72 # TEAM: some-team@chromium.org | 101 # TEAM: some-team@chromium.org |
73 # COMPONENT: V8>mock_component | 102 # COMPONENT: V8>mock_component |
74 """.splitlines() | 103 """.splitlines() |
75 | 104 |
76 open_name = 'checkteamtags.open' | 105 open_name = 'checkteamtags.open' |
77 | 106 |
78 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) | |
79 @mock.patch('sys.stdout', mock.MagicMock()) | 107 @mock.patch('sys.stdout', mock.MagicMock()) |
| 108 @mock.patch('os.path.exists', mock.MagicMock()) |
80 class CheckTeamTagsTest(unittest.TestCase): | 109 class CheckTeamTagsTest(unittest.TestCase): |
| 110 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 111 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
81 def testNoTags(self): | 112 def testNoTags(self): |
82 with mock.patch(open_name, create=True) as mock_open: | 113 with mock.patch(open_name, create=True) as mock_open: |
83 mock_open.return_value = mock_file(NO_TAGS) | 114 mock_open.return_value = mock_file(NO_TAGS) |
84 self.assertEqual(0, checkteamtags.main()) | 115 self.assertEqual(0, checkteamtags.main()) |
85 | 116 |
| 117 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 118 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
86 def testMultipleComponentTags(self): | 119 def testMultipleComponentTags(self): |
87 with mock.patch(open_name, create=True) as mock_open: | 120 with mock.patch(open_name, create=True) as mock_open: |
88 mock_open.return_value = mock_file(MULTIPLE_COMPONENT_TAGS) | 121 mock_open.return_value = mock_file(MULTIPLE_COMPONENT_TAGS) |
89 self.assertEqual(1, checkteamtags.main()) | 122 self.assertEqual(1, checkteamtags.main()) |
90 | 123 |
| 124 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 125 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
91 def testMultipleComponentsInTag(self): | 126 def testMultipleComponentsInTag(self): |
92 with mock.patch(open_name, create=True) as mock_open: | 127 with mock.patch(open_name, create=True) as mock_open: |
93 mock_open.return_value = mock_file(MULTIPLE_COMPONENTS_IN_TAG) | 128 mock_open.return_value = mock_file(MULTIPLE_COMPONENTS_IN_TAG) |
94 self.assertEqual(1, checkteamtags.main()) | 129 self.assertEqual(1, checkteamtags.main()) |
95 | 130 |
| 131 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 132 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
96 def testMissingComponent(self): | 133 def testMissingComponent(self): |
97 with mock.patch(open_name, create=True) as mock_open: | 134 with mock.patch(open_name, create=True) as mock_open: |
98 mock_open.return_value = mock_file(MISSING_COMPONENT) | 135 mock_open.return_value = mock_file(MISSING_COMPONENT) |
99 self.assertEqual(1, checkteamtags.main()) | 136 self.assertEqual(1, checkteamtags.main()) |
100 | 137 |
| 138 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 139 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
101 def testMultipleTeamTags(self): | 140 def testMultipleTeamTags(self): |
102 with mock.patch(open_name, create=True) as mock_open: | 141 with mock.patch(open_name, create=True) as mock_open: |
103 mock_open.return_value = mock_file(MULTIPLE_TEAM_TAGS) | 142 mock_open.return_value = mock_file(MULTIPLE_TEAM_TAGS) |
104 self.assertEqual(1, checkteamtags.main()) | 143 self.assertEqual(1, checkteamtags.main()) |
105 | 144 |
| 145 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 146 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
106 def testMultipleTeamsInTag(self): | 147 def testMultipleTeamsInTag(self): |
107 with mock.patch(open_name, create=True) as mock_open: | 148 with mock.patch(open_name, create=True) as mock_open: |
108 mock_open.return_value = mock_file(MULTIPLE_TEAMS_IN_TAG) | 149 mock_open.return_value = mock_file(MULTIPLE_TEAMS_IN_TAG) |
109 self.assertEqual(1, checkteamtags.main()) | 150 self.assertEqual(1, checkteamtags.main()) |
110 | 151 |
| 152 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 153 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
111 def testMissingTeam(self): | 154 def testMissingTeam(self): |
112 with mock.patch(open_name, create=True) as mock_open: | 155 with mock.patch(open_name, create=True) as mock_open: |
113 mock_open.return_value = mock_file(MISSING_TEAM) | 156 mock_open.return_value = mock_file(MISSING_TEAM) |
114 self.assertEqual(1, checkteamtags.main()) | 157 self.assertEqual(1, checkteamtags.main()) |
115 | 158 |
| 159 @mock.patch('urllib2.urlopen', mock_url_open()) |
| 160 @mock.patch('sys.argv', ['checkteamtags', '--bare' ,'OWNERS']) |
116 def testBasic(self): | 161 def testBasic(self): |
117 with mock.patch(open_name, create=True) as mock_open: | 162 with mock.patch(open_name, create=True) as mock_open: |
118 mock_open.return_value = mock_file(BASIC) | 163 mock_open.return_value = mock_file(BASIC) |
119 self.assertEqual(0, checkteamtags.main()) | 164 self.assertEqual(0, checkteamtags.main()) |
| 165 |
| 166 @mock.patch('urllib2.urlopen', mock_url_open({ |
| 167 'dir-to-component': { |
| 168 'some/dir': 'V8>mock_component', |
| 169 }, |
| 170 'component-to-team': { |
| 171 'V8>mock_component': 'some-other-team@chromium.org', |
| 172 }, |
| 173 })) |
| 174 @mock.patch('sys.argv', ['checkteamtags', '--bare', 'fakepath/OWNERS']) |
| 175 def testMappingFail(self): |
| 176 with mock.patch(open_name, create=True) as mock_open: |
| 177 mock_open.return_value = mock_file(BASIC) |
| 178 with mock.patch('owners_file_tags.open', create=True) as mock_open_2: |
| 179 mock_open_2.return_value = mock_file(BASIC) |
| 180 self.assertEqual(1, checkteamtags.main()) |
| 181 |
| 182 @mock.patch('urllib2.urlopen', mock_url_open({ |
| 183 'dir-to-component': { |
| 184 'some/dir': 'V8>mock_component', |
| 185 }, |
| 186 'component-to-team': { |
| 187 'V8>mock_component': 'some-other-team@chromium.org', |
| 188 }, |
| 189 })) |
| 190 @mock.patch('sys.argv', ['checkteamtags', '--bare', 'some/dir/OWNERS']) |
| 191 def testMappingPassRename(self): |
| 192 with mock.patch(open_name, create=True) as mock_open: |
| 193 mock_open.return_value = mock_file(BASIC) |
| 194 with mock.patch('owners_file_tags.open', create=True) as mock_open_2: |
| 195 mock_open_2.return_value = mock_file(BASIC) |
| 196 self.assertEqual(0, checkteamtags.main()) |
| 197 |
| 198 @mock.patch('urllib2.urlopen', mock_url_open({ |
| 199 'dir-to-component': { |
| 200 'some/dir/': 'V8>mock_component', |
| 201 }, |
| 202 'component-to-team': { |
| 203 'V8>mock_component': 'some-team@chromium.org', |
| 204 }, |
| 205 })) |
| 206 @mock.patch('sys.argv', ['checkteamtags', '--bare', 'other/dir/OWNERS']) |
| 207 def testMappingPassNew(self): |
| 208 with mock.patch(open_name, create=True) as mock_open: |
| 209 mock_open.return_value = mock_file(BASIC) |
| 210 with mock.patch('owners_file_tags.open', create=True) as mock_open_2: |
| 211 mock_open_2.return_value = mock_file(BASIC) |
| 212 self.assertEqual(0, checkteamtags.main()) |
OLD | NEW |