~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_add.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-03 16:38:08 UTC
  • mto: (1711.2.74 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1837.
  • Revision ID: john@arbash-meinel.com-20060703163808-180fac633586d62a
Cleanup .bzrignore, and include some useful ignores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import unittest
3
3
 
4
 
from bzrlib.selftest import TestCaseInTempDir, TestCase
5
 
from bzrlib.branch import Branch
6
 
from bzrlib.errors import NotBranchError, NotVersionedError
 
4
from bzrlib.add import smart_add, smart_add_tree
 
5
from bzrlib.tests import TestCaseWithTransport, TestCase
 
6
from bzrlib.errors import NoSuchFile
 
7
from bzrlib.inventory import InventoryFile, Inventory
 
8
from bzrlib.workingtree import WorkingTree
7
9
 
8
 
class TestSmartAdd(TestCaseInTempDir):
 
10
class TestSmartAdd(TestCaseWithTransport):
9
11
 
10
12
    def test_add_dot_from_root(self):
11
13
        """Test adding . from the root of the tree.""" 
12
14
        from bzrlib.add import smart_add
13
15
        paths = ("original/", "original/file1", "original/file2")
14
16
        self.build_tree(paths)
15
 
        branch = Branch(".", init=True)
16
 
        smart_add((".",), recurse=True)
 
17
        wt = self.make_branch_and_tree('.')
 
18
        branch = wt.branch
 
19
        smart_add_tree(wt, (u".",))
17
20
        for path in paths:
18
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
21
            self.assertNotEqual(wt.path2id(path), None)
19
22
 
20
23
    def test_add_dot_from_subdir(self):
21
24
        """Test adding . from a subdir of the tree.""" 
22
25
        from bzrlib.add import smart_add
23
26
        paths = ("original/", "original/file1", "original/file2")
24
27
        self.build_tree(paths)
25
 
        branch = Branch(".", init=True)
 
28
        wt = self.make_branch_and_tree('.')
 
29
        branch = wt.branch
26
30
        os.chdir("original")
27
 
        smart_add((".",), recurse=True)
 
31
        smart_add_tree(wt, (u".",))
28
32
        for path in paths:
29
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
33
            self.assertNotEqual(wt.path2id(path), None)
30
34
 
31
35
    def test_add_tree_from_above_tree(self):
32
36
        """Test adding a tree from above the tree.""" 
35
39
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
36
40
                        "branch/original/file2")
37
41
        self.build_tree(branch_paths)
38
 
        branch = Branch("branch", init=True)
39
 
        smart_add(("branch",))
 
42
        wt = self.make_branch_and_tree('branch')
 
43
        branch = wt.branch
 
44
        smart_add_tree(wt, ("branch",))
40
45
        for path in paths:
41
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
46
            self.assertNotEqual(wt.path2id(path), None)
42
47
 
43
48
    def test_add_above_tree_preserves_tree(self):
44
49
        """Test nested trees are not affect by an add above them."""
45
 
        from bzrlib.add import smart_add, add_reporter_null
46
 
        
 
50
        from bzrlib.add import smart_add
47
51
        paths = ("original/", "original/file1", "original/file2")
48
52
        child_paths = ("path",)
49
53
        full_child_paths = ("original/child", "original/child/path")
51
55
                       "original/child/", "original/child/path")
52
56
        
53
57
        self.build_tree(build_paths)
54
 
        branch = Branch(".", init=True)
55
 
        child_branch = Branch("original/child", init=True)
56
 
        smart_add((".",), True, add_reporter_null)
 
58
        wt = self.make_branch_and_tree('.')
 
59
        branch = wt.branch
 
60
        child_tree = self.make_branch_and_tree('original/child')
 
61
        smart_add_tree(wt, (".",))
57
62
        for path in paths:
