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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
from pathlib import PurePath
import pytest
from PyQt6.QtCore import QDateTime, QItemSelectionModel, Qt
from PyQt6.QtWidgets import QMenu
import vorta.borg
import vorta.utils
import vorta.views.archive_tab
from vorta.store.models import ArchiveModel
from vorta.views.diff_result import (
ChangeType,
DiffData,
DiffResultDialog,
DiffTree,
FileType,
parse_diff_json,
parse_diff_lines,
)
from vorta.views.partials.treemodel import FileTreeModel
def setup_diff_result_window(qtbot, mocker, tab, borg_json_output, json_mock_file="diff_archives"):
"""Sets up the diff result window."""
stdout, stderr = borg_json_output(json_mock_file)
popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0)
mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result)
compat = vorta.utils.borg_compat
def check(feature_name):
if feature_name == 'DIFF_JSON_LINES':
return False
return vorta.utils.BorgCompatibility.check(compat, feature_name)
mocker.patch.object(vorta.utils.borg_compat, 'check', check)
selection_model: QItemSelectionModel = tab.archiveTable.selectionModel()
model = tab.archiveTable.model()
flags = QItemSelectionModel.SelectionFlag.Rows
flags |= QItemSelectionModel.SelectionFlag.Select
selection_model.select(model.index(0, 0), flags)
selection_model.select(model.index(1, 0), flags)
tab.diff_action()
qtbot.waitUntil(lambda: hasattr(tab, '_resultwindow'), **pytest._wait_defaults)
assert hasattr(tab, '_resultwindow')
@pytest.mark.parametrize(
'json_mock_file, folder_root', [('diff_archives', 'test'), ('diff_archives_dict_issue', 'Users')]
)
def test_archive_diff(qapp, qtbot, mocker, borg_json_output, json_mock_file, folder_root, archive_env):
"""Tests basic functionality of archive diff."""
main, tab = archive_env
setup_diff_result_window(qtbot, mocker, tab, borg_json_output, json_mock_file)
model = tab._resultwindow.treeView.model().sourceModel()
assert model.root.children[0].subpath == folder_root
assert tab._resultwindow.archiveNameLabel_1.text() == 'test-archive'
tab._resultwindow.accept()
def test_diff_item_copy(qapp, qtbot, mocker, borg_json_output, archive_env):
"""Tests copy action by row selection and when passed an index."""
main, tab = archive_env
setup_diff_result_window(qtbot, mocker, tab, borg_json_output)
# mock the clipboard to ensure no changes are made to it during testing
mocker.patch.object(qapp.clipboard(), "setMimeData")
clipboard_spy = mocker.spy(qapp.clipboard(), "setMimeData")
# test 'diff_item_copy()' by passing it an item to copy
index = tab._resultwindow.treeView.model().index(0, 0)
assert index is not None
tab._resultwindow.diff_item_copy(index)
clipboard_data = clipboard_spy.call_args[0][0]
assert clipboard_data.hasText()
assert clipboard_data.text() == "/test"
clipboard_spy.reset_mock()
# test 'diff_item_copy()' by selecting a row to copy
flags = QItemSelectionModel.SelectionFlag.Rows
flags |= QItemSelectionModel.SelectionFlag.Select
tab._resultwindow.treeView.selectionModel().select(tab._resultwindow.treeView.model().index(0, 0), flags)
tab._resultwindow.diff_item_copy()
clipboard_data = clipboard_spy.call_args[0][0]
assert clipboard_data.hasText()
assert clipboard_data.text() == "/test"
def test_treeview_context_menu(qapp, qtbot, mocker, borg_json_output, archive_env):
"""Tests the diff result window context menu for expected actions."""
main, tab = archive_env
setup_diff_result_window(qtbot, mocker, tab, borg_json_output)
# Load the context menu at the first result in window
pos = tab._resultwindow.treeView.visualRect(tab._resultwindow.treeView.model().index(0, 0)).center()
tab._resultwindow.treeview_context_menu(pos)
qtbot.waitUntil(lambda: tab._resultwindow.findChild(QMenu) is not None, **pytest._wait_defaults)
context_menu = tab._resultwindow.findChild(QMenu)
assert context_menu is not None
# assert the actions are available in the context menu
expected_actions = ['Copy', 'Expand recursively']
for action in expected_actions:
assert any(menu_actions.text() == action for menu_actions in context_menu.actions())
@pytest.mark.parametrize(
'line, expected',
[
(
'changed link some/changed/link',
('some/changed/link', FileType.LINK, ChangeType.CHANGED_LINK, 0, 0, None, None, None, None, None),
),
(
' +77.8 kB -77.8 kB some/changed/file',
(
'some/changed/file',
FileType.FILE,
ChangeType.MODIFIED,
2 * 77800,
0,
None,
None,
None,
None,
(77800, 77800),
),
),
(
' +77.8 kB -77.8 kB [-rw-rw-rw- -> -rw-r--r--] some/changed/file',
(
'some/changed/file',
FileType.FILE,
ChangeType.MODIFIED,
2 * 77800,
0,
('-rw-rw-rw-', '-rw-r--r--'),
None,
None,
None,
(77800, 77800),
),
),
(
'[-rw-rw-rw- -> -rw-r--r--] some/changed/file',
(
'some/changed/file',
FileType.FILE,
ChangeType.MODE,
0,
0,
('-rw-rw-rw-', '-rw-r--r--'),
None,
None,
None,
None,
),
),
(
'added directory some/changed/dir',
('some/changed/dir', FileType.DIRECTORY, ChangeType.ADDED, 0, 0, None, None, None, None, None),
),
(
'removed directory some/changed/dir',
('some/changed/dir', FileType.DIRECTORY, ChangeType.REMOVED_DIR, 0, 0, None, None, None, None, None),
),
# Example from https://github.com/borgbase/vorta/issues/521
(
'[user:user -> nfsnobody:nfsnobody] home/user/arrays/test.txt',
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
0,
0,
None,
('user', 'user', 'nfsnobody', 'nfsnobody'),
None,
None,
None,
),
),
# Very short owner change, to check stripping whitespace from file path
(
'[a:a -> b:b] home/user/arrays/test.txt',
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
0,
0,
None,
('a', 'a', 'b', 'b'),
None,
None,
None,
),
),
# All file-related changes in one test
(
' +77.8 kB -800 B [user:user -> nfsnobody:nfsnobody] [-rw-rw-rw- -> -rw-r--r--] home/user/arrays/test.txt',
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
77800 + 800,
77000,
('-rw-rw-rw-', '-rw-r--r--'),
('user', 'user', 'nfsnobody', 'nfsnobody'),
None,
None,
(77800, 800),
),
),
],
)
def test_archive_diff_parser(line, expected):
model = DiffTree()
model.setMode(model.DisplayMode.FLAT)
parse_diff_lines([line], model)
assert model.rowCount() == 1
item = model.index(0, 0).internalPointer()
assert item.path == PurePath(expected[0]).parts
assert item.data == DiffData(*expected[1:])
@pytest.mark.parametrize(
'line, expected',
[
(
{'path': 'some/changed/link', 'changes': [{'type': 'changed link'}]},
('some/changed/link', FileType.LINK, ChangeType.CHANGED_LINK, 0, 0, None, None, None, None, None),
),
(
{'path': 'some/changed/file', 'changes': [{'type': 'modified', 'added': 77800, 'removed': 77800}]},
(
'some/changed/file',
FileType.FILE,
ChangeType.MODIFIED,
2 * 77800,
0,
None,
None,
None,
None,
(77800, 77800),
),
),
(
{
'path': 'some/changed/file',
'changes': [
{'type': 'modified', 'added': 77800, 'removed': 800},
{'type': 'mode', 'old_mode': '-rw-rw-rw-', 'new_mode': '-rw-r--r--'},
],
},
(
'some/changed/file',
FileType.FILE,
ChangeType.MODIFIED,
77800 + 800,
77000,
('-rw-rw-rw-', '-rw-r--r--'),
None,
None,
None,
(77800, 800),
),
),
(
{
'path': 'some/changed/file',
'changes': [{'type': 'mode', 'old_mode': '-rw-rw-rw-', 'new_mode': '-rw-r--r--'}],
},
(
'some/changed/file',
FileType.FILE,
ChangeType.MODE,
0,
0,
('-rw-rw-rw-', '-rw-r--r--'),
None,
None,
None,
None,
),
),
(
{'path': 'some/changed/dir', 'changes': [{'type': 'added directory'}]},
('some/changed/dir', FileType.DIRECTORY, ChangeType.ADDED, 0, 0, None, None, None, None, None),
),
(
{'path': 'some/changed/dir', 'changes': [{'type': 'removed directory'}]},
('some/changed/dir', FileType.DIRECTORY, ChangeType.REMOVED_DIR, 0, 0, None, None, None, None, None),
),
# Example from https://github.com/borgbase/vorta/issues/521
(
{
'path': 'home/user/arrays/test.txt',
'changes': [
{
'type': 'owner',
'old_user': 'user',
'new_user': 'nfsnobody',
'old_group': 'user',
'new_group': 'nfsnobody',
}
],
},
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
0,
0,
None,
('user', 'user', 'nfsnobody', 'nfsnobody'),
None,
None,
None,
),
),
# Very short owner change, to check stripping whitespace from file path
(
{
'path': 'home/user/arrays/test.txt',
'changes': [{'type': 'owner', 'old_user': 'a', 'new_user': 'b', 'old_group': 'a', 'new_group': 'b'}],
},
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
0,
0,
None,
('a', 'a', 'b', 'b'),
None,
None,
None,
),
),
# Short ctime change
(
{
'path': 'home/user/arrays',
'changes': [
{
'new_ctime': '2023-04-01T17:23:14.104630',
'old_ctime': '2023-03-03T23:40:17.073948',
'type': 'ctime',
}
],
},
(
'home/user/arrays',
FileType.FILE,
ChangeType.MODIFIED,
0,
0,
None,
None,
(
QDateTime.fromString('2023-03-03T23:40:17.073948', Qt.DateFormat.ISODateWithMs),
QDateTime.fromString('2023-04-01T17:23:14.104630', Qt.DateFormat.ISODateWithMs),
),
None,
None,
),
),
# Short mtime change
(
{
'path': 'home/user/arrays',
'changes': [
{
'new_mtime': '2023-04-01T17:23:14.104630',
'old_mtime': '2023-03-03T23:40:17.073948',
'type': 'mtime',
}
],
},
(
'home/user/arrays',
FileType.FILE,
ChangeType.MODIFIED,
0,
0,
None,
None,
None,
(
QDateTime.fromString('2023-03-03T23:40:17.073948', Qt.DateFormat.ISODateWithMs),
QDateTime.fromString('2023-04-01T17:23:14.104630', Qt.DateFormat.ISODateWithMs),
),
None,
),
),
# All file-related changes in one test
(
{
'path': 'home/user/arrays/test.txt',
'changes': [
{'type': 'modified', 'added': 77800, 'removed': 77800},
{'type': 'mode', 'old_mode': '-rw-rw-rw-', 'new_mode': '-rw-r--r--'},
{
'type': 'owner',
'old_user': 'user',
'new_user': 'nfsnobody',
'old_group': 'user',
'new_group': 'nfsnobody',
},
{
'new_ctime': '2023-04-01T17:23:14.104630',
'old_ctime': '2023-03-03T23:40:17.073948',
'type': 'ctime',
},
{
'new_mtime': '2023-04-01T17:15:50.290565',
'old_mtime': '2023-03-05T00:24:00.359045',
'type': 'mtime',
},
],
},
(
'home/user/arrays/test.txt',
FileType.FILE,
ChangeType.OWNER,
2 * 77800,
0,
('-rw-rw-rw-', '-rw-r--r--'),
('user', 'user', 'nfsnobody', 'nfsnobody'),
(
QDateTime.fromString('2023-03-03T23:40:17.073948', Qt.DateFormat.ISODateWithMs),
QDateTime.fromString('2023-04-01T17:23:14.104630', Qt.DateFormat.ISODateWithMs),
),
(
QDateTime.fromString('2023-03-05T00:24:00.359045', Qt.DateFormat.ISODateWithMs),
QDateTime.fromString('2023-04-01T17:15:50.290565', Qt.DateFormat.ISODateWithMs),
),
(77800, 77800),
),
),
],
)
def test_archive_diff_json_parser(line, expected):
model = DiffTree()
model.setMode(model.DisplayMode.FLAT)
parse_diff_json([line], model)
assert model.rowCount() == 1
item = model.index(0, 0).internalPointer()
assert item.path == PurePath(expected[0]).parts
assert item.data == DiffData(*expected[1:])
@pytest.mark.parametrize(
"selection, expected_mode, expected_bCollapseAllEnabled",
[
(0, FileTreeModel.DisplayMode.TREE, True),
(1, FileTreeModel.DisplayMode.SIMPLIFIED_TREE, True),
(2, FileTreeModel.DisplayMode.FLAT, False),
],
)
def test_change_display_mode(selection: int, expected_mode, expected_bCollapseAllEnabled):
dialog = DiffResultDialog(ArchiveModel(), ArchiveModel(), DiffTree())
dialog.change_display_mode(selection)
assert dialog.model.mode == expected_mode
assert dialog.bCollapseAll.isEnabled() == expected_bCollapseAllEnabled
|