~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_versioning.py

Handled simultaneous renames of parent and child better

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Tests of simple versioning operations"""
19
19
 
20
 
# TODO: test adding a file whose directory is not versioned
21
20
# TODO: test trying to commit within a directory that is not yet added
22
21
 
23
22
 
24
23
import os
25
24
 
26
 
from bzrlib.selftest import BzrTestBase, TestCaseInTempDir
 
25
from bzrlib.tests import BzrTestBase, TestCaseInTempDir
27
26
from bzrlib.branch import Branch
28
 
 
29
 
import logging
30
 
logger = logging.getLogger('bzr.test.versioning')
31
 
debug = logger.debug
 
27
from bzrlib.trace import mutter
 
28
from bzrlib.osutils import pathjoin
32
29
 
33
30
 
34
31
class TestVersioning(TestCaseInTempDir):
35
32
    
36
33
    def test_mkdir(self): 
37
34
        """Basic 'bzr mkdir' operation"""
38
 
        from bzrlib.commands import run_bzr
39
35
 
40
 
        run_bzr(['init'])
41
 
        run_bzr(['mkdir', 'foo'])
 
36
        self.run_bzr('init')
 
37
        self.run_bzr('mkdir', 'foo')
42
38
        self.assert_(os.path.isdir('foo'))
43
39
 
44
 
        self.assertRaises(OSError, run_bzr, ['mkdir', 'foo'])
 
40
        self.run_bzr('mkdir', 'foo', retcode=3)
45
41
 
46
42
        from bzrlib.diff import compare_trees
47
43
        from bzrlib.branch import Branch
48
 
        b = Branch('.')
 
44
        b = Branch.open(u'.')
49
45
        
50
46
        delta = compare_trees(b.basis_tree(), b.working_tree())
51
47
 
55
51
        self.assertEquals(delta.added[0][0], 'foo')
56
52
        self.failIf(delta.modified)
57
53
 
58
 
    def test_add_in_unversioned(self):
59
 
        """Try to add a file in an unversioned directory.
60
 
 
61
 
        smart_add may eventually add the parent as necessary, but simple
62
 
        branch add doesn't do that.
63
 
        """
64
 
        from bzrlib.branch import Branch
65
 
        from bzrlib.errors import NotVersionedError
66
 
 
67
 
        b = Branch('.', init=True)
68
 
 
69
 
        self.build_tree(['foo/',
70
 
                         'foo/hello'])
71
 
 
72
 
        self.assertRaises(NotVersionedError,
73
 
                          b.add,
74
 
                          'foo/hello')
75
 
        
76
 
        self.check_and_upgrade()
77
 
 
78
 
        
79
 
    def test_subdir_add(self):
80
 
        """Add in subdirectory should add only things from there down"""
81
 
        
82
 
        from bzrlib.branch import Branch
83
 
        from bzrlib.commands import run_bzr
84
 
        
85
 
        eq = self.assertEqual
86
 
        ass = self.assert_
87
 
        chdir = os.chdir
88
 
        
89
 
        b = Branch('.', init=True)
90
 
        self.build_tree(['src/', 'README'])
91
 
        
92
 
        eq(sorted(b.unknowns()),
93
 
           ['README', 'src'])
94
 
        
95
 
        eq(run_bzr(['add', 'src']), 0)
96
 
        
97
 
        self.build_tree(['src/foo.c'])
98
 
        
99
 
        chdir('src')
100
 
        eq(run_bzr(['add']), 0)
101
 
        
102
 
        eq(sorted(b.unknowns()), 
103
 
           ['README'])
104
 
        eq(len(b.inventory), 3)
105
 
                
106
 
        chdir('..')
107
 
        eq(run_bzr(['add']), 0)
108
 
        eq(list(b.unknowns()), [])
109
 
 
110
 
        self.check_and_upgrade()
111
 
 
112
 
 
113
 
    def check_and_upgrade(self):
 
54
    def test_mkdir_in_subdir(self):
 
55
        """'bzr mkdir' operation in subdirectory"""
 
56
 
 
57
        self.run_bzr('init')
 
58
        self.run_bzr('mkdir', 'dir')
 
59
        self.assert_(os.path.isdir('dir'))
 
60
 
 
61
        os.chdir('dir')
 
62
        self.log('Run mkdir in subdir')
 
63
        self.run_bzr('mkdir', 'subdir')
 
64
        self.assert_(os.path.isdir('subdir'))
 
65
        os.chdir('..')
 
66
 
 
67
        from bzrlib.diff import compare_trees
 
68
        from bzrlib.branch import Branch
 
69
        b = Branch.open(u'.')
 
70
        
 
71
        delta = compare_trees(b.basis_tree(), b.working_tree())
 
72
 
 
73
        self.log('delta.added = %r' % delta.added)
 
74
 
 
75
        self.assertEquals(len(delta.added), 2)
 
76
        self.assertEquals(delta.added[0][0], 'dir')
 
77
        self.assertEquals(delta.added[1][0], pathjoin('dir','subdir'))
 
78
        self.failIf(delta.modified)
 
79
 
 
80
    def test_mkdir_w_nested_trees(self):
 
81
        """'bzr mkdir' with nested trees"""
 
82
 
 
83
        self.run_bzr('init')
 
84
        os.mkdir('a')
 
85
        os.chdir('a')
 
86
        self.run_bzr('init')
 
87
        os.mkdir('b')
 
88
        os.chdir('b')
 
89
        self.run_bzr('init')
 
90
        os.chdir('../..')
 
91
 
 
92
        self.run_bzr('mkdir', 'dir', 'a/dir', 'a/b/dir')
 
93
        self.failUnless(os.path.isdir('dir'))
 
94
        self.failUnless(os.path.isdir('a/dir'))
 
95
        self.failUnless(os.path.isdir('a/b/dir'))
 
96
 
 
97
        from bzrlib.diff import compare_trees
 
98
        b = Branch.open(u'.')
 
99
        b_a = Branch.open('a')
 
100
        b_b = Branch.open('a/b')
 
101
        
 
102
        delta = compare_trees(b.basis_tree(), b.working_tree())
 
103
        self.assertEquals(len(delta.added), 1)
 
104
        self.assertEquals(delta.added[0][0], 'dir')
 
105
        self.failIf(delta.modified)
 
106
 
 
107
        delta = compare_trees(b_a.basis_tree(), b_a.working_tree())
 
108
        self.assertEquals(len(delta.added), 1)
 
109
        self.assertEquals(delta.added[0][0], 'dir')
 
110
        self.failIf(delta.modified)
 
111
 
 
112
        delta = compare_trees(b_b.basis_tree(), b_b.working_tree())
 
113
        self.assertEquals(len(delta.added), 1)
 
114
        self.assertEquals(delta.added[0][0], 'dir')
 
115
        self.failIf(delta.modified)
 
116
 
 
117
    def check_branch(self):
114
118
        """After all the above changes, run the check and upgrade commands.
