1# Copyright (C) 2005, 2006, 2007, 2009, 2011, 2012, 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"""Black-box tests for brz revno.
19"""
20
21import os
22
23from breezy import tests
24from breezy.errors import NoSuchRevision
25from breezy.bzr.tests.matchers import ContainsNoVfsCalls
26
27
28class TestRevno(tests.TestCaseWithTransport):
29
30    def test_revno(self):
31
32        def bzr(*args, **kwargs):
33            return self.run_bzr(*args, **kwargs)[0]
34
35        os.mkdir('a')
36        os.chdir('a')
37        bzr('init')
38        self.assertEqual(int(bzr('revno')), 0)
39
40        with open('foo', 'wb') as f:
41            f.write(b'foo\n')
42        bzr('add foo')
43        bzr('commit -m foo')
44        self.assertEqual(int(bzr('revno')), 1)
45
46        os.mkdir('baz')
47        bzr('add baz')
48        bzr('commit -m baz')
49        self.assertEqual(int(bzr('revno')), 2)
50
51        os.chdir('..')
52        self.assertEqual(int(bzr('revno a')), 2)
53        self.assertEqual(int(bzr('revno a/baz')), 2)
54
55    def test_revno_tree(self):
56        # Make branch and checkout
57        wt = self.make_branch_and_tree('branch')
58        checkout = wt.branch.create_checkout('checkout', lightweight=True)
59
60        # Get the checkout out of date
61        self.build_tree(['branch/file'])
62        wt.add(['file'])
63        wt.commit('mkfile')
64
65        # Make sure revno says we're on 1
66        out, err = self.run_bzr('revno checkout')
67        self.assertEqual('', err)
68        self.assertEqual('1\n', out)
69
70        # Make sure --tree knows it's still on 0
71        out, err = self.run_bzr('revno --tree checkout')
72        self.assertEqual('', err)
73        self.assertEqual('0\n', out)
74
75    def test_revno_tree_no_tree(self):
76        # Make treeless branch
77        b = self.make_branch('branch')
78
79        # Try getting it's --tree revno
80        out, err = self.run_bzr('revno --tree branch', retcode=3)
81        self.assertEqual('', out)
82        self.assertEqual('brz: ERROR: No WorkingTree exists for "branch".\n',
83                         err)
84
85    def test_dotted_revno_tree(self):
86        builder = self.make_branch_builder('branch')
87        builder.start_series()
88        builder.build_snapshot(None, [
89            ('add', ('', b'root-id', 'directory', None)),
90            ('add', ('file', b'file-id', 'file', b'content\n'))],
91            revision_id=b'A-id')
92        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
93        builder.build_snapshot([b'A-id', b'B-id'], [], revision_id=b'C-id')
94        builder.finish_series()
95        b = builder.get_branch()
96        co_b = b.create_checkout('checkout_b', lightweight=True,
97                                 revision_id=b'B-id')
98        out, err = self.run_bzr('revno checkout_b')
99        self.assertEqual('', err)
100        self.assertEqual('2\n', out)
101        out, err = self.run_bzr('revno --tree checkout_b')
102        self.assertEqual('', err)
103        self.assertEqual('1.1.1\n', out)
104
105    def test_stale_revno_tree(self):
106        builder = self.make_branch_builder('branch')
107        builder.start_series()
108        builder.build_snapshot(None, [
109            ('add', ('', b'root-id', 'directory', None)),
110            ('add', ('file', b'file-id', 'file', b'content\n'))],
111            revision_id=b'A-id')
112        builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
113        builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
114        builder.finish_series()
115        b = builder.get_branch()
116        # The branch is now at "C-id", but the checkout is still at "B-id"
117        # which is no longer in the history
118        co_b = b.create_checkout('checkout_b', lightweight=True,
119                                 revision_id=b'B-id')
120        out, err = self.run_bzr('revno checkout_b')
121        self.assertEqual('', err)
122        self.assertEqual('2\n', out)
123        out, err = self.run_bzr('revno --tree checkout_b')
124        self.assertEqual('', err)
125        self.assertEqual('???\n', out)
126
127    def test_revno_ghost(self):
128        builder = self.make_branch_builder('branch')
129        builder.start_series()
130        revid = builder.build_snapshot([b'aghost'], [
131            ('add', ('', b'root-id', 'directory', None)),
132            ('add', ('file', b'file-id', 'file', b'content\n'))],
133            revision_id=b'A-id', allow_leftmost_as_ghost=True)
134        builder.finish_series()
135        b = builder.get_branch()
136
137        def revision_id_to_revno(s, r):
138            raise NoSuchRevision(s, r)
139        self.overrideAttr(type(b), 'revision_id_to_dotted_revno', revision_id_to_revno)
140        self.overrideAttr(type(b), 'revision_id_to_revno', revision_id_to_revno)
141        out, err = self.run_bzr('revno branch')
142        self.assertEqual('', err)
143        self.assertEqual('???\n', out)
144
145    def test_revno_with_revision(self):
146        wt = self.make_branch_and_tree('.')
147        revid1 = wt.commit('rev1')
148        revid2 = wt.commit('rev2')
149
150        out, err = self.run_bzr('revno -r-2 .')
151        self.assertEqual('1\n', out)
152
153        out, err = self.run_bzr('revno -rrevid:%s .' % revid1.decode('utf-8'))
154        self.assertEqual('1\n', out)
155
156    def test_revno_and_tree_mutually_exclusive(self):
157        wt = self.make_branch_and_tree('.')
158        out, err = self.run_bzr('revno -r-2 --tree .', retcode=3)
159        self.assertEqual('', out)
160        self.assertEqual(
161            'brz: ERROR: --tree and --revision can not be used together\n',
162            err)
163