Skip to content

Commit f3da90e

Browse files
committed
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.
1 parent be011eb commit f3da90e

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
@@ -12,6 +12,11 @@ Breaking Changes
1212
* Remove support for `.myclirc` files in the current working directory.
1313

1414

15+
Bug Fixes
16+
---------
17+
* Ensure that `--batch` and `--logfile` files are distinct.
18+
19+
1520
Documentation
1621
---------
1722
* 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
@@ -327,6 +327,17 @@ def preprocess_cli_args(
327327
click.secho('Error: --batch and --checkpoint must be different files.', err=True, fg='red')
328328
sys.exit(1)
329329

330+
if (
331+
cli_args.logfile
332+
and os.path.exists(cli_args.logfile.name)
333+
and cli_args.batch
334+
and cli_args.batch != '-'
335+
and os.path.exists(cli_args.batch)
336+
):
337+
if os.stat(cli_args.batch) == os.stat(cli_args.logfile.name):
338+
click.secho('Error: --batch and --logfile must be different files.', err=True, fg='red')
339+
sys.exit(1)
340+
330341
if cli_args.verbose and cli_args.quiet:
331342
click.secho('Error: --verbose and --quiet are incompatible.', err=True, fg='red')
332343
sys.exit(1)

test/pytests/test_main.py

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

24512451

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

0 commit comments

Comments
 (0)