Skip to content

Commit 26591f3

Browse files
committed
chore: removing click.launch
It now uses `webbrowser.open` directly and includes more robust error handling to gracefully manage potential exceptions when launching the browser. Removes the `click.launch` fallback, as it was causing issues.
1 parent d1be606 commit 26591f3

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/algokit/cli/explore.py

Lines changed: 10 additions & 11 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,15 +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:
118+
# https://github.com/pallets/click/issues/2868 restore click.launch once bug is fixed
117119
try:
118-
click.launch(url)
119-
except Exception:
120-
# https://github.com/pallets/click/issues/2868
121-
try:
122-
if not webbrowser.open(url):
123-
logger.warning(f"Failed to open browser. Please open this URL manually: {url}")
124-
except Exception:
120+
if not webbrowser.open(url):
125121
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: 2 additions & 3 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,7 +24,7 @@ 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}")
@@ -36,7 +36,6 @@ def test_explore_wsl_exception(mocker: MockerFixture, caplog: pytest.LogCaptureF
3636
def test_explore_webbrowser_exception(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None:
3737
command = "localnet"
3838
mocker.patch("algokit.cli.explore.is_wsl", return_value=False)
39-
mocker.patch("click.launch", side_effect=Exception("Click Exception"))
4039
mocker.patch("webbrowser.open", side_effect=Exception("Webbrowser Exception"))
4140

4241
with caplog.at_level(logging.WARNING):

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)