1
# Copyright (C) 2006-2011 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, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test read_bundle works properly across various transports."""
19
from io import BytesIO
21
import breezy.mergeable
22
from ..bzr.bundle.serializer import write_bundle
23
import breezy.bzr.bzrdir
26
from .test_transport import TestTransportImplementation
27
from .per_transport import transport_test_permutations
28
import breezy.transport
29
import breezy.urlutils
30
from .scenarios import load_tests_apply_scenarios
33
load_tests = load_tests_apply_scenarios
36
def create_bundle_file(test_case):
37
test_case.build_tree(['tree/', 'tree/a', 'tree/subdir/'])
39
format = breezy.bzr.bzrdir.BzrDirFormat.get_default_format()
41
bzrdir = format.initialize('tree')
42
repo = bzrdir.create_repository()
43
branch = repo.controldir.create_branch()
44
wt = branch.controldir.create_workingtree()
46
wt.add(['a', 'subdir/'])
47
wt.commit('new project', rev_id=b'commit-1')
50
write_bundle(wt.branch.repository, wt.get_parent_ids()[0], b'null:', out)
55
class TestReadMergeableBundleFromURL(TestTransportImplementation):
56
"""Test that read_bundle works properly across multiple transports"""
58
scenarios = transport_test_permutations()
61
super(TestReadMergeableBundleFromURL, self).setUp()
62
self.bundle_name = 'test_bundle'
63
# read_mergeable_from_url will invoke get_transport which may *not*
64
# respect self._transport (i.e. returns a transport that is different
65
# from the one we want to test, so we must inject a correct transport
66
# into possible_transports first).
67
self.possible_transports = [self.get_transport(self.bundle_name)]
68
self.overrideEnv('BRZ_NO_SMART_VFS', None)
69
self.create_test_bundle()
71
def read_mergeable_from_url(self, url):
72
return breezy.mergeable.read_mergeable_from_url(
73
url, possible_transports=self.possible_transports)
75
def get_url(self, relpath=''):
76
return breezy.urlutils.join(self._server.get_url(), relpath)
78
def create_test_bundle(self):
79
out, wt = create_bundle_file(self)
80
if self.get_transport().is_readonly():
81
self.build_tree_contents([(self.bundle_name, out.getvalue())])
83
self.get_transport().put_file(self.bundle_name, out)
84
self.log('Put to: %s', self.get_url(self.bundle_name))
87
def test_read_mergeable_from_url(self):
88
info = self.read_mergeable_from_url(
89
str(self.get_url(self.bundle_name)))
90
revision = info.real_revisions[-1]
91
self.assertEqual(b'commit-1', revision.revision_id)
93
def test_read_fail(self):
94
# Trying to read from a directory, or non-bundle file
95
# should fail with NotABundle
96
self.assertRaises(errors.NotABundle,
97
self.read_mergeable_from_url, self.get_url('tree'))
98
self.assertRaises(errors.NotABundle,
99
self.read_mergeable_from_url, self.get_url('tree/a'))
101
def test_read_mergeable_respects_possible_transports(self):
102
if not isinstance(self.get_transport(self.bundle_name),
103
breezy.transport.ConnectedTransport):
104
# There is no point testing transport reuse for not connected
105
# transports (the test will fail even).
106
raise tests.TestSkipped(
107
'Need a ConnectedTransport to test transport reuse')
108
url = str(self.get_url(self.bundle_name))
109
self.read_mergeable_from_url(url)
110
self.assertEqual(1, len(self.possible_transports))