Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/webwright/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mimetypes
import os
import subprocess
import sys
from pathlib import Path
from typing import Annotated, Any

Expand Down Expand Up @@ -121,6 +122,8 @@ def parse_json_output(raw: str, *, action_field: str = "bash_command") -> dict[s


def _validate_bash_command(command: str) -> None:
if sys.platform == "win32":
return
result = subprocess.run(
["/bin/bash", "-n"],
input=command,
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/test_bash_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
from unittest.mock import MagicMock, patch

import pytest

from webwright.models.base import _validate_bash_command


def _make_proc(returncode: int, stderr: str = "") -> MagicMock:
proc = MagicMock()
proc.returncode = returncode
proc.stderr = stderr
proc.stdout = ""
return proc


def test_valid_command_passes_on_posix(monkeypatch) -> None:
monkeypatch.setattr(sys, "platform", "linux")
with patch("subprocess.run", return_value=_make_proc(0)):
_validate_bash_command("echo hello")


def test_invalid_syntax_raises_on_posix(monkeypatch) -> None:
monkeypatch.setattr(sys, "platform", "linux")
with patch("subprocess.run", return_value=_make_proc(1, "syntax error near unexpected token")):
with pytest.raises(ValueError, match="Invalid bash_command syntax"):
_validate_bash_command("if then fi done ;;;")


def test_validation_skipped_on_windows(monkeypatch) -> None:
monkeypatch.setattr(sys, "platform", "win32")
_validate_bash_command("if then fi done ;;;")


def test_validation_skipped_on_windows_does_not_call_subprocess(monkeypatch) -> None:
monkeypatch.setattr(sys, "platform", "win32")
with patch("subprocess.run") as mock_run:
_validate_bash_command("echo hi")
mock_run.assert_not_called()