Debian Codegraph @

tests/integrations/subprocess/test_ensure_console_is_safe.py

raw
1 import subprocess
2 from unittest.mock import MagicMock, Mock
3
4 import pytest
5
6 from briefcase.integrations.subprocess import Subprocess
7
8
9 @pytest.fixture
10 def mock_sub(mock_tools):
11 mock_tools.console.release_console_control = Mock(
12 wraps=mock_tools.console.release_console_control
13 )
14 mock_sub = Subprocess(mock_tools)
15 mock_sub._subprocess = MagicMock(spec_set=subprocess)
16 mock_sub._run_and_stream_output = MagicMock()
17 return mock_sub
18
19
20 @pytest.mark.parametrize("batch_script", ["HELLO.BAT", "hello.bat", "hElLo.BaT"])
21 def test_run_windows_batch_script(mock_sub, batch_script):
22 """Console control is released for a Windows batch script in run."""
23 # Console control is only released on Windows
24 mock_sub.tools.host_os = "Windows"
25
26 with mock_sub.tools.console.wait_bar("Testing..."):
27 mock_sub.run([batch_script, "World"])
28
29 mock_sub._run_and_stream_output.assert_called_with(
30 [batch_script, "World"],
31 filter_func=None,
32 )
33 mock_sub.tools.console.release_console_control.assert_called_once()
34
35
36 @pytest.mark.parametrize("batch_script", ["HELLO.BAT", "hello.bat", "hElLo.BaT"])
37 def test_check_output_windows_batch_script(mock_sub, batch_script, sub_check_output_kw):
38 """Console control is released for a Windows batch script in check_output."""
39 # Console control is only released on Windows for batch scripts
40 mock_sub.tools.host_os = "Windows"
41
42 with mock_sub.tools.console.wait_bar("Testing..."):
43 mock_sub.check_output([batch_script, "World"])
44
45 mock_sub._subprocess.check_output.assert_called_with(
46 [batch_script, "World"],
47 **sub_check_output_kw,
48 )
49 mock_sub.tools.console.release_console_control.assert_called_once()
50
51
52 @pytest.mark.parametrize("sub_kwargs", [{"stream_output": True}, {}])
53 def test_run_stream_output_true(mock_sub, sub_kwargs):
54 """Console control is not released when stream_output=True or is unspecified."""
55 with mock_sub.tools.console.wait_bar("Testing..."):
56 mock_sub.run(["Hello", "World"], **sub_kwargs)
57
58 mock_sub._run_and_stream_output.assert_called_with(
59 ["Hello", "World"],
60 filter_func=None,
61 )
62 mock_sub.tools.console.release_console_control.assert_not_called()
63
64
65 def test_run_stream_output_false(mock_sub, sub_kw):
66 """Console control is released when stream_output=False."""
67 with mock_sub.tools.console.wait_bar("Testing..."):
68 mock_sub.run(["Hello", "World"], stream_output=False)
69
70 mock_sub._subprocess.run.assert_called_with(["Hello", "World"], **sub_kw)
71 mock_sub.tools.console.release_console_control.assert_called_once()
72
73
74 @pytest.mark.parametrize(
75 ("cmdline", "kwargs"),
76 [
77 ([], {}),
78 (["Hello", "World"], {}),
79 (["Hello", "World"], {"val1": "value1", "val2": "value2"}),
80 ],
81 )
82 def test_negative_condition_not_controlled(
83 mock_sub,
84 cmdline,
85 sub_check_output_kw,
86 kwargs,
87 ):
88 """Passthrough to Subprocess if conditions to release console control are not met
89 while the console is not controlled."""
90 mock_sub.run(cmdline, **kwargs)
91 mock_sub._run_and_stream_output.assert_called_with(
92 cmdline,
93 filter_func=None,
94 **kwargs,
95 )
96
97 mock_sub.check_output(cmdline, **kwargs)
98
99 final_kwargs = {**sub_check_output_kw, **kwargs}
100 mock_sub._subprocess.check_output.assert_called_with(cmdline, **final_kwargs)
101 mock_sub.tools.console.release_console_control.assert_not_called()
102
103
104 @pytest.mark.parametrize(
105 ("cmdline", "kwargs"),
106 [
107 ([], {}),
108 (["Hello", "World"], {}),
109 (["Hello", "World"], {"val1": "value1", "val2": "value2"}),
110 ],
111 )
112 def test_negative_condition_controlled(mock_sub, cmdline, kwargs, sub_check_output_kw):
113 """Passthrough to Subprocess if conditions to release console control are not met
114 while the console is controlled."""
115 with mock_sub.tools.console.wait_bar("Testing..."):
116 mock_sub.run(cmdline, **kwargs)
117 mock_sub.check_output(cmdline, **kwargs)
118
119 mock_sub._run_and_stream_output.assert_called_with(
120 cmdline,
121 filter_func=None,
122 **kwargs,
123 )
124 final_kwargs = {**sub_check_output_kw, **kwargs}
125 mock_sub._subprocess.check_output.assert_called_with(cmdline, **final_kwargs)
126 mock_sub.tools.console.release_console_control.assert_not_called()