mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
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.
This commit is contained in:
parent
8b3739707d
commit
699258543a
3 changed files with 15 additions and 1 deletions
|
@ -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:
|
||||
|
|
|
@ -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."""
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue