~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_add.py

Add additional module for lsprof.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import unittest
 
3
 
 
4
from bzrlib.tests import TestCaseInTempDir, TestCase
 
5
from bzrlib.branch import Branch
 
6
from bzrlib.errors import NotBranchError
 
7
from bzrlib.inventory import InventoryFile
 
8
from bzrlib.workingtree import WorkingTree
 
9
 
 
10
class TestSmartAdd(TestCaseInTempDir):
 
11
 
 
12
    def test_add_dot_from_root(self):
 
13
        """Test adding . from the root of the tree.""" 
 
14
        from bzrlib.add import smart_add
 
15
        paths = ("original/", "original/file1", "original/file2")
 
16
        self.build_tree(paths)
 
17
        branch = Branch.initialize(u".")
 
18
        smart_add((u".",), recurse=True)
 
19
        for path in paths:
 
20
            self.assertNotEqual(branch.working_tree().path2id(path), None)
 
21
 
 
22
    def test_add_dot_from_subdir(self):
 
23
        """Test adding . from a subdir of the tree.""" 
 
24
        from bzrlib.add import smart_add
 
25
        paths = ("original/", "original/file1", "original/file2")
 
26
        self.build_tree(paths)
 
27
        branch = Branch.initialize(u".")
 
28
        os.chdir("original")
 
29
        smart_add((u".",), recurse=True)
 
30
        for path in paths:
 
31
            self.assertNotEqual(branch.working_tree().path2id(path), None)
 
32
 
 
33
    def test_add_tree_from_above_tree(self):
 
34
        """Test adding a tree from above the tree.""" 
 
35
        from bzrlib.add import smart_add
 
36
        paths = ("original/", "original/file1", "original/file2")
 
37
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
 
38
                        "branch/original/file2")
 
39
        self.build_tree(branch_paths)
 
40
        branch = Branch.initialize("branch")
 
41
        smart_add(("branch",))
 
42
        for path in paths:
 
43
            self.assertNotEqual(branch.working_tree().path2id(path), None)
 
44
 
 
45
    def test_add_above_tree_preserves_tree(self):
 
46
        """Test nested trees are not affect by an add above them."""
 
47
        from bzrlib.add import smart_add, add_reporter_null
 
48
        
 
49
        paths = ("original/", "original/file1", "original/file2")
 
50
        child_paths = ("path",)
 
51
        full_child_paths = ("original/child", "original/child/path")
 
52
        build_paths = ("original/", "original/file1", "original/file2", 
 
53
                       "original/child/", "original/child/path")
 
54
        
 
55
        self.build_tree(build_paths)
 
56
        branch = Branch.initialize(u".")
 
57
        child_branch = Branch.initialize("original/child")
 
58
        smart_add((u".",), True, add_reporter_null)
 
59
        for path in paths:
 
60
            self.assertNotEqual((path, branch.working_tree().path2id(path)),
 
61
                                (path, None))
 
62
        for path in full_child_paths:
 
63
            self.assertEqual((path, branch.working_tree().path2id(path)),
 
64
                             (path, None))
 
65
        for path in child_paths:
 
66
            self.assertEqual(child_branch.working_tree().path2id(path), None)
 
67
 
 
68
    def test_add_paths(self):
 
69
        """Test smart-adding a list of paths."""
 
70
        from bzrlib.add import smart_add
 
71
        paths = ("file1", "file2")
 
72
        self.build_tree(paths)
 
73
        branch = Branch.initialize(u".")
 
74
        smart_add(paths)
 
75
        for path in paths:
 
76
            self.assertNotEqual(branch.working_tree().path2id(path), None)
 
77
            
 
78
class TestSmartAddBranch(TestCaseInTempDir):
 
79
    """Test smart adds with a specified branch."""
 
80
 
 
81
    def test_add_dot_from_root(self):
 
82
        """Test adding . from the root of the tree.""" 
 
83
        from bzrlib.add import smart_add_tree
 
