From 699258543a4cf3ed0817f528ec5a268462d06cb5 Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Wed, 16 Nov 2022 18:11:45 +0200 Subject: [PATCH] Fail if a provided suffix cannot be applied If a user has provided an output filename for a document, then we should no longer accept suffixes. The reason is that we can't do something meaningful with it, as we can't alter the provided output filename. The proper behavior is to reject this action with an exception. Note that this acts more of a safeguard, since (currently) there is no path where a user may add a suffix to a document that already has an output filename. --- dangerzone/document.py | 5 ++++- dangerzone/errors.py | 7 +++++++ tests/test_document.py | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/dangerzone/document.py b/dangerzone/document.py index 415b92d..ca004e8 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -109,7 +109,10 @@ class Document: @suffix.setter def suffix(self, suf: str) -> None: - self._suffix = suf + if self._output_filename is None: + self._suffix = suf + else: + raise errors.SuffixNotApplicableException() @property def default_output_filename(self) -> str: diff --git a/dangerzone/errors.py b/dangerzone/errors.py index e012235..0c2d3dd 100644 --- a/dangerzone/errors.py +++ b/dangerzone/errors.py @@ -71,6 +71,13 @@ class OutputDirIsNotDirException(DocumentFilenameException): super().__init__("Specified output directory is actually not a directory") +class SuffixNotApplicableException(DocumentFilenameException): + """Exception for when the suffix cannot be applied to the output filename.""" + + def __init__(self) -> None: + super().__init__("Cannot set a suffix after setting an output filename") + + def handle_document_errors(func: F) -> F: """Log document-related errors and exit gracefully.""" diff --git a/tests/test_document.py b/tests/test_document.py index 264bcdd..1cff181 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -113,6 +113,10 @@ def test_set_output_filename_suffix(sample_doc: str) -> None: d.suffix = safe_extension assert d.output_filename.endswith(safe_extension) + d.output_filename = "something_else.pdf" + with pytest.raises(errors.SuffixNotApplicableException) as e: + d.suffix = "-new-trusted.pdf" + def test_is_unconverted_by_default(sample_doc: str) -> None: d = Document(sample_doc)