Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion automation_file/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ def _execute_dir(path: str) -> Any:


def _execute_str(raw: str) -> Any:
return execute_action(json.loads(raw))
parsed = json.loads(raw)
# PyBreeze (and other launchers) double-encode the action list on Windows
# to survive command-line escaping — peel the second JSON layer if we got
# a string back. Guarded by isinstance, so callers that pass a single-encoded
# payload still work on every platform.
if isinstance(parsed, str):
parsed = json.loads(parsed)
return execute_action(parsed)


_LEGACY_DISPATCH: dict[str, Callable[[str], Any]] = {
Expand Down
40 changes: 40 additions & 0 deletions tests/test_cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from __future__ import annotations

import json
from typing import Any

import pytest

from automation_file import __main__ as cli_main
Expand Down Expand Up @@ -55,3 +58,40 @@ def _fake_cli(argv: list[str] | None = None) -> int:
assert rc == 0
assert "--allowed-actions" not in captured["argv"]
assert captured["argv"] == ["--name", "automation_file", "--version", "1.0.0"]


def _capture_execute_action(monkeypatch: pytest.MonkeyPatch) -> list[Any]:
received: list[Any] = []

def _fake_execute(action_list: Any) -> dict:
received.append(action_list)
return {}

monkeypatch.setattr(cli_main, "execute_action", _fake_execute)
return received


def test_execute_str_accepts_single_encoded_payload(
monkeypatch: pytest.MonkeyPatch,
) -> None:
received = _capture_execute_action(monkeypatch)
actions = [["FA_create_file", {"file_path": "x.txt", "content": "hi"}]]

rc = cli_main.main(["--execute_str", json.dumps(actions)])

assert rc == 0
assert received == [actions]


def test_execute_str_accepts_double_encoded_payload(
monkeypatch: pytest.MonkeyPatch,
) -> None:
received = _capture_execute_action(monkeypatch)
actions = [["FA_create_file", {"file_path": "x.txt", "content": "hi"}]]

# PyBreeze on Windows wraps the JSON list once more before handing the
# argument to subprocess; the CLI must peel both layers off.
rc = cli_main.main(["--execute_str", json.dumps(json.dumps(actions))])

assert rc == 0
assert received == [actions]
Loading