~ubuntu-branches/ubuntu/utopic/bzr-fastimport/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/test_processor.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-05-05 20:23:22 UTC
  • mfrom: (0.1.3 squeeze) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505202322-t5ce971trg0ojsfj
Tags: 0.8.0~bzr181-1
* Move to section vcs.
* New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
import time
18
 
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    tests,
22
 
    )
23
 
 
24
 
from bzrlib.plugins.fastimport import (
25
 
    commands,
26
 
    errors,
27
 
    )
28
 
 
29
 
from bzrlib.plugins.fastimport.processors import (
30
 
    generic_processor,
31
 
    )
32
 
 
33
 
 
34
 
class TestRename(tests.TestCaseWithTransport):
35
 
 
36
 
    def get_handler(self):
37
 
        branch = self.make_branch('.')
38
 
        handler = generic_processor.GenericProcessor(branch.bzrdir)
39
 
        return (handler, branch)
40
 
 
41
 
    def get_command_iter(self, old_path, new_path):
42
 
        def command_list():
43
 
            author = ['', 'bugs@a.com', time.time(), time.timezone]
44
 
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
45
 
            def files_one():
46
 
                yield commands.FileModifyCommand(old_path, 'file', False,
47
 
                        None, "aaa")
48
 
            yield commands.CommitCommand('head', '1', author,
49
 
                committer, "commit 1", None, [], files_one)
50
 
            def files_two():
51
 
                yield commands.FileRenameCommand(old_path, new_path)
52
 
            yield commands.CommitCommand('head', '2', author,
53
 
                committer, "commit 2", ":1", [], files_two)
54
 
        return command_list
55
 
 
56
 
    # FIXME: [] as a default is bad, as it is mutable, but I want
57
 
    # to use None to mean "don't check this".
58
 
    def check_changes(self, changes, expected_added=[],
59
 
            expected_removed=[], expected_modified=[],
60
 
            expected_renamed=[]):
61
 
        """Check the changes in a TreeDelta
62
 
 
63
 
        This method checks that the TreeDelta contains the expected
64
 
        modifications between the two trees that were used to generate
65
 
        it. The required changes are passed in as a list, where
66
 
        each entry contains the needed information about the change.
67
 
 
68
 
        If you do not wish to assert anything about a particular
69
 
        category then pass None instead.
70
 
 
71
 
        changes: The TreeDelta to check.
72
 
        expected_added: a list of (filename,) tuples that must have
73
 
            been added in the delta.
74
 
        expected_removed: a list of (filename,) tuples that must have
75
 
            been removed in the delta.
76
 
        expected_modified: a list of (filename,) tuples that must have
77
 
            been modified in the delta.
78
 
        expected_renamed: a list of (old_path, new_path) tuples that
79
 
            must have been renamed in the delta.
80
 
        """
81
 
        renamed = changes.renamed
82
 
        added = changes.added
83
 
        removed = changes.removed
84
 
        modified = changes.modified
85
 
        if expected_renamed is not None:
86
 
            self.assertEquals(len(renamed), len(expected_renamed),
87
 
                "%s is renamed, expected %s" % (renamed, expected_renamed))
88
 
            renamed_files = [(item[0], item[1]) for item in renamed]
89
 
            for expected_renamed_entry in expected_renamed:
90
 
                self.assertTrue(expected_renamed_entry in renamed_files,
91
 
                    "%s is not renamed, %s are" % (str(expected_renamed_entry),
92
 
                        renamed_files))
93
 
        if expected_added is not None:
94
 
            self.assertEquals(len(added), len(expected_added),
95
 
                "%s is added" % str(added))
96
 
            added_files = [(item[0],) for item in added]
97
 
            for expected_added_entry in expected_added:
98
 
                self.assertTrue(expected_added_entry in added_files,
99
 
                    "%s is not added, %s are" % (str(expected_added_entry),
100
 
                        added_files))
101
 
        if expected_removed is not None:
102
 
            self.assertEquals(len(removed), len(expected_removed),
103
 
                "%s is removed" % str(removed))
104
 
            removed_files = [(item[0],) for item in removed]
