The get_ancestry() method was removed from bzr sometime in the past.
[bzr-fastimport.git] / tests / test_commands.py
blob88fefd279bcfac283f2216f45f0574c5bba40732
1 # Copyright (C) 2010 Canonical Ltd
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.
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.
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/>.
16 """Test the command implementations."""
18 import os
19 import re
20 import tempfile
21 import gzip
23 from bzrlib import tests
24 from bzrlib.tests.blackbox import ExternalBase
26 from bzrlib.plugins.fastimport.cmds import (
27 _get_source_stream,
30 from bzrlib.plugins.fastimport.tests import (
31 FastimportFeature,
35 class TestSourceStream(tests.TestCase):
37 _test_needs_features = [FastimportFeature]
39 def test_get_source_stream_stdin(self):
40 # - returns standard in
41 self.assertIsNot(None, _get_source_stream("-"))
43 def test_get_source_gz(self):
44 # files ending in .gz are automatically decompressed.
45 fd, filename = tempfile.mkstemp(suffix=".gz")
46 f = gzip.GzipFile(fileobj=os.fdopen(fd, "w"), mode='w')
47 f.write("bla")
48 f.close()
49 stream = _get_source_stream(filename)
50 self.assertIsNot("bla", stream.read())
52 def test_get_source_file(self):
53 # other files are opened as regular files.
54 fd, filename = tempfile.mkstemp()
55 f = os.fdopen(fd, 'w')
56 f.write("bla")
57 f.close()
58 stream = _get_source_stream(filename)
59 self.assertIsNot("bla", stream.read())
62 fast_export_baseline_data = """commit refs/heads/master
63 mark :1
64 committer
65 data 15
66 add c, remove b
67 M 644 inline a
68 data 13
69 test 1
70 test 3
71 M 644 inline c
72 data 6
73 test 4
74 commit refs/heads/master
75 mark :2
76 committer
77 data 14
78 modify a again
79 from :1
80 M 644 inline a
81 data 20
82 test 1
83 test 3
84 test 5
85 commit refs/heads/master
86 mark :3
87 committer
88 data 5
89 add d
90 from :2
91 M 644 inline d
92 data 6
93 test 6
94 """
96 class TestFastExport(ExternalBase):
98 def test_empty(self):
99 self.make_branch_and_tree("br")
100 self.assertEquals("", self.run_bzr("fast-export br")[0])
102 def test_pointless(self):
103 tree = self.make_branch_and_tree("br")
104 tree.commit("pointless")
105 data = self.run_bzr("fast-export br")[0]
106 self.assertTrue(data.startswith('commit refs/heads/master\nmark :1\ncommitter'))
108 def test_file(self):
109 tree = self.make_branch_and_tree("br")
110 tree.commit("pointless")
111 data = self.run_bzr("fast-export br br.fi")[0]
112 self.assertEquals("", data)
113 try:
114 self.assertPathExists("br.fi")
115 except AttributeError: # bzr < 2.4
116 self.failUnlessExists("br.fi")
118 def test_tag_rewriting(self):
119 tree = self.make_branch_and_tree("br")
120 tree.commit("pointless")
121 self.assertTrue(tree.branch.supports_tags())
122 rev_id = tree.branch.dotted_revno_to_revision_id((1,))
123 tree.branch.tags.set_tag("goodTag", rev_id)
124 tree.branch.tags.set_tag("bad Tag", rev_id)
126 # first check --no-rewrite-tag-names
127 data = self.run_bzr("fast-export --plain --no-rewrite-tag-names br")[0]
128 self.assertNotEqual(-1, data.find("reset refs/tags/goodTag"))
129 self.assertEqual(data.find("reset refs/tags/"), data.rfind("reset refs/tags/"))
131 # and now with --rewrite-tag-names
132 data = self.run_bzr("fast-export --plain --rewrite-tag-names br")[0]
133 self.assertNotEqual(-1, data.find("reset refs/tags/goodTag"))
134 # "bad Tag" should be exported as bad_Tag
135 self.assertNotEqual(-1, data.find("reset refs/tags/bad_Tag"))
137 def test_baseline_option(self):
138 tree = self.make_branch_and_tree("bl")
140 # Revision 1
141 file('bl/a', 'w').write('test 1')
142 tree.add('a')
143 tree.commit(message='add a')
145 # Revision 2
146 file('bl/b', 'w').write('test 2')
147 file('bl/a', 'a').write('\ntest 3')
148 tree.add('b')
149 tree.commit(message='add b, modify a')
151 # Revision 3
152 file('bl/c', 'w').write('test 4')
153 tree.add('c')
154 tree.remove('b')
155 tree.commit(message='add c, remove b')
157 # Revision 4
158 file('bl/a', 'a').write('\ntest 5')
159 tree.commit(message='modify a again')
161 # Revision 5
162 file('bl/d', 'w').write('test 6')
163 tree.add('d')
164 tree.commit(message='add d')
166 # This exports the baseline state at Revision 3,
167 # followed by the deltas for 4 and 5
168 data = self.run_bzr("fast-export --baseline -r 3.. bl")[0]
169 data = re.sub('committer.*', 'committer', data)
170 self.assertEquals(fast_export_baseline_data, data)
172 # Also confirm that --baseline with no args is identical to full export
173 data1 = self.run_bzr("fast-export --baseline bl")[0]
174 data2 = self.run_bzr("fast-export bl")[0]
175 self.assertEquals(data1, data2)
177 simple_fast_import_stream = """commit refs/heads/master
178 mark :1
179 committer Jelmer Vernooij <jelmer@samba.org> 1299718135 +0100
180 data 7
181 initial
185 class TestFastImportInfo(ExternalBase):
187 def test_simple(self):
188 self.build_tree_contents([('simple.fi', simple_fast_import_stream)])
189 output = self.run_bzr("fast-import-info simple.fi")[0]
190 self.assertEquals(output, """Command counts:
191 \t0\tblob
192 \t0\tcheckpoint
193 \t1\tcommit
194 \t0\tfeature
195 \t0\tprogress
196 \t0\treset
197 \t0\ttag
198 File command counts:
199 \t0\tfilemodify
200 \t0\tfiledelete
201 \t0\tfilecopy
202 \t0\tfilerename
203 \t0\tfiledeleteall
204 Parent counts:
205 \t1\tparents-0
206 \t0\ttotal revisions merged
207 Commit analysis:
208 \tno\texecutables
209 \tno\tseparate authors found
210 \tno\tsymlinks
211 \tno\tblobs referenced by SHA
212 Head analysis:
213 \t[':1']\trefs/heads/master
214 Merges:
215 """)
218 class TestFastImport(ExternalBase):
220 def test_empty(self):
221 self.build_tree_contents([('empty.fi', "")])
222 self.make_branch_and_tree("br")
223 self.assertEquals("", self.run_bzr("fast-import empty.fi br")[0])
225 def test_file(self):
226 tree = self.make_branch_and_tree("br")
227 self.build_tree_contents([('file.fi', simple_fast_import_stream)])
228 data = self.run_bzr("fast-import file.fi br")[0]
229 self.assertEquals(1, tree.branch.revno())
231 def test_missing_bytes(self):
232 self.build_tree_contents([('empty.fi', """
233 commit refs/heads/master
234 mark :1
235 committer
236 data 15
237 """)])
238 self.make_branch_and_tree("br")
239 self.run_bzr_error(['bzr: ERROR: 4: Parse error: line 4: Command commit is missing section committer\n'], "fast-import empty.fi br")
242 class TestFastImportFilter(ExternalBase):
244 def test_empty(self):
245 self.build_tree_contents([('empty.fi', "")])
246 self.make_branch_and_tree("br")
247 self.assertEquals("", self.run_bzr("fast-import-filter -")[0])
249 def test_default_stdin(self):
250 self.build_tree_contents([('empty.fi', "")])
251 self.make_branch_and_tree("br")
252 self.assertEquals("", self.run_bzr("fast-import-filter")[0])