From 95335653052b7fc1136bf1cbaecbc0aec4dc7743 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Tue, 23 Jun 2026 06:34:15 -0400 Subject: [PATCH] ensure --batch and --logfile are distinct files Motivation: to avoid some kind of infinite loop, in which mycli reads from a file which is constantly being written to. --- changelog.md | 5 +++++ mycli/main.py | 11 +++++++++++ test/pytests/test_main.py | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/changelog.md b/changelog.md index f517bf2e..146bc907 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,11 @@ Features * Add `--warn-batch` flag, which is off by default. +Bug Fixes +--------- +* Ensure that `--batch` and `--logfile` files are distinct. + + Documentation --------- * Show section when recommending `default_character_set` setting. diff --git a/mycli/main.py b/mycli/main.py index 4a35d641..28a7f403 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -333,6 +333,17 @@ def preprocess_cli_args( click.secho('Error: --batch and --checkpoint must be different files.', err=True, fg='red') sys.exit(1) + if ( + cli_args.logfile + and os.path.exists(cli_args.logfile.name) + and cli_args.batch + and cli_args.batch != '-' + and os.path.exists(cli_args.batch) + ): + if os.path.samefile(cli_args.batch, cli_args.logfile.name): + click.secho('Error: --batch and --logfile must be different files.', err=True, fg='red') + sys.exit(1) + if cli_args.verbose and cli_args.quiet: click.secho('Error: --verbose and --quiet are incompatible.', err=True, fg='red') sys.exit(1) diff --git a/test/pytests/test_main.py b/test/pytests/test_main.py index 7c5e14e8..a6a22562 100644 --- a/test/pytests/test_main.py +++ b/test/pytests/test_main.py @@ -2451,6 +2451,26 @@ def test_preprocess_cli_args_rejects_same_batch_and_checkpoint_file( assert 'Error: --batch and --checkpoint must be different files.' in capsys.readouterr().err +def test_preprocess_cli_args_rejects_same_batch_and_logfile( + capsys: pytest.CaptureFixture[str], + tmp_path: Path, +) -> None: + batch_path = tmp_path / 'batch.sql' + batch_path.write_text('select 1;\n', encoding='utf-8') + cli_args = CliArgs() + cli_args.batch = str(batch_path) + cli_args.logfile = batch_path.open('a', encoding='utf-8') # type: ignore[assignment] + + try: + with pytest.raises(SystemExit) as excinfo: + preprocess_cli_args(cli_args, valid_connection_scheme) + finally: + cli_args.logfile.close() + + assert excinfo.value.code == 1 + assert 'Error: --batch and --logfile must be different files.' in capsys.readouterr().err + + def test_preprocess_cli_args_rejects_verbose_and_quiet(capsys: pytest.CaptureFixture[str]) -> None: cli_args = CliArgs() cli_args.verbose = 1