~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import unittest

from bzrlib.tests import TestCaseInTempDir, TestCase
from bzrlib.branch import Branch
from bzrlib.errors import NotBranchError
from bzrlib.inventory import InventoryFile, Inventory
from bzrlib.workingtree import WorkingTree

class TestSmartAdd(TestCaseInTempDir):

    def test_add_dot_from_root(self):
        """Test adding . from the root of the tree.""" 
        from bzrlib.add import smart_add
        paths = ("original/", "original/file1", "original/file2")
        self.build_tree(paths)
        branch = Branch.initialize(u".")
        smart_add((u".",))
        for path in paths:
            self.assertNotEqual(branch.working_tree().path2id(path), None)

    def test_add_dot_from_subdir(self):
        """Test adding . from a subdir of the tree.""" 
        from bzrlib.add import smart_add
        paths = ("original/", "original/file1", "original/file2")
        self.build_tree(paths)
        branch = Branch.initialize(u".")
        os.chdir("original")
        smart_add((u".",))
        for path in paths:
            self.assertNotEqual(branch.working_tree().path2id(path), None)

    def test_add_tree_from_above_tree(self):
        """Test adding a tree from above the tree.""" 
        from bzrlib.add import smart_add
        paths = ("original/", "original/file1", "original/file2")
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
                        "branch/original/file2")
        self.build_tree(branch_paths)
        branch = Branch.initialize("branch")
        smart_add(("branch",))
        for path in paths:
            self.assertNotEqual(branch.working_tree().path2id(path), None)

    def test_add_above_tree_preserves_tree(self):
        """Test nested trees are not affect by an add above them."""
        from bzrlib.add import smart_add
        paths = ("original/", "original/file1", "original/file2")
        child_paths = ("path",)
        full_child_paths = ("original/child", "original/child/path")
        build_paths = ("original/", "original/file1", "original/file2", 
                       "original/child/", "original/child/path")
        
        self.build_tree(build_paths)
        branch = Branch.initialize(u".")
        child_branch = Branch.initialize("original/child")
        smart_add((u".",))
        for path in paths:
            self.assertNotEqual((path, branch.working_tree().path2id(path)),
                                (path, None))
        for path in full_child_paths:
            self.assertEqual((path, branch.working_tree().path2id(path)),
                             (path, None))
        for path in child_paths:
            self.assertEqual(child_branch.working_tree().path2id(path), None)

    def test_add_paths(self):
        """Test smart-adding a list of paths."""
        from bzrlib.add import smart_add
        paths = ("file1", "file2")
        self.build_tree(paths)
        branch = Branch.initialize(u".")
        smart_add(paths)
        for path in paths:
            self.assertNotEqual(branch.working_tree().path2id(path), None)

    def test_add_dry_run(self):
        """Test a dry run add, make sure nothing is added."""
        from bzrlib.commands import run_bzr
        eq = self.assertEqual
        b = Branch.initialize(u'.')
        t = b.working_tree()
        self.build_tree(['inertiatic/', 'inertiatic/esp'])
        eq(list(t.unknowns()), ['inertiatic'])
        self.capture('add --dry-run .')
        eq(list(t.unknowns()), ['inertiatic'])

class TestSmartAddBranch(TestCaseInTempDir):
    """Test smart adds with a specified branch."""

    def test_add_dot_from_root(self):
        """Test adding . from the root of the tree.""" 
        from bzrlib.add import smart_add_tree
        paths = ("original/", "original/file1", "original/file2")
        self.build_tree(paths)
        Branch.initialize(u".")
        tree = WorkingTree()
        smart_add_tree(tree, (u".",))
        for path in paths:
            self.assertNotEqual(tree.path2id(path), None)

    def test_add_dot_from_subdir(self):
        """Test adding . from a subdir of the tree.""" 
        from bzrlib.add import smart_add_tree
        paths = ("original/", "original/file1", "original/file2")
        self.build_tree(paths)
        Branch.initialize(u".")
        tree = WorkingTree()
        os.chdir("original")
        smart_add_tree(tree, (u".",))
        for path in paths:
            self.assertNotEqual(tree.path2id(path), None)

    def test_add_tree_from_above_tree(self):
        """Test adding a tree from above the tree.""" 
        from bzrlib.add import smart_add_tree
        paths = ("original/", "original/file1", "original/file2")
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
                        "branch/original/file2")
        self.build_tree(branch_paths)
        Branch.initialize("branch")
        tree = WorkingTree("branch")
        smart_add_tree(tree, ("branch",))
        for path in paths:
            self.assertNotEqual(tree.path2id(path), None)

    def test_add_above_tree_preserves_tree(self):
        """Test nested trees are not affect by an add above them."""
        from bzrlib.add import smart_add_tree
        paths = ("original/", "original/file1", "original/file2")
        child_paths = ("path")
        full_child_paths = ("original/child", "original/child/path")
        build_paths = ("original/", "original/file1", "original/file2", 
                       "original/child/", "original/child/path")
        self.build_tree(build_paths)
        Branch.initialize(u".")
        tree = WorkingTree()
        child_branch = Branch.initialize("original/child")
        smart_add_tree(tree, (u".",))
        for path in paths:
            self.assertNotEqual((path, tree.path2id(path)),
                                (path, None))
        for path in full_child_paths:
            self.assertEqual((path, tree.path2id(path)),
                             (path, None))
        for path in child_paths:
            self.assertEqual(child_branch.working_tree().path2id(path), None)

    def test_add_paths(self):
        """Test smart-adding a list of paths."""
        from bzrlib.add import smart_add_tree
        paths = ("file1", "file2")
        self.build_tree(paths)
        Branch.initialize(u".")
        tree = WorkingTree()
        smart_add_tree(tree, paths)
        for path in paths:
            self.assertNotEqual(tree.path2id(path), None)

class TestAddActions(TestCaseInTempDir):

    def test_null(self):
        from bzrlib.add import add_action_null
        self.run_action(add_action_null, "", False)

    def test_add(self):
        self.entry = InventoryFile("id", "name", None)
        from bzrlib.add import add_action_add
        self.run_action(add_action_add, "", True)

    def test_add_and_print(self):
        from bzrlib.add import add_action_add_and_print
        self.run_action(add_action_add_and_print, "added path\n", True)

    def test_print(self):
        from bzrlib.add import add_action_print
        self.run_action(add_action_print, "added path\n", False)

    def run_action(self, action, output, should_add):
        from StringIO import StringIO
        inv = Inventory()
        stdout = StringIO()

        self.apply_redirected(None, stdout, None, action, inv, 'path', 'file')
        self.assertEqual(stdout.getvalue(), output)

        if should_add:
            self.assertNotEqual(inv.path2id('path'), None)
        else:
            self.assertEqual(inv.path2id('path'), None)