12597987f54678722000290f0fdebd499f49f4e0
[jelmer/python-fastimport.git] / fastimport / tests / test_commands.py
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 import (
21     commands,
22     )
23
24
25 class TestBlobDisplay(TestCase):
26
27     def test_blob(self):
28         c = commands.BlobCommand("1", "hello world")
29         self.assertEqual("blob\nmark :1\ndata 11\nhello world", repr(c))
30
31     def test_blob_no_mark(self):
32         c = commands.BlobCommand(None, "hello world")
33         self.assertEqual("blob\ndata 11\nhello world", repr(c))
34
35
36 class TestCheckpointDisplay(TestCase):
37
38     def test_checkpoint(self):
39         c = commands.CheckpointCommand()
40         self.assertEqual("checkpoint", repr(c))
41
42
43 class TestCommitDisplay(TestCase):
44
45     def test_commit(self):
46         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
47         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
48         c = commands.CommitCommand("refs/heads/master", "bbb", None, committer,
49             "release v1.0", ":aaa", None, None)
50         self.assertEqual(
51             "commit refs/heads/master\n"
52             "mark :bbb\n"
53             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
54             "data 12\n"
55             "release v1.0\n"
56             "from :aaa",
57             repr(c))
58
59     def test_commit_unicode_committer(self):
60         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
61         name = u'\u013d\xf3r\xe9m \xcdp\u0161\xfam'
62         name_utf8 = name.encode('utf8')
63         committer = (name, 'test@example.com', 1234567890, -6 * 3600)
64         c = commands.CommitCommand("refs/heads/master", "bbb", None, committer,
65             "release v1.0", ":aaa", None, None)
66         self.assertEqual(
67             "commit refs/heads/master\n"
68             "mark :bbb\n"
69             "committer %s <test@example.com> 1234567890 -0600\n"
70             "data 12\n"
71             "release v1.0\n"
72             "from :aaa" % (name_utf8,),
73             repr(c))
74
75     def test_commit_no_mark(self):
76         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
77         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
78         c = commands.CommitCommand("refs/heads/master", None, None, committer,
79             "release v1.0", ":aaa", None, None)
80         self.assertEqual(
81             "commit refs/heads/master\n"
82             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
83             "data 12\n"
84             "release v1.0\n"
85             "from :aaa",
86             repr(c))
87
88     def test_commit_no_from(self):
89         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
90         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
91         c = commands.CommitCommand("refs/heads/master", "bbb", None, committer,
92             "release v1.0", None, None, None)
93         self.assertEqual(
94             "commit refs/heads/master\n"
95             "mark :bbb\n"
96             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
97             "data 12\n"
98             "release v1.0",
99             repr(c))
100
101     def test_commit_with_author(self):
102         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
103         author = ('Sue Wong', 'sue@example.com', 1234565432, -6 * 3600)
104         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
105         c = commands.CommitCommand("refs/heads/master", "bbb", author,
106             committer, "release v1.0", ":aaa", None, None)
107         self.assertEqual(
108             "commit refs/heads/master\n"
109             "mark :bbb\n"
110             "author Sue Wong <sue@example.com> 1234565432 -0600\n"
111             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
112             "data 12\n"
113             "release v1.0\n"
114             "from :aaa",
115             repr(c))
116
117     def test_commit_with_merges(self):
118         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
119         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
120         c = commands.CommitCommand("refs/heads/master", "ddd", None, committer,
121                 "release v1.0", ":aaa", [':bbb', ':ccc'], None)
122         self.assertEqual(
123             "commit refs/heads/master\n"
124             "mark :ddd\n"
125             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
126             "data 12\n"
127             "release v1.0\n"
128             "from :aaa\n"
129             "merge :bbb\n"
130             "merge :ccc",
131             repr(c))
132
133     def test_commit_with_filecommands(self):
134         file_cmds = iter([
135             commands.FileDeleteCommand('readme.txt'),
136             commands.FileModifyCommand('NEWS', 0100644, None,
137                 'blah blah blah'),
138             ])
139         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
140         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
141         c = commands.CommitCommand("refs/heads/master", "bbb", None, committer,
142             "release v1.0", ":aaa", None, file_cmds)
143         self.assertEqual(
144             "commit refs/heads/master\n"
145             "mark :bbb\n"
146             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
147             "data 12\n"
148             "release v1.0\n"
149             "from :aaa\n"
150             "D readme.txt\n"
151             "M 644 inline NEWS\n"
152             "data 14\n"
153             "blah blah blah",
154             repr(c))
155
156     def test_commit_with_more_authors(self):
157         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
158         author = ('Sue Wong', 'sue@example.com', 1234565432, -6 * 3600)
159         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
160         more_authors = [
161             ('Al Smith', 'al@example.com', 1234565432, -6 * 3600),
162             ('Bill Jones', 'bill@example.com', 1234565432, -6 * 3600),
163             ]
164         c = commands.CommitCommand("refs/heads/master", "bbb", author,
165             committer, "release v1.0", ":aaa", None, None,
166             more_authors=more_authors)
167         self.assertEqual(
168             "commit refs/heads/master\n"
169             "mark :bbb\n"
170             "author Sue Wong <sue@example.com> 1234565432 -0600\n"
171             "author Al Smith <al@example.com> 1234565432 -0600\n"
172             "author Bill Jones <bill@example.com> 1234565432 -0600\n"
173             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
174             "data 12\n"
175             "release v1.0\n"
176             "from :aaa",
177             repr(c))
178
179     def test_commit_with_properties(self):
180         # user tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
181         committer = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
182         properties = {
183             u'greeting':  u'hello',
184             u'planet':    u'world',
185             }
186         c = commands.CommitCommand("refs/heads/master", "bbb", None,
187             committer, "release v1.0", ":aaa", None, None,
188             properties=properties)
189         self.assertEqual(
190             "commit refs/heads/master\n"
191             "mark :bbb\n"
192             "committer Joe Wong <joe@example.com> 1234567890 -0600\n"
193             "data 12\n"
194             "release v1.0\n"
195             "from :aaa\n"
196             "property greeting 5 hello\n"
197             "property planet 5 world",
198             repr(c))
199
200
201 class TestFeatureDisplay(TestCase):
202
203     def test_feature(self):
204         c = commands.FeatureCommand("dwim")
205         self.assertEqual("feature dwim", repr(c))
206
207     def test_feature_with_value(self):
208         c = commands.FeatureCommand("dwim", "please")
209         self.assertEqual("feature dwim=please", repr(c))
210
211
212 class TestProgressDisplay(TestCase):
213
214     def test_progress(self):
215         c = commands.ProgressCommand("doing foo")
216         self.assertEqual("progress doing foo", repr(c))
217
218
219 class TestResetDisplay(TestCase):
220
221     def test_reset(self):
222         c = commands.ResetCommand("refs/tags/v1.0", ":xxx")
223         self.assertEqual("reset refs/tags/v1.0\nfrom :xxx\n", repr(c))
224
225     def test_reset_no_from(self):
226         c = commands.ResetCommand("refs/remotes/origin/master", None)
227         self.assertEqual("reset refs/remotes/origin/master", repr(c))
228
229
230 class TestTagDisplay(TestCase):
231
232     def test_tag(self):
233         # tagger tuple is (name, email, secs-since-epoch, secs-offset-from-utc)
234         tagger = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
235         c = commands.TagCommand("refs/tags/v1.0", ":xxx", tagger, "create v1.0")
236         self.assertEqual(
237             "tag refs/tags/v1.0\n"
238             "from :xxx\n"
239             "tagger Joe Wong <joe@example.com> 1234567890 -0600\n"
240             "data 11\n"
241             "create v1.0",
242             repr(c))
243
244     def test_tag_no_from(self):
245         tagger = ('Joe Wong', 'joe@example.com', 1234567890, -6 * 3600)
246         c = commands.TagCommand("refs/tags/v1.0", None, tagger, "create v1.0")
247         self.assertEqual(
248             "tag refs/tags/v1.0\n"
249             "tagger Joe Wong <joe@example.com> 1234567890 -0600\n"
250             "data 11\n"
251             "create v1.0",
252             repr(c))
253
254
255 class TestFileModifyDisplay(TestCase):
256
257     def test_filemodify_file(self):
258         c = commands.FileModifyCommand("foo/bar", 0100644, ":23", None)
259         self.assertEqual("M 644 :23 foo/bar", repr(c))
260
261     def test_filemodify_file_executable(self):
262         c = commands.FileModifyCommand("foo/bar", 0100755, ":23", None)
263         self.assertEqual("M 755 :23 foo/bar", repr(c))
264
265     def test_filemodify_file_internal(self):
266         c = commands.FileModifyCommand("foo/bar", 0100644, None,
267             "hello world")
268         self.assertEqual("M 644 inline foo/bar\ndata 11\nhello world", repr(c))
269
270     def test_filemodify_symlink(self):
271         c = commands.FileModifyCommand("foo/bar", 0120000, None, "baz")
272         self.assertEqual("M 120000 inline foo/bar\ndata 3\nbaz", repr(c))
273
274     def test_filemodify_treeref(self):
275         c = commands.FileModifyCommand("tree-info", 0160000,
276             "revision-id-info", None)
277         self.assertEqual("M 160000 revision-id-info tree-info", repr(c))
278
279
280 class TestFileDeleteDisplay(TestCase):
281
282     def test_filedelete(self):
283         c = commands.FileDeleteCommand("foo/bar")
284         self.assertEqual("D foo/bar", repr(c))
285
286
287 class TestFileCopyDisplay(TestCase):
288
289     def test_filecopy(self):
290         c = commands.FileCopyCommand("foo/bar", "foo/baz")
291         self.assertEqual("C foo/bar foo/baz", repr(c))
292
293     def test_filecopy_quoted(self):
294         # Check the first path is quoted if it contains spaces
295         c = commands.FileCopyCommand("foo/b a r", "foo/b a z")
296         self.assertEqual('C "foo/b a r" foo/b a z', repr(c))
297
298
299 class TestFileRenameDisplay(TestCase):
300
301     def test_filerename(self):
302         c = commands.FileRenameCommand("foo/bar", "foo/baz")
303         self.assertEqual("R foo/bar foo/baz", repr(c))
304
305     def test_filerename_quoted(self):
306         # Check the first path is quoted if it contains spaces
307         c = commands.FileRenameCommand("foo/b a r", "foo/b a z")
308         self.assertEqual('R "foo/b a r" foo/b a z', repr(c))
309
310
311 class TestFileDeleteAllDisplay(TestCase):
312
313     def test_filedeleteall(self):
314         c = commands.FileDeleteAllCommand()
315         self.assertEqual("deleteall", repr(c))
316
317
318 class TestPathChecking(TestCase):
319
320     def test_filemodify_path_checking(self):
321         self.assertRaises(ValueError, commands.FileModifyCommand, "",
322             0100644, None, "text")
323         self.assertRaises(ValueError, commands.FileModifyCommand, None,
324             0100644, None, "text")
325
326     def test_filedelete_path_checking(self):
327         self.assertRaises(ValueError, commands.FileDeleteCommand, "")
328         self.assertRaises(ValueError, commands.FileDeleteCommand, None)
329
330     def test_filerename_path_checking(self):
331         self.assertRaises(ValueError, commands.FileRenameCommand, "", "foo")
332         self.assertRaises(ValueError, commands.FileRenameCommand, None, "foo")
333         self.assertRaises(ValueError, commands.FileRenameCommand, "foo", "")
334         self.assertRaises(ValueError, commands.FileRenameCommand, "foo", None)
335
336     def test_filecopy_path_checking(self):
337         self.assertRaises(ValueError, commands.FileCopyCommand, "", "foo")
338         self.assertRaises(ValueError, commands.FileCopyCommand, None, "foo")
339         self.assertRaises(ValueError, commands.FileCopyCommand, "foo", "")
340         self.assertRaises(ValueError, commands.FileCopyCommand, "foo", None)