Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
598 lines (491 loc) · 23 KB
/
Copy pathtest_cli.py
File metadata and controls
598 lines (491 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# SPDX-License-Identifier: Apache-2.0
"""Command-line and child-process gates for cabledyn-run, cabledyn-study, and cabledyn-deck."""
from __future__ import annotations
import hashlib
import json
import os
import subprocess
import sys
from pathlib import Path
import pytest
from cabledyn import (
CableDynDriver,
DeckFile,
DriverExecutionError,
StudyOutputError,
deck_cli,
generate_deck_cases,
run_study,
study_cli,
)
from cabledyn import cli as run_cli
from cabledyn import study as study_module
ROOT = Path(__file__).resolve().parents[2]
CHAIN = ROOT / "examples" / "chain_catenary_r3_100m.dat"
# ---------------------------------------------------------------------------
# CableDynDriver against a real child process
# ---------------------------------------------------------------------------
def test_driver_runs_a_real_child_process_in_the_deck_directory(fake_driver, deck):
result = CableDynDriver(fake_driver).run(deck, "results/base", timeout=60)
assert result.returncode == 0
assert result.main_output == deck.parent / "results" / "base.out"
assert result.static_output == deck.parent / "results" / "base.static.out"
assert "fake run complete" in result.stdout
history = result.read_main()
assert history.channels == ("Time(s)", "FairTen1", "AnchTen1")
assert history.values.shape == (201, 3)
assert result.read_static().line_ids == (1,)
log = (deck.parent / "results" / "argv.log").read_text(encoding="utf-8")
working_directory, deck_argument, _ = log.strip().split("|")
assert Path(working_directory) == deck.parent
assert Path(deck_argument) == deck
def test_driver_version_reads_the_banner_and_reports_failures(fake_driver, monkeypatch):
driver = CableDynDriver(fake_driver)
assert driver.version() == "CableDyn_driver fake 0.0.0"
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "bad-version")
with pytest.raises(DriverExecutionError, match="exit code 4") as caught:
driver.version()
assert caught.value.returncode == 4
assert "broken" in caught.value.stderr
@pytest.mark.parametrize("timeout", [0.0, -1.0, float("inf"), float("nan"), True, "soon"])
def test_driver_rejects_invalid_timeouts_before_starting(fake_driver, deck, timeout):
driver = CableDynDriver(fake_driver)
with pytest.raises(ValueError, match="timeout must be finite and positive"):
driver.run(deck, "base", timeout=timeout)
with pytest.raises(ValueError, match="timeout must be finite and positive"):
driver.version(timeout=timeout)
assert not (deck.parent / "argv.log").exists()
def test_driver_version_requires_a_timeout(fake_driver):
with pytest.raises(ValueError, match="timeout"):
CableDynDriver(fake_driver).version(timeout=None) # type: ignore[arg-type]
@pytest.mark.parametrize(
("mode", "message"),
[
("fail", "exit code 3: CableDyn_driver: deck line 3: synthetic failure"),
("partial-fail", "exit code 3"),
("no-output", "did not create"),
("bad-output", "invalid main output"),
],
)
def test_failed_overwrite_restores_the_previous_results(
fake_driver, deck, monkeypatch, mode, message
):
driver = CableDynDriver(fake_driver)
first = driver.run(deck, "base")
previous = {path.name: path.read_bytes() for path in deck.parent.glob("base*.out")}
assert set(previous) == {"base.out", "base.static.out"}
monkeypatch.setenv("FAKE_CABLEDYN_MODE", mode)
with pytest.raises(DriverExecutionError, match=message):
driver.run(deck, "base", overwrite=True)
restored = {path.name: path.read_bytes() for path in deck.parent.glob("base*.out")}
assert restored == previous
assert first.read_main().values.shape == (201, 3)
assert not list(deck.parent.glob(".base.previous-*"))
def test_successful_overwrite_replaces_and_removes_stale_auxiliary_files(
fake_driver, deck, monkeypatch
):
driver = CableDynDriver(fake_driver)
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "lines")
lines = driver.run(deck, "base")
assert lines.line_ids == (1,)
assert lines.read_line_positions(1).values.shape == (2, 7)
assert lines.read_line_tensions(1).values.shape == (2, 2)
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "ok")
rerun = driver.run(deck, "base", overwrite=True)
assert rerun.line_outputs == ()
assert not (deck.parent / "base.Line1.p.out").exists()
assert not list(deck.parent.glob(".base.previous-*"))
def test_interrupted_overwrite_restores_previous_results(fake_driver, deck, monkeypatch):
driver = CableDynDriver(fake_driver)
driver.run(deck, "base")
before = (deck.parent / "base.out").read_bytes()
def interrupted(*args, **kwargs):
Path(f"{deck.parent / 'base'}.out").write_text("partial", encoding="ascii")
raise KeyboardInterrupt
monkeypatch.setattr(subprocess, "run", interrupted)
with pytest.raises(KeyboardInterrupt):
driver.run(deck, "base", overwrite=True)
assert (deck.parent / "base.out").read_bytes() == before
def test_run_timeout_is_forwarded_as_a_float(tmp_path, deck, monkeypatch, fake_driver):
seen = {}
def fake_run(command, **kwargs):
seen.update(kwargs)
raise subprocess.TimeoutExpired(command, kwargs["timeout"], output=None, stderr=b"late")
monkeypatch.setattr(subprocess, "run", fake_run)
with pytest.raises(DriverExecutionError, match=r"2.5 s timeout") as caught:
CableDynDriver(fake_driver).run(deck, "base", timeout=2.5)
assert seen["timeout"] == 2.5
assert caught.value.stderr == "late"
assert caught.value.stdout == ""
def test_run_validates_paths(fake_driver, deck, tmp_path):
driver = CableDynDriver(fake_driver)
with pytest.raises(NotADirectoryError):
driver.run(deck, "base", cwd=tmp_path / "missing")
with pytest.raises(FileNotFoundError):
driver.run(tmp_path / "missing.dat", "base")
with pytest.raises(ValueError, match="file stem"):
driver.run(deck, ".")
with pytest.raises(ValueError, match=r"omit the .out"):
driver.run(deck, "base.out")
def test_driver_discovery_uses_environment_then_path(fake_driver, monkeypatch, tmp_path):
monkeypatch.setenv("CABLEDYN_DRIVER", str(fake_driver))
assert CableDynDriver().executable == fake_driver.resolve()
monkeypatch.delenv("CABLEDYN_DRIVER")
monkeypatch.setattr(
"shutil.which", lambda name: str(fake_driver) if name == "cabledyn" else None
)
assert CableDynDriver().executable == fake_driver.resolve()
def test_driver_result_line_helpers_validate_requests(fake_driver, deck, monkeypatch):
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "lines")
result = CableDynDriver(fake_driver).run(deck, "base")
for bad in (0, -1, True, 1.5):
with pytest.raises(ValueError, match="positive integer"):
result.read_line_positions(bad) # type: ignore[arg-type]
with pytest.raises(FileNotFoundError, match="Line 2"):
result.read_line_tensions(2)
no_static = type(result)(**{**result.__dict__, "static_output": None})
with pytest.raises(FileNotFoundError, match="static"):
no_static.read_static()
# ---------------------------------------------------------------------------
# cabledyn-run
# ---------------------------------------------------------------------------
def test_cabledyn_run_prints_the_main_output(fake_driver, deck, capsys):
assert (
run_cli.main([str(deck), "run1", "--executable", str(fake_driver), "--timeout", "60"]) == 0
)
assert capsys.readouterr().out.strip() == str(deck.parent / "run1.out")
@pytest.mark.parametrize(
"arguments",
[
["--timeout", "0"],
["--timeout", "nan"],
["--timeout", "-5"],
],
)
def test_cabledyn_run_rejects_bad_timeouts(fake_driver, deck, capsys, arguments):
with pytest.raises(SystemExit) as caught:
run_cli.main([str(deck), "run1", "--executable", str(fake_driver), *arguments])
assert caught.value.code == 1
assert "timeout must be finite and positive" in capsys.readouterr().err
def test_cabledyn_run_reports_native_failures_and_existing_outputs(
fake_driver, deck, capsys, monkeypatch
):
assert run_cli.main([str(deck), "run1", "--executable", str(fake_driver)]) == 0
with pytest.raises(SystemExit) as caught:
run_cli.main([str(deck), "run1", "--executable", str(fake_driver)])
assert caught.value.code == 1
assert "already exist" in capsys.readouterr().err
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "fail")
with pytest.raises(SystemExit) as caught:
run_cli.main([str(deck), "run1", "--executable", str(fake_driver), "--overwrite"])
assert caught.value.code == 1
assert "synthetic failure" in capsys.readouterr().err
assert (deck.parent / "run1.out").is_file()
def test_cabledyn_run_reports_a_missing_executable(deck, tmp_path, capsys):
with pytest.raises(SystemExit) as caught:
run_cli.main([str(deck), "run1", "--executable", str(tmp_path / "missing.exe")])
assert caught.value.code == 1
assert "could not locate CableDyn_driver" in capsys.readouterr().err
def test_console_scripts_run_as_modules(fake_driver, deck):
completed = subprocess.run(
[
sys.executable,
"-m",
"cabledyn.cli",
str(deck),
"module_run",
"--executable",
str(fake_driver),
],
capture_output=True,
text=True,
check=False,
timeout=120,
)
assert completed.returncode == 0, completed.stderr
assert completed.stdout.strip().endswith("module_run.out")
# ---------------------------------------------------------------------------
# cabledyn-study
# ---------------------------------------------------------------------------
def _manifest(tmp_path: Path, names=("case_a", "case_b")) -> Path:
source = tmp_path / "source.dat"
source.write_text("source deck\n", encoding="ascii")
rows = []
for name in names:
deck = tmp_path / f"{name}.dat"
deck.write_text(f"generated {name}\n", encoding="ascii")
rows.append(
{
"name": name,
"deck": str(deck),
"sha256": hashlib.sha256(deck.read_bytes()).hexdigest(),
}
)
manifest = tmp_path / "cases.json"
manifest.write_text(
json.dumps(
{
"schema": "cabledyn-deck-cases-v1",
"source": str(source),
"source_sha256": hashlib.sha256(source.read_bytes()).hexdigest(),
"cases": rows,
}
),
encoding="utf-8",
)
return manifest
def test_cabledyn_study_success_with_a_real_child_process(fake_driver, tmp_path, capsys):
manifest = _manifest(tmp_path)
code = study_cli.main(
[
str(manifest),
"--executable",
str(fake_driver),
"--jobs",
"2",
"--channel",
"FairTen1",
"--start",
"1",
"--stop",
"9",
"--timeout",
"120",
]
)
assert code == 0
out = capsys.readouterr().out
assert "case_a: completed" in out and "case_b: completed" in out
record = json.loads((tmp_path / "study-results" / "study.json").read_text(encoding="utf-8"))
assert record["solver_version"] == "CableDyn_driver fake 0.0.0"
assert record["period"] == {"start": 1.0, "stop": 9.0}
assert [case["status"] for case in record["cases"]] == ["completed", "completed"]
def test_cabledyn_study_failed_case_exits_one(fake_driver, tmp_path, capsys, monkeypatch):
monkeypatch.setenv("FAKE_CABLEDYN_MODE", "fail")
manifest = _manifest(tmp_path, names=("only",))
assert study_cli.main([str(manifest), "--executable", str(fake_driver)]) == 1
out = capsys.readouterr().out
assert "only: failed: CableDyn driver failed with exit code 3" in out
assert (tmp_path / "study-results" / "only.stderr.log").read_text(
encoding="utf-8"
).strip().splitlines() == [
"CableDyn_driver: deck line 3: synthetic failure",
"CableDyn_driver: ended with exit code 3",
]
def test_cabledyn_study_preflight_errors_exit_two(fake_driver, tmp_path, capsys):
manifest = _manifest(tmp_path)
Path(tmp_path / "case_a.dat").write_text("tampered\n", encoding="ascii")
with pytest.raises(SystemExit) as caught:
study_cli.main([str(manifest), "--executable", str(fake_driver)])
assert caught.value.code == 2
assert "SHA-256" in capsys.readouterr().err
assert not (tmp_path / "study-results").exists()
def test_cabledyn_study_artifact_write_failure_exits_three(
fake_driver, tmp_path, capsys, monkeypatch
):
manifest = _manifest(tmp_path, names=("only",))
def full_disk(*args, **kwargs):
raise OSError(28, "No space left on device")
monkeypatch.setattr(study_module, "_write_summary", full_disk)
with pytest.raises(SystemExit) as caught:
study_cli.main([str(manifest), "--executable", str(fake_driver)])
assert caught.value.code == 3
err = capsys.readouterr().err
assert "cases ran but" in err and "summary.csv" in err
assert (tmp_path / "study-results" / "only.out").is_file()
def test_study_manifest_write_failure_raises_study_output_error(fake_driver, tmp_path, monkeypatch):
manifest = _manifest(tmp_path, names=("only",))
original = study_module._atomic_text
def failing(path, text):
if path.name == "study.json":
raise PermissionError(13, "locked")
original(path, text)
monkeypatch.setattr(study_module, "_atomic_text", failing)
with pytest.raises(StudyOutputError, match=r"study.json") as caught:
run_study(manifest, executable=fake_driver)
assert isinstance(caught.value, OSError)
assert (tmp_path / "study-results" / "summary.csv").is_file()
def test_summary_writer_cleans_up_its_temporary_file(tmp_path, monkeypatch):
target = tmp_path / "summary.csv"
def broken(*args, **kwargs):
raise OSError("replace failed")
monkeypatch.setattr(study_module.os, "replace", broken)
with pytest.raises(OSError, match="replace failed"):
study_module._write_summary(target, ("a",), [{"a": 1}])
assert list(tmp_path.iterdir()) == []
# ---------------------------------------------------------------------------
# cabledyn-deck
# ---------------------------------------------------------------------------
def test_cabledyn_deck_validate_show_set_and_generate(tmp_path, capsys):
assert deck_cli.main(["validate", str(CHAIN)]) == 0
assert "valid:" in capsys.readouterr().out
assert deck_cli.main(["show", str(CHAIN)]) == 0
shown = capsys.readouterr().out
assert "line_types: 1" in shown and "outputs:" in shown
edited = tmp_path / "edited.dat"
assert deck_cli.main(["set", str(CHAIN), str(edited), "point.2.z", "-1e-3"]) == 0
assert DeckFile.read(edited).points[1].tokens[4] == "-0.001"
spec = tmp_path / "spec.json"
spec.write_text(json.dumps({"soft": {"option.kBot": 1.0e5}}), encoding="utf-8")
assert deck_cli.main(["generate", str(CHAIN), str(spec), str(tmp_path / "cases")]) == 0
assert capsys.readouterr().out.strip().endswith("soft.dat")
def test_cabledyn_deck_set_refuses_to_replace_its_source(tmp_path, capsys):
source = tmp_path / "base.dat"
source.write_bytes(CHAIN.read_bytes())
before = source.read_bytes()
with pytest.raises(SystemExit) as caught:
deck_cli.main(["set", str(source), str(source), "option.kBot", "2e5", "--overwrite"])
assert caught.value.code == 1
assert "must not replace the source deck" in capsys.readouterr().err
link = tmp_path / "alias.dat"
try:
os.link(source, link)
except OSError:
pytest.skip("hard links are not supported here")
with pytest.raises(SystemExit):
deck_cli.main(["set", str(source), str(link), "option.kBot", "2e5", "--overwrite"])
assert source.read_bytes() == before
def test_generate_refuses_a_hard_link_alias_of_the_source(tmp_path):
output = tmp_path / "cases"
output.mkdir()
source = output / "base.dat"
source.write_bytes(CHAIN.read_bytes())
try:
os.link(source, output / "alias.dat")
except OSError:
pytest.skip("hard links are not supported here")
with pytest.raises(ValueError, match="must not replace the source deck"):
generate_deck_cases(source, output, {"alias": {"option.kBot": 2.0e5}}, overwrite=True)
@pytest.mark.parametrize(
("arguments", "message"),
[
(["set", "{deck}", "{out}", "point.2.z", "null"], "not null or an object"),
(["set", "{deck}", "{out}", "nonsense", "1"], "unsupported deck selector"),
(["generate", "{deck}", "{spec}", "{out}"], "duplicate key"),
(["generate", "{deck}", "{list_spec}", "{out}"], "case spec must map"),
],
)
def test_cabledyn_deck_reports_errors(tmp_path, capsys, arguments, message):
spec = tmp_path / "spec.json"
spec.write_text('{"a": {"option.kBot": 1}, "a": {"option.kBot": 2}}', encoding="utf-8")
list_spec = tmp_path / "list.json"
list_spec.write_text('{"a": [1]}', encoding="utf-8")
values = {
"deck": str(CHAIN),
"out": str(tmp_path / "out.dat"),
"spec": str(spec),
"list_spec": str(list_spec),
}
with pytest.raises(SystemExit) as caught:
deck_cli.main([item.format(**values) for item in arguments])
assert caught.value.code == 1
assert message in capsys.readouterr().err
# ---------------------------------------------------------------------------
# DriverResult readers and discovery edge cases
# ---------------------------------------------------------------------------
def _result(tmp_path: Path, **overrides):
from cabledyn import DriverResult
fields = {
"executable": tmp_path / "driver",
"deck": tmp_path / "deck.dat",
"output_root": tmp_path / "run",
"main_output": tmp_path / "run.out",
"static_output": None,
"elements_output": None,
"line_outputs": (),
"rod_outputs": (),
"stdout": "",
"stderr": "",
"returncode": 0,
}
fields.update(overrides)
return DriverResult(**fields)
def test_driver_result_rejects_tables_of_the_wrong_kind(tmp_path):
from cabledyn import OutputFormatError
static = tmp_path / "run.static.out"
static.write_text("LineID Node ArcLength\n1 1 0\n", encoding="ascii")
history = tmp_path / "run.out"
history.write_text("Time(s) FairTen1\n0 1\n", encoding="ascii")
with pytest.raises(OutputFormatError, match="not a time history"):
_result(tmp_path, main_output=static).read_main()
with pytest.raises(OutputFormatError, match="not a line profile"):
_result(tmp_path, static_output=history).read_static()
positions = tmp_path / "run.Line1.p.out"
positions.write_text("Node X(m) Y(m)\n1 0 0\n", encoding="ascii") # no Z(m)
tensions = tmp_path / "run.Line1.t.out"
tensions.write_text("Segment Force(N)\n1 5\n", encoding="ascii") # no Tension(N)
result = _result(tmp_path, line_outputs=(positions, tensions, tmp_path / "run.Rod1.p.out"))
assert result.line_ids == (1,)
with pytest.raises(OutputFormatError, match="expected a line-position output"):
result.read_line_positions(1)
with pytest.raises(OutputFormatError, match="expected a line-tension output"):
result.read_line_tensions(1)
duplicate = _result(tmp_path, line_outputs=(positions, tmp_path / "xrun.Line1.p.out"))
with pytest.raises(OutputFormatError, match="multiple outputs"):
duplicate.read_line_positions(1)
def test_driver_discovery_reports_every_candidate(monkeypatch):
from cabledyn import DriverNotFoundError
monkeypatch.delenv("CABLEDYN_DRIVER", raising=False)
monkeypatch.setattr("shutil.which", lambda name: None)
with pytest.raises(
DriverNotFoundError, match="CABLEDYN_DRIVER, the bundled driver or PATH"
) as caught:
CableDynDriver()
assert "CableDyn_driver.exe" in str(caught.value) and "cabledyn" in str(caught.value)
assert str(Path("bin") / "CableDyn_driver") in str(caught.value)
def _fake_driver(path: Path) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("")
path.chmod(0o755)
return path
def test_bundled_driver_comes_before_path(monkeypatch, tmp_path):
# a wheel's own driver (matching its bundled library) wins over one on PATH
from cabledyn import driver as driver_module
bundled = _fake_driver(tmp_path / "pkg" / "bin" / "CableDyn_driver.exe")
on_path = _fake_driver(tmp_path / "path" / "CableDyn_driver.exe")
monkeypatch.delenv("CABLEDYN_DRIVER", raising=False)
monkeypatch.setattr(driver_module, "bundled_driver", lambda: bundled)
monkeypatch.setattr("shutil.which", lambda name: str(on_path))
assert driver_module._resolve_executable(None) == bundled
# without a bundled driver, PATH is searched as before
monkeypatch.setattr(
driver_module, "bundled_driver", lambda: tmp_path / "missing" / "CableDyn_driver.exe"
)
assert driver_module._resolve_executable(None) == on_path.resolve()
def test_cabledyn_driver_variable_comes_before_the_bundled_driver(monkeypatch, tmp_path):
from cabledyn import driver as driver_module
bundled = _fake_driver(tmp_path / "pkg" / "bin" / "CableDyn_driver.exe")
chosen = _fake_driver(tmp_path / "chosen" / "CableDyn_driver.exe")
monkeypatch.setenv("CABLEDYN_DRIVER", str(chosen))
monkeypatch.setattr(driver_module, "bundled_driver", lambda: bundled)
assert driver_module._resolve_executable(None) == chosen.resolve()
def test_posix_environment_merge_keeps_case_distinct_names(monkeypatch):
from cabledyn import driver as driver_module
monkeypatch.setattr(driver_module.os, "name", "posix")
monkeypatch.setattr(driver_module.os, "environ", {"PATH": "inherited"})
assert driver_module._merged_environment({"Path": "override"}) == {
"PATH": "inherited",
"Path": "override",
}
def test_environment_overrides_reach_the_child_process(fake_driver, deck):
with pytest.raises(DriverExecutionError, match="synthetic failure"):
CableDynDriver(fake_driver).run(deck, "envrun", env={"FAKE_CABLEDYN_MODE": "fail"})
def test_version_timeout_decodes_byte_captures(fake_driver, monkeypatch):
def slow(command, **kwargs):
raise subprocess.TimeoutExpired(
command, kwargs["timeout"], output=b"partial \xff", stderr=None
)
monkeypatch.setattr(subprocess, "run", slow)
with pytest.raises(DriverExecutionError, match="version query exceeded the 1 s") as caught:
CableDynDriver(fake_driver).version(timeout=1.0)
assert caught.value.stdout == "partial �"
assert caught.value.stderr == ""
def test_bundled_driver_accepts_both_toolchain_names(tmp_path):
from cabledyn import driver as driver_module
suffix = ".exe" if os.name == "nt" else ""
package = tmp_path / "cabledyn"
assert driver_module.bundled_driver(package) == package / "bin" / f"CableDyn_driver{suffix}"
gnu = _fake_driver(package / "bin" / f"cabledyn{suffix}")
assert driver_module.bundled_driver(package) == gnu
msvc = _fake_driver(package / "bin" / f"CableDyn_driver{suffix}")
assert driver_module.bundled_driver(package) == msvc