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."""
23 from bzrlib
import tests
24 from bzrlib
.tests
.blackbox
import ExternalBase
26 from bzrlib
.plugins
.fastimport
.cmds
import (
30 from bzrlib
.plugins
.fastimport
.tests
import (
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')
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')
58 stream
= _get_source_stream(filename
)
59 self
.assertIsNot("bla", stream
.read())
62 fast_export_baseline_data
= """commit refs/heads/master
74 commit refs/heads/master
85 commit refs/heads/master
96 class TestFastExport(ExternalBase
):
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'))
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
)
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")
141 file('bl/a', 'w').write('test 1')
143 tree
.commit(message
='add a')
146 file('bl/b', 'w').write('test 2')
147 file('bl/a', 'a').write('\ntest 3')
149 tree
.commit(message
='add b, modify a')
152 file('bl/c', 'w').write('test 4')
155 tree
.commit(message
='add c, remove b')
158 file('bl/a', 'a').write('\ntest 5')
159 tree
.commit(message
='modify a again')
162 file('bl/d', 'w').write('test 6')
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
179 committer Jelmer Vernooij <jelmer@samba.org> 1299718135 +0100
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:
206 \t0\ttotal revisions merged
209 \tno\tseparate authors found
211 \tno\tblobs referenced by SHA
213 \t[':1']\trefs/heads/master
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])
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
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])