Refactor our tests that check if we are in a container

This commit is contained in:
James R. Barlow 2024-10-27 11:55:22 -07:00
parent a67a11e61c
commit 18b59c57b4
No known key found for this signature in database
GPG Key ID: E54A300D567E1260
6 changed files with 26 additions and 24 deletions

View File

@ -28,7 +28,13 @@ from ocrmypdf.exceptions import (
MissingDependencyError, MissingDependencyError,
OutputFileAccessError, OutputFileAccessError,
) )
from ocrmypdf.helpers import is_file_writable, monotonic, safe_symlink from ocrmypdf.helpers import (
is_file_writable,
monotonic,
running_in_docker,
running_in_snap,
safe_symlink,
)
from ocrmypdf.subprocess import check_external_program from ocrmypdf.subprocess import check_external_program
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -237,18 +243,6 @@ def check_options(options: Namespace, plugin_manager: PluginManager) -> None:
_check_plugin_options(options, plugin_manager) _check_plugin_options(options, plugin_manager)
def _in_docker():
return Path('/.dockerenv').exists()
def _in_snap():
try:
cgroup_text = Path('/proc/self/cgroup').read_text()
return 'snap.ocrmypdf' in cgroup_text
except FileNotFoundError:
return False
def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str]: def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str]:
if options.input_file == '-': if options.input_file == '-':
# stdin # stdin
@ -272,7 +266,7 @@ def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str]
return target, os.fspath(options.input_file) return target, os.fspath(options.input_file)
except FileNotFoundError as e: except FileNotFoundError as e:
msg = f"File not found - {options.input_file}" msg = f"File not found - {options.input_file}"
if _in_docker(): # pragma: no cover if running_in_docker(): # pragma: no cover
msg += ( msg += (
"\nDocker cannot access your working directory unless you " "\nDocker cannot access your working directory unless you "
"explicitly share it with the Docker container and set up" "explicitly share it with the Docker container and set up"
@ -282,7 +276,7 @@ def create_input_file(options: Namespace, work_folder: Path) -> tuple[Path, str]
"\tdocker run -i --rm jbarlow83/ocrmypdf - - <input.pdf >output.pdf" "\tdocker run -i --rm jbarlow83/ocrmypdf - - <input.pdf >output.pdf"
"\n" "\n"
) )
elif _in_snap(): # pragma: no cover elif running_in_snap(): # pragma: no cover
msg += ( msg += (
"\nSnap applications cannot access files outside of " "\nSnap applications cannot access files outside of "
"your home directory unless you explicitly allow it. " "your home directory unless you explicitly allow it. "

View File

@ -335,3 +335,17 @@ def pikepdf_enable_mmap() -> None:
) )
except AttributeError: except AttributeError:
log.debug("pikepdf mmap not available") log.debug("pikepdf mmap not available")
def running_in_docker() -> bool:
"""Returns True if we seem to be running in a Docker container."""
return Path('/.dockerenv').exists()
def running_in_snap() -> bool:
"""Returns True if we seem to be running in a Snap container."""
try:
cgroup_text = Path('/proc/self/cgroup').read_text()
return 'snap.ocrmypdf' in cgroup_text
except FileNotFoundError:
return False

View File

@ -24,11 +24,6 @@ def is_macos():
return platform.system() == 'Darwin' return platform.system() == 'Darwin'
def running_in_docker():
# Docker creates a file named /.dockerenv in all supported versions
return Path('/.dockerenv').exists()
def have_unpaper(): def have_unpaper():
try: try:
unpaper.version() unpaper.version()

View File

@ -8,7 +8,7 @@ from subprocess import run
import pytest import pytest
from .conftest import running_in_docker from ocrmypdf.helpers import running_in_docker
pytestmark = pytest.mark.skipif( pytestmark = pytest.mark.skipif(
running_in_docker(), running_in_docker(),

View File

@ -13,8 +13,7 @@ import pytest
from packaging.version import Version from packaging.version import Version
from ocrmypdf import helpers from ocrmypdf import helpers
from ocrmypdf.helpers import running_in_docker
from .conftest import running_in_docker
needs_symlink = pytest.mark.skipif(os.name == 'nt', reason='needs posix symlink') needs_symlink = pytest.mark.skipif(os.name == 'nt', reason='needs posix symlink')
windows_only = pytest.mark.skipif(os.name != 'nt', reason="Windows test") windows_only = pytest.mark.skipif(os.name != 'nt', reason="Windows test")

View File

@ -18,6 +18,7 @@ from PIL import Image
import ocrmypdf import ocrmypdf
from ocrmypdf._exec import tesseract from ocrmypdf._exec import tesseract
from ocrmypdf.exceptions import ExitCode, MissingDependencyError from ocrmypdf.exceptions import ExitCode, MissingDependencyError
from ocrmypdf.helpers import running_in_docker
from ocrmypdf.pdfa import file_claims_pdfa from ocrmypdf.pdfa import file_claims_pdfa
from ocrmypdf.pdfinfo import Colorspace, Encoding, PdfInfo from ocrmypdf.pdfinfo import Colorspace, Encoding, PdfInfo
from ocrmypdf.subprocess import get_version from ocrmypdf.subprocess import get_version
@ -29,7 +30,6 @@ from .conftest import (
is_macos, is_macos,
run_ocrmypdf, run_ocrmypdf,
run_ocrmypdf_api, run_ocrmypdf_api,
running_in_docker,
) )
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name