Add dummy isolation provider to CLI

When enabled, the conversion part does nothing but print some simulated
output. This can be useful for testing non-conversion code (e.g. GUI).

Activated with the hidden flag --unsafe-dummy-conversion.
This commit is contained in:
deeplow 2023-01-03 14:06:27 +00:00
parent 538df18709
commit da0cb6b3c5
No known key found for this signature in database
GPG key ID: 577982871529A52A
5 changed files with 30 additions and 7 deletions

View file

@ -33,6 +33,9 @@ def print_header(s: str) -> None:
flag_value=True, flag_value=True,
help=f"Archives the unsafe version in a subdirectory named '{ARCHIVE_SUBDIR}'", help=f"Archives the unsafe version in a subdirectory named '{ARCHIVE_SUBDIR}'",
) )
@click.option(
"--unsafe-dummy-conversion", "dummy_conversion", flag_value=True, hidden=True
)
@click.argument( @click.argument(
"filenames", "filenames",
required=True, required=True,
@ -47,9 +50,14 @@ def cli_main(
ocr_lang: Optional[str], ocr_lang: Optional[str],
filenames: List[str], filenames: List[str],
archive: bool, archive: bool,
dummy_conversion: bool,
) -> None: ) -> None:
setup_logging() setup_logging()
dangerzone = DangerzoneCore()
if getattr(sys, "dangerzone_dev", False) and dummy_conversion:
dangerzone = DangerzoneCore(Dummy())
else:
dangerzone = DangerzoneCore(Container())
display_banner() display_banner()
if len(filenames) == 1 and output_filename: if len(filenames) == 1 and output_filename:

View file

@ -13,7 +13,7 @@ from PySide2 import QtCore, QtGui, QtWidgets
if platform.system() == "Linux": if platform.system() == "Linux":
from xdg.DesktopEntry import DesktopEntry from xdg.DesktopEntry import DesktopEntry
from ..isolation_provider.base import IsolationProvider from ..isolation_provider.container import Container
from ..logic import DangerzoneCore from ..logic import DangerzoneCore
from ..settings import Settings from ..settings import Settings
from ..util import get_resource_path from ..util import get_resource_path
@ -27,7 +27,7 @@ class DangerzoneGui(DangerzoneCore):
""" """
def __init__(self, app: QtWidgets.QApplication) -> None: def __init__(self, app: QtWidgets.QApplication) -> None:
super().__init__() super().__init__(isolation_provider=Container())
# Qt app # Qt app
self.app = app self.app = app

View file

@ -12,9 +12,9 @@ from typing import Callable, List, Optional
import appdirs import appdirs
import colorama import colorama
from . import errors, isolation_provider from . import errors
from .document import Document from .document import Document
from .isolation_provider.container import Container from .isolation_provider.base import IsolationProvider
from .settings import Settings from .settings import Settings
from .util import get_resource_path from .util import get_resource_path
@ -26,7 +26,7 @@ class DangerzoneCore(object):
Singleton of shared state / functionality throughout the app Singleton of shared state / functionality throughout the app
""" """
def __init__(self) -> None: def __init__(self, isolation_provider: IsolationProvider) -> None:
# Initialize terminal colors # Initialize terminal colors
colorama.init(autoreset=True) colorama.init(autoreset=True)
@ -42,7 +42,7 @@ class DangerzoneCore(object):
self.documents: List[Document] = [] self.documents: List[Document] = []
self.isolation_provider = Container() self.isolation_provider = isolation_provider
def add_document_from_filename( def add_document_from_filename(
self, self,

BIN
share/dummy_document.pdf Normal file

Binary file not shown.

View file

@ -263,6 +263,21 @@ class TestCliConversion(TestCliBasic):
assert os.path.exists(archived_doc_path) assert os.path.exists(archived_doc_path)
assert os.path.exists(safe_doc_path) assert os.path.exists(safe_doc_path)
def test_dummy_conversion(self, tmp_path: Path) -> None:
result = self.run_cli([self.sample_doc, "--unsafe-dummy-conversion"])
result.assert_success()
def test_dummy_conversion_bulk(self, tmp_path: Path) -> None:
filenames = ["1.pdf", "2.pdf", "3.pdf"]
file_paths = []
for filename in filenames:
doc_path = str(tmp_path / filename)
shutil.copyfile(self.sample_doc, doc_path)
file_paths.append(doc_path)
result = self.run_cli(["--unsafe-dummy-conversion", *file_paths])
result.assert_success()
class TestSecurity(TestCli): class TestSecurity(TestCli):
def test_suspicious_double_dash_file(self, tmp_path: Path) -> None: def test_suspicious_double_dash_file(self, tmp_path: Path) -> None: