Skip to content
Myr

✨ Update `launch` to not print anything when opening urls (#1035)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Patrick Arminio · committed by GitHub · parent af49fda · Browse files · 3 files+120 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+120 added−1 removed

  • tests:+51 added−0 removed
  • other:+69 added−1 removed

3 files+120 added−1 removed

added:tests/test_launch.py+51 added
1import subprocess2from unittest.mock import patch34import pytest5import typer67url = "http://example.com"8910@pytest.mark.parametrize(11 "system, command",12 [13 ("Darwin", "open"),14 ("Linux", "xdg-open"),15 ("FreeBSD", "xdg-open"),16 ],17)18def test_launch_url_unix(system: str, command: str):19 with patch("platform.system", return_value=system), patch(20 "shutil.which", return_value=True21 ), patch("subprocess.Popen") as mock_popen:22 typer.launch(url)2324 mock_popen.assert_called_once_with(25 [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT26 )272829def test_launch_url_windows():30 with patch("platform.system", return_value="Windows"), patch(31 "webbrowser.open"32 ) as mock_webbrowser_open:33 typer.launch(url)3435 mock_webbrowser_open.assert_called_once_with(url)363738def test_launch_url_no_xdg_open():39 with patch("platform.system", return_value="Linux"), patch(40 "shutil.which", return_value=None41 ), patch("webbrowser.open") as mock_webbrowser_open:42 typer.launch(url)4344 mock_webbrowser_open.assert_called_once_with(url)454647def test_calls_original_launch_when_not_passing_urls():48 with patch("typer.main.click.launch", return_value=0) as launch_mock:49 typer.launch("not a url")5051 launch_mock.assert_called_once_with("not a url")
modified:typer/__init__.py+1 added-1 removed
10 unmodified lines
11from click.termui import confirm as confirm12from click.termui import echo_via_pager as echo_via_pager13from click.termui import edit as edit14from click.termui import getchar as getchar15from click.termui import launch as launch15from click.termui import pause as pause16from click.termui import progressbar as progressbar17from click.termui import prompt as prompt18from click.termui import secho as secho
7 unmodified lines
26from click.utils import open_file as open_file2728from . import colors as colors29from .main import Typer as Typer30from .main import launch as launch31from .main import run as run32from .models import CallbackParam as CallbackParam33from .models import Context as Context34from .models import FileBinaryRead as FileBinaryRead
5 unmodified lines
modified:typer/main.py+68 added
1import inspect2import os3import platform4import shutil5import subprocess6import sys7import traceback8from datetime import datetime9from enum import Enum
1070 unmodified lines
1080def run(function: Callable[..., Any]) -> None:1081 app = Typer(add_completion=False)1082 app.command()(function)1083 app()108410851086def _is_macos() -> bool:1087 return platform.system() == "Darwin"108810891090def _is_linux_or_bsd() -> bool:1091 if platform.system() == "Linux":1092 return True10931094 return "BSD" in platform.system()109510961097def launch(url: str, wait: bool = False, locate: bool = False) -> int:1098 """This function launches the given URL (or filename) in the default1099 viewer application for this file type. If this is an executable, it1100 might launch the executable in a new session. The return value is1101 the exit code of the launched application. Usually, ``0`` indicates1102 success.11031104 This function handles url in different operating systems separately:1105 - On macOS (Darwin), it uses the 'open' command.1106 - On Linux and BSD, it uses 'xdg-open' if available.1107 - On Windows (and other OSes), it uses the standard webbrowser module.11081109 The function avoids, when possible, using the webbrowser module on Linux and macOS1110 to prevent spammy terminal messages from some browsers (e.g., Chrome).11111112 Examples::11131114 typer.launch("https://typer.tiangolo.com/")1115 typer.launch("/my/downloaded/file", locate=True)11161117 :param url: URL or filename of the thing to launch.1118 :param wait: Wait for the program to exit before returning. This1119 only works if the launched program blocks. In particular,1120 ``xdg-open`` on Linux does not block.1121 :param locate: if this is set to `True` then instead of launching the1122 application associated with the URL it will attempt to1123 launch a file manager with the file located. This1124 might have weird effects if the URL does not point to1125 the filesystem.1126 """11271128 if url.startswith("http://") or url.startswith("https://"):1129 if _is_macos():1130 return subprocess.Popen(1131 ["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT1132 ).wait()11331134 has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None11351136 if has_xdg_open:1137 return subprocess.Popen(1138 ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT1139 ).wait()11401141 import webbrowser11421143 webbrowser.open(url)11441145 return 011461147 else:1148 return click.launch(url)