From ba5adb33c0c1dbbd59fdc1b2f0f10af07a87f79c Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Mon, 9 Oct 2023 00:43:27 +0300 Subject: [PATCH] Fix a bug in "Change Selection" Fix a bug in the "Change Selection" action, whereby changing your selection and picking files from another directory results in: "Dangerzone does not support adding documents from multiple locations. The newly added documents were ignored." To fix this, change the output directory when we change selection as well. --- dangerzone/gui/main_window.py | 20 ++++++++------------ tests/gui/test_main_window.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 408f109..7b398a7 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -488,24 +488,20 @@ class ContentWidget(QtWidgets.QWidget): ).exec_() return - # Get previously selected documents - self.dangerzone.clear_documents() - self.documents_list.clear() - - # Assumed all files in batch are in the same directory - first_doc = docs[0] - output_dir = os.path.dirname(first_doc.input_filename) - if not self.dangerzone.output_dir: - self.dangerzone.output_dir = output_dir - elif self.dangerzone.output_dir != output_dir: + # Ensure all files in batch are in the same directory + dirnames = {os.path.dirname(doc.input_filename) for doc in docs} + if len(dirnames) > 1: Alert( self.dangerzone, message="Dangerzone does not support adding documents from multiple locations.\n\n The newly added documents were ignored.", has_cancel=False, ).exec_() return - else: - self.dangerzone.output_dir = output_dir + + # Clear previously selected documents + self.dangerzone.clear_documents() + self.documents_list.clear() + self.dangerzone.output_dir = list(dirnames)[0] for doc in docs: self.dangerzone.add_document(doc) diff --git a/tests/gui/test_main_window.py b/tests/gui/test_main_window.py index 161372b..d7fb66a 100644 --- a/tests/gui/test_main_window.py +++ b/tests/gui/test_main_window.py @@ -1,3 +1,6 @@ +import os +import pathlib +import shutil import time import typing @@ -335,6 +338,7 @@ def test_change_document_button( mocker: MockerFixture, sample_pdf: str, sample_doc: str, + tmp_path: pathlib.Path, ) -> None: # Setup first doc selection file_dialog_mock = mocker.MagicMock() @@ -350,7 +354,9 @@ def test_change_document_button( file_dialog_mock.accept() # Setup doc change - file_dialog_mock.selectedFiles.return_value = (sample_doc,) + shutil.copy(sample_doc, tmp_path) + tmp_sample_doc = tmp_path / os.path.basename(sample_doc) + file_dialog_mock.selectedFiles.return_value = (tmp_sample_doc,) # When clicking on "select docs" button with qtbot.waitSignal(content_widget.documents_added): @@ -370,4 +376,4 @@ def test_change_document_button( for doc in content_widget.dangerzone.get_unconverted_documents() ] assert len(docs) is 1 - assert docs[0] == sample_doc + assert docs[0] == str(tmp_sample_doc)