84
        paths = ("original/", "original/file1", "original/file2")
 
85
        self.build_tree(paths)
 
86
        Branch.initialize(u".")
 
87
        tree = WorkingTree()
 
88
        smart_add_tree(tree, (u".",))
 
89
        for path in paths:
 
90
            self.assertNotEqual(tree.path2id(path), None)
 
91
 
 
92
    def test_add_dot_from_subdir(self):
 
93
        """Test adding . from a subdir of the tree.""" 
 
94
        from bzrlib.add import smart_add_tree
 
95
        paths = ("original/", "original/file1", "original/file2")
 
96
        self.build_tree(paths)
 
97
        Branch.initialize(u".")
 
98
        tree = WorkingTree()
 
99
        os.chdir("original")
 
100
        smart_add_tree(tree, (u".",))
 
101
        for path in paths:
 
102
            self.assertNotEqual(tree.path2id(path), None)
 
103
 
 
104
    def test_add_tree_from_above_tree(self):
 
105
        """Test adding a tree from above the tree.""" 
 
106
        from bzrlib.add import smart_add_tree
 
107
        paths = ("original/", "original/file1", "original/file2")
 
108
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
 
109
                        "branch/original/file2")
 
110
        self.build_tree(branch_paths)
 
111
        Branch.initialize("branch")
 
112
        tree = WorkingTree("branch")
 
113
        smart_add_tree(tree, ("branch",))
 
114
        for path in paths:
 
115
            self.assertNotEqual(tree.path2id(path), None)
 
116
 
 
117
    def test_add_above_tree_preserves_tree(self):
 
118
        """Test nested trees are not affect by an add above them."""
 
119
        from bzrlib.add import smart_add_tree
 
120
        paths = ("original/", "original/file1", "original/file2")
 
121
        child_paths = ("path")
 
122
        full_child_paths = ("original/child", "original/child/path")
 
123
        build_paths = ("original/", "original/file1", "original/file2", 
 
124
                       "original/child/", "original/child/path")
 
125
        self.build_tree(build_paths)
 
126
        Branch.initialize(u".")
 
127
        tree = WorkingTree()
 
128
        child_branch = Branch.initialize("original/child")
 
129
        smart_add_tree(tree, (u".",))
 
130
        for path in paths:
 
131
            self.assertNotEqual((path, tree.path2id(path)),
 
132
                                (path, None))
 
133
        for path in full_child_paths:
 
134
            self.assertEqual((path, tree.path2id(path)),
 
135
                             (path, None))
 
136
        for path in child_paths:
 
137
            self.assertEqual(child_branch.working_tree().path2id(path), None)
 
138
 
 
139
    def test_add_paths(self):
 
140
        """Test smart-adding a list of paths."""
 
141
        from bzrlib.add import smart_add_tree
 
142
        paths = ("file1", "file2")
 
143
        self.build_tree(paths)
 
144
        Branch.initialize(u".")
 
145
        tree = WorkingTree()
 
146
        smart_add_tree(tree, paths)
 
147
        for path in paths:
 
148
            self.assertNotEqual(tree.path2id(path), None)
 
149
 
 
150
class TestAddCallbacks(TestCaseInTempDir):
 
151
 
 
152
    def setUp(self):
 
153
        super(TestAddCallbacks, self).setUp()
 
154
        self.entry = InventoryFile("id", "name", None)
 
155
 
 
156
    def test_null_callback(self):
 
157
        from bzrlib.add import add_reporter_null
 
158
        add_reporter_null('path', 'file', self.entry)
 
159
 
 
160
    def test_print_callback(self):
 
161
        from bzrlib.add import add_reporter_print
 
162
        from StringIO import StringIO
 
163
        stdout = StringIO()
 
164
        self.apply_redirected(None, stdout, None, add_reporter_print,
 
165
                              'path', 'file', self.entry)
 
166
        self.assertEqual(stdout.getvalue(), "added path\n")