105
 
            for expected_removed_entry in expected_removed:
106
 
                self.assertTrue(expected_removed_entry in removed_files,
107
 
                    "%s is not removed, %s are" % (str(expected_removed_entry),
108
 
                        removed_files))
109
 
        if expected_modified is not None:
110
 
            self.assertEquals(len(modified), len(expected_modified),
111
 
                "%s is modified" % str(modified))
112
 
            modified_files = [(item[0],) for item in modified]
113
 
            for expected_modified_entry in expected_modified:
114
 
                self.assertTrue(expected_modified_entry in modified_files,
115
 
                    "%s is not modified, %s are" % (str(expected_modified_entry),
116
 
                        modified_files))
117
 
 
118
 
    def test_rename_in_root(self):
119
 
        (handler, branch) = self.get_handler()
120
 
        old_path = 'a'
121
 
        new_path = 'b'
122
 
        command_list = self.get_command_iter(old_path, new_path)
123
 
        handler.process(command_list)
124
 
        repo = branch.repository
125
 
        revtree1 = repo.revision_tree(branch.revision_history()[0])
126
 
        revtree2 = repo.revision_tree(branch.revision_history()[1])
127
 
        changes = revtree2.changes_from(revtree1)
128
 
        self.assertEqual(revtree1.get_revision_id(),
129
 
                         revtree1.inventory.root.children['a'].revision)
130
 
        self.assertEqual(revtree2.get_revision_id(),
131
 
                         revtree2.inventory.root.children['b'].revision)
132
 
        self.check_changes(changes, expected_renamed=[(old_path, new_path)])
133
 
 
134
 
    def test_rename_in_subdir(self):
135
 
        (handler, branch) = self.get_handler()
136
 
        old_path = 'a/a'
137
 
        new_path = 'a/b'
138
 
        command_list = self.get_command_iter(old_path, new_path)
139
 
        handler.process(command_list)
140
 
        repo = branch.repository
141
 
        revtree1 = repo.revision_tree(branch.revision_history()[0])
142
 
        revtree2 = repo.revision_tree(branch.revision_history()[1])
143
 
        changes = revtree2.changes_from(revtree1)
144
 
        self.check_changes(changes, expected_renamed=[(old_path, new_path)])
145
 
 
146
 
    def test_move_to_new_dir(self):
147
 
        (handler, branch) = self.get_handler()
148
 
        old_path = 'a/a'
149
 
        new_path = 'b/a'
150
 
        command_list = self.get_command_iter(old_path, new_path)
151
 
        handler.process(command_list)
152
 
        repo = branch.repository
153
 
        revtree1 = repo.revision_tree(branch.revision_history()[0])
154
 
        revtree2 = repo.revision_tree(branch.revision_history()[1])
155
 
        changes = revtree2.changes_from(revtree1)
156
 
        self.check_changes(changes, expected_renamed=[(old_path, new_path)],
157
 
            expected_added=[('b',)])
158
 
 
159
 
 
160
 
class TestFileKinds(tests.TestCaseWithTransport):
161
 
 
162
 
    def get_handler(self):
163
 
        branch = self.make_branch('.')
164
 
        handler = generic_processor.GenericProcessor(branch.bzrdir)
165
 
        return (handler, branch)
166
 
 
167
 
    def get_command_iter(self, path, kind, content):
168
 
        def command_list():
169
 
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
170
 
            def files_one():
171
 
                yield commands.FileModifyCommand(path, kind, False,
172
 
                        None, content)
173
 
            yield commands.CommitCommand('head', '1', None,
174
 
                committer, "commit 1", None, [], files_one)
175
 
        return command_list
176
 
 
177
 
    def test_import_plainfile(self):
178
 
        (handler, branch) = self.get_handler()
179
 
        command_list = self.get_command_iter('foo', 'file', 'aaa')
180
 
 
181
 
    def test_import_symlink(self):
182
 
        (handler, branch) = self.get_handler()
183
 
        command_list = self.get_command_iter('foo', 'symlink', 'bar')
184
 
        handler.process(command_list)