Skip to content

Commit 852b168

Browse files
authored
Merge pull request #1962 from dbcli/RW/ensure-distinct-batch-logfile
Ensure `--batch` and `--logfile` are distinct files
2 parents e610e75 + 9533565 commit 852b168

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Features
1818
* Add `--warn-batch` flag, which is off by default.
1919

2020

21+
Bug Fixes
22+
---------
23+
* Ensure that `--batch` and `--logfile` files are distinct.
24+
25+
2126
Documentation
2227
---------
2328
* Show section when recommending `default_character_set` setting.

mycli/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ def preprocess_cli_args(
333333
click.secho('Error: --batch and --checkpoint must be different files.', err=True, fg='red')
334334
sys.exit(1)
335335

336+
if (
337+
cli_args.logfile
338+
and os.path.exists(cli_args.logfile.name)
339+
and cli_args.batch
340+
and cli_args.batch != '-'
341+
and os.path.exists(cli_args.batch)
342+
):
343+
if os.path.samefile(cli_args.batch, cli_args.logfile.name):
344+
click.secho('Error: --batch and --logfile must be different files.', err=True, fg='red')
345+
sys.exit(1)
346+
336347
if cli_args.verbose and cli_args.quiet:
337348
click.secho('Error: --verbose and --quiet are incompatible.', err=True, fg='red')
338349
sys.exit(1)

test/pytests/test_main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,26 @@ def test_preprocess_cli_args_rejects_same_batch_and_checkpoint_file(
24512451
assert 'Error: --batch and --checkpoint must be different files.' in capsys.readouterr().err
24522452

24532453

2454+
def test_preprocess_cli_args_rejects_same_batch_and_logfile(
2455+
capsys: pytest.CaptureFixture[str],
2456+
tmp_path: Path,
2457+
) -> None:
2458+
batch_path = tmp_path / 'batch.sql'
2459+
batch_path.write_text('select 1;\n', encoding='utf-8')
2460+
cli_args = CliArgs()
2461+
cli_args.batch = str(batch_path)
2462+
cli_args.logfile = batch_path.open('a', encoding='utf-8') # type: ignore[assignment]
2463+
2464+
try:
2465+
with pytest.raises(SystemExit) as excinfo:
2466+
preprocess_cli_args(cli_args, valid_connection_scheme)
2467+
finally:
2468+
cli_args.logfile.close()
2469+
2470+
assert excinfo.value.code == 1
2471+
assert 'Error: --batch and --logfile must be different files.' in capsys.readouterr().err
2472+
2473+
24542474
def test_preprocess_cli_args_rejects_verbose_and_quiet(capsys: pytest.CaptureFixture[str]) -> None:
24552475
cli_args = CliArgs()
24562476
cli_args.verbose = 1

0 commit comments

Comments
 (0)