Skip to content

Commit eb128f7

Browse files
dnicolodisigmavirus24
authored andcommitted
Use monkeypatch.setenv() instead of home grown helpers.set_env()
Just reducing the amount of code that needs to be maintained.
1 parent 5a783b7 commit eb128f7

File tree

4 files changed

+23
-42
lines changed

4 files changed

+23
-42
lines changed

tests/helpers.py

-24
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414
"""Test functions useful across twine's tests."""
1515

16-
import contextlib
1716
import os
1817
import pathlib
1918

@@ -23,26 +22,3 @@
2322
WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0-py2.py3-none-any.whl")
2423
NEW_SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5.tar.gz")
2524
NEW_WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5-py2.py3-none-any.whl")
26-
27-
28-
@contextlib.contextmanager
29-
def set_env(**environ):
30-
"""Set the process environment variables temporarily.
31-
32-
>>> with set_env(PLUGINS_DIR=u'test/plugins'):
33-
... "PLUGINS_DIR" in os.environ
34-
True
35-
36-
>>> "PLUGINS_DIR" in os.environ
37-
False
38-
39-
:param environ: Environment variables to set
40-
:type environ: dict[str, unicode]
41-
"""
42-
old_environ = dict(os.environ)
43-
os.environ.update(environ)
44-
try:
45-
yield
46-
finally:
47-
os.environ.clear()
48-
os.environ.update(old_environ)

tests/test_register.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ def none_register(*args, **settings_kwargs):
9494
"TWINE_PASSWORD": "pypipassword",
9595
"TWINE_CERT": "/foo/bar.crt",
9696
}
97-
with helpers.set_env(**testenv):
98-
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
97+
for key, value in testenv.items():
98+
monkeypatch.setenv(key, value)
99+
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
99100
register_settings = replaced_register.calls[0].args[0]
100101
assert "pypipassword" == register_settings.password
101102
assert "pypiuser" == register_settings.username
@@ -128,8 +129,9 @@ def none_register(*args, **settings_kwargs):
128129
"TWINE_PASSWORD": "pypipassword",
129130
"TWINE_CERT": "/foo/bar.crt",
130131
}
131-
with helpers.set_env(**testenv):
132-
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
132+
for key, value in testenv.items():
133+
monkeypatch.setenv(key, value)
134+
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
133135
register_settings = replaced_register.calls[0].args[0]
134136
assert "pypipassword" == register_settings.password
135137
assert "someusername" == register_settings.username

tests/test_upload.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,9 @@ def none_upload(*args, **settings_kwargs):
568568
"TWINE_PASSWORD": "pypipassword",
569569
"TWINE_CERT": "/foo/bar.crt",
570570
}
571-
with helpers.set_env(**testenv):
572-
cli.dispatch(["upload", "path/to/file"])
571+
for key, value in testenv.items():
572+
monkeypatch.setenv(key, value)
573+
cli.dispatch(["upload", "path/to/file"])
573574
upload_settings = replaced_upload.calls[0].args[0]
574575
assert "pypipassword" == upload_settings.password
575576
assert "pypiuser" == upload_settings.username
@@ -601,8 +602,9 @@ def none_upload(*args, **settings_kwargs):
601602
"TWINE_PASSWORD": "pypipassword",
602603
"TWINE_CERT": "/foo/bar.crt",
603604
}
604-
with helpers.set_env(**testenv):
605-
cli.dispatch(["upload", "path/to/file"])
605+
for key, value in testenv.items():
606+
monkeypatch.setenv(key, value)
607+
cli.dispatch(["upload", "path/to/file"])
606608
upload_settings = replaced_upload.calls[0].args[0]
607609
assert "pypipassword" == upload_settings.password
608610
assert "someusername" == upload_settings.username

tests/test_utils.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
from twine import exceptions
2222
from twine import utils
2323

24-
from . import helpers
25-
2624

2725
def test_get_config(write_config_file):
2826
config_file = write_config_file(
@@ -279,16 +277,19 @@ def test_get_userpass_value(cli_value, config, key, strategy, expected):
279277
("URL", "https://example.org", {"URL": "https://pypi.org"}, "https://pypi.org"),
280278
],
281279
)
282-
def test_default_to_environment_action(env_name, default, environ, expected):
280+
def test_default_to_environment_action(
281+
monkeypatch, env_name, default, environ, expected
282+
):
283283
option_strings = ("-x", "--example")
284284
dest = "example"
285-
with helpers.set_env(**environ):
286-
action = utils.EnvironmentDefault(
287-
env=env_name,
288-
default=default,
289-
option_strings=option_strings,
290-
dest=dest,
291-
)
285+
for key, value in environ.items():
286+
monkeypatch.setenv(key, value)
287+
action = utils.EnvironmentDefault(
288+
env=env_name,
289+
default=default,
290+
option_strings=option_strings,
291+
dest=dest,
292+
)
292293
assert action.env == env_name
293294
assert action.default == expected
294295

0 commit comments

Comments
 (0)