1
# Copyright (C) 2005 by 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
18
"""Tests of simple versioning operations"""
 
 
20
# TODO: test trying to commit within a directory that is not yet added
 
 
25
from bzrlib.selftest import BzrTestBase, TestCaseInTempDir
 
 
26
from bzrlib.branch import Branch
 
 
29
logger = logging.getLogger('bzr.test.versioning')
 
 
33
class TestVersioning(TestCaseInTempDir):
 
 
36
        """Basic 'bzr mkdir' operation"""
 
 
37
        from bzrlib.commands import run_bzr
 
 
40
        run_bzr(['mkdir', 'foo'])
 
 
41
        self.assert_(os.path.isdir('foo'))
 
 
43
        self.assertRaises(OSError, run_bzr, ['mkdir', 'foo'])
 
 
45
        from bzrlib.diff import compare_trees
 
 
46
        from bzrlib.branch import Branch
 
 
49
        delta = compare_trees(b.basis_tree(), b.working_tree())
 
 
51
        self.log('delta.added = %r' % delta.added)
 
 
53
        self.assertEquals(len(delta.added), 1)
 
 
54
        self.assertEquals(delta.added[0][0], 'foo')
 
 
55
        self.failIf(delta.modified)
 
 
57
    def test_branch_add_in_unversioned(self):
 
 
58
        """Try to add a file in an unversioned directory.
 
 
60
        "bzr add" adds the parent as necessary, but simple branch add
 
 
63
        from bzrlib.branch import Branch
 
 
64
        from bzrlib.errors import NotVersionedError
 
 
66
        b = Branch.initialize('.')
 
 
68
        self.build_tree(['foo/',
 
 
71
        self.assertRaises(NotVersionedError,
 
 
77
    def test_add_in_unversioned(self):
 
 
78
        """Try to add a file in an unversioned directory.
 
 
80
        "bzr add" should add the parent(s) as necessary.
 
 
82
        from bzrlib.branch import Branch
 
 
83
        from bzrlib.commands import run_bzr
 
 
86
        b = Branch.initialize('.')
 
 
88
        self.build_tree(['inertiatic/', 'inertiatic/esp'])
 
 
89
        eq(list(b.unknowns()), ['inertiatic'])
 
 
90
        run_bzr(['add', 'inertiatic/esp'])
 
 
91
        eq(list(b.unknowns()), [])
 
 
93
        # Multiple unversioned parents
 
 
94
        self.build_tree(['veil/', 'veil/cerpin/', 'veil/cerpin/taxt'])
 
 
95
        eq(list(b.unknowns()), ['veil'])
 
 
96
        run_bzr(['add', 'veil/cerpin/taxt'])
 
 
97
        eq(list(b.unknowns()), [])
 
 
99
        # Check whacky paths work
 
 
100
        self.build_tree(['cicatriz/', 'cicatriz/esp'])
 
 
101
        eq(list(b.unknowns()), ['cicatriz'])
 
 
102
        run_bzr(['add', 'inertiatic/../cicatriz/esp'])
 
 
103
        eq(list(b.unknowns()), [])
 
 
105
    def test_add_in_versioned(self):
 
 
106
        """Try to add a file in a versioned directory.
 
 
108
        "bzr add" should do this happily.
 
 
110
        from bzrlib.branch import Branch
 
 
111
        from bzrlib.commands import run_bzr
 
 
112
        eq = self.assertEqual
 
 
114
        b = Branch.initialize('.')
 
 
116
        self.build_tree(['inertiatic/', 'inertiatic/esp'])
 
 
117
        eq(list(b.unknowns()), ['inertiatic'])
 
 
118
        run_bzr(['add', '--no-recurse', 'inertiatic'])
 
 
119
        eq(list(b.unknowns()), ['inertiatic'+os.sep+'esp'])
 
 
120
        run_bzr(['add', 'inertiatic/esp'])
 
 
121
        eq(list(b.unknowns()), [])
 
 
123
    def test_subdir_add(self):
 
 
124
        """Add in subdirectory should add only things from there down"""
 
 
126
        from bzrlib.branch import Branch
 
 
127
        from bzrlib.commands import run_bzr
 
 
129
        eq = self.assertEqual
 
 
133
        b = Branch.initialize('.')
 
 
134
        self.build_tree(['src/', 'README'])
 
 
136
        eq(sorted(b.unknowns()),
 
 
139
        eq(run_bzr(['add', 'src']), 0)
 
 
141
        self.build_tree(['src/foo.c'])
 
 
144
        eq(run_bzr(['add']), 0)
 
 
146
        eq(sorted(b.unknowns()), 
 
 
148
        eq(len(b.inventory), 3)
 
 
151
        eq(run_bzr(['add']), 0)
 
 
152
        eq(list(b.unknowns()), [])
 
 
156
    def check_branch(self):
 
 
157
        """After all the above changes, run the check and upgrade commands.
 
 
159
        The upgrade should be a no-op."""
 
 
161
        debug('branch has %d revisions', b.revno())
 
 
163
        debug('check branch...')
 
 
164
        from bzrlib.check import check
 
 
169
class SubdirCommit(TestCaseInTempDir):
 
 
171
    def test_subdir_commit(self):
 
 
172
        """Test committing a subdirectory, and committing within a directory."""
 
 
173
        run_bzr = self.run_bzr
 
 
174
        eq = self.assertEqual
 
 
176
        self.build_tree(['a/', 'b/'])
 
 
181
        for fn in ('a/one', 'b/two', 'top'):
 
 
182
            file(fn, 'w').write('old contents')
 
 
185
        run_bzr('commit', '-m', 'first revision')
 
 
187
        for fn in ('a/one', 'b/two', 'top'):
 
 
188
            file(fn, 'w').write('new contents')
 
 
190
        debug('start selective subdir commit')
 
 
191
        run_bzr('commit', 'a', '-m', 'commit a only')
 
 
193
        old = b.revision_tree(b.get_rev_id(1))
 
 
194
        new = b.revision_tree(b.get_rev_id(2))
 
 
196
        eq(new.get_file_by_path('b/two').read(), 'old contents')
 
 
197
        eq(new.get_file_by_path('top').read(), 'old contents')
 
 
198
        eq(new.get_file_by_path('a/one').read(), 'new contents')
 
 
201
        # commit from here should do nothing
 
 
202
        run_bzr('commit', '.', '-m', 'commit subdir only', '--unchanged')
 
 
203
        v3 = b.revision_tree(b.get_rev_id(3))
 
 
204
        eq(v3.get_file_by_path('b/two').read(), 'old contents')
 
 
205
        eq(v3.get_file_by_path('top').read(), 'old contents')
 
 
206
        eq(v3.get_file_by_path('a/one').read(), 'new contents')
 
 
208
        # commit in subdirectory commits whole tree
 
 
209
        run_bzr('commit', '-m', 'commit whole tree from subdir')
 
 
210
        v4 = b.revision_tree(b.get_rev_id(4))
 
 
211
        eq(v4.get_file_by_path('b/two').read(), 'new contents')        
 
 
212
        eq(v4.get_file_by_path('top').read(), 'new contents')
 
 
214
        # TODO: factor out some kind of assert_tree_state() method
 
 
217
if __name__ == '__main__':