Debian Codegraph @

lib/tests/test_treewidget.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2009 Gary van der Merwe <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either versio0n 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 import os
20
21 from breezy.tests import TestCase, TestCaseWithTransport
22 from breezy import tests
23 from breezy.workingtree import WorkingTree
24 from breezy import ignores
25 from breezy.bzr.conflicts import TextConflict
26
27 from PyQt5 import QtCore
28 from PyQt5.QtTest import QTest
29 from breezy.plugins.qbrz.lib import tests as qtests
30 from breezy.plugins.qbrz.lib.treewidget import (
31 TreeWidget,
32 TreeModel,
33 TreeFilterProxyModel,
34 ModelItemData,
35 InternalItem,
36 PersistantItemReference,
37 group_large_dirs,
38 )
39 from breezy.plugins.qbrz.lib.tests.modeltest import ModelTest
40
41
42 # The filter_scenarios are at the end of the file
43 def load_tests(loader, basic_tests, pattern):
44 result = loader.suiteClass()
45
46 tree_tests, remaining_tests = tests.split_suite_by_condition(basic_tests, tests.condition_isinstance((TestTreeWidget,)))
47 tests.multiply_tests(tree_tests, tree_scenarios, result)
48
49 filter_tests, remaining_tests = tests.split_suite_by_condition(remaining_tests, tests.condition_isinstance((TestTreeFilterProxyModel,)))
50 tests.multiply_tests(filter_tests, filter_scenarios, result)
51
52 # No parametrization for the remaining tests
53 result.addTests(remaining_tests)
54
55 return result
56
57
58 class TestTreeWidget(qtests.QTestCase):
59
60 # Set by load_tests
61 make_tree = None
62 modify_tree = None
63 changes_mode = False
64
65 def setUp(self):
66 super(TestTreeWidget, self).setUp()
67 self.tree, self.branch = self.make_tree(self)
68
69 def make_working_tree(self):
70 # tree = WorkingTree()
71 tree = self.make_branch_and_tree('trunk')
72 self.build_tree_contents([('trunk/textconflict', b'base'),])
73 tree.add(['textconflict'], ids=[b'textconflict-id'])
74 tree.commit('a', rev_id=b'rev-a', committer="[email protected]", timestamp=1166046000.00, timezone=0)
75
76 branch_tree = tree.controldir.sprout('branch').open_workingtree()
77 self.build_tree_contents([('branch/textconflict', b'other'),])
78 branch_tree.commit('b', rev_id=b'rev-b', committer="[email protected]", timestamp=1166046000.00, timezone=0)
79 self.branch_tree = branch_tree
80
81 self.build_tree(['trunk/dir/'])
82 self.build_tree_contents([('trunk/dir/dirchild', b''),
83 ('trunk/unmodified', b''),
84 ('trunk/renamed', b''),
85 ('trunk/moved', b''),
86 ('trunk/movedandrenamed', b''),
87 ('trunk/removed', b''),
88 ('trunk/missing', b''),
89 ('trunk/modified', b'old'),
90 ('trunk/textconflict', b'this'),
91 ])
92 tree.add(['dir'], ids=[b'dir-id'])
93 tree.add(['dir/dirchild'], ids=[b'dirchild-id'])
94 tree.add(['unmodified'], ids=[b'unmodified-id'])
95 tree.add(['renamed'], ids=[b'renamed-id'])
96 tree.add(['moved'], ids=[b'moved-id'])
97 tree.add(['movedandrenamed'], ids=[b'movedandrenamed-id'])
98 tree.add(['removed'], ids=[b'removed-id'])
99 tree.add(['missing'], ids=[b'missing-id'])
100 tree.add(['modified'], ids=[b'modified-id'])
101 tree.commit('c', rev_id=b'rev-c', committer="[email protected]", timestamp=1166046000.00, timezone=0)
102
103 return tree, tree.branch
104
105 def modify_working_tree(self, tree):
106 if 0:
107 tree = WorkingTree()
108 # RJLRJL: patched out renames as calls seem to be insoluble
109 # and related to a problem with finding the id vs rel_path
110 tree.merge_from_branch(self.branch_tree.branch, b'rev-b')
111
112 self.build_tree_contents([('trunk/added', b''),
113 ('trunk/addedmissing', b''),
114 ('trunk/modified', b'new'),
115 ('trunk/unversioned', b''),
116 ])
117 tree.add(['added'], ids=[b'added-id'])
118 tree.add(['addedmissing'], ids=[b'addedmissing-id'])
119
120 tree.rename_one('renamed', 'renamed1')
121 tree.move(('moved',), 'dir')
122 tree.rename_one('movedandrenamed', 'movedandrenamed1')
123 tree.move(('movedandrenamed1',), 'dir')
124 tree.remove(('removed',))
125 os.remove('trunk/missing')
126 os.remove('trunk/addedmissing')
127
128 # test for https://bugs.launchpad.net/qbrz/+bug/538753
129 # must sort before trunk/dir
130 self.build_tree(['trunk/a-newdir/'])
131 self.build_tree_contents([('trunk/a-newdir/newdirchild', b'')])
132 tree.add(['a-newdir'], ids=[b'a-newdir-id'])
133 tree.add(['a-newdir/newdirchild'], ids=[b'newdirchild-id'])
134 # manually add conflicts for files that don't exist
135 # See https://bugs.launchpad.net/qbrz/+bug/528548
136 tree.add_conflicts([TextConflict('nofileconflict')])
137
138 def make_rev_tree(self):
139 tree = self.make_branch_and_tree('tree')
140 self.build_tree(['tree/b/'])
141 self.build_tree_contents([('tree/a', b''),
142 ('tree/b/c', b''),
143 ])
144 tree.add(['a'], ids=[b'a-id'])
145 tree.add(['b'], ids=[b'b-id'])
146 tree.add(['b/c'], ids=[b'c-id'])
147 tree.commit('a', rev_id=b'rev-1', committer="[email protected]", timestamp=1166046000.00, timezone=0)
148 revtree = tree.branch.repository.revision_tree(b'rev-1')
149 return revtree, tree.branch
150
151 def run_model_tests(self):
152 # Check that indexes point to their correct items.
153 def check_item_children(index):
154 item = self.widget.tree_model.inventory_data[index.internalId()]
155 if item.children_ids:
156 for row, child_id in enumerate(item.children_ids):
157 child_index = self.widget.tree_model.index(row, 0, index)
158 self.assertEqual(child_index.internalId(), child_id)
159 check_item_children(child_index)
160
161 if self.widget.tree_model.inventory_data:
162 check_item_children(self.widget.tree_model._index_from_id(0, 0)) # root
163
164 ModelTest(self.widget.tree_model, None)
165 ModelTest(self.widget.tree_filter_model, None)
166
167 def test_show_widget(self):
168 widget = TreeWidget()
169 self.widget = widget
170 widget.setGeometry(0, 0, 1000, 1000)
171 QTest.qWaitForWindowExposed(widget)
172 self.widget.show()
173 self.run_model_tests()
174
175 self.addCleanup(widget.close)
176 # make the widget bigger so that we can see what is going on.
177 widget.setGeometry(0,0,1000,1000)
178 QtCore.QCoreApplication.processEvents()
179 widget.show()
180
181 QTest.qWaitForWindowExposed(widget)
182 QtCore.QCoreApplication.processEvents()
183 widget.set_tree(self.tree, self.branch, changes_mode=self.changes_mode)
184 self.widget.show()
185 self.run_model_tests()
186
187 widget.update()
188 QTest.qWaitForWindowExposed(widget)
189 QtCore.QCoreApplication.processEvents()
190 widget.expandAll()
191 QTest.qWaitForWindowExposed(widget)
192 self.run_model_tests()
193
194 widget.update()
195 QTest.qWaitForWindowExposed(widget)
196 QtCore.QCoreApplication.processEvents()
197
198 # RJL 2023: self.modify_tree (which is actually
199 # TestTreeWidget.modify_working_tree - see tree_scenarios) fails
200 # or at least the refresh() afterwards does
201 # Suspicion is that remame_one might be faulty
202 # BUGBUG: patched out - rename seems to fail
203 self.modify_tree(self, self.tree)
204 QTest.qWaitForWindowExposed(widget)
205 widget.refresh()
206 QTest.qWaitForWindowExposed(widget)
207 self.run_model_tests()
208
209 widget.update()
210 QTest.qWaitForWindowExposed(widget)
211 QtCore.QCoreApplication.processEvents()
212 widget.expandAll()
213 QTest.qWaitForWindowExposed(widget)
214 self.run_model_tests()
215
216 widget.update()
217 QTest.qWaitForWindowExposed(widget)
218 QtCore.QCoreApplication.processEvents()
219
220
221 tree_scenarios = (
222 ('Working-Tree',
223 {'make_tree': TestTreeWidget.make_working_tree,
224 'modify_tree': TestTreeWidget.modify_working_tree,}),
225 ('Working-Tree-Changes-Mode',
226 {'make_tree': TestTreeWidget.make_working_tree,
227 'modify_tree': TestTreeWidget.modify_working_tree,
228 'changes_mode': True}),
229 ('Revision-Tree',
230 {'make_tree': TestTreeWidget.make_rev_tree,
231 'modify_tree': lambda self, tree: None,}),
232 )
233
234
235 class TestTreeFilterProxyModel(qtests.QTestCase):
236
237 # Set by load_tests
238 filter = None
239 expected_visible = None
240
241 def test_filters(self):
242 tree = self.make_branch_and_tree('tree')
243
244 self.build_tree(['tree/dir-with-unversioned/', 'tree/ignored-dir-with-child/',])
245 self.build_tree_contents([('tree/dir-with-unversioned/child', b''),
246 ('tree/ignored-dir-with-child/child', b''),
247 ('tree/unchanged', b''),
248 ('tree/changed', b'old'),
249 ('tree/unversioned', b''),
250 ('tree/ignored', b''),
251 ])
252 tree.add(['dir-with-unversioned'], ids=[b'dir-with-unversioned-id'])
253 tree.add(['unchanged'], ids=[b'unchanged-id'])
254 tree.add(['changed'], ids=[b'changed-id'])
255 ignores.tree_ignores_add_patterns(tree, ['ignored-dir-with-child', 'ignored'])
256
257 tree.commit('a', rev_id=b'rev-a', committer="[email protected]", timestamp=1166046000.00, timezone=0)
258
259 self.build_tree_contents([('tree/changed', b'bnew')])
260
261 self.model = TreeModel()
262 load_dirs = [PersistantItemReference(None, 'dir-with-unversioned'), PersistantItemReference(None, 'ignored-dir-with-child')]
263 self.model.set_tree(tree, branch=tree.branch, load_dirs=load_dirs)
264 self.filter_model = TreeFilterProxyModel()
265 self.filter_model.setSourceModel(self.model)
266 self.filter_model.setFilters(self.filter)
267 self.expected_visible.sort()
268 self.assertEqual(self.getVisiblePaths(), self.expected_visible)
269
270 def getVisiblePaths(self):
271 # RJLRJL this appears to have an off-by-one error which does seem
272 # unlikely - however, changes in both breezy and python might now
273 # be causing problems. In other words, it doesn't seem to handle
274 # children properly
275 visible_paths = []
276 # New QModelIndex objects are created by the model using the QAbstractItemModel.createIndex() function.
277 # An invalid model index can be constructed with the QModelIndex constructor. Invalid indexes are often
278 # used as parent indexes when referring to top-level items in a model. (from the PyQt4 docs)
279 parent_indexes_to_visit = [QtCore.QModelIndex()]
280 while parent_indexes_to_visit:
281 parent_index = parent_indexes_to_visit.pop()
282 rows = self.filter_model.rowCount(parent_index)
283 for row in range(0, rows):
284 # we get a QModelIndex into index from row, column (0) and the current parent
285 index = self.filter_model.index(row, 0, parent_index)
286 # and the data (the path in this case)
287 the_path = self.filter_model.data(index, self.model.PATH)
288 visible_paths.append(the_path)
289 if self.filter_model.hasChildren(index):
290 # It appears that, although this will be visited, it won't be recorded
291 # as row_count will be zero whereas the first lot are, as they are children
292 # of the base QtCore parent
293 parent_indexes_to_visit.append(index)
294 visible_paths.sort()
295 return visible_paths
296
297 def test_unversioned_move_conflict(self):
298 """Test for bug reported as lp:557603 lp:712931 lp:815822 lp:876180"""
299 tree = self.make_branch_and_tree("parent")
300 tree.commit("Base revision")
301 childtree = tree.controldir.sprout("child").open_workingtree()
302 self.build_tree(["parent/f", "child/f"])
303 childtree.add(["f"])
304 childtree.commit("Adding f")
305 tree.merge_from_branch(childtree.branch)
306 self.assertLength(1, tree.conflicts())
307 self.assertPathExists("parent/f.moved")
308 os.remove("parent/f.moved")
309 # At this point, the tree has a pending merge adding 'f' and a removed
310 # unversioned duplicate 'f.moved', which is enough to trigger the bug.
311 self.model = TreeModel()
312 load_dirs = [PersistantItemReference(None, "parent")]
313 self.model.set_tree(tree, branch=tree.branch, load_dirs=load_dirs)
314 self.filter_model = TreeFilterProxyModel()
315 self.filter_model.setSourceModel(self.model)
316 self.filter_model.setFilters(self.filter)
317 expected_paths = []
318 if self.filter[TreeFilterProxyModel.CHANGED]:
319 expected_paths.append("f")
320 self.assertEqual(self.getVisiblePaths(), expected_paths)
321
322
323 class TestTreeWidgetSelectAll(qtests.QTestCase):
324
325 def setUp(self):
326 super(TestTreeWidgetSelectAll, self).setUp()
327 tree = self.make_branch_and_tree('tree')
328
329 self.build_tree(['tree/dir-with-unversioned/',
330 'tree/ignored-dir-with-child/',
331 'tree/unversioned-with-ignored/',
332 'tree/unversioned-with-ignored/ignored-dir-with-child/',
333 ])
334 self.build_tree_contents([('tree/dir-with-unversioned/child', b''),
335 ('tree/ignored-dir-with-child/child', b''),
336 ('tree/unversioned-with-ignored/ignored-dir-with-child/child', b''),
337 ('tree/unchanged', b''),
338 ('tree/changed', b'old'),
339 ('tree/unversioned', b''),
340 ('tree/ignored', b''),
341 ])
342 tree.add(['dir-with-unversioned'], ids=[b'dir-with-unversioned-id'])
343 tree.add(['unchanged'], ids=[b'unchanged-id'])
344 tree.add(['changed'], ids=[b'changed-id'])
345 ignores.tree_ignores_add_patterns(tree, ['ignored-dir-with-child', 'ignored'])
346
347 tree.commit('a', rev_id=b'rev-a', committer="[email protected]", timestamp=1166046000.00, timezone=0)
348
349 self.build_tree_contents([('tree/changed', b'new')])
350 self.tree = tree
351
352 def assertSelectedPaths(self, treewidget, paths):
353 # if 0: treewidget = TreeWidget()
354 selected = [item.path for item in treewidget.tree_model.iter_checked()]
355 # we do not care for the order in this test.
356 self.assertEqual(set(selected), set(paths))
357
358 def test_add_selectall(self):
359 import breezy.plugins.qbrz.lib.add
360 self.win = breezy.plugins.qbrz.lib.add.AddWindow(self.tree, None)
361 QTest.qWaitForWindowExposed(self.win)
362 self.win.show()
363 self.addCleanup(self.cleanup_win)
364 self.win.initial_load()
365 QTest.qWaitForWindowExposed(self.win)
366 self.win.show()
367 self.win.show_ignored_checkbox.click()
368 self.win.show()
369 self.assertSelectedPaths(self.win.filelist_widget, ['dir-with-unversioned/child', 'unversioned', 'unversioned-with-ignored'])
370
371 def test_commit_selectall(self):
372 import breezy.plugins.qbrz.lib.commit
373 self.win = breezy.plugins.qbrz.lib.commit.CommitWindow(tree=self.tree, selected_list=None)
374 self.win.show()
375 QTest.qWaitForWindowExposed(self.win)
376 self.addCleanup(self.cleanup_win)
377 self.win.load()
378 QTest.qWaitForWindowExposed(self.win)
379 self.assertSelectedPaths(self.win.filelist_widget, ['changed'])
380 QTest.qWaitForWindowExposed(self.win)
381 # self.win.show_nonversioned_checkbox.setCheckState(QtCore.Qt.Checked)
382 self.win.show_nonversioned_checkbox.click()
383 QTest.qWaitForWindowExposed(self.win)
384 self.win.show()
385 # self.win.selectall_checkbox.setCheckState(QtCore.Qt.Unchecked)
386 self.win.selectall_checkbox.click()
387 QTest.qWaitForWindowExposed(self.win)
388 # RJLRJL: this import pdb...` commented-out code was already present, so looks like this was always problematic
389 # import pdb; pdb.set_trace()
390 self.assertSelectedPaths(self.win.filelist_widget, ['changed', 'dir-with-unversioned/child',
391 'unversioned', 'unversioned-with-ignored'])
392
393 def test_revert_selectall(self):
394 import breezy.plugins.qbrz.lib.revert
395 self.win = breezy.plugins.qbrz.lib.revert.RevertWindow(self.tree, None)
396 self.addCleanup(self.cleanup_win)
397 self.win.initial_load()
398 self.win.selectall_checkbox.click()
399 self.assertSelectedPaths(self.win.filelist, ['changed'])
400
401 def cleanup_win(self):
402 # Sometimes the model was getting deleted before the widget, and the
403 # widget was trying to query the model. So we delete everything here.
404 self.win.deleteLater()
405 try:
406 self.win.filelist_widget.deleteLater()
407 self.win.filelist_widget.tree_model.deleteLater()
408 except AttributeError:
409 self.win.filelist.deleteLater()
410 self.win.filelist.tree_model.deleteLater()
411 QtCore.QCoreApplication.processEvents()
412
413
414 class TestModelItemData(TestCase):
415
416 def _make_unversioned_model_list(self, iterable):
417 return [ModelItemData(path, InternalItem(path, kind, None))
418 for path, kind in iterable]
419
420 def test_sort_key_one_dir(self):
421 models = self._make_unversioned_model_list((
422 ("b", "directory"),
423 ("d", "directory"),
424 ("a", "file"),
425 ("c", "file")))
426 self.assertEqual(models, sorted(reversed(models),
427 key=ModelItemData.dirs_first_sort_key))
428
429 def test_sort_key_sub_dirs(self):
430 models = self._make_unversioned_model_list((
431 ('a', 'directory'),
432 ('a/f', 'file'),
433 ('b', 'directory'),
434 ('b/f', 'file')))
435 self.assertEqual(models, sorted(reversed(models),
436 key=ModelItemData.dirs_first_sort_key))
437
438 models = self._make_unversioned_model_list((
439 ('b', 'directory'),
440 ('b/y', 'directory'),
441 ('b/y/z', 'file'),
442 ('b/x', 'file'),
443 ('a', 'file'),
444 ('c', 'file')))
445 self.assertEqual(models, sorted(reversed(models),
446 key=ModelItemData.dirs_first_sort_key))
447
448
449 # See [[../treewidget.py]] TreeFilterProxyModel
450 # UNCHANGED, CHANGED, UNVERSIONED, IGNORED
451 # RJLRJL check for .bzrignore vs .brzignore
452 filter_scenarios = (
453 ('All',
454 {'filter': (True, True, True, True),
455 'expected_visible': ['dir-with-unversioned',
456 'dir-with-unversioned/child',
457 'ignored-dir-with-child',
458 'ignored-dir-with-child/child',
459 'unchanged',
460 'changed',
461 'unversioned',
462 'ignored',
463 '.bzrignore',
464 ],}),
465 ('Unchanged',
466 {'filter': (True, False, False, False),
467 'expected_visible': ['dir-with-unversioned',
468 'unchanged',
469 '.bzrignore',
470 ],}),
471 ('Changed',
472 {'filter': (False, True, False, False),
473 'expected_visible': ['changed', ],}),
474 ('Unversioned',
475 {'filter': (False, False, True, False),
476 'expected_visible': ['dir-with-unversioned',
477 'dir-with-unversioned/child',
478 'unversioned',
479 ],}),
480 ('Ignored',
481 {'filter': (False, False, False, True),
482 'expected_visible': ['ignored-dir-with-child',
483 'ignored',
484 ],}),
485 ('Ignored+Unversioned',
486 {'filter': (False, False, True, True),
487 'expected_visible': ['ignored-dir-with-child/child',
488 'ignored-dir-with-child',
489 'ignored',
490 'dir-with-unversioned',
491 'dir-with-unversioned/child',
492 'unversioned',
493 ],}),
494 )
495
496
497 class TestGroupLargeDirs(TestCase):
498
499 def test_no_group_small(self):
500 paths = frozenset(("a/1", "a/2", "a/3", "b"))
501 self.assertEqual(group_large_dirs(paths), {"":paths})
502
503 def test_no_group_no_parents_with_others(self):
504 paths = frozenset(("a/1", "a/2", "a/3", "a/4"))
505 self.assertEqual(group_large_dirs(paths), {"":paths})
506
507 def test_group_large_and_parents_with_others(self):
508 paths = frozenset(("a/1", "a/2", "a/3", "a/4", "b"))
509 self.assertEqual(group_large_dirs(paths),
510 {'': set(['a', 'b']),
511 'a': set(['a/1', 'a/2', 'a/3', 'a/4'])})
512
513 def test_group_container(self):
514 paths = frozenset(("a/1", "a/2", "a/3", "a", ))
515 self.assertEqual(group_large_dirs(paths),
516 {'': set(['a']),
517 'a': set(['a/1', 'a/2', 'a/3'])})
518
519 def test_no_paths(self):
520 paths = frozenset()
521 self.assertEqual(group_large_dirs(paths),
522 {'': set([])})
523
524 def test_group_deeper_dir(self):
525 paths = frozenset(("a/b/1", "a/b/2", "a/b/3", "a/b/4", "c"))
526 self.assertEqual(group_large_dirs(paths),
527 {'': set(['a/b', 'c']),
528 'a/b': set(['a/b/1', 'a/b/2', 'a/b/3', 'a/b/4'])})
529
530 def test_subdir_included(self):
531 paths = frozenset([
532 'b',
533 'b/1',
534 'b/2',
535 'b/3',
536 'b/4',
537 'b/c',
538 'b/c/1',
539 'b/c/2',
540 'b/c/3',
541 'b/c/4',
542 ])
543 self.assertEqual(group_large_dirs(paths),
544 {'': set(['b']),
545 'b': set(['b/1', 'b/2', 'b/3', 'b/4', 'b/c']),
546 'b/c': set(['b/c/1', 'b/c/2', 'b/c/3', 'b/c/4'])})
547
548 def test_bug_580798(self):
549 # Test for Bug #580798
550 paths = frozenset(('a',
551 'a/b1/c1',
552 'a/b2/d1', 'a/b2/d2', 'a/b2/d3', 'a/b2/d4',
553 ))
554 self.assertEqual(group_large_dirs(paths),
555 {'': set(['a']),
556 'a': set(['a/b1/c1', 'a/b2']),
557 'a/b2': set(['a/b2/d1', 'a/b2/d2', 'a/b2/d3', 'a/b2/d4']),
558 })