98
 
class MockDiffWidget(object):
 
100
 
    def set_diff_text_sections(self, sections):
 
101
 
        self.sections = list(sections)
 
104
 
class MockWindow(object):
 
106
 
        self.diff = MockDiffWidget()
 
107
 
        self.merge_successful = False
 
109
 
    def set_title(self, title):
 
112
 
    def _get_save_path(self, basename):
 
115
 
    def _get_merge_target(self):
 
121
 
    def _merge_successful(self):
 
122
 
        self.merge_successful = True
 
124
 
    def _conflicts(self):
 
125
 
        self.conflicts = True
 
127
 
    def _handle_error(self, e):
 
128
 
        self.handled_error = e
 
131
 
class TestDiffController(tests.TestCaseWithTransport):
 
133
 
    def get_controller(self):
 
134
 
        window = MockWindow()
 
135
 
        return DiffController('load-path', eg_diff.splitlines(True), window)
 
137
 
    def test_get_diff_sections(self):
 
138
 
        controller = self.get_controller()
 
139
 
        controller = DiffController('.', eg_diff.splitlines(True),
 
141
 
        sections = list(controller.get_diff_sections())
 
142
 
        self.assertEqual('Complete Diff', sections[0][0])
 
143
 
        self.assertIs(None, sections[0][1])
 
144
 
        self.assertEqual(eg_diff, sections[0][2])
 
146
 
        self.assertEqual('tests/test_diff.py', sections[1][0])
 
147
 
        self.assertEqual('tests/test_diff.py', sections[1][1])
 
148
 
        self.assertEqual(''.join(eg_diff.splitlines(True)[1:]),
 
151
 
    def test_initialize_window(self):
 
152
 
        controller = self.get_controller()
 
153
 
        controller.initialize_window(controller.window)
 
154
 
        self.assertEqual(2, len(controller.window.diff.sections))
 
155
 
        self.assertEqual('load-path - diff', controller.window.title)
 
157
 
    def test_perform_save(self):
 
158
 
        self.build_tree_contents([('load-path', 'foo')])
 
159
 
        controller = self.get_controller()
 
160
 
        controller.perform_save(None)
 
161
 
        self.assertFileEqual('foo', 'save-path')
 
164
 
class TestMergeDirectiveController(tests.TestCaseWithTransport):
 
166
 
    def make_this_other_directive(self):
 
167
 
        this = self.make_branch_and_tree('this')
 
168
 
        this.commit('first commit')
 
169
 
        other = this.bzrdir.sprout('other').open_workingtree()
 
170
 
        self.build_tree_contents([('other/foo', 'bar')])
 
172
 
        other.commit('second commit')
 
175
 
            directive = MergeDirective2.from_objects(other.branch.repository,
 
176
 
                                                     other.last_revision(), 0,
 
180
 
        return this, other, directive
 
182
 
    def make_merged_window(self, directive):
 
183
 
        window = MockWindow()
 
184
 
        controller = MergeDirectiveController('directive', directive, window)
 
185
 
        controller.perform_merge(window)
 
188
 
    def test_perform_merge_success(self):
 
189
 
        this, other, directive = self.make_this_other_directive()
 
190
 
        window = self.make_merged_window(directive)
 
191
 
        self.assertTrue(window.merge_successful)
 
192
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
 
193
 
        self.assertFileEqual('bar', 'this/foo')
 
195
 
    def test_perform_merge_conflicts(self):
 
196
 
        this, other, directive = self.make_this_other_directive()
 
197
 
        self.build_tree_contents([('this/foo', 'bar')])
 
199
 
        this.commit('message')
 
200
 
        window = self.make_merged_window(directive)
 
201
 
        self.assertFalse(window.merge_successful)
 
202
 
        self.assertTrue(window.conflicts)
 
203
 
        self.assertEqual(other.last_revision(), this.get_parent_ids()[1])
 
204
 
        self.assertFileEqual('bar', 'this/foo')
 
206
 
    def test_perform_merge_uncommitted_changes(self):
 
207
 
        this, other, directive = self.make_this_other_directive()
 
208
 
        self.build_tree_contents([('this/foo', 'bar')])
 
210
 
        window = self.make_merged_window(directive)
 
211
 
        self.assertIsInstance(window.handled_error, errors.UncommittedChanges)
 
214
72
class Test_IterChangesToStatus(tests.TestCaseWithTransport):
 
216
74
    def assertStatusEqual(self, expected, tree):
 
217
 
        values = iter_changes_to_status(tree.basis_tree(), tree)
 
 
75
        values = _iter_changes_to_status(tree.basis_tree(), tree)
 
218
76
        self.assertEqual(expected, values)
 
220
78
    def test_status_added(self):
 
 
307
165
            [('a-id', 'a', 'removed', 'a'),
 
308
166
             ('b-id', 'b', 'removed', 'b/'),
 
311
 
    def test_status_missing_file(self):
 
312
 
        this = self.make_branch_and_tree('this')
 
313
 
        self.build_tree(['this/foo'])
 
314
 
        this.add(['foo'], ['foo-id'])
 
317
 
        other = this.bzrdir.sprout('other').open_workingtree()
 
319
 
        os.remove('this/foo')
 
320
 
        this.remove('foo', force=True)
 
321
 
        this.commit('remove')
 
323
 
        f = open('other/foo', 'wt')
 
325
 
            f.write('Modified\n')
 
328
 
        other.commit('modified')
 
330
 
        this.merge_from_branch(other.branch)
 
331
 
        conflicts.resolve(this)
 
333
 
        self.assertStatusEqual(
 
334
 
            [('foo-id', 'foo.OTHER', 'missing', 'foo.OTHER'),],
 
337
 
    def test_status_missing_directory(self):
 
338
 
        this = self.make_branch_and_tree('this')
 
339
 
        self.build_tree(['this/foo/', 'this/foo/bar'])
 
340
 
        this.add(['foo', 'foo/bar'], ['foo-id', 'bar-id'])
 
343
 
        other = this.bzrdir.sprout('other').open_workingtree()
 
345
 
        os.remove('this/foo/bar')
 
347
 
        this.remove('foo', force=True)
 
348
 
        this.commit('remove')
 
350
 
        f = open('other/foo/bar', 'wt')
 
352
 
            f.write('Modified\n')
 
355
 
        other.commit('modified')
 
357
 
        this.merge_from_branch(other.branch)
 
358
 
        conflicts.resolve(this)
 
360
 
        self.assertStatusEqual(
 
361
 
            [('foo-id', u'foo', 'added', u'foo/'),
 
362
 
             ('bar-id', u'foo/bar.OTHER', 'missing', u'foo/bar.OTHER'),],