|
| 1 | +"""Tests for set_config_filename and set_config_content. |
| 2 | +
|
| 3 | +Both functions must run before liblsl's config is loaded (on first use), so |
| 4 | +the effect-verifying tests run in a fresh Python subprocess. In-process smoke |
| 5 | +tests only verify that the call path works — they may be no-ops if liblsl has |
| 6 | +already initialized in the current process. |
| 7 | +""" |
| 8 | + |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import textwrap |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +import pylsl |
| 16 | + |
| 17 | + |
| 18 | +def test_set_config_content_accepts_string(): |
| 19 | + # Safe to call even after init — content is simply ignored once liblsl has loaded. |
| 20 | + pylsl.set_config_content("[lab]\nSessionID = ignored_after_init\n") |
| 21 | + |
| 22 | + |
| 23 | +def test_set_config_filename_accepts_string(): |
| 24 | + pylsl.set_config_filename("/nonexistent/path/to/lsl.cfg") |
| 25 | + |
| 26 | + |
| 27 | +def test_set_config_content_rejects_non_string(): |
| 28 | + with pytest.raises(AttributeError): |
| 29 | + pylsl.set_config_content(b"not a str") |
| 30 | + |
| 31 | + |
| 32 | +def test_set_config_filename_rejects_non_string(): |
| 33 | + with pytest.raises(AttributeError): |
| 34 | + pylsl.set_config_filename(12345) |
| 35 | + |
| 36 | + |
| 37 | +def _run_in_subprocess(script: str) -> str: |
| 38 | + result = subprocess.run( |
| 39 | + [sys.executable, "-c", script], |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + check=True, |
| 43 | + ) |
| 44 | + return result.stdout.strip() |
| 45 | + |
| 46 | + |
| 47 | +def test_set_config_content_applies_session_id(): |
| 48 | + """Setting content before any LSL call should update the session id that |
| 49 | + outlets inherit from the library config.""" |
| 50 | + session_id = "pytest_content_session" |
| 51 | + script = textwrap.dedent(f""" |
| 52 | + import pylsl |
| 53 | + pylsl.set_config_content('[lab]\\nSessionID = {session_id}\\n') |
| 54 | + info = pylsl.StreamInfo('T', 'EEG', 1, 100, 'float32', 'src') |
| 55 | + outlet = pylsl.StreamOutlet(info) |
| 56 | + print(outlet.get_info().session_id()) |
| 57 | + """) |
| 58 | + assert _run_in_subprocess(script) == session_id |
| 59 | + |
| 60 | + |
| 61 | +def test_set_config_filename_applies_session_id(tmp_path): |
| 62 | + session_id = "pytest_filename_session" |
| 63 | + cfg = tmp_path / "lsl.cfg" |
| 64 | + cfg.write_text(f"[lab]\nSessionID = {session_id}\n") |
| 65 | + script = textwrap.dedent(f""" |
| 66 | + import pylsl |
| 67 | + pylsl.set_config_filename({str(cfg)!r}) |
| 68 | + info = pylsl.StreamInfo('T', 'EEG', 1, 100, 'float32', 'src') |
| 69 | + outlet = pylsl.StreamOutlet(info) |
| 70 | + print(outlet.get_info().session_id()) |
| 71 | + """) |
| 72 | + assert _run_in_subprocess(script) == session_id |
0 commit comments