diff --git a/packages/cli/src/pywrangler/utils.py b/packages/cli/src/pywrangler/utils.py index 7a923aa9..e6cf9c7a 100644 --- a/packages/cli/src/pywrangler/utils.py +++ b/packages/cli/src/pywrangler/utils.py @@ -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}") diff --git a/packages/cli/tests/test_cli.py b/packages/cli/tests/test_cli.py index 2bb62986..ef5e7cb9 100644 --- a/packages/cli/tests/test_cli.py +++ b/packages/cli/tests/test_cli.py @@ -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."""