Skip to content

Commit

Permalink
Use monkeypatch.setenv() instead of home grown helpers.set_env()
Browse files Browse the repository at this point in the history
Just reducing the amount of code that needs to be maintained.
  • Loading branch information
dnicolodi authored and sigmavirus24 committed Dec 19, 2024
1 parent 5a783b7 commit eb128f7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 42 deletions.
24 changes: 0 additions & 24 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
"""Test functions useful across twine's tests."""

import contextlib
import os
import pathlib

Expand All @@ -23,26 +22,3 @@
WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0-py2.py3-none-any.whl")
NEW_SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5.tar.gz")
NEW_WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5-py2.py3-none-any.whl")


@contextlib.contextmanager
def set_env(**environ):
"""Set the process environment variables temporarily.
>>> with set_env(PLUGINS_DIR=u'test/plugins'):
... "PLUGINS_DIR" in os.environ
True
>>> "PLUGINS_DIR" in os.environ
False
:param environ: Environment variables to set
:type environ: dict[str, unicode]
"""
old_environ = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_environ)
10 changes: 6 additions & 4 deletions tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def none_register(*args, **settings_kwargs):
"TWINE_PASSWORD": "pypipassword",
"TWINE_CERT": "/foo/bar.crt",
}
with helpers.set_env(**testenv):
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
for key, value in testenv.items():
monkeypatch.setenv(key, value)
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
register_settings = replaced_register.calls[0].args[0]
assert "pypipassword" == register_settings.password
assert "pypiuser" == register_settings.username
Expand Down Expand Up @@ -128,8 +129,9 @@ def none_register(*args, **settings_kwargs):
"TWINE_PASSWORD": "pypipassword",
"TWINE_CERT": "/foo/bar.crt",
}
with helpers.set_env(**testenv):
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
for key, value in testenv.items():
monkeypatch.setenv(key, value)
cli.dispatch(["register", helpers.WHEEL_FIXTURE])
register_settings = replaced_register.calls[0].args[0]
assert "pypipassword" == register_settings.password
assert "someusername" == register_settings.username
Expand Down
10 changes: 6 additions & 4 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,9 @@ def none_upload(*args, **settings_kwargs):
"TWINE_PASSWORD": "pypipassword",
"TWINE_CERT": "/foo/bar.crt",
}
with helpers.set_env(**testenv):
cli.dispatch(["upload", "path/to/file"])
for key, value in testenv.items():
monkeypatch.setenv(key, value)
cli.dispatch(["upload", "path/to/file"])
upload_settings = replaced_upload.calls[0].args[0]
assert "pypipassword" == upload_settings.password
assert "pypiuser" == upload_settings.username
Expand Down Expand Up @@ -601,8 +602,9 @@ def none_upload(*args, **settings_kwargs):
"TWINE_PASSWORD": "pypipassword",
"TWINE_CERT": "/foo/bar.crt",
}
with helpers.set_env(**testenv):
cli.dispatch(["upload", "path/to/file"])
for key, value in testenv.items():
monkeypatch.setenv(key, value)
cli.dispatch(["upload", "path/to/file"])
upload_settings = replaced_upload.calls[0].args[0]
assert "pypipassword" == upload_settings.password
assert "someusername" == upload_settings.username
Expand Down
21 changes: 11 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from twine import exceptions
from twine import utils

from . import helpers


def test_get_config(write_config_file):
config_file = write_config_file(
Expand Down Expand Up @@ -279,16 +277,19 @@ def test_get_userpass_value(cli_value, config, key, strategy, expected):
("URL", "https://example.org", {"URL": "https://pypi.org"}, "https://pypi.org"),
],
)
def test_default_to_environment_action(env_name, default, environ, expected):
def test_default_to_environment_action(
monkeypatch, env_name, default, environ, expected
):
option_strings = ("-x", "--example")
dest = "example"
with helpers.set_env(**environ):
action = utils.EnvironmentDefault(
env=env_name,
default=default,
option_strings=option_strings,
dest=dest,
)
for key, value in environ.items():
monkeypatch.setenv(key, value)
action = utils.EnvironmentDefault(
env=env_name,
default=default,
option_strings=option_strings,
dest=dest,
)
assert action.env == env_name
assert action.default == expected

Expand Down

0 comments on commit eb128f7

Please sign in to comment.