OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 unittest | 5 import unittest |
6 | 6 |
7 from webkitpy.common.system.executive import Executive, ScriptError | |
8 from webkitpy.common.system.executive_mock import MockExecutive | 7 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 | 8 from webkitpy.common.system.filesystem_mock import MockFileSystem |
11 from webkitpy.common.checkout.git import Git | 9 from webkitpy.common.checkout.git import Git |
12 | 10 |
13 | 11 |
14 class GitTestWithRealFilesystemAndExecutive(unittest.TestCase): | |
15 | |
16 def setUp(self): | |
17 self.executive = Executive() | |
18 self.filesystem = FileSystem() | |
19 self.original_cwd = self.filesystem.getcwd() | |
20 | |
21 # Set up fresh git repository with one commit. | |
22 self.untracking_checkout_path = self._mkdtemp(suffix='-git_unittest_untr
acking') | |
23 self._run(['git', 'init', self.untracking_checkout_path]) | |
24 self._chdir(self.untracking_checkout_path) | |
25 self._write_text_file('foo_file', 'foo') | |
26 self._run(['git', 'add', 'foo_file']) | |
27 self._run(['git', 'commit', '-am', 'dummy commit']) | |
28 self.untracking_git = Git(cwd=self.untracking_checkout_path, filesystem=
self.filesystem, executive=self.executive) | |
29 | |
30 # Then set up a second git repo that tracks the first one. | |
31 self.tracking_git_checkout_path = self._mkdtemp(suffix='-git_unittest_tr
acking') | |
32 self._run(['git', 'clone', '--quiet', self.untracking_checkout_path, sel
f.tracking_git_checkout_path]) | |
33 self._chdir(self.tracking_git_checkout_path) | |
34 self.tracking_git = Git(cwd=self.tracking_git_checkout_path, filesystem=
self.filesystem, executive=self.executive) | |
35 | |
36 def tearDown(self): | |
37 self._chdir(self.original_cwd) | |
38 self._run(['rm', '-rf', self.tracking_git_checkout_path]) | |
39 self._run(['rm', '-rf', self.untracking_checkout_path]) | |
40 | |
41 def _join(self, *comps): | |
42 return self.filesystem.join(*comps) | |
43 | |
44 def _chdir(self, path): | |
45 self.filesystem.chdir(path) | |
46 | |
47 def _mkdir(self, path): | |
48 assert not self.filesystem.exists(path) | |
49 self.filesystem.maybe_make_directory(path) | |
50 | |
51 def _mkdtemp(self, **kwargs): | |
52 return str(self.filesystem.mkdtemp(**kwargs)) | |
53 | |
54 def _remove(self, path): | |
55 self.filesystem.remove(path) | |
56 | |
57 def _run(self, *args, **kwargs): | |
58 return self.executive.run_command(*args, **kwargs) | |
59 | |
60 def _write_text_file(self, path, contents): | |
61 self.filesystem.write_text_file(path, contents) | |
62 | |
63 def test_add_list(self): | |
64 self._chdir(self.untracking_checkout_path) | |
65 git = self.untracking_git | |
66 self._mkdir('added_dir') | |
67 self._write_text_file('added_dir/added_file', 'new stuff') | |
68 print self._run(['ls', 'added_dir']) | |
69 print self._run(['pwd']) | |
70 print self._run(['cat', 'added_dir/added_file']) | |
71 git.add_list(['added_dir/added_file']) | |
72 self.assertIn('added_dir/added_file', git.added_files()) | |
73 | |
74 def test_delete_recursively(self): | |
75 self._chdir(self.untracking_checkout_path) | |
76 git = self.untracking_git | |
77 self._mkdir('added_dir') | |
78 self._write_text_file('added_dir/added_file', 'new stuff') | |
79 git.add_list(['added_dir/added_file']) | |
80 self.assertIn('added_dir/added_file', git.added_files()) | |
81 git.delete_list(['added_dir/added_file']) | |
82 self.assertNotIn('added_dir', git.added_files()) | |
83 | |
84 def test_delete_recursively_or_not(self): | |
85 self._chdir(self.untracking_checkout_path) | |
86 git = self.untracking_git | |
87 self._mkdir('added_dir') | |
88 self._write_text_file('added_dir/added_file', 'new stuff') | |
89 self._write_text_file('added_dir/another_added_file', 'more new stuff') | |
90 git.add_list(['added_dir/added_file', 'added_dir/another_added_file']) | |
91 self.assertIn('added_dir/added_file', git.added_files()) | |
92 self.assertIn('added_dir/another_added_file', git.added_files()) | |
93 git.delete_list(['added_dir/added_file']) | |
94 self.assertIn('added_dir/another_added_file', git.added_files()) | |
95 | |
96 def test_exists(self): | |
97 self._chdir(self.untracking_checkout_path) | |
98 git = self.untracking_git | |
99 self._chdir(git.checkout_root) | |
100 self.assertFalse(git.exists('foo.txt')) | |
101 self._write_text_file('foo.txt', 'some stuff') | |
102 self.assertFalse(git.exists('foo.txt')) | |
103 git.add_list(['foo.txt']) | |
104 git.commit_locally_with_message('adding foo') | |
105 self.assertTrue(git.exists('foo.txt')) | |
106 git.delete_list(['foo.txt']) | |
107 git.commit_locally_with_message('deleting foo') | |
108 self.assertFalse(git.exists('foo.txt')) | |
109 | |
110 def test_move(self): | |
111 self._chdir(self.untracking_checkout_path) | |
112 git = self.untracking_git | |
113 self._write_text_file('added_file', 'new stuff') | |
114 git.add_list(['added_file']) | |
115 git.move('added_file', 'moved_file') | |
116 self.assertIn('moved_file', git.added_files()) | |
117 | |
118 def test_move_recursive(self): | |
119 self._chdir(self.untracking_checkout_path) | |
120 git = self.untracking_git | |
121 self._mkdir('added_dir') | |
122 self._write_text_file('added_dir/added_file', 'new stuff') | |
123 self._write_text_file('added_dir/another_added_file', 'more new stuff') | |
124 git.add_list(['added_dir']) | |
125 git.move('added_dir', 'moved_dir') | |
126 self.assertIn('moved_dir/added_file', git.added_files()) | |
127 self.assertIn('moved_dir/another_added_file', git.added_files()) | |
128 | |
129 def test_remote_branch_ref(self): | |
130 # This tests a protected method. pylint: disable=protected-access | |
131 self.assertEqual(self.tracking_git._remote_branch_ref(), 'refs/remotes/o
rigin/master') | |
132 self._chdir(self.untracking_checkout_path) | |
133 self.assertRaises(ScriptError, self.untracking_git._remote_branch_ref) | |
134 | |
135 def test_create_patch(self): | |
136 self._chdir(self.tracking_git_checkout_path) | |
137 git = self.tracking_git | |
138 self._write_text_file('test_file_commit1', 'contents') | |
139 self._run(['git', 'add', 'test_file_commit1']) | |
140 git.commit_locally_with_message('message') | |
141 git._patch_order = lambda: '' # pylint: disable=protected-access | |
142 patch = git.create_patch() | |
143 self.assertNotRegexpMatches(patch, r'Subversion Revision:') | |
144 | |
145 def test_patches_have_filenames_with_prefixes(self): | |
146 self._chdir(self.tracking_git_checkout_path) | |
147 git = self.tracking_git | |
148 self._write_text_file('test_file_commit1', 'contents') | |
149 self._run(['git', 'add', 'test_file_commit1']) | |
150 git.commit_locally_with_message('message') | |
151 | |
152 # Even if diff.noprefix is enabled, create_patch() produces diffs with p
refixes. | |
153 self._run(['git', 'config', 'diff.noprefix', 'true']) | |
154 git._patch_order = lambda: '' # pylint: disable=protected-access | |
155 patch = git.create_patch() | |
156 self.assertRegexpMatches(patch, r'^diff --git a/test_file_commit1 b/test
_file_commit1') | |
157 | |
158 def test_rename_files(self): | |
159 self._chdir(self.tracking_git_checkout_path) | |
160 git = self.tracking_git | |
161 git.move('foo_file', 'bar_file') | |
162 git.commit_locally_with_message('message') | |
163 | |
164 def test_commit_position_from_git_log(self): | |
165 # This tests a protected method. pylint: disable=protected-access | |
166 git_log = """ | |
167 commit 624c3081c0 | |
168 Author: foobarbaz1 <foobarbaz1@chromium.org> | |
169 Date: Mon Sep 28 19:10:30 2015 -0700 | |
170 | |
171 Test foo bar baz qux 123. | |
172 | |
173 BUG=000000 | |
174 | |
175 Review URL: https://codereview.chromium.org/999999999 | |
176 | |
177 Cr-Commit-Position: refs/heads/master@{#1234567} | |
178 """ | |
179 self._chdir(self.tracking_git_checkout_path) | |
180 git = self.tracking_git | |
181 self.assertEqual(git._commit_position_from_git_log(git_log), 1234567) | |
182 | |
183 def test_timestamp_of_revision(self): | |
184 # This tests a protected method. pylint: disable=protected-access | |
185 self._chdir(self.tracking_git_checkout_path) | |
186 git = self.tracking_git | |
187 position_regex = git._commit_position_regex_for_timestamp() | |
188 git.most_recent_log_matching(position_regex, git.checkout_root) | |
189 | |
190 | |
191 class GitTestWithMock(unittest.TestCase): | 12 class GitTestWithMock(unittest.TestCase): |
192 | 13 |
193 def make_git(self): | 14 def make_git(self): |
194 git = Git(cwd='.', executive=MockExecutive(), filesystem=MockFileSystem(
)) | 15 git = Git(cwd='.', executive=MockExecutive(), filesystem=MockFileSystem(
)) |
195 git.read_git_config = lambda *args, **kw: 'MOCKKEY:MOCKVALUE' | 16 git.read_git_config = lambda *args, **kw: 'MOCKKEY:MOCKVALUE' |
196 return git | 17 return git |
197 | 18 |
198 def _assert_timestamp_of_revision(self, canned_git_output, expected): | 19 def _assert_timestamp_of_revision(self, canned_git_output, expected): |
199 git = self.make_git() | 20 git = self.make_git() |
200 git.find_checkout_root = lambda path: '' | 21 git.find_checkout_root = lambda path: '' |
(...skipping 22 matching lines...) Expand all Loading... |
223 ] | 44 ] |
224 git.run = lambda args: '\x00'.join(status_lines) + '\x00' | 45 git.run = lambda args: '\x00'.join(status_lines) + '\x00' |
225 self.assertEqual( | 46 self.assertEqual( |
226 git.unstaged_changes(), | 47 git.unstaged_changes(), |
227 { | 48 { |
228 'd/modified.txt': 'M', | 49 'd/modified.txt': 'M', |
229 'd/deleted.txt': 'D', | 50 'd/deleted.txt': 'D', |
230 'd/untracked.txt': '?', | 51 'd/untracked.txt': '?', |
231 'a': '?', | 52 'a': '?', |
232 }) | 53 }) |
OLD | NEW |