115
119
 
116
120
        The upgrade should be a no-op."""
117
 
        b = Branch('.')
118
 
        debug('branch has %d revisions', b.revno())
119
 
        
120
 
        debug('check branch...')
121
 
        from bzrlib.check import check
122
 
        check(b)
123
 
        
124
 
        debug('upgrade branch...')
125
 
        from bzrlib.upgrade import upgrade
126
 
        upgrade(b)
127
 
        
128
 
        debug('check branch...')
129
 
        from bzrlib.check import check
130
 
        check(b)
131
 
        
132
 
 
133
 
        
134
 
        
 
121
        b = Branch.open(u'.')
 
122
        mutter('branch has %d revisions', b.revno())
 
123
        
 
124
        mutter('check branch...')
 
125
        from bzrlib.check import check
 
126
        check(b, False)
 
127
 
 
128
 
135
129
class SubdirCommit(TestCaseInTempDir):
136
130
 
137
131
    def test_subdir_commit(self):
142
136
        self.build_tree(['a/', 'b/'])
143
137
        
144
138
        run_bzr('init')
145
 
        b = Branch('.')
 
139
        b = Branch.open(u'.')
146
140
        
147
141
        for fn in ('a/one', 'b/two', 'top'):
148
142
            file(fn, 'w').write('old contents')
153
147
        for fn in ('a/one', 'b/two', 'top'):
154
148
            file(fn, 'w').write('new contents')
155
149
            
 
150
        mutter('start selective subdir commit')
156
151
        run_bzr('commit', 'a', '-m', 'commit a only')
157
152
        
158
 
        old = b.revision_tree(b.lookup_revision(1))
159
 
        new = b.revision_tree(b.lookup_revision(2))
 
153
        old = b.repository.revision_tree(b.get_rev_id(1))
 
154
        new = b.repository.revision_tree(b.get_rev_id(2))
160
155
        
161
156
        eq(new.get_file_by_path('b/two').read(), 'old contents')
162
157
        eq(new.get_file_by_path('top').read(), 'old contents')
165
160
        os.chdir('a')
166
161
        # commit from here should do nothing
167
162
        run_bzr('commit', '.', '-m', 'commit subdir only', '--unchanged')
168
 
        v3 = b.revision_tree(b.lookup_revision(3))
 
163
        v3 = b.repository.revision_tree(b.get_rev_id(3))
169
164
        eq(v3.get_file_by_path('b/two').read(), 'old contents')
170
165
        eq(v3.get_file_by_path('top').read(), 'old contents')
171
166
        eq(v3.get_file_by_path('a/one').read(), 'new contents')
172
167
                
173
168
        # commit in subdirectory commits whole tree
174
169
        run_bzr('commit', '-m', 'commit whole tree from subdir')
175
 
        v4 = b.revision_tree(b.lookup_revision(4))
 
170
        v4 = b.repository.revision_tree(b.get_rev_id(4))
176
171
        eq(v4.get_file_by_path('b/two').read(), 'new contents')        
177
172
        eq(v4.get_file_by_path('top').read(), 'new contents')
178
173
        
179
174
        # TODO: factor out some kind of assert_tree_state() method
180
175
        
 
176
 
 
177
if __name__ == '__main__':
 
178
    import unittest
 
179
    unittest.main()
 
180