Issue warning if Monkeypatch.setenv/delenv receive non-strings in Python 2

Fixes the bug described in:

	https://github.com/tox-dev/tox/pull/1025#discussion_r221273830

Which is more evident when using `unicode_literals`.
This commit is contained in:
Bruno Oliveira
2018-09-29 19:55:04 -03:00
parent 5d2d64c190
commit d24a7e6c5a
3 changed files with 44 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ import os
import sys
import textwrap
import six
import pytest
from _pytest.monkeypatch import MonkeyPatch
@@ -192,6 +194,30 @@ def test_delenv():
del os.environ[name]
@pytest.mark.skipif(six.PY3, reason="Python 2 only test")
class TestEnvironKeysWarning(object):
"""
os.environ needs keys to be native strings, otherwise it will cause problems with other modules (notably
subprocess). We only test this behavior on Python 2, because Python 3 raises an error right away.
"""
VAR_NAME = u"PYTEST_INTERNAL_MY_VAR"
def test_setenv_unicode_key(self, monkeypatch):
with pytest.warns(
pytest.PytestWarning,
match="Environment variable name {!r} should be str".format(self.VAR_NAME),
):
monkeypatch.setenv(self.VAR_NAME, "2")
def test_delenv_unicode_key(self, monkeypatch):
with pytest.warns(
pytest.PytestWarning,
match="Environment variable name {!r} should be str".format(self.VAR_NAME),
):
monkeypatch.delenv(self.VAR_NAME, raising=False)
def test_setenv_prepend():
import os