OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import unittest | |
6 | |
7 from webkitpy.common.system.executive import Executive, ScriptError | |
8 from webkitpy.common.system.executive_mock import MockExecutive | |
9 from webkitpy.common.system.filesystem import FileSystem | |
10 from webkitpy.common.system.filesystem_mock import MockFileSystem | |
11 from webkitpy.common.checkout.scm.detection import detect_scm_system | |
12 from webkitpy.common.checkout.scm.git import Git | |
13 | |
14 | |
15 class GitTestWithRealFilesystemAndExecutive(unittest.TestCase): | |
16 | |
17 def setUp(self): | |
18 self.executive = Executive() | |
19 self.filesystem = FileSystem() | |
20 self.original_cwd = self.filesystem.getcwd() | |
21 | |
22 # Set up fresh git repository with one commit. | |
23 self.untracking_checkout_path = self._mkdtemp(suffix='-git_unittest_untr acking') | |
24 self._run(['git', 'init', self.untracking_checkout_path]) | |
25 self._chdir(self.untracking_checkout_path) | |
26 self._write_text_file('foo_file', 'foo') | |
27 self._run(['git', 'add', 'foo_file']) | |
28 self._run(['git', 'commit', '-am', 'dummy commit']) | |
29 self.untracking_scm = detect_scm_system(self.untracking_checkout_path) | |
30 | |
31 # Then set up a second git repo that tracks the first one. | |
32 self.tracking_git_checkout_path = self._mkdtemp(suffix='-git_unittest_tr acking') | |
33 self._run(['git', 'clone', '--quiet', self.untracking_checkout_path, sel f.tracking_git_checkout_path]) | |
34 self._chdir(self.tracking_git_checkout_path) | |
35 self.tracking_scm = detect_scm_system(self.tracking_git_checkout_path) | |
36 | |
37 def tearDown(self): | |
38 self._chdir(self.original_cwd) | |
39 self._run(['rm', '-rf', self.tracking_git_checkout_path]) | |
40 self._run(['rm', '-rf', self.untracking_checkout_path]) | |
41 | |
42 def _join(self, *comps): | |
43 return self.filesystem.join(*comps) | |
44 | |
45 def _chdir(self, path): | |
46 self.filesystem.chdir(path) | |
47 | |
48 def _mkdir(self, path): | |
49 assert not self.filesystem.exists(path) | |
50 self.filesystem.maybe_make_directory(path) | |
51 | |
52 def _mkdtemp(self, **kwargs): | |
53 return str(self.filesystem.mkdtemp(**kwargs)) | |
54 | |
55 def _remove(self, path): | |
56 self.filesystem.remove(path) | |
57 | |
58 def _run(self, *args, **kwargs): | |
59 return self.executive.run_command(*args, **kwargs) | |
60 | |
61 def _run_silent(self, args, **kwargs): | |
62 self.executive.run_command(args, **kwargs) | |
63 | |
64 def _write_text_file(self, path, contents): | |
65 self.filesystem.write_text_file(path, contents) | |
66 | |
67 def _write_binary_file(self, path, contents): | |
68 self.filesystem.write_binary_file(path, contents) | |
69 | |
70 def _make_diff(self, command, *args): | |
71 # We use this wrapper to disable output decoding. diffs should be treate d as | |
72 # binary files since they may include text files of multiple different e ncodings. | |
73 return self._run([command, 'diff'] + list(args), decode_output=False) | |
74 | |
75 def _git_diff(self, *args): | |
76 return self._make_diff('git', *args) | |
77 | |
78 def test_add_list(self): | |
qyearsley
2016/12/20 19:20:24
These methods (test_add_list, test_delete*, test_e
Dirk Pranke
2017/01/09 04:44:52
Right. First, we normally don't want tests actuall
| |
79 self._chdir(self.untracking_checkout_path) | |
80 git = self.untracking_scm | |
81 self._mkdir('added_dir') | |
82 self._write_text_file('added_dir/added_file', 'new stuff') | |
83 print self._run(['ls', 'added_dir']) | |
84 print self._run(['pwd']) | |
85 print self._run(['cat', 'added_dir/added_file']) | |
86 git.add_list(['added_dir/added_file']) | |
87 self.assertIn('added_dir/added_file', git.added_files()) | |
88 | |
89 def test_delete_recursively(self): | |
90 self._chdir(self.untracking_checkout_path) | |
91 git = self.untracking_scm | |
92 self._mkdir('added_dir') | |
93 self._write_text_file('added_dir/added_file', 'new stuff') | |
94 git.add_list(['added_dir/added_file']) | |
95 self.assertIn('added_dir/added_file', git.added_files()) | |
96 git.delete_list(['added_dir/added_file']) | |
97 self.assertNotIn('added_dir', git.added_files()) | |
98 | |
99 def test_delete_recursively_or_not(self): | |
100 self._chdir(self.untracking_checkout_path) | |
101 git = self.untracking_scm | |
102 self._mkdir('added_dir') | |
103 self._write_text_file('added_dir/added_file', 'new stuff') | |
104 self._write_text_file('added_dir/another_added_file', 'more new stuff') | |
105 git.add_list(['added_dir/added_file', 'added_dir/another_added_file']) | |
106 self.assertIn('added_dir/added_file', git.added_files()) | |
107 self.assertIn('added_dir/another_added_file', git.added_files()) | |
108 git.delete_list(['added_dir/added_file']) | |
109 self.assertIn('added_dir/another_added_file', git.added_files()) | |
110 | |
111 def test_exists(self): | |
112 self._chdir(self.untracking_checkout_path) | |
113 git = self.untracking_scm | |
114 self._chdir(git.checkout_root) | |
115 self.assertFalse(git.exists('foo.txt')) | |
116 self._write_text_file('foo.txt', 'some stuff') | |
117 self.assertFalse(git.exists('foo.txt')) | |
118 git.add_list(['foo.txt']) | |
119 git.commit_locally_with_message('adding foo') | |
120 self.assertTrue(git.exists('foo.txt')) | |
121 git.delete_list(['foo.txt']) | |
122 git.commit_locally_with_message('deleting foo') | |
123 self.assertFalse(git.exists('foo.txt')) | |
124 | |
125 def test_move(self): | |
126 self._chdir(self.untracking_checkout_path) | |
127 git = self.untracking_scm | |
128 self._write_text_file('added_file', 'new stuff') | |
129 git.add_list(['added_file']) | |
130 git.move('added_file', 'moved_file') | |
131 self.assertIn('moved_file', git.added_files()) | |
132 | |
133 def test_move_recursive(self): | |
134 self._chdir(self.untracking_checkout_path) | |
135 git = self.untracking_scm | |
136 self._mkdir('added_dir') | |
137 self._write_text_file('added_dir/added_file', 'new stuff') | |
138 self._write_text_file('added_dir/another_added_file', 'more new stuff') | |
139 git.add_list(['added_dir']) | |
140 git.move('added_dir', 'moved_dir') | |
141 self.assertIn('moved_dir/added_file', git.added_files()) | |
142 self.assertIn('moved_dir/another_added_file', git.added_files()) | |
143 | |
144 def test_remote_branch_ref(self): | |
145 # This tests a protected method. pylint: disable=protected-access | |
146 self.assertEqual(self.tracking_scm._remote_branch_ref(), 'refs/remotes/o rigin/master') | |
147 self._chdir(self.untracking_checkout_path) | |
148 self.assertRaises(ScriptError, self.untracking_scm._remote_branch_ref) | |
149 | |
150 def test_create_patch(self): | |
151 self._chdir(self.tracking_git_checkout_path) | |
152 git = self.tracking_scm | |
153 self._write_text_file('test_file_commit1', 'contents') | |
154 self._run(['git', 'add', 'test_file_commit1']) | |
155 git.commit_locally_with_message('message') | |
156 git._patch_order = lambda: '' # pylint: disable=protected-access | |
157 patch = git.create_patch() | |
158 self.assertNotRegexpMatches(patch, r'Subversion Revision:') | |
159 | |
160 def test_patches_have_filenames_with_prefixes(self): | |
161 self._chdir(self.tracking_git_checkout_path) | |
162 git = self.tracking_scm | |
163 self._write_text_file('test_file_commit1', 'contents') | |
164 self._run(['git', 'add', 'test_file_commit1']) | |
165 git.commit_locally_with_message('message') | |
166 | |
167 # Even if diff.noprefix is enabled, create_patch() produces diffs with p refixes. | |
168 self._run(['git', 'config', 'diff.noprefix', 'true']) | |
169 git._patch_order = lambda: '' # pylint: disable=protected-access | |
170 patch = git.create_patch() | |
171 self.assertRegexpMatches(patch, r'^diff --git a/test_file_commit1 b/test _file_commit1') | |
172 | |
173 def test_rename_files(self): | |
174 self._chdir(self.tracking_git_checkout_path) | |
175 git = self.tracking_scm | |
176 git.move('foo_file', 'bar_file') | |
177 git.commit_locally_with_message('message') | |
178 | |
179 def test_commit_position_from_git_log(self): | |
180 # This tests a protected method. pylint: disable=protected-access | |
181 git_log = """ | |
182 commit 624c3081c0 | |
183 Author: foobarbaz1 <foobarbaz1@chromium.org> | |
184 Date: Mon Sep 28 19:10:30 2015 -0700 | |
185 | |
186 Test foo bar baz qux 123. | |
187 | |
188 BUG=000000 | |
189 | |
190 Review URL: https://codereview.chromium.org/999999999 | |
191 | |
192 Cr-Commit-Position: refs/heads/master@{#1234567} | |
193 """ | |
194 self._chdir(self.tracking_git_checkout_path) | |
195 git = self.tracking_scm | |
196 self.assertEqual(git._commit_position_from_git_log(git_log), 1234567) | |
197 | |
198 def test_timestamp_of_revision(self): | |
199 # This tests a protected method. pylint: disable=protected-access | |
200 self._chdir(self.tracking_git_checkout_path) | |
201 git = self.tracking_scm | |
202 position_regex = git._commit_position_regex_for_timestamp() | |
203 git.most_recent_log_matching(position_regex, git.checkout_root) | |
204 | |
205 | |
206 class GitTestWithMock(unittest.TestCase): | |
207 | |
208 def make_scm(self): | |
209 git = Git(cwd='.', executive=MockExecutive(), filesystem=MockFileSystem( )) | |
210 git.read_git_config = lambda *args, **kw: 'MOCKKEY:MOCKVALUE' | |
211 return git | |
212 | |
213 def _assert_timestamp_of_revision(self, canned_git_output, expected): | |
214 git = self.make_scm() | |
215 git.find_checkout_root = lambda path: '' | |
216 # Modifying protected method. pylint: disable=protected-access | |
217 git._run_git = lambda args: canned_git_output | |
218 self.assertEqual(git.timestamp_of_revision('some-path', '12345'), expect ed) | |
219 | |
220 def test_timestamp_of_revision_utc(self): | |
221 self._assert_timestamp_of_revision('Date: 2013-02-08 08:05:49 +0000', '2 013-02-08T08:05:49Z') | |
222 | |
223 def test_timestamp_of_revision_positive_timezone(self): | |
224 self._assert_timestamp_of_revision('Date: 2013-02-08 01:02:03 +0130', '2 013-02-07T23:32:03Z') | |
225 | |
226 def test_timestamp_of_revision_pacific_timezone(self): | |
227 self._assert_timestamp_of_revision('Date: 2013-02-08 01:55:21 -0800', '2 013-02-08T09:55:21Z') | |
OLD | NEW |