Skip to content
Myr

test_launch.py

This file

Path

tests/test_launch.py

51 lines 路 1.3 KiB 路 Python

Last changed by

Not known.

History of this file

Take it

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")