Skip to content

Commit ffed204

Browse files
committed
Show a warning when non-str is given to Monkeypatch.setenv
1 parent 9d971d3 commit ffed204

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/_pytest/monkeypatch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,15 @@ def setenv(self, name, value, prepend=None):
225225
""" Set environment variable ``name`` to ``value``. If ``prepend``
226226
is a character, read the current environment variable value
227227
and prepend the ``value`` adjoined with the ``prepend`` character."""
228-
value = str(value)
228+
if not isinstance(value, str):
229+
warnings.warn(
230+
pytest.PytestWarning(
231+
"Environment variable value {!r} should be str, converted to str implicitly".format(
232+
value
233+
)
234+
)
235+
)
236+
value = str(value)
229237
if prepend and name in os.environ:
230238
value = value + prepend + os.environ[name]
231239
self._warn_if_env_name_is_not_str(name)

testing/test_monkeypatch.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,39 @@ def test_delenv():
194194
del os.environ[name]
195195

196196

197-
@pytest.mark.skipif(six.PY3, reason="Python 2 only test")
198-
class TestEnvironKeysWarning(object):
197+
class TestEnvironWarnings(object):
199198
"""
200-
os.environ needs keys to be native strings, otherwise it will cause problems with other modules (notably
201-
subprocess). We only test this behavior on Python 2, because Python 3 raises an error right away.
199+
os.environ keys and values should be native strings, otherwise it will cause problems with other modules (notably
200+
subprocess). On Python 2 os.environ accepts anything without complaining, while Python 3 does the right thing
201+
and raises an error.
202202
"""
203203

204204
VAR_NAME = u"PYTEST_INTERNAL_MY_VAR"
205205

206+
@pytest.mark.skipif(six.PY3, reason="Python 2 only test")
206207
def test_setenv_unicode_key(self, monkeypatch):
207208
with pytest.warns(
208209
pytest.PytestWarning,
209210
match="Environment variable name {!r} should be str".format(self.VAR_NAME),
210211
):
211212
monkeypatch.setenv(self.VAR_NAME, "2")
212213

214+
@pytest.mark.skipif(six.PY3, reason="Python 2 only test")
213215
def test_delenv_unicode_key(self, monkeypatch):
214216
with pytest.warns(
215217
pytest.PytestWarning,
216218
match="Environment variable name {!r} should be str".format(self.VAR_NAME),
217219
):
218220
monkeypatch.delenv(self.VAR_NAME, raising=False)
219221

222+
def test_setenv_non_str_warning(self, monkeypatch):
223+
value = u"2" if six.PY2 else b"2"
224+
msg = (
225+
"Environment variable value {!r} should be str, converted to str implicitly"
226+
)
227+
with pytest.warns(pytest.PytestWarning, match=msg.format(value)):
228+
monkeypatch.setenv(str(self.VAR_NAME), value)
229+
220230

221231
def test_setenv_prepend():
222232
import os

0 commit comments

Comments
 (0)