| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 import os | 6 import os |
| 7 import posixpath |
| 7 import sys | 8 import sys |
| 8 import unittest | 9 import unittest |
| 9 from appengine_url_fetcher import AppEngineUrlFetcher | 10 from appengine_url_fetcher import AppEngineUrlFetcher |
| 11 from extensions_paths import ( |
| 12 ARTICLES_TEMPLATES, EXTENSIONS, DOCS, JSON_TEMPLATES, PUBLIC_TEMPLATES) |
| 10 from fake_fetchers import ConfigureFakeFetchers | 13 from fake_fetchers import ConfigureFakeFetchers |
| 11 from file_system import FileNotFoundError | 14 from file_system import FileNotFoundError |
| 12 from rietveld_patcher import RietveldPatcher | 15 from rietveld_patcher import RietveldPatcher |
| 13 from svn_constants import EXTENSIONS_PATH | |
| 14 import url_constants | 16 import url_constants |
| 15 | 17 |
| 18 |
| 19 def _PrefixWith(prefix, lst): |
| 20 return [posixpath.join(prefix, item) for item in lst] |
| 21 |
| 22 |
| 16 class RietveldPatcherTest(unittest.TestCase): | 23 class RietveldPatcherTest(unittest.TestCase): |
| 17 def setUp(self): | 24 def setUp(self): |
| 18 ConfigureFakeFetchers() | 25 ConfigureFakeFetchers() |
| 19 self._patcher = RietveldPatcher( | 26 self._patcher = RietveldPatcher( |
| 20 EXTENSIONS_PATH, | |
| 21 '14096030', | 27 '14096030', |
| 22 AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER)) | 28 AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER)) |
| 23 | 29 |
| 24 def _ReadLocalFile(self, filename): | 30 def _ReadLocalFile(self, filename): |
| 25 with open(os.path.join(sys.path[0], | 31 with open(os.path.join(sys.path[0], |
| 26 'test_data', | 32 'test_data', |
| 27 'rietveld_patcher', | 33 'rietveld_patcher', |
| 28 'expected', | 34 'expected', |
| 29 filename), 'r') as f: | 35 filename), 'r') as f: |
| 30 return f.read() | 36 return f.read() |
| 31 | 37 |
| 32 def _ApplySingle(self, path, binary=False): | 38 def _ApplySingle(self, path, binary=False): |
| 33 return self._patcher.Apply([path], None, binary).Get()[path] | 39 return self._patcher.Apply([path], None, binary).Get()[path] |
| 34 | 40 |
| 35 def testGetVersion(self): | 41 def testGetVersion(self): |
| 36 self.assertEqual(self._patcher.GetVersion(), '22002') | 42 self.assertEqual(self._patcher.GetVersion(), '22002') |
| 37 | 43 |
| 38 def testGetPatchedFiles(self): | 44 def testGetPatchedFiles(self): |
| 39 added, deleted, modified = self._patcher.GetPatchedFiles() | 45 added, deleted, modified = self._patcher.GetPatchedFiles() |
| 40 self.assertEqual(sorted(added), | 46 self.assertEqual( |
| 41 sorted([ | 47 sorted(added), |
| 42 u'docs/templates/articles/test_foo.html', | 48 _PrefixWith(DOCS, ['examples/test', |
| 43 u'docs/templates/public/extensions/test_foo.html'])) | 49 'templates/articles/test_foo.html', |
| 44 self.assertEqual(sorted(deleted), | 50 'templates/public/extensions/test_foo.html'])) |
| 45 sorted([ | 51 self.assertEqual(deleted, |
| 46 u'docs/templates/public/extensions/runtime.html'])) | 52 ['%s/extensions/runtime.html' % PUBLIC_TEMPLATES]) |
| 47 self.assertEqual(sorted(modified), | 53 self.assertEqual( |
| 48 sorted([ | 54 sorted(modified), |
| 49 u'api/test.json', | 55 _PrefixWith(EXTENSIONS, ['api/test.json', |
| 50 u'docs/templates/json/extensions_sidenav.json'])) | 56 'docs/templates/json/extensions_sidenav.json', |
| 57 'manifest.h'])) |
| 51 | 58 |
| 52 def testApply(self): | 59 def testApply(self): |
| 53 article_path = 'docs/templates/articles/test_foo.html' | 60 article_path = '%s/test_foo.html' % ARTICLES_TEMPLATES |
| 54 | 61 |
| 55 # Make sure RietveldPatcher handles |binary| correctly. | 62 # Make sure RietveldPatcher handles |binary| correctly. |
| 56 self.assertTrue(isinstance(self._ApplySingle(article_path, True), str), | 63 self.assertTrue(isinstance(self._ApplySingle(article_path, True), str), |
| 57 'Expected result is binary. It was text.') | 64 'Expected result is binary. It was text.') |
| 58 self.assertTrue(isinstance(self._ApplySingle(article_path), unicode), | 65 self.assertTrue(isinstance(self._ApplySingle(article_path), unicode), |
| 59 'Expected result is text. It was binary.') | 66 'Expected result is text. It was binary.') |
| 60 | 67 |
| 61 # Apply to an added file. | 68 # Apply to an added file. |
| 62 self.assertEqual( | 69 self.assertEqual( |
| 63 self._ReadLocalFile('test_foo.html'), | 70 self._ReadLocalFile('test_foo.html'), |
| 64 self._ApplySingle( | 71 self._ApplySingle('%s/extensions/test_foo.html' % PUBLIC_TEMPLATES)) |
| 65 'docs/templates/public/extensions/test_foo.html')) | |
| 66 | 72 |
| 67 # Apply to a modified file. | 73 # Apply to a modified file. |
| 68 self.assertEqual( | 74 self.assertEqual( |
| 69 self._ReadLocalFile('extensions_sidenav.json'), | 75 self._ReadLocalFile('extensions_sidenav.json'), |
| 70 self._ApplySingle( | 76 self._ApplySingle('%s/extensions_sidenav.json' % JSON_TEMPLATES)) |
| 71 'docs/templates/json/extensions_sidenav.json')) | |
| 72 | 77 |
| 73 # Applying to a deleted file doesn't throw exceptions. It just returns | 78 # Applying to a deleted file doesn't throw exceptions. It just returns |
| 74 # empty content. | 79 # empty content. |
| 75 # self.assertRaises(FileNotFoundError, self._ApplySingle, | 80 # self.assertRaises(FileNotFoundError, self._ApplySingle, |
| 76 # 'docs/templates/public/extensions/runtime.html') | 81 # 'docs/templates/public/extensions/runtime.html') |
| 77 | 82 |
| 78 # Apply to an unknown file. | 83 # Apply to an unknown file. |
| 79 self.assertRaises(FileNotFoundError, self._ApplySingle, 'not_existing') | 84 self.assertRaises(FileNotFoundError, self._ApplySingle, 'not_existing') |
| 80 | 85 |
| 81 if __name__ == '__main__': | 86 if __name__ == '__main__': |
| 82 unittest.main() | 87 unittest.main() |
| OLD | NEW |