~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/versioning.py

  • Committer: Lalo Martins
  • Date: 2005-09-08 00:40:15 UTC
  • mto: (1185.1.22)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: lalo@exoweb.net-20050908004014-bb63b3378ac8ff58
turned get_revision_info into a RevisionSpec class

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