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
10 changes: 7 additions & 3 deletions packages/cli/src/pywrangler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,21 @@ def check_wrangler_version() -> None:
click.exceptions.Exit: If wrangler is not installed or version is too old.
"""
result = run_command(
["npx", "--yes", "wrangler", "--version"], capture_output=True, check=False
["npx", "--yes", "--silent", "wrangler", "--version"],
capture_output=True,
check=False,
)
if result.returncode != 0:
logger.error("Failed to get wrangler version. Is wrangler installed?")
logger.error("Install wrangler with: npm install wrangler@latest")
raise click.exceptions.Exit(code=1)

# Parse version from output like "wrangler 4.42.1" or " ⛅️ wrangler 4.42.1"
# Parse version from output like "wrangler 4.42.1" or " ⛅️ wrangler 4.42.1" or "4.42.1"
version_line = result.stdout.strip()
# Extract version number using regex
version_match = re.search(r"(\d+)\.(\d+)\.(\d+)", version_line)
version_match = re.search(
r"wrangler\s+v?(\d+)\.(\d+)\.(\d+)", version_line
) or re.search(r"^\s*v?(\d+)\.(\d+)\.(\d+)\s*$", version_line, re.MULTILINE)

if not version_match:
logger.error(f"Could not parse wrangler version from: {version_line}")
Expand Down
30 changes: 29 additions & 1 deletion packages/cli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,38 @@ def test_check_wrangler_version_sufficient(mock_run_command):

# Verify the command was called correctly
mock_run_command.assert_called_once_with(
["npx", "--yes", "wrangler", "--version"], capture_output=True, check=False
["npx", "--yes", "--silent", "wrangler", "--version"],
capture_output=True,
check=False,
)


@pytest.mark.parametrize(
"stdout",
[
"4.148.0",
"\n ⛅️ wrangler 4.148.0\n────────────────────\n",
# npm might print notices before the version
# https://github.com/cloudflare/workers-py/issues/290
"npm notice run my-project@0.0.0 npx\nnpm notice run 'wrangler' --version\n"
"\n ⛅️ wrangler 4.148.0\n────────────────────\n",
"npm notice run my-project@0.0.0 npx\nnpm notice run 'wrangler' --version\n"
"4.148.0\n",
],
)
@patch("pywrangler.utils.run_command")
def test_check_wrangler_version_ignores_npm_notices(mock_run_command, stdout):
"""Regression test for cloudflare/workers-py#290."""
from pywrangler.utils import check_wrangler_version

mock_result = Mock()
mock_result.returncode = 0
mock_result.stdout = stdout
mock_run_command.return_value = mock_result

check_wrangler_version()


@patch("pywrangler.utils.run_command")
def test_check_wrangler_version_insufficient(mock_run_command):
"""Test that check_wrangler_version fails with insufficient version."""
Expand Down
Loading