|
1 | 1 | import io |
2 | 2 | import subprocess |
3 | | -from unittest.mock import patch |
| 3 | +from unittest.mock import Mock, patch |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | import typer |
@@ -52,6 +52,41 @@ def test_launch_url_no_xdg_open(): |
52 | 52 | mock_webbrowser_open.assert_called_once_with(url) |
53 | 53 |
|
54 | 54 |
|
| 55 | +def test_launch_url_linux_xdg_open_does_not_wait_by_default(): |
| 56 | + proc = Mock() |
| 57 | + |
| 58 | + with ( |
| 59 | + patch("platform.system", return_value="Linux"), |
| 60 | + patch("shutil.which", return_value=True), |
| 61 | + patch("subprocess.Popen", return_value=proc) as mock_popen, |
| 62 | + ): |
| 63 | + result = typer.launch(url) |
| 64 | + |
| 65 | + assert result == 0 |
| 66 | + mock_popen.assert_called_once_with( |
| 67 | + ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT |
| 68 | + ) |
| 69 | + proc.wait.assert_not_called() |
| 70 | + |
| 71 | + |
| 72 | +def test_launch_url_linux_xdg_open_waits_when_requested(): |
| 73 | + proc = Mock() |
| 74 | + proc.wait.return_value = 42 |
| 75 | + |
| 76 | + with ( |
| 77 | + patch("platform.system", return_value="Linux"), |
| 78 | + patch("shutil.which", return_value=True), |
| 79 | + patch("subprocess.Popen", return_value=proc) as mock_popen, |
| 80 | + ): |
| 81 | + result = typer.launch(url, wait=True) |
| 82 | + |
| 83 | + assert result == 42 |
| 84 | + mock_popen.assert_called_once_with( |
| 85 | + ["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT |
| 86 | + ) |
| 87 | + proc.wait.assert_called_once_with() |
| 88 | + |
| 89 | + |
55 | 90 | @pytest.fixture |
56 | 91 | def allow_dev_null(monkeypatch): |
57 | 92 | real_open = open |
|
0 commit comments