comparison hgext3rd/fastimport/vendor/python_fastimport/tests/test_commands.py @ 86:28704a2a7461 vendor/python-fastimport

Import python-fastimport-0.9.8
author Roy Marples <roy@marples.name>
date Tue, 19 Jan 2021 22:56:34 +0000
parents
children
comparison
equal deleted inserted replaced
85:1f5544a8870b 86:28704a2a7461
1 # Copyright (C) 2009 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 """Test how Commands are displayed"""
17
18 from unittest import TestCase
19
20 from fastimport.helpers import (
21 repr_bytes,
22 utf8_bytes_string,
23 )
24
25 from fastimport import (
26 commands,
27 )
28
29
30 class TestBlobDisplay(TestCase):
31
32 def test_blob(self):
33 c = commands.BlobCommand(b"1", b"hello world")
34 self.assertEqual(b"blob\nmark :1\ndata 11\nhello world", repr_bytes(c))
35
36 def test_blob_no_mark(self):
37 c = commands.BlobCommand(None, b"hello world")
38 self.assertEqual(b"blob\ndata 11\nhello world", repr_bytes(c))
39
40
41 class TestCheckpointDisplay(TestCase):
42
43 def test_checkpoint(self):
44 c = commands.CheckpointCommand()
45 self.assertEqual(b'checkpoint', repr_bytes(c))
46
47
48 class TestCommitDisplay(TestCase):
49
50 def test_commit(self):
51 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
52 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
53 c = commands.CommitCommand(b"refs/heads/master", b"bbb", None, committer,
54 b"release v1.0", b":aaa", None, None)
55 self.assertEqual(
56 b"commit refs/heads/master\n"
57 b"mark :bbb\n"
58 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
59 b"data 12\n"
60 b"release v1.0\n"
61 b"from :aaa",
62 repr_bytes(c))
63
64 def test_commit_unicode_committer(self):
65 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
66 name = u'\u013d\xf3r\xe9m \xcdp\u0161\xfam'
67
68 commit_utf8 = utf8_bytes_string(
69 u"commit refs/heads/master\n"
70 u"mark :bbb\n"
71 u"committer %s <test@example.com> 1234567890 -0600\n"
72 u"data 12\n"
73 u"release v1.0\n"
74 u"from :aaa" % (name,)
75 )
76
77 committer = (name, b'test@example.com', 1234567890, -6 * 3600)
78 c = commands.CommitCommand(b'refs/heads/master', b'bbb', None, committer,
79 b'release v1.0', b':aaa', None, None)
80
81 self.assertEqual(commit_utf8, repr_bytes(c))
82
83 def test_commit_no_mark(self):
84 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
85 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
86 c = commands.CommitCommand(b'refs/heads/master', None, None, committer,
87 b'release v1.0', b':aaa', None, None)
88 self.assertEqual(
89 b"commit refs/heads/master\n"
90 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
91 b"data 12\n"
92 b"release v1.0\n"
93 b"from :aaa",
94 repr_bytes(c))
95
96 def test_commit_no_from(self):
97 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
98 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
99 c = commands.CommitCommand(b"refs/heads/master", b"bbb", None, committer,
100 b"release v1.0", None, None, None)
101 self.assertEqual(
102 b"commit refs/heads/master\n"
103 b"mark :bbb\n"
104 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
105 b"data 12\n"
106 b"release v1.0",
107 repr_bytes(c))
108
109 def test_commit_with_author(self):
110 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
111 author = (b'Sue Wong', b'sue@example.com', 1234565432, -6 * 3600)
112 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
113 c = commands.CommitCommand(b'refs/heads/master', b'bbb', author,
114 committer, b'release v1.0', b':aaa', None, None)
115 self.assertEqual(
116 b"commit refs/heads/master\n"
117 b"mark :bbb\n"
118 b"author Sue Wong <sue@example.com> 1234565432 -0600\n"
119 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
120 b"data 12\n"
121 b"release v1.0\n"
122 b"from :aaa",
123 repr_bytes(c))
124
125 def test_commit_with_merges(self):
126 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
127 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
128 c = commands.CommitCommand(b"refs/heads/master", b"ddd", None, committer,
129 b'release v1.0', b":aaa", [b':bbb', b':ccc'], None)
130 self.assertEqual(
131 b"commit refs/heads/master\n"
132 b"mark :ddd\n"
133 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
134 b"data 12\n"
135 b"release v1.0\n"
136 b"from :aaa\n"
137 b"merge :bbb\n"
138 b"merge :ccc",
139 repr_bytes(c))
140
141 def test_commit_with_filecommands(self):
142 file_cmds = iter([
143 commands.FileDeleteCommand(b'readme.txt'),
144 commands.FileModifyCommand(b'NEWS', 0o100644, None,
145 b'blah blah blah'),
146 ])
147 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
148 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
149 c = commands.CommitCommand(b'refs/heads/master', b'bbb', None, committer,
150 b'release v1.0', b':aaa', None, file_cmds)
151 self.assertEqual(
152 b"commit refs/heads/master\n"
153 b"mark :bbb\n"
154 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
155 b"data 12\n"
156 b"release v1.0\n"
157 b"from :aaa\n"
158 b"D readme.txt\n"
159 b"M 644 inline NEWS\n"
160 b"data 14\n"
161 b"blah blah blah",
162 repr_bytes(c))
163
164 def test_commit_with_more_authors(self):
165 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
166 author = (b'Sue Wong', b'sue@example.com', 1234565432, -6 * 3600)
167 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
168 more_authors = [
169 (b'Al Smith', b'al@example.com', 1234565432, -6 * 3600),
170 (b'Bill Jones', b'bill@example.com', 1234565432, -6 * 3600),
171 ]
172 c = commands.CommitCommand(b'refs/heads/master', b'bbb', author,
173 committer, b'release v1.0', b':aaa', None, None,
174 more_authors=more_authors)
175 self.assertEqual(
176 b"commit refs/heads/master\n"
177 b"mark :bbb\n"
178 b"author Sue Wong <sue@example.com> 1234565432 -0600\n"
179 b"author Al Smith <al@example.com> 1234565432 -0600\n"
180 b"author Bill Jones <bill@example.com> 1234565432 -0600\n"
181 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
182 b"data 12\n"
183 b"release v1.0\n"
184 b"from :aaa",
185 repr_bytes(c))
186
187 def test_commit_with_properties(self):
188 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
189 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
190 properties = {
191 u'greeting': u'hello',
192 u'planet': u'world',
193 }
194 c = commands.CommitCommand(b'refs/heads/master', b'bbb', None,
195 committer, b'release v1.0', b':aaa', None, None,
196 properties=properties)
197 self.assertEqual(
198 b"commit refs/heads/master\n"
199 b"mark :bbb\n"
200 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
201 b"data 12\n"
202 b"release v1.0\n"
203 b"from :aaa\n"
204 b"property greeting 5 hello\n"
205 b"property planet 5 world",
206 repr_bytes(c))
207
208 def test_commit_with_int_mark(self):
209 # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
210 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
211 properties = {
212 u'greeting': u'hello',
213 u'planet': u'world',
214 }
215 c = commands.CommitCommand(b'refs/heads/master', 123, None,
216 committer, b'release v1.0', b':aaa', None, None,
217 properties=properties)
218 self.assertEqual(
219 b"commit refs/heads/master\n"
220 b"mark :123\n"
221 b"committer Joe Wong <joe@example.com> 1234567890 -0600\n"
222 b"data 12\n"
223 b"release v1.0\n"
224 b"from :aaa\n"
225 b"property greeting 5 hello\n"
226 b"property planet 5 world",
227 repr_bytes(c))
228
229 class TestCommitCopy(TestCase):
230
231 def setUp(self):
232 super(TestCommitCopy, self).setUp()
233 file_cmds = iter([
234 commands.FileDeleteCommand(b'readme.txt'),
235 commands.FileModifyCommand(b'NEWS', 0o100644, None, b'blah blah blah'),
236 ])
237
238 committer = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
239 self.c = commands.CommitCommand(
240 b'refs/heads/master', b'bbb', None, committer,
241 b'release v1.0', b':aaa', None, file_cmds)
242
243 def test_simple_copy(self):
244 c2 = self.c.copy()
245
246 self.assertFalse(self.c is c2)
247 self.assertEqual(repr_bytes(self.c), repr_bytes(c2))
248
249 def test_replace_attr(self):
250 c2 = self.c.copy(mark=b'ccc')
251 self.assertEqual(
252 repr_bytes(self.c).replace(b'mark :bbb', b'mark :ccc'),
253 repr_bytes(c2)
254 )
255
256 def test_invalid_attribute(self):
257 self.assertRaises(TypeError, self.c.copy, invalid=True)
258
259 class TestFeatureDisplay(TestCase):
260
261 def test_feature(self):
262 c = commands.FeatureCommand(b"dwim")
263 self.assertEqual(b"feature dwim", repr_bytes(c))
264
265 def test_feature_with_value(self):
266 c = commands.FeatureCommand(b"dwim", b"please")
267 self.assertEqual(b"feature dwim=please", repr_bytes(c))
268
269
270 class TestProgressDisplay(TestCase):
271
272 def test_progress(self):
273 c = commands.ProgressCommand(b"doing foo")
274 self.assertEqual(b"progress doing foo", repr_bytes(c))
275
276
277 class TestResetDisplay(TestCase):
278
279 def test_reset(self):
280 c = commands.ResetCommand(b"refs/tags/v1.0", b":xxx")
281 self.assertEqual(b"reset refs/tags/v1.0\nfrom :xxx\n", repr_bytes(c))
282
283 def test_reset_no_from(self):
284 c = commands.ResetCommand(b'refs/remotes/origin/master', None)
285 self.assertEqual(b'reset refs/remotes/origin/master', repr_bytes(c))
286
287
288 class TestTagDisplay(TestCase):
289
290 def test_tag(self):
291 # tagger tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
292 tagger = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
293 c = commands.TagCommand(b'refs/tags/v1.0', b':xxx', tagger, b'create v1.0')
294 self.assertEqual(
295 b"tag refs/tags/v1.0\n"
296 b"from :xxx\n"
297 b"tagger Joe Wong <joe@example.com> 1234567890 -0600\n"
298 b"data 11\n"
299 b"create v1.0",
300 repr_bytes(c))
301
302 def test_tag_no_from(self):
303 tagger = (b'Joe Wong', b'joe@example.com', 1234567890, -6 * 3600)
304 c = commands.TagCommand(b'refs/tags/v1.0', None, tagger, b'create v1.0')
305 self.assertEqual(
306 b"tag refs/tags/v1.0\n"
307 b"tagger Joe Wong <joe@example.com> 1234567890 -0600\n"
308 b"data 11\n"
309 b"create v1.0",
310 repr_bytes(c))
311
312
313 class TestFileModifyDisplay(TestCase):
314
315 def test_filemodify_file(self):
316 c = commands.FileModifyCommand(b'foo/bar', 0o100644, b':23', None)
317 self.assertEqual(b'M 644 :23 foo/bar', repr_bytes(c))
318
319 def test_filemodify_file_executable(self):
320 c = commands.FileModifyCommand(b'foo/bar', 0o100755, b':23', None)
321 self.assertEqual(b'M 755 :23 foo/bar', repr_bytes(c))
322
323 def test_filemodify_file_internal(self):
324 c = commands.FileModifyCommand(b'foo/bar', 0o100644, None,
325 b'hello world')
326 self.assertEqual(b'M 644 inline foo/bar\ndata 11\nhello world', repr_bytes(c))
327
328 def test_filemodify_symlink(self):
329 c = commands.FileModifyCommand(b'foo/bar', 0o120000, None, b'baz')
330 self.assertEqual(b'M 120000 inline foo/bar\ndata 3\nbaz', repr_bytes(c))
331
332 def test_filemodify_treeref(self):
333 c = commands.FileModifyCommand(b'tree-info', 0o160000,
334 b'revision-id-info', None)
335 self.assertEqual(b'M 160000 revision-id-info tree-info', repr_bytes(c))
336
337
338 class TestFileDeleteDisplay(TestCase):
339
340 def test_filedelete(self):
341 c = commands.FileDeleteCommand(b'foo/bar')
342 self.assertEqual(b'D foo/bar', repr_bytes(c))
343
344
345 class TestFileCopyDisplay(TestCase):
346
347 def test_filecopy(self):
348 c = commands.FileCopyCommand(b'foo/bar', b'foo/baz')
349 self.assertEqual(b'C foo/bar foo/baz', repr_bytes(c))
350
351 def test_filecopy_quoted(self):
352 # Check the first path is quoted if it contains spaces
353 c = commands.FileCopyCommand(b'foo/b a r', b'foo/b a z')
354 self.assertEqual(b'C "foo/b a r" foo/b a z', repr_bytes(c))
355
356
357 class TestFileRenameDisplay(TestCase):
358
359 def test_filerename(self):
360 c = commands.FileRenameCommand(b'foo/bar', b'foo/baz')
361 self.assertEqual(b'R foo/bar foo/baz', repr_bytes(c))
362
363 def test_filerename_quoted(self):
364 # Check the first path is quoted if it contains spaces
365 c = commands.FileRenameCommand(b'foo/b a r', b'foo/b a z')
366 self.assertEqual(b'R "foo/b a r" foo/b a z', repr_bytes(c))
367
368
369 class TestFileDeleteAllDisplay(TestCase):
370
371 def test_filedeleteall(self):
372 c = commands.FileDeleteAllCommand()
373 self.assertEqual(b'deleteall', repr_bytes(c))
374
375 class TestNotesDisplay(TestCase):
376
377 def test_noteonly(self):
378 c = commands.NoteModifyCommand(b'foo', b'A basic note')
379 self.assertEqual(b'N inline :foo\ndata 12\nA basic note', repr_bytes(c))
380
381 def test_notecommit(self):
382 committer = (b'Ed Mund', b'ed@example.org', 1234565432, 0)
383
384 commits = [
385 commands.CommitCommand(
386 ref=b'refs/heads/master',
387 mark=b'1',
388 author=committer,
389 committer=committer,
390 message=b'test\n',
391 from_=None,
392 merges=[],
393 file_iter=[
394 commands.FileModifyCommand(b'bar', 0o100644, None, b'')
395 ]),
396 commands.CommitCommand(
397 ref=b'refs/notes/commits',
398 mark=None,
399 author=None,
400 committer=committer,
401 message=b"Notes added by 'git notes add'\n",
402 from_=None,
403 merges=[],
404 file_iter=[
405 commands.NoteModifyCommand(b'1', b'Test note\n')
406 ]),
407 commands.CommitCommand(
408 ref=b'refs/notes/test',
409 mark=None,
410 author=None,
411 committer=committer,
412 message=b"Notes added by 'git notes add'\n",
413 from_=None,
414 merges=[],
415 file_iter=[
416 commands.NoteModifyCommand(b'1', b'Test test\n')
417 ])
418 ]
419
420 self.assertEqual(
421 b"""commit refs/heads/master
422 mark :1
423 author Ed Mund <ed@example.org> 1234565432 +0000
424 committer Ed Mund <ed@example.org> 1234565432 +0000
425 data 5
426 test
427
428 M 644 inline bar
429 data 0
430 commit refs/notes/commits
431 committer Ed Mund <ed@example.org> 1234565432 +0000
432 data 31
433 Notes added by 'git notes add'
434
435 N inline :1
436 data 10
437 Test note
438 commit refs/notes/test
439 committer Ed Mund <ed@example.org> 1234565432 +0000
440 data 31
441 Notes added by 'git notes add'
442
443 N inline :1
444 data 10
445 Test test
446 """, b''.join([repr_bytes(s) for s in commits]))
447
448
449 class TestPathChecking(TestCase):
450
451 def test_filemodify_path_checking(self):
452 self.assertRaises(ValueError, commands.FileModifyCommand, b'',
453 0o100644, None, b'text')
454 self.assertRaises(ValueError, commands.FileModifyCommand, None,
455 0o100644, None, b'text')
456
457 def test_filedelete_path_checking(self):
458 self.assertRaises(ValueError, commands.FileDeleteCommand, b'')
459 self.assertRaises(ValueError, commands.FileDeleteCommand, None)
460
461 def test_filerename_path_checking(self):
462 self.assertRaises(ValueError, commands.FileRenameCommand, b'', b'foo')
463 self.assertRaises(ValueError, commands.FileRenameCommand, None, b'foo')
464 self.assertRaises(ValueError, commands.FileRenameCommand, b'foo', b'')
465 self.assertRaises(ValueError, commands.FileRenameCommand, b'foo', None)
466
467 def test_filecopy_path_checking(self):
468 self.assertRaises(ValueError, commands.FileCopyCommand, b'', b'foo')
469 self.assertRaises(ValueError, commands.FileCopyCommand, None, b'foo')
470 self.assertRaises(ValueError, commands.FileCopyCommand, b'foo', b'')
471 self.assertRaises(ValueError, commands.FileCopyCommand, b'foo', None)