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, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18from breezy import (
19    controldir,
20    )
21from breezy.tests import TestCaseWithTransport
22
23
24class TestReference(TestCaseWithTransport):
25
26    def get_default_format(self):
27        return controldir.format_registry.make_controldir('development-subtree')
28
29    def test_no_args_lists(self):
30        tree = self.make_branch_and_tree('branch')
31        branch = tree.branch
32        tree.add_reference(self.make_branch_and_tree('branch/path'))
33        tree.add_reference(self.make_branch_and_tree('branch/lath'))
34        tree.set_reference_info('path', 'http://example.org')
35        tree.set_reference_info('lath', 'http://example.org/2')
36        out, err = self.run_bzr('reference', working_dir='branch')
37        lines = out.splitlines()
38        self.assertEqual('lath http://example.org/2', lines[0])
39        self.assertEqual('path http://example.org', lines[1])
40
41    def make_tree_with_reference(self):
42        tree = self.make_branch_and_tree('tree')
43        subtree = self.make_branch_and_tree('tree/newpath')
44        tree.add_reference(subtree)
45        tree.set_reference_info('newpath', 'http://example.org')
46        tree.commit('add reference')
47        return tree
48
49    def test_uses_working_tree_location(self):
50        tree = self.make_tree_with_reference()
51        out, err = self.run_bzr('reference', working_dir='tree')
52        self.assertContainsRe(out, 'newpath http://example.org\n')
53
54    def test_uses_basis_tree_location(self):
55        tree = self.make_tree_with_reference()
56        tree.controldir.destroy_workingtree()
57        out, err = self.run_bzr('reference', working_dir='tree')
58        self.assertContainsRe(out, 'newpath http://example.org\n')
59
60    def test_one_arg_displays(self):
61        tree = self.make_tree_with_reference()
62        out, err = self.run_bzr('reference newpath', working_dir='tree')
63        self.assertEqual('newpath http://example.org\n', out)
64
65    def test_one_arg_uses_containing_tree(self):
66        tree = self.make_tree_with_reference()
67        out, err = self.run_bzr('reference -d tree newpath')
68        self.assertEqual('newpath http://example.org\n', out)
69
70    def test_two_args_sets(self):
71        tree = self.make_branch_and_tree('tree')
72        self.build_tree(['tree/file'])
73        tree.add('file')
74        out, err = self.run_bzr('reference -d tree file http://example.org')
75        location = tree.get_reference_info('file')
76        self.assertEqual('http://example.org', location)
77        self.assertEqual('', out)
78        self.assertEqual('', err)
79
80    def test_missing_file(self):
81        tree = self.make_branch_and_tree('tree')
82        out, err = self.run_bzr('reference file http://example.org',
83                                working_dir='tree', retcode=3)
84        self.assertEqual('brz: ERROR: file is not versioned.\n', err)
85
86    def test_missing_file_forced(self):
87        tree = self.make_branch_and_tree('tree')
88        tree.add_reference(self.make_branch_and_tree('tree/file'))
89        out, err = self.run_bzr(
90            'reference --force-unversioned file http://example.org',
91            working_dir='tree')
92        location = tree.get_reference_info('file')
93        self.assertEqual('http://example.org', location)
94        self.assertEqual('', out)
95        self.assertEqual('', err)
96