1# Copyright (C) 2009, 2011, 2016 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, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18"""Tests specific to Branch implementations that use foreign VCS'es."""
19
20from breezy.branch import (
21    UnstackableBranchFormat,
22    )
23from breezy.errors import (
24    IncompatibleFormat,
25    )
26from breezy.revision import (
27    NULL_REVISION,
28    )
29from breezy.tests import (
30    TestCaseWithTransport,
31    )
32
33
34class ForeignBranchFactory(object):
35    """Factory of branches for ForeignBranchTests."""
36
37    def make_empty_branch(self, transport):
38        """Create an empty branch with no commits in it."""
39        raise NotImplementedError(self.make_empty_branch)
40
41    def make_branch(self, transport):
42        """Create *some* branch, may be empty or not."""
43        return self.make_empty_branch(transport)
44
45
46class ForeignBranchTests(TestCaseWithTransport):
47    """Basic tests for foreign branch implementations.
48
49    These tests mainly make sure that the implementation covers the required
50    bits of the API and returns reasonable values.
51    """
52    branch_factory = None  # Set to an instance of ForeignBranchFactory by scenario
53
54    def make_empty_branch(self):
55        return self.branch_factory.make_empty_branch(self.get_transport())
56
57    def make_branch(self):
58        return self.branch_factory.make_branch(self.get_transport())
59
60    def test_set_parent(self):
61        """Test that setting the parent works."""
62        branch = self.make_branch()
63        branch.set_parent("foobar")
64
65    def test_set_push_location(self):
66        """Test that setting the push location works."""
67        branch = self.make_branch()
68        branch.set_push_location("http://bar/bloe")
69
70    def test_repr_type(self):
71        branch = self.make_branch()
72        self.assertIsInstance(repr(branch), str)
73
74    def test_get_parent(self):
75        """Test that getting the parent location works, and returns None."""
76        # TODO: Allow this to be non-None when foreign branches add support
77        #       for storing this URL.
78        branch = self.make_branch()
79        self.assertIs(None, branch.get_parent())
80
81    def test_get_push_location(self):
82        """Test that getting the push location works, and returns None."""
83        # TODO: Allow this to be non-None when foreign branches add support
84        #       for storing this URL.
85        branch = self.make_branch()
86        self.assertIs(None, branch.get_push_location())
87
88    def test_attributes(self):
89        """Check that various required attributes are present."""
90        branch = self.make_branch()
91        self.assertIsNot(None, getattr(branch, "repository", None))
92        self.assertIsNot(None, getattr(branch, "mapping", None))
93        self.assertIsNot(None, getattr(branch, "_format", None))
94        self.assertIsNot(None, getattr(branch, "base", None))
95
96    def test__get_nick(self):
97        """Make sure _get_nick is implemented and returns a string."""
98        branch = self.make_branch()
99        self.assertIsInstance(branch._get_nick(local=False), str)
100        self.assertIsInstance(branch._get_nick(local=True), str)
101
102    def test_null_revid_revno(self):
103        """null: should return revno 0."""
104        branch = self.make_branch()
105        self.assertEqual(0, branch.revision_id_to_revno(NULL_REVISION))
106
107    def test_get_stacked_on_url(self):
108        """Test that get_stacked_on_url() behaves as expected.
109
110        Inter-Format stacking doesn't work yet, so all foreign implementations
111        should raise UnstackableBranchFormat at the moment.
112        """
113        branch = self.make_branch()
114        self.assertRaises(UnstackableBranchFormat,
115                          branch.get_stacked_on_url)
116
117    def test_get_physical_lock_status(self):
118        branch = self.make_branch()
119        self.assertFalse(branch.get_physical_lock_status())
120
121    def test_last_revision_empty_branch(self):
122        branch = self.make_empty_branch()
123        self.assertEqual(NULL_REVISION, branch.last_revision())
124        self.assertEqual(0, branch.revno())
125        self.assertEqual((0, NULL_REVISION), branch.last_revision_info())
126
127
128class ForeignBranchFormatTests(TestCaseWithTransport):
129    """Basic tests for foreign branch format objects."""
130
131    branch_format = None  # Set to a BranchFormat instance by adapter
132
133    def test_initialize(self):
134        """Test this format is not initializable.
135
136        Remote branches may be initializable on their own, but none currently
137        support living in .bzr/branch.
138        """
139        bzrdir = self.make_controldir('dir')
140        self.assertRaises(IncompatibleFormat,
141                          self.branch_format.initialize, bzrdir)
142
143    def test_get_format_description_type(self):
144        self.assertIsInstance(self.branch_format.get_format_description(), str)
145
146    def test_network_name(self):
147        self.assertIsInstance(self.branch_format.network_name(), bytes)
148