##// END OF EJS Templates
chore(tests): fixed some broken bad tests
super-admin -
r5225:9f604a24 default
parent child Browse files
Show More
@@ -1,591 +1,597 b''
1
1
2 # Copyright (C) 2010-2023 RhodeCode GmbH
2 # Copyright (C) 2010-2023 RhodeCode GmbH
3 #
3 #
4 # This program is free software: you can redistribute it and/or modify
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License, version 3
5 # it under the terms of the GNU Affero General Public License, version 3
6 # (only), as published by the Free Software Foundation.
6 # (only), as published by the Free Software Foundation.
7 #
7 #
8 # This program is distributed in the hope that it will be useful,
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
11 # GNU General Public License for more details.
12 #
12 #
13 # You should have received a copy of the GNU Affero General Public License
13 # You should have received a copy of the GNU Affero General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #
15 #
16 # This program is dual-licensed. If you wish to learn more about the
16 # This program is dual-licensed. If you wish to learn more about the
17 # RhodeCode Enterprise Edition, including its added features, Support services,
17 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 # and proprietary license terms, please see https://rhodecode.com/licenses/
19
19
20 import datetime
20 import datetime
21 import time
21 import time
22
22
23 import pytest
23 import pytest
24
24
25 from rhodecode.lib.str_utils import safe_bytes
25 from rhodecode.lib.str_utils import safe_bytes
26 from rhodecode.lib.vcs.backends.base import (
26 from rhodecode.lib.vcs.backends.base import (
27 CollectionGenerator, FILEMODE_DEFAULT, EmptyCommit)
27 CollectionGenerator, FILEMODE_DEFAULT, EmptyCommit)
28 from rhodecode.lib.vcs.exceptions import (
28 from rhodecode.lib.vcs.exceptions import (
29 BranchDoesNotExistError, CommitDoesNotExistError,
29 BranchDoesNotExistError, CommitDoesNotExistError,
30 RepositoryError, EmptyRepositoryError)
30 RepositoryError, EmptyRepositoryError)
31 from rhodecode.lib.vcs.nodes import (
31 from rhodecode.lib.vcs.nodes import (
32 FileNode, AddedFileNodesGenerator,
32 FileNode, AddedFileNodesGenerator,
33 ChangedFileNodesGenerator, RemovedFileNodesGenerator)
33 ChangedFileNodesGenerator, RemovedFileNodesGenerator)
34 from rhodecode.tests import get_new_dir
34 from rhodecode.tests import get_new_dir
35 from rhodecode.tests.vcs.conftest import BackendTestMixin
35 from rhodecode.tests.vcs.conftest import BackendTestMixin
36
36
37
37
38 class TestBaseChangeset(object):
38 class TestBaseChangeset(object):
39
39
40 def test_is_deprecated(self):
40 def test_is_deprecated(self):
41 from rhodecode.lib.vcs.backends.base import BaseChangeset
41 from rhodecode.lib.vcs.backends.base import BaseChangeset
42 pytest.deprecated_call(BaseChangeset)
42 pytest.deprecated_call(BaseChangeset)
43
43
44
44
45 class TestEmptyCommit(object):
45 class TestEmptyCommit(object):
46
46
47 def test_branch_without_alias_returns_none(self):
47 def test_branch_without_alias_returns_none(self):
48 commit = EmptyCommit()
48 commit = EmptyCommit()
49 assert commit.branch is None
49 assert commit.branch is None
50
50
51
51
52 @pytest.mark.usefixtures("vcs_repository_support")
52 @pytest.mark.usefixtures("vcs_repository_support")
53 class TestCommitsInNonEmptyRepo(BackendTestMixin):
53 class TestCommitsInNonEmptyRepo(BackendTestMixin):
54 recreate_repo_per_test = True
54 recreate_repo_per_test = True
55
55
56 @classmethod
56 @classmethod
57 def _get_commits(cls):
57 def _get_commits(cls):
58 start_date = datetime.datetime(2010, 1, 1, 20)
58 start_date = datetime.datetime(2010, 1, 1, 20)
59 for x in range(5):
59 for x in range(5):
60 yield {
60 yield {
61 'message': 'Commit %d' % x,
61 'message': 'Commit %d' % x,
62 'author': 'Joe Doe <joe.doe@example.com>',
62 'author': 'Joe Doe <joe.doe@example.com>',
63 'date': start_date + datetime.timedelta(hours=12 * x),
63 'date': start_date + datetime.timedelta(hours=12 * x),
64 'added': [
64 'added': [
65 FileNode(b'file_%d.txt' % x,
65 FileNode(b'file_%d.txt' % x,
66 content=b'Foobar %d' % x),
66 content=b'Foobar %d' % x),
67 ],
67 ],
68 }
68 }
69
69
70 def test_walk_returns_empty_list_in_case_of_file(self):
70 def test_walk_returns_empty_list_in_case_of_file(self):
71 result = list(self.tip.walk('file_0.txt'))
71 result = list(self.tip.walk('file_0.txt'))
72 assert result == []
72 assert result == []
73
73
74 @pytest.mark.backends("git", "hg")
74 @pytest.mark.backends("git", "hg")
75 def test_new_branch(self):
75 def test_new_branch(self):
76 self.imc.add(FileNode(b'docs/index.txt', content=b'Documentation\n'))
76 self.imc.add(FileNode(b'docs/index.txt', content=b'Documentation\n'))
77 foobar_tip = self.imc.commit(
77 foobar_tip = self.imc.commit(
78 message='New branch: foobar',
78 message='New branch: foobar',
79 author='joe <joe@rhodecode.com>',
79 author='joe <joe@rhodecode.com>',
80 branch='foobar',
80 branch='foobar',
81 )
81 )
82 assert 'foobar' in self.repo.branches
82 assert 'foobar' in self.repo.branches
83 assert foobar_tip.branch == 'foobar'
83 assert foobar_tip.branch == 'foobar'
84 # 'foobar' should be the only branch that contains the new commit
84 # 'foobar' should be the only branch that contains the new commit
85 branch = list(self.repo.branches.values())
85 branch = list(self.repo.branches.values())
86 assert branch[0] != branch[1]
86 assert branch[0] != branch[1]
87
87
88 @pytest.mark.backends("git", "hg")
88 @pytest.mark.backends("git", "hg")
89 def test_new_head_in_default_branch(self):
89 def test_new_head_in_default_branch(self):
90 tip = self.repo.get_commit()
90 tip = self.repo.get_commit()
91 self.imc.add(FileNode(b'docs/index.txt', content=b'Documentation\n'))
91
92 self.imc.add(
93 FileNode(b"docs/index.txt", content=b"Documentation\n")
94 )
92 foobar_tip = self.imc.commit(
95 foobar_tip = self.imc.commit(
93 message='New branch: foobar',
96 message="New branch: foobar",
94 author='joe <joe@rhodecode.com>',
97 author="joe <joe@rhodecode.com>",
95 branch='foobar',
98 branch="foobar",
96 parents=[tip],
99 parents=[tip],
97 )
100 )
98 self.imc.change(FileNode(b'docs/index.txt', content=b'Documentation\nand more...\n'))
101 self.imc.change(
102 FileNode(b"docs/index.txt", content=b"Documentation\nand more...\n")
103 )
104 assert foobar_tip.branch == "foobar"
99 newtip = self.imc.commit(
105 newtip = self.imc.commit(
100 message='At default branch',
106 message="At foobar_tip branch",
101 author='joe <joe@rhodecode.com>',
107 author="joe <joe@rhodecode.com>",
102 branch=foobar_tip.branch,
108 branch=foobar_tip.branch,
103 parents=[foobar_tip],
109 parents=[foobar_tip],
104 )
110 )
105
111
106 newest_tip = self.imc.commit(
112 newest_tip = self.imc.commit(
107 message='Merged with %s' % foobar_tip.raw_id,
113 message=f"Merged with {foobar_tip.raw_id}",
108 author='joe <joe@rhodecode.com>',
114 author="joe <joe@rhodecode.com>",
109 branch=self.backend_class.DEFAULT_BRANCH_NAME,
115 branch=self.backend_class.DEFAULT_BRANCH_NAME,
110 parents=[newtip, foobar_tip],
116 parents=[tip, newtip],
111 )
117 )
112
118
113 assert newest_tip.branch == self.backend_class.DEFAULT_BRANCH_NAME
119 assert newest_tip.branch == self.backend_class.DEFAULT_BRANCH_NAME
114
120
115 @pytest.mark.backends("git", "hg")
121 @pytest.mark.backends("git", "hg")
116 def test_get_commits_respects_branch_name(self):
122 def test_get_commits_respects_branch_name(self):
117 """
123 """
118 * e1930d0 (HEAD, master) Back in default branch
124 * e1930d0 (HEAD, master) Back in default branch
119 | * e1930d0 (docs) New Branch: docs2
125 | * e1930d0 (docs) New Branch: docs2
120 | * dcc14fa New branch: docs
126 | * dcc14fa New branch: docs
121 |/
127 |/
122 * e63c41a Initial commit
128 * e63c41a Initial commit
123 ...
129 ...
124 * 624d3db Commit 0
130 * 624d3db Commit 0
125
131
126 :return:
132 :return:
127 """
133 """
128 DEFAULT_BRANCH = self.repo.DEFAULT_BRANCH_NAME
134 DEFAULT_BRANCH = self.repo.DEFAULT_BRANCH_NAME
129 TEST_BRANCH = 'docs'
135 TEST_BRANCH = 'docs'
130 org_tip = self.repo.get_commit()
136 org_tip = self.repo.get_commit()
131
137
132 self.imc.add(FileNode(b'readme.txt', content=b'Document\n'))
138 self.imc.add(FileNode(b'readme.txt', content=b'Document\n'))
133 initial = self.imc.commit(
139 initial = self.imc.commit(
134 message='Initial commit',
140 message='Initial commit',
135 author='joe <joe@rhodecode.com>',
141 author='joe <joe@rhodecode.com>',
136 parents=[org_tip],
142 parents=[org_tip],
137 branch=DEFAULT_BRANCH,)
143 branch=DEFAULT_BRANCH,)
138
144
139 self.imc.add(FileNode(b'newdoc.txt', content=b'foobar\n'))
145 self.imc.add(FileNode(b'newdoc.txt', content=b'foobar\n'))
140 docs_branch_commit1 = self.imc.commit(
146 docs_branch_commit1 = self.imc.commit(
141 message='New branch: docs',
147 message='New branch: docs',
142 author='joe <joe@rhodecode.com>',
148 author='joe <joe@rhodecode.com>',
143 parents=[initial],
149 parents=[initial],
144 branch=TEST_BRANCH,)
150 branch=TEST_BRANCH,)
145
151
146 self.imc.add(FileNode(b'newdoc2.txt', content=b'foobar2\n'))
152 self.imc.add(FileNode(b'newdoc2.txt', content=b'foobar2\n'))
147 docs_branch_commit2 = self.imc.commit(
153 docs_branch_commit2 = self.imc.commit(
148 message='New branch: docs2',
154 message='New branch: docs2',
149 author='joe <joe@rhodecode.com>',
155 author='joe <joe@rhodecode.com>',
150 parents=[docs_branch_commit1],
156 parents=[docs_branch_commit1],
151 branch=TEST_BRANCH,)
157 branch=TEST_BRANCH,)
152
158
153 self.imc.add(FileNode(b'newfile', content=b'hello world\n'))
159 self.imc.add(FileNode(b'newfile', content=b'hello world\n'))
154 self.imc.commit(
160 self.imc.commit(
155 message='Back in default branch',
161 message='Back in default branch',
156 author='joe <joe@rhodecode.com>',
162 author='joe <joe@rhodecode.com>',
157 parents=[initial],
163 parents=[initial],
158 branch=DEFAULT_BRANCH,)
164 branch=DEFAULT_BRANCH,)
159
165
160 default_branch_commits = self.repo.get_commits(branch_name=DEFAULT_BRANCH)
166 default_branch_commits = self.repo.get_commits(branch_name=DEFAULT_BRANCH)
161 assert docs_branch_commit1 not in list(default_branch_commits)
167 assert docs_branch_commit1 not in list(default_branch_commits)
162 assert docs_branch_commit2 not in list(default_branch_commits)
168 assert docs_branch_commit2 not in list(default_branch_commits)
163
169
164 docs_branch_commits = self.repo.get_commits(
170 docs_branch_commits = self.repo.get_commits(
165 start_id=self.repo.commit_ids[0], end_id=self.repo.commit_ids[-1],
171 start_id=self.repo.commit_ids[0], end_id=self.repo.commit_ids[-1],
166 branch_name=TEST_BRANCH)
172 branch_name=TEST_BRANCH)
167 assert docs_branch_commit1 in list(docs_branch_commits)
173 assert docs_branch_commit1 in list(docs_branch_commits)
168 assert docs_branch_commit2 in list(docs_branch_commits)
174 assert docs_branch_commit2 in list(docs_branch_commits)
169
175
170 @pytest.mark.backends("svn")
176 @pytest.mark.backends("svn")
171 def test_get_commits_respects_branch_name_svn(self, vcsbackend_svn):
177 def test_get_commits_respects_branch_name_svn(self, vcsbackend_svn):
172 repo = vcsbackend_svn['svn-simple-layout']
178 repo = vcsbackend_svn['svn-simple-layout']
173 commits = repo.get_commits(branch_name='trunk')
179 commits = repo.get_commits(branch_name='trunk')
174 commit_indexes = [c.idx for c in commits]
180 commit_indexes = [c.idx for c in commits]
175 assert commit_indexes == [1, 2, 3, 7, 12, 15]
181 assert commit_indexes == [1, 2, 3, 7, 12, 15]
176
182
177 def test_get_commit_by_index(self):
183 def test_get_commit_by_index(self):
178 for idx in [1, 2, 3, 4]:
184 for idx in [1, 2, 3, 4]:
179 assert idx == self.repo.get_commit(commit_idx=idx).idx
185 assert idx == self.repo.get_commit(commit_idx=idx).idx
180
186
181 def test_get_commit_by_branch(self):
187 def test_get_commit_by_branch(self):
182 for branch, commit_id in self.repo.branches.items():
188 for branch, commit_id in self.repo.branches.items():
183 assert commit_id == self.repo.get_commit(branch).raw_id
189 assert commit_id == self.repo.get_commit(branch).raw_id
184
190
185 def test_get_commit_by_tag(self):
191 def test_get_commit_by_tag(self):
186 for tag, commit_id in self.repo.tags.items():
192 for tag, commit_id in self.repo.tags.items():
187 assert commit_id == self.repo.get_commit(tag).raw_id
193 assert commit_id == self.repo.get_commit(tag).raw_id
188
194
189 def test_get_commit_parents(self):
195 def test_get_commit_parents(self):
190 repo = self.repo
196 repo = self.repo
191 for test_idx in [1, 2, 3]:
197 for test_idx in [1, 2, 3]:
192 commit = repo.get_commit(commit_idx=test_idx - 1)
198 commit = repo.get_commit(commit_idx=test_idx - 1)
193 assert [commit] == repo.get_commit(commit_idx=test_idx).parents
199 assert [commit] == repo.get_commit(commit_idx=test_idx).parents
194
200
195 def test_get_commit_children(self):
201 def test_get_commit_children(self):
196 repo = self.repo
202 repo = self.repo
197 for test_idx in [1, 2, 3]:
203 for test_idx in [1, 2, 3]:
198 commit = repo.get_commit(commit_idx=test_idx + 1)
204 commit = repo.get_commit(commit_idx=test_idx + 1)
199 assert [commit] == repo.get_commit(commit_idx=test_idx).children
205 assert [commit] == repo.get_commit(commit_idx=test_idx).children
200
206
201
207
202 @pytest.mark.usefixtures("vcs_repository_support")
208 @pytest.mark.usefixtures("vcs_repository_support")
203 class TestCommits(BackendTestMixin):
209 class TestCommits(BackendTestMixin):
204 recreate_repo_per_test = False
210 recreate_repo_per_test = False
205
211
206 @classmethod
212 @classmethod
207 def _get_commits(cls):
213 def _get_commits(cls):
208 start_date = datetime.datetime(2010, 1, 1, 20)
214 start_date = datetime.datetime(2010, 1, 1, 20)
209 for x in range(5):
215 for x in range(5):
210 yield {
216 yield {
211 'message': 'Commit %d' % x,
217 'message': 'Commit %d' % x,
212 'author': 'Joe Doe <joe.doe@example.com>',
218 'author': 'Joe Doe <joe.doe@example.com>',
213 'date': start_date + datetime.timedelta(hours=12 * x),
219 'date': start_date + datetime.timedelta(hours=12 * x),
214 'added': [
220 'added': [
215 FileNode(b'file_%d.txt' % x,
221 FileNode(b'file_%d.txt' % x,
216 content=b'Foobar %d' % x)
222 content=b'Foobar %d' % x)
217 ],
223 ],
218 }
224 }
219
225
220 def test_simple(self):
226 def test_simple(self):
221 tip = self.repo.get_commit()
227 tip = self.repo.get_commit()
222 assert tip.date, datetime.datetime(2010, 1, 3 == 20)
228 assert tip.date, datetime.datetime(2010, 1, 3 == 20)
223
229
224 def test_simple_serialized_commit(self):
230 def test_simple_serialized_commit(self):
225 tip = self.repo.get_commit()
231 tip = self.repo.get_commit()
226 # json.dumps(tip) uses .__json__() method
232 # json.dumps(tip) uses .__json__() method
227 data = tip.__json__()
233 data = tip.__json__()
228 assert 'branch' in data
234 assert 'branch' in data
229 assert data['revision']
235 assert data['revision']
230
236
231 def test_retrieve_tip(self):
237 def test_retrieve_tip(self):
232 tip = self.repo.get_commit('tip')
238 tip = self.repo.get_commit('tip')
233 assert tip == self.repo.get_commit()
239 assert tip == self.repo.get_commit()
234
240
235 def test_invalid(self):
241 def test_invalid(self):
236 with pytest.raises(CommitDoesNotExistError):
242 with pytest.raises(CommitDoesNotExistError):
237 self.repo.get_commit(commit_idx=123456789)
243 self.repo.get_commit(commit_idx=123456789)
238
244
239 def test_idx(self):
245 def test_idx(self):
240 commit = self.repo[0]
246 commit = self.repo[0]
241 assert commit.idx == 0
247 assert commit.idx == 0
242
248
243 def test_negative_idx(self):
249 def test_negative_idx(self):
244 commit = self.repo.get_commit(commit_idx=-1)
250 commit = self.repo.get_commit(commit_idx=-1)
245 assert commit.idx >= 0
251 assert commit.idx >= 0
246
252
247 def test_revision_is_deprecated(self):
253 def test_revision_is_deprecated(self):
248 def get_revision(commit):
254 def get_revision(commit):
249 return commit.revision
255 return commit.revision
250
256
251 commit = self.repo[0]
257 commit = self.repo[0]
252 pytest.deprecated_call(get_revision, commit)
258 pytest.deprecated_call(get_revision, commit)
253
259
254 def test_size(self):
260 def test_size(self):
255 tip = self.repo.get_commit()
261 tip = self.repo.get_commit()
256 size = 5 * len('Foobar N') # Size of 5 files
262 size = 5 * len('Foobar N') # Size of 5 files
257 assert tip.size == size
263 assert tip.size == size
258
264
259 def test_size_at_commit(self):
265 def test_size_at_commit(self):
260 tip = self.repo.get_commit()
266 tip = self.repo.get_commit()
261 size = 5 * len('Foobar N') # Size of 5 files
267 size = 5 * len('Foobar N') # Size of 5 files
262 assert self.repo.size_at_commit(tip.raw_id) == size
268 assert self.repo.size_at_commit(tip.raw_id) == size
263
269
264 def test_size_at_first_commit(self):
270 def test_size_at_first_commit(self):
265 commit = self.repo[0]
271 commit = self.repo[0]
266 size = len('Foobar N') # Size of 1 file
272 size = len('Foobar N') # Size of 1 file
267 assert self.repo.size_at_commit(commit.raw_id) == size
273 assert self.repo.size_at_commit(commit.raw_id) == size
268
274
269 def test_author(self):
275 def test_author(self):
270 tip = self.repo.get_commit()
276 tip = self.repo.get_commit()
271 assert_text_equal(tip.author, 'Joe Doe <joe.doe@example.com>')
277 assert_text_equal(tip.author, 'Joe Doe <joe.doe@example.com>')
272
278
273 def test_author_name(self):
279 def test_author_name(self):
274 tip = self.repo.get_commit()
280 tip = self.repo.get_commit()
275 assert_text_equal(tip.author_name, 'Joe Doe')
281 assert_text_equal(tip.author_name, 'Joe Doe')
276
282
277 def test_author_email(self):
283 def test_author_email(self):
278 tip = self.repo.get_commit()
284 tip = self.repo.get_commit()
279 assert_text_equal(tip.author_email, 'joe.doe@example.com')
285 assert_text_equal(tip.author_email, 'joe.doe@example.com')
280
286
281 def test_message(self):
287 def test_message(self):
282 tip = self.repo.get_commit()
288 tip = self.repo.get_commit()
283 assert_text_equal(tip.message, 'Commit 4')
289 assert_text_equal(tip.message, 'Commit 4')
284
290
285 def test_diff(self):
291 def test_diff(self):
286 tip = self.repo.get_commit()
292 tip = self.repo.get_commit()
287 diff = tip.diff()
293 diff = tip.diff()
288 assert b"+Foobar 4" in diff.raw.tobytes()
294 assert b"+Foobar 4" in diff.raw.tobytes()
289
295
290 def test_prev(self):
296 def test_prev(self):
291 tip = self.repo.get_commit()
297 tip = self.repo.get_commit()
292 prev_commit = tip.prev()
298 prev_commit = tip.prev()
293 assert prev_commit.message == 'Commit 3'
299 assert prev_commit.message == 'Commit 3'
294
300
295 def test_prev_raises_on_first_commit(self):
301 def test_prev_raises_on_first_commit(self):
296 commit = self.repo.get_commit(commit_idx=0)
302 commit = self.repo.get_commit(commit_idx=0)
297 with pytest.raises(CommitDoesNotExistError):
303 with pytest.raises(CommitDoesNotExistError):
298 commit.prev()
304 commit.prev()
299
305
300 def test_prev_works_on_second_commit_issue_183(self):
306 def test_prev_works_on_second_commit_issue_183(self):
301 commit = self.repo.get_commit(commit_idx=1)
307 commit = self.repo.get_commit(commit_idx=1)
302 prev_commit = commit.prev()
308 prev_commit = commit.prev()
303 assert prev_commit.idx == 0
309 assert prev_commit.idx == 0
304
310
305 def test_next(self):
311 def test_next(self):
306 commit = self.repo.get_commit(commit_idx=2)
312 commit = self.repo.get_commit(commit_idx=2)
307 next_commit = commit.next()
313 next_commit = commit.next()
308 assert next_commit.message == 'Commit 3'
314 assert next_commit.message == 'Commit 3'
309
315
310 def test_next_raises_on_tip(self):
316 def test_next_raises_on_tip(self):
311 commit = self.repo.get_commit()
317 commit = self.repo.get_commit()
312 with pytest.raises(CommitDoesNotExistError):
318 with pytest.raises(CommitDoesNotExistError):
313 commit.next()
319 commit.next()
314
320
315 def test_get_path_commit(self):
321 def test_get_path_commit(self):
316 commit = self.repo.get_commit()
322 commit = self.repo.get_commit()
317 commit.get_path_commit('file_4.txt')
323 commit.get_path_commit('file_4.txt')
318 assert commit.message == 'Commit 4'
324 assert commit.message == 'Commit 4'
319
325
320 def test_get_filenodes_generator(self):
326 def test_get_filenodes_generator(self):
321 tip = self.repo.get_commit()
327 tip = self.repo.get_commit()
322 filepaths = [node.path for node in tip.get_filenodes_generator()]
328 filepaths = [node.path for node in tip.get_filenodes_generator()]
323 assert filepaths == ['file_%d.txt' % x for x in range(5)]
329 assert filepaths == ['file_%d.txt' % x for x in range(5)]
324
330
325 def test_get_file_annotate(self):
331 def test_get_file_annotate(self):
326 file_added_commit = self.repo.get_commit(commit_idx=3)
332 file_added_commit = self.repo.get_commit(commit_idx=3)
327 annotations = list(file_added_commit.get_file_annotate('file_3.txt'))
333 annotations = list(file_added_commit.get_file_annotate('file_3.txt'))
328
334
329 line_no, commit_id, commit_loader, line = annotations[0]
335 line_no, commit_id, commit_loader, line = annotations[0]
330
336
331 assert line_no == 1
337 assert line_no == 1
332 assert commit_id == file_added_commit.raw_id
338 assert commit_id == file_added_commit.raw_id
333 assert commit_loader() == file_added_commit
339 assert commit_loader() == file_added_commit
334 assert b'Foobar 3' in line
340 assert b'Foobar 3' in line
335
341
336 def test_get_file_annotate_does_not_exist(self):
342 def test_get_file_annotate_does_not_exist(self):
337 file_added_commit = self.repo.get_commit(commit_idx=2)
343 file_added_commit = self.repo.get_commit(commit_idx=2)
338 # TODO: Should use a specific exception class here?
344 # TODO: Should use a specific exception class here?
339 with pytest.raises(Exception):
345 with pytest.raises(Exception):
340 list(file_added_commit.get_file_annotate('file_3.txt'))
346 list(file_added_commit.get_file_annotate('file_3.txt'))
341
347
342 def test_get_file_annotate_tip(self):
348 def test_get_file_annotate_tip(self):
343 tip = self.repo.get_commit()
349 tip = self.repo.get_commit()
344 commit = self.repo.get_commit(commit_idx=3)
350 commit = self.repo.get_commit(commit_idx=3)
345 expected_values = list(commit.get_file_annotate('file_3.txt'))
351 expected_values = list(commit.get_file_annotate('file_3.txt'))
346 annotations = list(tip.get_file_annotate('file_3.txt'))
352 annotations = list(tip.get_file_annotate('file_3.txt'))
347
353
348 # Note: Skip index 2 because the loader function is not the same
354 # Note: Skip index 2 because the loader function is not the same
349 for idx in (0, 1, 3):
355 for idx in (0, 1, 3):
350 assert annotations[0][idx] == expected_values[0][idx]
356 assert annotations[0][idx] == expected_values[0][idx]
351
357
352 def test_get_commits_is_ordered_by_date(self):
358 def test_get_commits_is_ordered_by_date(self):
353 commits = self.repo.get_commits()
359 commits = self.repo.get_commits()
354 assert isinstance(commits, CollectionGenerator)
360 assert isinstance(commits, CollectionGenerator)
355 assert len(commits) == 0 or len(commits) != 0
361 assert len(commits) == 0 or len(commits) != 0
356 commits = list(commits)
362 commits = list(commits)
357 ordered_by_date = sorted(commits, key=lambda commit: commit.date)
363 ordered_by_date = sorted(commits, key=lambda commit: commit.date)
358 assert commits == ordered_by_date
364 assert commits == ordered_by_date
359
365
360 def test_get_commits_respects_start(self):
366 def test_get_commits_respects_start(self):
361 second_id = self.repo.commit_ids[1]
367 second_id = self.repo.commit_ids[1]
362 commits = self.repo.get_commits(start_id=second_id)
368 commits = self.repo.get_commits(start_id=second_id)
363 assert isinstance(commits, CollectionGenerator)
369 assert isinstance(commits, CollectionGenerator)
364 commits = list(commits)
370 commits = list(commits)
365 assert len(commits) == 4
371 assert len(commits) == 4
366
372
367 def test_get_commits_includes_start_commit(self):
373 def test_get_commits_includes_start_commit(self):
368 second_id = self.repo.commit_ids[1]
374 second_id = self.repo.commit_ids[1]
369 commits = self.repo.get_commits(start_id=second_id)
375 commits = self.repo.get_commits(start_id=second_id)
370 assert isinstance(commits, CollectionGenerator)
376 assert isinstance(commits, CollectionGenerator)
371 commits = list(commits)
377 commits = list(commits)
372 assert commits[0].raw_id == second_id
378 assert commits[0].raw_id == second_id
373
379
374 def test_get_commits_respects_end(self):
380 def test_get_commits_respects_end(self):
375 second_id = self.repo.commit_ids[1]
381 second_id = self.repo.commit_ids[1]
376 commits = self.repo.get_commits(end_id=second_id)
382 commits = self.repo.get_commits(end_id=second_id)
377 assert isinstance(commits, CollectionGenerator)
383 assert isinstance(commits, CollectionGenerator)
378 commits = list(commits)
384 commits = list(commits)
379 assert commits[-1].raw_id == second_id
385 assert commits[-1].raw_id == second_id
380 assert len(commits) == 2
386 assert len(commits) == 2
381
387
382 def test_get_commits_respects_both_start_and_end(self):
388 def test_get_commits_respects_both_start_and_end(self):
383 second_id = self.repo.commit_ids[1]
389 second_id = self.repo.commit_ids[1]
384 third_id = self.repo.commit_ids[2]
390 third_id = self.repo.commit_ids[2]
385 commits = self.repo.get_commits(start_id=second_id, end_id=third_id)
391 commits = self.repo.get_commits(start_id=second_id, end_id=third_id)
386 assert isinstance(commits, CollectionGenerator)
392 assert isinstance(commits, CollectionGenerator)
387 commits = list(commits)
393 commits = list(commits)
388 assert len(commits) == 2
394 assert len(commits) == 2
389
395
390 def test_get_commits_on_empty_repo_raises_EmptyRepository_error(self):
396 def test_get_commits_on_empty_repo_raises_EmptyRepository_error(self):
391 repo_path = get_new_dir(str(time.time()))
397 repo_path = get_new_dir(str(time.time()))
392 repo = self.Backend(repo_path, create=True)
398 repo = self.Backend(repo_path, create=True)
393
399
394 with pytest.raises(EmptyRepositoryError):
400 with pytest.raises(EmptyRepositoryError):
395 list(repo.get_commits(start_id='foobar'))
401 list(repo.get_commits(start_id='foobar'))
396
402
397 def test_get_commits_respects_hidden(self):
403 def test_get_commits_respects_hidden(self):
398 commits = self.repo.get_commits(show_hidden=True)
404 commits = self.repo.get_commits(show_hidden=True)
399 assert isinstance(commits, CollectionGenerator)
405 assert isinstance(commits, CollectionGenerator)
400 assert len(commits) == 5
406 assert len(commits) == 5
401
407
402 def test_get_commits_includes_end_commit(self):
408 def test_get_commits_includes_end_commit(self):
403 second_id = self.repo.commit_ids[1]
409 second_id = self.repo.commit_ids[1]
404 commits = self.repo.get_commits(end_id=second_id)
410 commits = self.repo.get_commits(end_id=second_id)
405 assert isinstance(commits, CollectionGenerator)
411 assert isinstance(commits, CollectionGenerator)
406 assert len(commits) == 2
412 assert len(commits) == 2
407 commits = list(commits)
413 commits = list(commits)
408 assert commits[-1].raw_id == second_id
414 assert commits[-1].raw_id == second_id
409
415
410 def test_get_commits_respects_start_date(self):
416 def test_get_commits_respects_start_date(self):
411 start_date = datetime.datetime(2010, 1, 2)
417 start_date = datetime.datetime(2010, 1, 2)
412 commits = self.repo.get_commits(start_date=start_date)
418 commits = self.repo.get_commits(start_date=start_date)
413 assert isinstance(commits, CollectionGenerator)
419 assert isinstance(commits, CollectionGenerator)
414 # Should be 4 commits after 2010-01-02 00:00:00
420 # Should be 4 commits after 2010-01-02 00:00:00
415 assert len(commits) == 4
421 assert len(commits) == 4
416 for c in commits:
422 for c in commits:
417 assert c.date >= start_date
423 assert c.date >= start_date
418
424
419 def test_get_commits_respects_start_date_with_branch(self):
425 def test_get_commits_respects_start_date_with_branch(self):
420 start_date = datetime.datetime(2010, 1, 2)
426 start_date = datetime.datetime(2010, 1, 2)
421 commits = self.repo.get_commits(
427 commits = self.repo.get_commits(
422 start_date=start_date, branch_name=self.repo.DEFAULT_BRANCH_NAME)
428 start_date=start_date, branch_name=self.repo.DEFAULT_BRANCH_NAME)
423 assert isinstance(commits, CollectionGenerator)
429 assert isinstance(commits, CollectionGenerator)
424 # Should be 4 commits after 2010-01-02 00:00:00
430 # Should be 4 commits after 2010-01-02 00:00:00
425 assert len(commits) == 4
431 assert len(commits) == 4
426 for c in commits:
432 for c in commits:
427 assert c.date >= start_date
433 assert c.date >= start_date
428
434
429 def test_get_commits_respects_start_date_and_end_date(self):
435 def test_get_commits_respects_start_date_and_end_date(self):
430 start_date = datetime.datetime(2010, 1, 2)
436 start_date = datetime.datetime(2010, 1, 2)
431 end_date = datetime.datetime(2010, 1, 3)
437 end_date = datetime.datetime(2010, 1, 3)
432 commits = self.repo.get_commits(start_date=start_date,
438 commits = self.repo.get_commits(start_date=start_date,
433 end_date=end_date)
439 end_date=end_date)
434 assert isinstance(commits, CollectionGenerator)
440 assert isinstance(commits, CollectionGenerator)
435 assert len(commits) == 2
441 assert len(commits) == 2
436 for c in commits:
442 for c in commits:
437 assert c.date >= start_date
443 assert c.date >= start_date
438 assert c.date <= end_date
444 assert c.date <= end_date
439
445
440 def test_get_commits_respects_end_date(self):
446 def test_get_commits_respects_end_date(self):
441 end_date = datetime.datetime(2010, 1, 2)
447 end_date = datetime.datetime(2010, 1, 2)
442 commits = self.repo.get_commits(end_date=end_date)
448 commits = self.repo.get_commits(end_date=end_date)
443 assert isinstance(commits, CollectionGenerator)
449 assert isinstance(commits, CollectionGenerator)
444 assert len(commits) == 1
450 assert len(commits) == 1
445 for c in commits:
451 for c in commits:
446 assert c.date <= end_date
452 assert c.date <= end_date
447
453
448 def test_get_commits_respects_reverse(self):
454 def test_get_commits_respects_reverse(self):
449 commits = self.repo.get_commits() # no longer reverse support
455 commits = self.repo.get_commits() # no longer reverse support
450 assert isinstance(commits, CollectionGenerator)
456 assert isinstance(commits, CollectionGenerator)
451 assert len(commits) == 5
457 assert len(commits) == 5
452 commit_ids = reversed([c.raw_id for c in commits])
458 commit_ids = reversed([c.raw_id for c in commits])
453 assert list(commit_ids) == list(reversed(self.repo.commit_ids))
459 assert list(commit_ids) == list(reversed(self.repo.commit_ids))
454
460
455 def test_get_commits_slice_generator(self):
461 def test_get_commits_slice_generator(self):
456 commits = self.repo.get_commits(
462 commits = self.repo.get_commits(
457 branch_name=self.repo.DEFAULT_BRANCH_NAME)
463 branch_name=self.repo.DEFAULT_BRANCH_NAME)
458 assert isinstance(commits, CollectionGenerator)
464 assert isinstance(commits, CollectionGenerator)
459 commit_slice = list(commits[1:3])
465 commit_slice = list(commits[1:3])
460 assert len(commit_slice) == 2
466 assert len(commit_slice) == 2
461
467
462 def test_get_commits_raise_commitdoesnotexist_for_wrong_start(self):
468 def test_get_commits_raise_commitdoesnotexist_for_wrong_start(self):
463 with pytest.raises(CommitDoesNotExistError):
469 with pytest.raises(CommitDoesNotExistError):
464 list(self.repo.get_commits(start_id='foobar'))
470 list(self.repo.get_commits(start_id='foobar'))
465
471
466 def test_get_commits_raise_commitdoesnotexist_for_wrong_end(self):
472 def test_get_commits_raise_commitdoesnotexist_for_wrong_end(self):
467 with pytest.raises(CommitDoesNotExistError):
473 with pytest.raises(CommitDoesNotExistError):
468 list(self.repo.get_commits(end_id='foobar'))
474 list(self.repo.get_commits(end_id='foobar'))
469
475
470 def test_get_commits_raise_branchdoesnotexist_for_wrong_branch_name(self):
476 def test_get_commits_raise_branchdoesnotexist_for_wrong_branch_name(self):
471 with pytest.raises(BranchDoesNotExistError):
477 with pytest.raises(BranchDoesNotExistError):
472 list(self.repo.get_commits(branch_name='foobar'))
478 list(self.repo.get_commits(branch_name='foobar'))
473
479
474 def test_get_commits_raise_repositoryerror_for_wrong_start_end(self):
480 def test_get_commits_raise_repositoryerror_for_wrong_start_end(self):
475 start_id = self.repo.commit_ids[-1]
481 start_id = self.repo.commit_ids[-1]
476 end_id = self.repo.commit_ids[0]
482 end_id = self.repo.commit_ids[0]
477 with pytest.raises(RepositoryError):
483 with pytest.raises(RepositoryError):
478 list(self.repo.get_commits(start_id=start_id, end_id=end_id))
484 list(self.repo.get_commits(start_id=start_id, end_id=end_id))
479
485
480 def test_get_commits_raises_for_numerical_ids(self):
486 def test_get_commits_raises_for_numerical_ids(self):
481 with pytest.raises(TypeError):
487 with pytest.raises(TypeError):
482 self.repo.get_commits(start_id=1, end_id=2)
488 self.repo.get_commits(start_id=1, end_id=2)
483
489
484 def test_commit_equality(self):
490 def test_commit_equality(self):
485 commit1 = self.repo.get_commit(self.repo.commit_ids[0])
491 commit1 = self.repo.get_commit(self.repo.commit_ids[0])
486 commit2 = self.repo.get_commit(self.repo.commit_ids[1])
492 commit2 = self.repo.get_commit(self.repo.commit_ids[1])
487
493
488 assert commit1 == commit1
494 assert commit1 == commit1
489 assert commit2 == commit2
495 assert commit2 == commit2
490 assert commit1 != commit2
496 assert commit1 != commit2
491 assert commit2 != commit1
497 assert commit2 != commit1
492 assert commit1 is not None
498 assert commit1 is not None
493 assert commit2 is not None
499 assert commit2 is not None
494 assert 1 != commit1
500 assert 1 != commit1
495 assert 'string' != commit1
501 assert 'string' != commit1
496
502
497
503
498 @pytest.mark.parametrize("filename, expected", [
504 @pytest.mark.parametrize("filename, expected", [
499 ("README.rst", False),
505 ("README.rst", False),
500 ("README", True),
506 ("README", True),
501 ])
507 ])
502 def test_commit_is_link(vcsbackend, filename, expected):
508 def test_commit_is_link(vcsbackend, filename, expected):
503 commit = vcsbackend.repo.get_commit()
509 commit = vcsbackend.repo.get_commit()
504 link_status = commit.is_link(filename)
510 link_status = commit.is_link(filename)
505 assert link_status is expected
511 assert link_status is expected
506
512
507
513
508 @pytest.mark.usefixtures("vcs_repository_support")
514 @pytest.mark.usefixtures("vcs_repository_support")
509 class TestCommitsChanges(BackendTestMixin):
515 class TestCommitsChanges(BackendTestMixin):
510 recreate_repo_per_test = False
516 recreate_repo_per_test = False
511
517
512 @classmethod
518 @classmethod
513 def _get_commits(cls):
519 def _get_commits(cls):
514 return [
520 return [
515 {
521 {
516 'message': 'Initial',
522 'message': 'Initial',
517 'author': 'Joe Doe <joe.doe@example.com>',
523 'author': 'Joe Doe <joe.doe@example.com>',
518 'date': datetime.datetime(2010, 1, 1, 20),
524 'date': datetime.datetime(2010, 1, 1, 20),
519 'added': [
525 'added': [
520 FileNode(b'foo/bar', content=b'foo'),
526 FileNode(b'foo/bar', content=b'foo'),
521 FileNode(safe_bytes('foo/bał'), content=b'foo'),
527 FileNode(safe_bytes('foo/bał'), content=b'foo'),
522 FileNode(b'foobar', content=b'foo'),
528 FileNode(b'foobar', content=b'foo'),
523 FileNode(b'qwe', content=b'foo'),
529 FileNode(b'qwe', content=b'foo'),
524 ],
530 ],
525 },
531 },
526 {
532 {
527 'message': 'Massive changes',
533 'message': 'Massive changes',
528 'author': 'Joe Doe <joe.doe@example.com>',
534 'author': 'Joe Doe <joe.doe@example.com>',
529 'date': datetime.datetime(2010, 1, 1, 22),
535 'date': datetime.datetime(2010, 1, 1, 22),
530 'added': [FileNode(b'fallout', content=b'War never changes')],
536 'added': [FileNode(b'fallout', content=b'War never changes')],
531 'changed': [
537 'changed': [
532 FileNode(b'foo/bar', content=b'baz'),
538 FileNode(b'foo/bar', content=b'baz'),
533 FileNode(b'foobar', content=b'baz'),
539 FileNode(b'foobar', content=b'baz'),
534 ],
540 ],
535 'removed': [FileNode(b'qwe')],
541 'removed': [FileNode(b'qwe')],
536 },
542 },
537 ]
543 ]
538
544
539 def test_initial_commit(self, local_dt_to_utc):
545 def test_initial_commit(self, local_dt_to_utc):
540 commit = self.repo.get_commit(commit_idx=0)
546 commit = self.repo.get_commit(commit_idx=0)
541 assert set(commit.added) == {
547 assert set(commit.added) == {
542 commit.get_node('foo/bar'),
548 commit.get_node('foo/bar'),
543 commit.get_node('foo/bał'),
549 commit.get_node('foo/bał'),
544 commit.get_node('foobar'),
550 commit.get_node('foobar'),
545 commit.get_node('qwe')
551 commit.get_node('qwe')
546 }
552 }
547 assert set(commit.changed) == set()
553 assert set(commit.changed) == set()
548 assert set(commit.removed) == set()
554 assert set(commit.removed) == set()
549 assert set(commit.affected_files) == {'foo/bar', 'foo/bał', 'foobar', 'qwe'}
555 assert set(commit.affected_files) == {'foo/bar', 'foo/bał', 'foobar', 'qwe'}
550 assert commit.date == local_dt_to_utc(
556 assert commit.date == local_dt_to_utc(
551 datetime.datetime(2010, 1, 1, 20, 0))
557 datetime.datetime(2010, 1, 1, 20, 0))
552
558
553 def test_head_added(self):
559 def test_head_added(self):
554 commit = self.repo.get_commit()
560 commit = self.repo.get_commit()
555 assert isinstance(commit.added, AddedFileNodesGenerator)
561 assert isinstance(commit.added, AddedFileNodesGenerator)
556 assert set(commit.added) == {commit.get_node('fallout')}
562 assert set(commit.added) == {commit.get_node('fallout')}
557 assert isinstance(commit.changed, ChangedFileNodesGenerator)
563 assert isinstance(commit.changed, ChangedFileNodesGenerator)
558 assert set(commit.changed) == {commit.get_node('foo/bar'), commit.get_node('foobar')}
564 assert set(commit.changed) == {commit.get_node('foo/bar'), commit.get_node('foobar')}
559 assert isinstance(commit.removed, RemovedFileNodesGenerator)
565 assert isinstance(commit.removed, RemovedFileNodesGenerator)
560 assert len(commit.removed) == 1
566 assert len(commit.removed) == 1
561 assert list(commit.removed)[0].path == 'qwe'
567 assert list(commit.removed)[0].path == 'qwe'
562
568
563 def test_get_filemode(self):
569 def test_get_filemode(self):
564 commit = self.repo.get_commit()
570 commit = self.repo.get_commit()
565 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bar')
571 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bar')
566
572
567 def test_get_filemode_non_ascii(self):
573 def test_get_filemode_non_ascii(self):
568 commit = self.repo.get_commit()
574 commit = self.repo.get_commit()
569 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bał')
575 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bał')
570 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bał')
576 assert FILEMODE_DEFAULT == commit.get_file_mode('foo/bał')
571
577
572 def test_get_path_history(self):
578 def test_get_path_history(self):
573 commit = self.repo.get_commit()
579 commit = self.repo.get_commit()
574 history = commit.get_path_history('foo/bar')
580 history = commit.get_path_history('foo/bar')
575 assert len(history) == 2
581 assert len(history) == 2
576
582
577 def test_get_path_history_with_limit(self):
583 def test_get_path_history_with_limit(self):
578 commit = self.repo.get_commit()
584 commit = self.repo.get_commit()
579 history = commit.get_path_history('foo/bar', limit=1)
585 history = commit.get_path_history('foo/bar', limit=1)
580 assert len(history) == 1
586 assert len(history) == 1
581
587
582 def test_get_path_history_first_commit(self):
588 def test_get_path_history_first_commit(self):
583 commit = self.repo[0]
589 commit = self.repo[0]
584 history = commit.get_path_history('foo/bar')
590 history = commit.get_path_history('foo/bar')
585 assert len(history) == 1
591 assert len(history) == 1
586
592
587
593
588 def assert_text_equal(expected, given):
594 def assert_text_equal(expected, given):
589 assert expected == given
595 assert expected == given
590 assert isinstance(expected, str)
596 assert isinstance(expected, str)
591 assert isinstance(given, str)
597 assert isinstance(given, str)
General Comments 0
You need to be logged in to leave comments. Login now