diff --git a/src/ocrmypdf/_validation.py b/src/ocrmypdf/_validation.py index 82a9cc35..a3dd7448 100644 --- a/src/ocrmypdf/_validation.py +++ b/src/ocrmypdf/_validation.py @@ -112,6 +112,10 @@ def check_options_sidecar(options): "--sidecar filename must be specified when output file is stdout." ) options.sidecar = options.output_file + '.txt' + if options.sidecar == options.input_file or options.sidecar == options.output_file: + raise BadArgsError( + "--sidecar file must be different from the input and output files" + ) def check_options_preprocessing(options): diff --git a/src/ocrmypdf/cli.py b/src/ocrmypdf/cli.py index 24d5e036..204b7b31 100644 --- a/src/ocrmypdf/cli.py +++ b/src/ocrmypdf/cli.py @@ -167,7 +167,8 @@ Online documentation is located at: metavar='FILE', help="Generate sidecar text files that contain the same text recognized " "by Tesseract. This may be useful for building a OCR text database. " - "If FILE is omitted, the sidecar file be named {output_file}.txt " + "If FILE is omitted, the sidecar file be named {output_file}.txt; the next " + "argument must NOT be the name of the input PDF. " "If FILE is set to '-', the sidecar is written to stdout (a " "convenient way to preview OCR quality). The output file and sidecar " "may not both use stdout at the same time.", diff --git a/tests/test_validation.py b/tests/test_validation.py index 6eb53e48..3393fd9f 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -18,6 +18,8 @@ from ocrmypdf.cli import get_parser from ocrmypdf.exceptions import BadArgsError, MissingDependencyError from ocrmypdf.pdfinfo import PdfInfo +run_ocrmypdf_api = pytest.helpers.run_ocrmypdf_api + def make_opts_pm(input_file='a.pdf', output_file='b.pdf', language='eng', **kwargs): if language is not None: @@ -270,3 +272,9 @@ def test_two_languages(): *make_opts_pm(language='fakelang1+fakelang2'), {'fakelang1', 'fakelang2'} ) mock.assert_called() + + +def test_sidecar_equals_output(resources, no_outpdf): + op = no_outpdf + with pytest.raises(BadArgsError, match=r'--sidecar'): + run_ocrmypdf_api(resources / 'trivial.pdf', op, '--sidecar', op)