File: test_launch.py

package info (click to toggle)
typer 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: python: 16,702; javascript: 280; sh: 28; makefile: 27
file content (51 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import subprocess
from unittest.mock import patch

import pytest
import typer

url = "http://example.com"


@pytest.mark.parametrize(
    "system, command",
    [
        ("Darwin", "open"),
        ("Linux", "xdg-open"),
        ("FreeBSD", "xdg-open"),
    ],
)
def test_launch_url_unix(system: str, command: str):
    with patch("platform.system", return_value=system), patch(
        "shutil.which", return_value=True
    ), patch("subprocess.Popen") as mock_popen:
        typer.launch(url)

    mock_popen.assert_called_once_with(
        [command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
    )


def test_launch_url_windows():
    with patch("platform.system", return_value="Windows"), patch(
        "webbrowser.open"
    ) as mock_webbrowser_open:
        typer.launch(url)

    mock_webbrowser_open.assert_called_once_with(url)


def test_launch_url_no_xdg_open():
    with patch("platform.system", return_value="Linux"), patch(
        "shutil.which", return_value=None
    ), patch("webbrowser.open") as mock_webbrowser_open:
        typer.launch(url)

    mock_webbrowser_open.assert_called_once_with(url)


def test_calls_original_launch_when_not_passing_urls():
    with patch("typer.main.click.launch", return_value=0) as launch_mock:
        typer.launch("not a url")

    launch_mock.assert_called_once_with("not a url")