Skip to content

Commit 71088cf

Browse files
authored
fix: add fallback for browser opening in explore command
fix: add fallback for browser opening in explore command
2 parents 40518a4 + 26591f3 commit 71088cf

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/algokit/cli/explore.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ def get_explore_url(network: str) -> str:
9999
def explore_command(network: str) -> None:
100100
url = get_explore_url(network)
101101
logger.info(f"Opening {network} explorer in your default browser")
102+
logger.info(f"URL: {url}")
102103

103-
if is_wsl():
104-
import webbrowser
104+
import webbrowser
105105

106+
if is_wsl():
106107
warning = (
107108
"Unable to open browser from WSL environment.\n"
108109
"Ensure 'wslu' is installed: (https://wslutiliti.es/wslu/install.html),\n"
@@ -111,7 +112,13 @@ def explore_command(network: str) -> None:
111112
try:
112113
if not webbrowser.open(url):
113114
logger.warning(warning)
114-
except Exception as e:
115-
logger.warning(warning, exc_info=e)
115+
except Exception:
116+
logger.warning(warning)
116117
else:
117-
click.launch(url)
118+
# https://github.com/pallets/click/issues/2868 restore click.launch once bug is fixed
119+
try:
120+
if not webbrowser.open(url):
121+
logger.warning(f"Failed to open browser. Please open this URL manually: {url}")
122+
except Exception as e:
123+
logger.debug("Error opening browser", exc_info=e)
124+
logger.warning(f"Failed to open browser. Please open this URL manually: {url}")

tests/explore/test_explore.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.mark.parametrize("command", ["", "localnet", "testnet", "mainnet"])
1313
def test_explore(command: str, mocker: MockerFixture) -> None:
14-
launch_mock = mocker.patch("click.launch")
14+
launch_mock = mocker.patch("webbrowser.open")
1515
result = invoke(f"explore {command}")
1616

1717
assert result.exit_code == 0
@@ -24,10 +24,22 @@ def test_explore(command: str, mocker: MockerFixture) -> None:
2424
def test_explore_wsl_exception(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None:
2525
command = "localnet"
2626
mocker.patch("algokit.cli.explore.is_wsl", return_value=True)
27-
mocker.patch("webbrowser.open", side_effect=Exception("Test Exception"))
27+
mocker.patch("webbrowser.open", return_value=False)
2828

2929
with caplog.at_level(logging.WARNING):
3030
result = invoke(f"explore {command}")
3131

3232
assert result.exit_code == 0
3333
assert any("Unable to open browser from WSL" in message for message in caplog.messages)
34+
35+
36+
def test_explore_webbrowser_exception(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None:
37+
command = "localnet"
38+
mocker.patch("algokit.cli.explore.is_wsl", return_value=False)
39+
mocker.patch("webbrowser.open", side_effect=Exception("Webbrowser Exception"))
40+
41+
with caplog.at_level(logging.WARNING):
42+
result = invoke(f"explore {command}")
43+
44+
assert result.exit_code == 0
45+
assert any("Failed to open browser. Please open this URL manually:" in message for message in caplog.messages)

tests/explore/test_explore.test_explore.localnet.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Opening localnet explorer in your default browser
2+
URL: https://explore.algokit.io/localnet
23
----
34
launch args:
45
----

tests/explore/test_explore.test_explore.mainnet.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Opening mainnet explorer in your default browser
2+
URL: https://explore.algokit.io/mainnet
23
----
34
launch args:
45
----

tests/explore/test_explore.test_explore.testnet.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Opening testnet explorer in your default browser
2+
URL: https://explore.algokit.io/testnet
23
----
34
launch args:
45
----

0 commit comments

Comments
 (0)