Codebase list python-procrunner / abb67e6
Deprecate unnamed arguments to the run() function (#62) Markus Gerstel authored 3 years ago GitHub committed 3 years ago
4 changed file(s) with 50 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
00 =======
11 History
22 =======
3
4 2.2.0
5 -----
6 * Calling the run() function with unnamed arguments (other than the command
7 list as the first argument) is now deprecated. As a number of arguments
8 will be removed in a future version the use of unnamed arguments will
9 cause future confusion. `Use explicit keyword arguments instead (#62). <https://github.com/DiamondLightSource/python-procrunner/pull/62>`_
310
411 2.1.0 (2020-09-05)
512 ------------------
00 import codecs
1 import functools
12 import io
23 import logging
34 import os
412413 self._extras.update(dictionary)
413414
414415
416 def _deprecate_argument_calling(f):
417 @functools.wraps(f)
418 def wrapper(*args, **kwargs):
419 if len(args) > 1:
420 warnings.warn(
421 "Calling procrunner.run() with unnamed arguments (apart from "
422 "the command) is deprecated. Use keyword arguments instead.",
423 DeprecationWarning,
424 stacklevel=2,
425 )
426 return f(*args, **kwargs)
427
428 return wrapper
429
430
431 @_deprecate_argument_calling
415432 def run(
416433 command,
417434 timeout=None,
2020
2121 with pytest.raises(RuntimeError):
2222 with pytest.warns(DeprecationWarning, match="timeout"):
23 procrunner.run(task, -1, False)
23 procrunner.run(task, timeout=-1, debug=False)
2424
2525 assert mock_subprocess.Popen.called
2626 assert mock_process.terminate.called
4141 task = ["___"]
4242
4343 with pytest.raises(RuntimeError):
44 procrunner.run(task, -1, False, raise_timeout_exception=True)
44 procrunner.run(task, timeout=-1, debug=False, raise_timeout_exception=True)
4545
4646 assert mock_subprocess.Popen.called
4747 assert mock_process.terminate.called
8282
8383 actual = procrunner.run(
8484 command,
85 0.5,
86 False,
85 timeout=0.5,
86 debug=False,
8787 callback_stdout=mock.sentinel.callback_stdout,
8888 callback_stderr=mock.sentinel.callback_stderr,
8989 working_directory=mock.sentinel.cwd,
127127 def test_default_process_environment_is_parent_environment(mock_subprocess):
128128 mock_subprocess.Popen.side_effect = NotImplementedError() # cut calls short
129129 with pytest.raises(NotImplementedError):
130 procrunner.run([mock.Mock()], -1, False, raise_timeout_exception=True)
130 procrunner.run(
131 [mock.Mock()], timeout=-1, debug=False, raise_timeout_exception=True
132 )
131133 assert mock_subprocess.Popen.call_args[1]["env"] == os.environ
132134
133135
139141 with pytest.raises(NotImplementedError):
140142 procrunner.run(
141143 [mock.Mock()],
142 -1,
143 False,
144 timeout=-1,
145 debug=False,
144146 environment=copy.copy(mock_env),
145147 raise_timeout_exception=True,
146148 )
156158 with pytest.raises(NotImplementedError):
157159 procrunner.run(
158160 [mock.Mock()],
159 -1,
160 False,
161 timeout=-1,
162 debug=False,
161163 environment=copy.copy(mock_env1),
162164 environment_override=copy.copy(mock_env2),
163165 raise_timeout_exception=True,
174176 with pytest.raises(NotImplementedError):
175177 procrunner.run(
176178 [mock.Mock()],
177 -1,
178 False,
179 timeout=-1,
180 debug=False,
179181 environment_override=copy.copy(mock_env2),
180182 raise_timeout_exception=True,
181183 )
203205 with pytest.raises(NotImplementedError):
204206 procrunner.run(
205207 [mock.Mock()],
206 -1,
207 False,
208 timeout=-1,
209 debug=False,
208210 environment_override={
209211 random_environment_variable: "X" + random_environment_value
210212 },
117117 assert te.value.stderr == b""
118118 assert te.value.timeout == 0.1
119119 assert te.value.cmd == command
120
121
122 def test_argument_deprecation(tmp_path):
123 with pytest.warns(DeprecationWarning, match="keyword arguments"):
124 result = procrunner.run(
125 [sys.executable, "-V"],
126 None,
127 working_directory=tmp_path,
128 )
129 assert not result.returncode
130 assert result.stderr or result.stdout