58
 
            self.assertNotEqual((path, branch.inventory.path2id(path)),
 
63
            self.assertNotEqual((path, wt.path2id(path)),
59
64
                                (path, None))
60
65
        for path in full_child_paths:
61
 
            self.assertEqual((path, branch.inventory.path2id(path)),
 
66
            self.assertEqual((path, wt.path2id(path)),
62
67
                             (path, None))
63
68
        for path in child_paths:
64
 
            self.assertEqual(child_branch.inventory.path2id(path), None)
 
69
            self.assertEqual(child_tree.path2id(path), None)
65
70
 
66
71
    def test_add_paths(self):
67
72
        """Test smart-adding a list of paths."""
68
73
        from bzrlib.add import smart_add
69
74
        paths = ("file1", "file2")
70
75
        self.build_tree(paths)
71
 
        branch = Branch(".", init=True)
72
 
        smart_add(paths)
 
76
        wt = self.make_branch_and_tree('.')
 
77
        smart_add_tree(wt, paths)
73
78
        for path in paths:
74
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
75
 
            
76
 
class TestSmartAddBranch(TestCaseInTempDir):
 
79
            self.assertNotEqual(wt.path2id(path), None)
 
80
    
 
81
    def test_add_ignored_nested_paths(self):
 
82
        """Test smart-adding a list of paths which includes ignored ones."""
 
83
        wt = self.make_branch_and_tree('.')
 
84
        tree_shape = ("adir/", "adir/CVS/", "adir/CVS/afile", "adir/CVS/afile2")
 
85
        add_paths = ("adir/CVS", "adir/CVS/afile", "adir")
 
86
        expected_paths = ("adir", "adir/CVS", "adir/CVS/afile", "adir/CVS/afile2")
 
87
        self.build_tree(tree_shape)
 
88
        smart_add_tree(wt, add_paths)
 
89
        for path in expected_paths:
 
90
            self.assertNotEqual(wt.path2id(path), None, "No id added for %s" % path)
 
91
 
 
92
    def test_save_false(self):
 
93
        """Test smart-adding a path with save set to false."""
 
94
        wt = self.make_branch_and_tree('.')
 
95
        self.build_tree(['file'])
 
96
        smart_add_tree(wt, ['file'], save=False)
 
97
        self.assertNotEqual(wt.path2id('file'), None, "No id added for 'file'")
 
98
        wt.read_working_inventory()
 
99
        self.assertEqual(wt.path2id('file'), None)
 
100
 
 
101
    def test_add_dry_run(self):
 
102
        """Test a dry run add, make sure nothing is added."""
 
103
        from bzrlib.commands import run_bzr
 
104
        eq = self.assertEqual
 
105
        wt = self.make_branch_and_tree('.')
 
106
        branch = wt.branch
 
107
        self.build_tree(['inertiatic/', 'inertiatic/esp'])
 
108
        eq(list(wt.unknowns()), ['inertiatic'])
 
109
        self.capture('add --dry-run .')
 
110
        eq(list(wt.unknowns()), ['inertiatic'])
 
111
 
 
112
    def test_add_non_existant(self):
 
113
        """Test smart-adding a file that does not exist."""
 
114
        from bzrlib.add import smart_add
 
115
        wt = self.make_branch_and_tree('.')
 
116
        branch = wt.branch
 
117
        self.assertRaises(NoSuchFile, smart_add_tree, wt, 'non-existant-file')
 
118
 
 
119
    def test_returns_and_ignores(self):
 
120
        """Correctly returns added/ignored files"""
 
121
        from bzrlib.commands import run_bzr
 
122
        wt = self.make_branch_and_tree('.')
 
123
        branch = wt.branch
 
124
        # no files should be ignored by default, so we need to create
 
125
        # an ignore rule - we create one for the pyc files, which means
 
126
        # CVS should not be ignored.
 
127
        self.build_tree(['inertiatic/', 'inertiatic/esp', 'inertiatic/CVS', 
 
128
                        'inertiatic/foo.pyc'])
 
129
        self.build_tree_contents([('.bzrignore', '*.py[oc]\n')])
 
130
        added, ignored = smart_add_tree(wt, u'.')
 
131
        self.assertSubset(('inertiatic', 'inertiatic/esp', 'inertiatic/CVS'),
 
132
                          added)
 
133
        self.assertSubset(('*.py[oc]',), ignored)
 
134
        self.assertSubset(('inertiatic/foo.pyc',), ignored['*.py[oc]'])
 
135
 
 
136
 
 
137
class TestSmartAddTree(TestCaseWithTransport):
77
138
    """Test smart adds with a specified branch."""
78
139
 
79
140
    def test_add_dot_from_root(self):
80
141
        """Test adding . from the root of the tree.""" 
81
 
        from bzrlib.add import smart_add_branch
82
142
        paths = ("original/", "original/file1", "original/file2")
83
143
        self.build_tree(paths)
84
 
        branch = Branch(".", init=True)
85
 
        smart_add_branch(branch, (".",))
 
144
        wt = self.make_branch_and_tree('.')
 
145
        branch = wt.branch
 
146
        smart_add_tree(wt, (u".",))
86
147
        for path in paths:
87
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
148
            self.assertNotEqual(wt.path2id(path), None)
88
149
 
89
150
    def test_add_dot_from_subdir(self):
90
151
        """Test adding . from a subdir of the tree.""" 
91
 
        from bzrlib.add import smart_add_branch
 
152
        from bzrlib.add import smart_add_tree
92
153
        paths = ("original/", "original/file1", "original/file2")
93
154
        self.build_tree(paths)
94
 
        branch = Branch(".", init=True)
 
155
        wt = self.make_branch_and_tree('.')
 
156
        branch = wt.branch
95
157
        os.chdir("original")
96
 
        smart_add_branch(branch, (".",))
 
158
        smart_add_tree(wt, (u".",))
97
159
        for path in paths:
98
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
160
            self.assertNotEqual(wt.path2id(path), None)
99
161
 
100
162
    def test_add_tree_from_above_tree(self):
101
163
        """Test adding a tree from above the tree.""" 
102
 
        from bzrlib.add import smart_add_branch
 
164
        from bzrlib.add import smart_add_tree
103
165
        paths = ("original/", "original/file1", "original/file2")
104
166
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
105
167
                        "branch/original/file2")
106
168
        self.build_tree(branch_paths)
107
 
        branch = Branch("branch", init=True)
108
 
        smart_add_branch(branch, ("branch",))
 
169
        tree = self.make_branch_and_tree('branch')
 
170
        branch = tree.branch
 
171
        smart_add_tree(tree, ("branch",))
109
172
        for path in paths:
110
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
 
173
            self.assertNotEqual(tree.path2id(path), None)
111
174
 
112
175
    def test_add_above_tree_preserves_tree(self):
113
176
        """Test nested trees are not affect by an add above them."""
114
 
        from bzrlib.add import smart_add_branch
 
177
        from bzrlib.add import smart_add_tree
115
178
        paths = ("original/", "original/file1", "original/file2")
116
179
        child_paths = ("path")
117
180
        full_child_paths = ("original/child", "original/child/path")
118
181
        build_paths = ("original/", "original/file1", "original/file2", 
119
182
                       "original/child/", "original/child/path")
120
183
        self.build_tree(build_paths)
121
 
        branch = Branch(".", init=True)
122
 
        child_branch = Branch("original/child", init=True)
123
 
        smart_add_branch(branch, (".",))
 
184
        tree = self.make_branch_and_tree('.')
 
185
        branch = tree.branch
 
186
        child_tree = self.make_branch_and_tree("original/child")
 
187
        smart_add_tree(tree, (u".",))
124
188
        for path in paths:
125
 
            self.assertNotEqual((path, branch.inventory.path2id(path)),
 
189
            self.assertNotEqual((path, tree.path2id(path)),
126
190
                                (path, None))
127
191
        for path in full_child_paths:
128
 
            self.assertEqual((path, branch.inventory.path2id(path)),
 
192
            self.assertEqual((path, tree.path2id(path)),
129
193
                             (path, None))
130
194
        for path in child_paths:
131
 
            self.assertEqual(child_branch.inventory.path2id(path), None)
 
195
            self.assertEqual(child_tree.path2id(path), None)
132
196
 
133
197
    def test_add_paths(self):
134
198
        """Test smart-adding a list of paths."""
135
 
        from bzrlib.add import smart_add_branch
 
199
        from bzrlib.add import smart_add_tree
136
200
        paths = ("file1", "file2")
137
201
        self.build_tree(paths)
138
 
        branch = Branch(".", init=True)
139
 
        smart_add_branch(branch, paths)
 
202
        wt = self.make_branch_and_tree('.')
 
203
        branch = wt.branch
 
204
        smart_add_tree(wt, paths)
140
205
        for path in paths:
141
 
            self.assertNotEqual(branch.inventory.path2id(path), None)
142
 
 
143
 
class TestAddCallbacks(TestCaseInTempDir):
144
 
 
145
 
    def setUp(self):
146
 
        from bzrlib.inventory import InventoryEntry
147
 
        super(TestAddCallbacks, self).setUp()
148
 
        self.entry = InventoryEntry("id", "name", "file", None)
149
 
 
150
 
    def test_null_callback(self):
151
 
        from bzrlib.add import add_reporter_null
152
 
        add_reporter_null('path', 'file', self.entry)
153
 
 
154
 
    def test_print_callback(self):
155
 
        from bzrlib.add import add_reporter_print
156
 
        from StringIO import StringIO
 
206
            self.assertNotEqual(wt.path2id(path), None)
 
207
 
 
208
 
 
209
class TestAddActions(TestCase):
 
210
 
 
211
    def test_quiet(self):
 
212
        self.run_action("")
 
213
 
 
214
    def test__print(self):
 
215
        self.run_action("added path\n")
 
216
 
 
217
    def run_action(self, output):
 
218
        from bzrlib.add import AddAction, FastPath
 
219
        from cStringIO import StringIO
 
220
        inv = Inventory()
157
221
        stdout = StringIO()
158
 
        self.apply_redirected(None, stdout, None, add_reporter_print,
159
 
                              'path', 'file', self.entry)
160
 
        self.assertEqual(stdout.getvalue(), "added path\n")
 
222
        action = AddAction(to_file=stdout, should_print=bool(output))
 
223
 
 
224
        self.apply_redirected(None, stdout, None, action, inv, None, FastPath('path'), 'file')
 
225
        self.assertEqual(stdout.getvalue(), output)