| 1 |
from __future__ import annotations |
| 2 |
from io import BytesIO |
| 3 |
from pathlib import Path |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
|
| 7 |
def run_duct_command(cli_args: list[str], **kwargs: Any) -> int: |
| 8 |
"""Helper to run duct with test-friendly defaults. |
| 9 |
|
| 10 |
Args: |
| 11 |
cli_args: Command and its arguments as a list (e.g., ["echo", "hello"]) |
| 12 |
**kwargs: Override any duct_execute parameters |
| 13 |
|
| 14 |
Returns: |
| 15 |
Exit code from the executed command |
| 16 |
""" |
| 17 |
from con_duct._duct_main import DUCT_OUTPUT_PREFIX, EXECUTION_SUMMARY_FORMAT |
| 18 |
from con_duct._duct_main import execute as duct_execute |
| 19 |
from con_duct._models import Outputs, RecordTypes, SessionMode |
| 20 |
|
| 21 |
command = cli_args[0] |
| 22 |
command_args = cli_args[1:] if len(cli_args) > 1 else [] |
| 23 |
|
| 24 |
defaults = { |
| 25 |
"output_prefix": DUCT_OUTPUT_PREFIX, |
| 26 |
"sample_interval": 1.0, |
| 27 |
"report_interval": 60.0, |
| 28 |
"fail_time": 3.0, |
| 29 |
"clobber": False, |
| 30 |
"capture_outputs": Outputs.ALL, |
| 31 |
"outputs": Outputs.ALL, |
| 32 |
"record_types": RecordTypes.ALL, |
| 33 |
"summary_format": EXECUTION_SUMMARY_FORMAT, |
| 34 |
"colors": False, |
| 35 |
"mode": SessionMode.NEW_SESSION, |
| 36 |
"message": "", |
| 37 |
} |
| 38 |
defaults.update(kwargs) |
| 39 |
|
| 40 |
return duct_execute(command=command, command_args=command_args, **defaults) |
| 41 |
|
| 42 |
|
| 43 |
class MockStream: |
| 44 |
"""Mocks stderr or stdout""" |
| 45 |
|
| 46 |
def __init__(self) -> None: |
| 47 |
self.buffer = BytesIO() |
| 48 |
|
| 49 |
def getvalue(self) -> bytes: |
| 50 |
return self.buffer.getvalue() |
| 51 |
|
| 52 |
|
| 53 |
def assert_files(parent_dir: str, file_list: list[str], exists: bool = True) -> None: |
| 54 |
if exists: |
| 55 |
for file_path in file_list: |
| 56 |
assert Path( |
| 57 |
parent_dir, file_path |
| 58 |
).exists(), f"Expected file does not exist: {file_path}" |
| 59 |
else: |
| 60 |
for file_path in file_list: |
| 61 |
assert not Path( |
| 62 |
parent_dir, file_path |
| 63 |
).exists(), f"Unexpected file should not exist: {file_path}" |