| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Unit tests for post_processors/chromium_copyright.py.""" | |
| 7 | |
| 8 import datetime | |
| 9 import os | |
| 10 import sys | |
| 11 import unittest | |
| 12 | |
| 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 14 PROJECT_DIR = os.path.join(ROOT_DIR, '..') | |
| 15 sys.path.insert(0, PROJECT_DIR) | |
| 16 | |
| 17 import find_depot_tools # pylint: disable=W0611 | |
| 18 from testing_support import trial_dir | |
| 19 import patch | |
| 20 | |
| 21 from post_processors import chromium_copyright | |
| 22 | |
| 23 | |
| 24 GIT_NEW = ( | |
| 25 'diff --git a/foo b/foo\n' | |
| 26 'new file mode 100644\n' | |
| 27 'index 0000000..5716ca5\n' | |
| 28 '--- /dev/null\n' | |
| 29 '+++ b/foo\n' | |
| 30 '@@ -0,0 +1 @@\n' | |
| 31 '+bar\n') | |
| 32 | |
| 33 GIT_PATCH = ( | |
| 34 'diff --git a/foo3 b/foo3\n' | |
| 35 'index 257cc56..5716ca5\n' | |
| 36 '--- a/foo3\n' | |
| 37 '+++ b/foo3\n' | |
| 38 '@@ -1 +1 @@\n' | |
| 39 '-foo\n' | |
| 40 '+bar\n') | |
| 41 | |
| 42 class CCTest(trial_dir.TestCase): | |
| 43 def setUp(self): | |
| 44 super(CCTest, self).setUp() | |
| 45 class FakeCheckout(object): | |
| 46 project_path = self.root_dir | |
| 47 self.checkout = FakeCheckout() | |
| 48 open(self.path('foo1'), 'w').write('bar') | |
| 49 | |
| 50 @staticmethod | |
| 51 def get_patch(): | |
| 52 return patch.PatchSet([ | |
| 53 patch.FilePatchDelete('foo2', True), | |
| 54 patch.FilePatchDiff('foo', GIT_NEW, []), | |
| 55 patch.FilePatchBinary('foo1', 'data', [], True), | |
| 56 patch.FilePatchDiff('foo3', GIT_PATCH, []), | |
| 57 ]) | |
| 58 | |
| 59 def path(self, base_file): | |
| 60 return os.path.join(self.root_dir, base_file) | |
| 61 | |
| 62 def full_check(self, content, expected): | |
| 63 """End-to-end test. That's all that matters.""" | |
| 64 foo_path = self.path('foo') | |
| 65 foo3_path = self.path('foo3') | |
| 66 open(foo_path, 'w').write(content) | |
| 67 open(foo3_path, 'w').write(content) | |
| 68 for p in self.get_patch(): | |
| 69 chromium_copyright.process(self.checkout, p) | |
| 70 self.assertEquals(expected, open(foo_path).read()) | |
| 71 self.assertEquals(content, open(foo3_path).read()) | |
| 72 | |
| 73 def test_2_times(self): | |
| 74 content = ( | |
| 75 'Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' | |
| 76 'Copyright (c) 2010 The Chromium Authors. All rights reserved.\n') | |
| 77 expected = ( | |
| 78 'Copyright %s The Chromium Authors. All rights reserved.\n' | |
| 79 'Copyright (c) 2010 The Chromium Authors. All rights reserved.\n') % ( | |
| 80 datetime.date.today().year) | |
| 81 self.full_check(content, expected) | |
| 82 | |
| 83 def test_5_lines(self): | |
| 84 content = ( | |
| 85 '0\n' | |
| 86 '1\n' | |
| 87 '2\n' | |
| 88 '3\n' | |
| 89 'Copyright (c) 2010 The Chromium Authors. All rights reserved.\n') | |
| 90 expected = ( | |
| 91 '0\n' | |
| 92 '1\n' | |
| 93 '2\n' | |
| 94 '3\n' | |
| 95 'Copyright %s The Chromium Authors. All rights reserved.\n') % ( | |
| 96 datetime.date.today().year) | |
| 97 self.full_check(content, expected) | |
| 98 | |
| 99 def test_6_lines(self): | |
| 100 content = ( | |
| 101 '0\n' | |
| 102 '1\n' | |
| 103 '2\n' | |
| 104 '3\n' | |
| 105 '4\n' | |
| 106 'Copyright (c) 2010 The Chromium Authors. All rights reserved.\n') | |
| 107 expected = content | |
| 108 self.full_check(content, expected) | |
| 109 | |
| 110 def test_re(self): | |
| 111 input_base = 'Copyright (c) 2010 The Chromium Authors. All rights reserved.' | |
| 112 input_no_copyright = ('Copyright 2010 The Chromium Authors. ' | |
| 113 'All rights reserved.') | |
| 114 expected_base = ('Copyright %s The Chromium Authors. All rights reserved.' % | |
| 115 datetime.date.today().year) | |
| 116 self.full_check(input_base, expected_base) | |
| 117 self.full_check('a ' + input_base, 'a ' + expected_base) | |
| 118 self.full_check('// ' + input_base, '// ' + expected_base) | |
| 119 self.full_check('// ' + input_base + '\n', '// ' + expected_base + '\n') | |
| 120 self.full_check(input_base + '\n', expected_base + '\n') | |
| 121 self.full_check(input_no_copyright, expected_base) | |
| 122 self.full_check('a ' + input_no_copyright, 'a ' + expected_base) | |
| 123 self.full_check('// ' + input_no_copyright, '// ' + expected_base) | |
| 124 self.full_check('// ' + input_no_copyright + '\n', '// ' + expected_base + | |
| 125 '\n') | |
| 126 self.full_check(input_no_copyright + '\n', expected_base + '\n') | |
| 127 ## \r are not supported. | |
| 128 #self.full_check( | |
| 129 # '// Copyright (c) 2010 The Chromium Authors. All rights reserved.\r\n', | |
| 130 # '// Copyright %s The Chromium Authors. All rights reserved.\r\n' % | |
| 131 # datetime.date.today().year) | |
| 132 | |
| 133 | |
| 134 if __name__ == '__main__': | |
| 135 unittest.main() | |
| OLD | NEW |