fix: validate library and data paths at the application boundary
This commit is contained in:
1 parent
47c2cfecc6
commit
6d2b6de027
10 files changed
+336
-36
No files matched your search
@@ -1,11 +1,94 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize('filename, contents, code', [
|
||||
('empty.lmc', b'', 'INPUT_EMPTY'),
|
||||
('missing.lmc', None, 'INPUT_MISSING'),
|
||||
('library.txt', b'library', 'INPUT_SUFFIX'),
|
||||
])
|
||||
def test_explicit_cli_block_validates_lmc_before_dispatch(tmp_path, monkeypatch, capsys, filename, contents, code):
|
||||
from cbb_refresh_verify import application, verify_runner
|
||||
|
||||
path = tmp_path / filename
|
||||
if contents is not None:
|
||||
path.write_bytes(contents)
|
||||
monkeypatch.setattr(verify_runner, 'run_batch', lambda *a, **kw: pytest.fail('invalid input dispatched'))
|
||||
|
||||
assert application.main(['--batch-lmc', str(path), '--block', 'A',
|
||||
'--data-root', str(tmp_path / 'data')]) == 1
|
||||
assert code in capsys.readouterr().err
|
||||
|
||||
|
||||
def test_explicit_cli_block_reports_unreadable_lmc(tmp_path, monkeypatch, capsys):
|
||||
from cbb_refresh_verify import application, verify_runner
|
||||
|
||||
path = tmp_path / 'library.lmc'
|
||||
path.write_bytes(b'library')
|
||||
original_open = Path.open
|
||||
|
||||
def open_path(candidate, *args, **kwargs):
|
||||
if candidate == path:
|
||||
raise PermissionError('denied')
|
||||
return original_open(candidate, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(Path, 'open', open_path)
|
||||
monkeypatch.setattr(verify_runner, 'run_batch', lambda *a, **kw: pytest.fail('unreadable input dispatched'))
|
||||
assert application.main(['--batch-lmc', str(path), '--block', 'A',
|
||||
'--data-root', str(tmp_path / 'data')]) == 1
|
||||
assert 'INPUT_PERMISSION' in capsys.readouterr().err
|
||||
|
||||
|
||||
def test_cli_unwritable_data_root_returns_input_exit_code(tmp_path, capsys):
|
||||
from cbb_refresh_verify import application
|
||||
|
||||
path = tmp_path / 'data'
|
||||
path.touch()
|
||||
assert application.main(['--batch-lmc', str(tmp_path / 'library.lmc'), '--data-root', str(path)]) == 1
|
||||
error = capsys.readouterr().err
|
||||
assert 'DATA_ROOT_UNWRITABLE' in error and str(path) in error
|
||||
assert 'Traceback' not in error
|
||||
|
||||
|
||||
def test_windowed_cli_can_write_to_redirected_windows_stderr(tmp_path):
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
path = tmp_path / 'data'
|
||||
path.touch()
|
||||
command = (
|
||||
'import sys; from cbb_refresh_verify.application import main; '
|
||||
'sys.stderr = None; raise SystemExit(main(sys.argv[1:]))'
|
||||
)
|
||||
result = subprocess.run([sys.executable, '-X', 'utf8', '-c', command,
|
||||
'--batch-lmc', str(tmp_path / 'library.lmc'), '--data-root', str(path)],
|
||||
capture_output=True, encoding='utf-8', timeout=15,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
assert result.returncode == 1
|
||||
assert 'DATA_ROOT_UNWRITABLE' in result.stderr and str(path) in result.stderr
|
||||
|
||||
|
||||
def test_desktop_unwritable_data_root_shows_chinese_startup_error(tmp_path, monkeypatch):
|
||||
import win32api
|
||||
from cbb_refresh_verify import application
|
||||
|
||||
path = tmp_path / 'data'
|
||||
path.touch()
|
||||
messages = []
|
||||
monkeypatch.setattr(win32api, 'MessageBox', lambda parent, detail, title, flags: messages.append((title, detail)))
|
||||
|
||||
assert application.main(['--data-root', str(path)]) == 1
|
||||
assert len(messages) == 1
|
||||
assert '启动失败' in messages[0][0]
|
||||
assert '无法写入数据目录' in messages[0][1] and str(path) in messages[0][1]
|
||||
|
||||
|
||||
def test_cli_signal_requests_cooperative_batch_stop(tmp_path, monkeypatch):
|
||||
import signal
|
||||
from cbb_refresh_verify import application, verify_runner
|
||||
library = tmp_path/'center.lmc'
|
||||
library.touch()
|
||||
library.write_bytes(b'library')
|
||||
old = signal.getsignal(signal.SIGINT)
|
||||
def run(requests, *, stop):
|
||||
signal.getsignal(signal.SIGINT)(signal.SIGINT, None)
|
||||
@@ -65,6 +148,7 @@ def test_batch_cli_acquires_unc_lease_before_directory_scan(tmp_path, monkeypatc
|
||||
monkeypatch.setattr(application, 'configure', lambda *_args: type('C', (), {
|
||||
'root': tmp_path,
|
||||
'logger': type('L', (), {'info': lambda *a, **k: None,
|
||||
'warning': lambda *a, **k: None,
|
||||
'exception': lambda *a, **k: None,
|
||||
'handlers': ()})(),
|
||||
})())
|
||||
|
||||
Reference in new issue
Block a user