| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for rietveld.py.""" | 6 """Unit tests for rietveld.py.""" |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 import unittest | 11 import unittest |
| 12 | 12 |
| 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) | 14 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) |
| 15 | 15 |
| 16 import patch | 16 import patch |
| 17 import rietveld | 17 import rietveld |
| 18 | 18 |
| 19 # Access to a protected member XX of a client class | 19 # Access to a protected member XX of a client class |
| 20 # pylint: disable=W0212 | 20 # pylint: disable=W0212 |
| 21 GIT_COPY_FULL = ( |
| 22 'diff --git a/PRESUBMIT.py b/file_a\n' |
| 23 'similarity index 100%\n' |
| 24 'copy from PRESUBMIT.py\n' |
| 25 'copy to file_a\n') |
| 26 |
| 27 |
| 28 NORMAL_DIFF = ( |
| 29 '--- file_a\n' |
| 30 '+++ file_a\n' |
| 31 '@@ -80,10 +80,13 @@\n' |
| 32 ' // Foo\n' |
| 33 ' // Bar\n' |
| 34 ' void foo() {\n' |
| 35 '- return bar;\n' |
| 36 '+ return foo;\n' |
| 37 ' }\n' |
| 38 ' \n' |
| 39 ' \n') |
| 21 | 40 |
| 22 | 41 |
| 23 def _api(files): | 42 def _api(files): |
| 24 """Mock a rietveld api request.""" | 43 """Mock a rietveld api request.""" |
| 25 return rietveld.json.dumps({'files': files}) | 44 return rietveld.json.dumps({'files': files}) |
| 26 | 45 |
| 27 | 46 |
| 28 def _file( | 47 def _file( |
| 29 status, is_binary=False, num_chunks=1, chunk_id=789, property_changes=''): | 48 status, is_binary=False, num_chunks=1, chunk_id=789, property_changes=''): |
| 30 """Mock a file in a rietveld api request.""" | 49 """Mock a file in a rietveld api request.""" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 def test_invalid_status(self): | 149 def test_invalid_status(self): |
| 131 self.requests = [ | 150 self.requests = [ |
| 132 ('/api/123/456', _api({'file_a': _file('B')})), | 151 ('/api/123/456', _api({'file_a': _file('B')})), |
| 133 ] | 152 ] |
| 134 try: | 153 try: |
| 135 self.rietveld.get_patch(123, 456) | 154 self.rietveld.get_patch(123, 456) |
| 136 self.fail() | 155 self.fail() |
| 137 except patch.UnsupportedPatchFormat, e: | 156 except patch.UnsupportedPatchFormat, e: |
| 138 self.assertEquals('file_a', e.filename) | 157 self.assertEquals('file_a', e.filename) |
| 139 | 158 |
| 140 def test_add_plus(self): | 159 def test_add_plus_merge(self): |
| 160 # svn:mergeinfo is dropped. |
| 161 diff = GIT_COPY_FULL |
| 141 properties = ( | 162 properties = ( |
| 142 '\nAdded: svn:mergeinfo\n' | 163 '\nAdded: svn:mergeinfo\n' |
| 143 ' Merged /branches/funky/file_b:r69-2775\n') | 164 ' Merged /branches/funky/file_b:r69-2775\n') |
| 144 self.requests = [ | 165 self.requests = [ |
| 145 ('/api/123/456', | 166 ('/api/123/456', |
| 146 _api({'file_a': _file('A+', property_changes=properties)})), | 167 _api({'file_a': _file('A+', property_changes=properties)})), |
| 168 ('/download/issue123_456_789.diff', diff), |
| 147 ] | 169 ] |
| 148 try: | 170 patches = self.rietveld.get_patch(123, 456) |
| 149 self.rietveld.get_patch(123, 456) | 171 self.assertEquals(1, len(patches.patches)) |
| 150 self.fail() | 172 self._check_patch( |
| 151 except patch.UnsupportedPatchFormat, e: | 173 patches.patches[0], |
| 152 self.assertEquals('file_a', e.filename) | 174 'file_a', |
| 175 diff, |
| 176 is_git_diff=True, |
| 177 is_new=True, |
| 178 patchlevel=1) |
| 179 |
| 180 def test_add_plus_eol_style(self): |
| 181 diff = GIT_COPY_FULL |
| 182 properties = '\nAdded: svn:eol-style\n + LF\n' |
| 183 self.requests = [ |
| 184 ('/api/123/456', |
| 185 _api({'file_a': _file('A+', property_changes=properties)})), |
| 186 ('/download/issue123_456_789.diff', diff), |
| 187 ] |
| 188 patches = self.rietveld.get_patch(123, 456) |
| 189 self.assertEquals(1, len(patches.patches)) |
| 190 self._check_patch( |
| 191 patches.patches[0], |
| 192 'file_a', |
| 193 diff, |
| 194 is_git_diff=True, |
| 195 is_new=True, |
| 196 patchlevel=1, |
| 197 svn_properties=[('svn:eol-style', 'LF')]) |
| 198 |
| 199 def test_add_empty(self): |
| 200 # http://codereview.chromium.org/api/7530007/5001 |
| 201 # http://codereview.chromium.org/download/issue7530007_5001_4011.diff |
| 202 diff = ( |
| 203 'Index: scripts/master/factory/skia/__init__.py\n' |
| 204 '===================================================================\n') |
| 205 self.requests = [ |
| 206 ('/api/123/456', _api({'__init__.py': _file('A ', num_chunks=0)})), |
| 207 ('/download/issue123_456_789.diff', diff), |
| 208 ] |
| 209 patches = self.rietveld.get_patch(123, 456) |
| 210 self.assertEquals(1, len(patches.patches)) |
| 211 self._check_patch( |
| 212 patches.patches[0], |
| 213 '__init__.py', |
| 214 diff, |
| 215 is_new=True) |
| 153 | 216 |
| 154 def test_delete(self): | 217 def test_delete(self): |
| 155 self.requests = [ | 218 self.requests = [ |
| 156 ('/api/123/456', _api({'file_a': _file('D')})), | 219 ('/api/123/456', _api({'file_a': _file('D')})), |
| 157 ] | 220 ] |
| 158 patches = self.rietveld.get_patch(123, 456) | 221 patches = self.rietveld.get_patch(123, 456) |
| 159 self.assertEquals(1, len(patches.patches)) | 222 self.assertEquals(1, len(patches.patches)) |
| 160 self._check_patch(patches.patches[0], 'file_a', None, is_delete=True) | 223 self._check_patch(patches.patches[0], 'file_a', None, is_delete=True) |
| 161 | 224 |
| 162 def test_m_plus(self): | 225 def test_m_plus(self): |
| 226 diff = NORMAL_DIFF |
| 163 properties = '\nAdded: svn:eol-style\n + LF\n' | 227 properties = '\nAdded: svn:eol-style\n + LF\n' |
| 164 self.requests = [ | 228 self.requests = [ |
| 165 ('/api/123/456', | 229 ('/api/123/456', |
| 166 _api({'file_a': _file('M+', property_changes=properties)})), | 230 _api({'file_a': _file('M+', property_changes=properties)})), |
| 231 ('/download/issue123_456_789.diff', diff), |
| 232 ] |
| 233 patches = self.rietveld.get_patch(123, 456) |
| 234 self.assertEquals(1, len(patches.patches)) |
| 235 self._check_patch( |
| 236 patches.patches[0], |
| 237 'file_a', |
| 238 diff, |
| 239 svn_properties=[('svn:eol-style', 'LF')]) |
| 240 |
| 241 def test_m_plus_unknown_prop(self): |
| 242 properties = '\nAdded: svn:foobar\n + stuff\n' |
| 243 self.requests = [ |
| 244 ('/api/123/456', |
| 245 _api({'file_a': _file('M+', property_changes=properties)})), |
| 167 ] | 246 ] |
| 168 try: | 247 try: |
| 169 self.rietveld.get_patch(123, 456) | 248 self.rietveld.get_patch(123, 456) |
| 170 self.fail() | 249 self.fail() |
| 171 except patch.UnsupportedPatchFormat, e: | 250 except patch.UnsupportedPatchFormat, e: |
| 172 self.assertEquals('file_a', e.filename) | 251 self.assertEquals('file_a', e.filename) |
| 173 | 252 |
| 174 def test_svn_properties(self): | 253 def test_svn_properties(self): |
| 175 # Line too long (N/80) | 254 # Line too long (N/80) |
| 176 # pylint: disable=C0301 | 255 # pylint: disable=C0301 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 206 self.fail() | 285 self.fail() |
| 207 except rietveld.patch.UnsupportedPatchFormat, e: | 286 except rietveld.patch.UnsupportedPatchFormat, e: |
| 208 self.assertEquals('foo', e.filename) | 287 self.assertEquals('foo', e.filename) |
| 209 # TODO(maruel): Change with no diff, only svn property change: | 288 # TODO(maruel): Change with no diff, only svn property change: |
| 210 # http://codereview.chromium.org/6462019/ | 289 # http://codereview.chromium.org/6462019/ |
| 211 | 290 |
| 212 | 291 |
| 213 if __name__ == '__main__': | 292 if __name__ == '__main__': |
| 214 logging.basicConfig(level=logging.ERROR) | 293 logging.basicConfig(level=logging.ERROR) |
| 215 unittest.main() | 294 unittest.main() |
| OLD | NEW |