Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of missing config file #770

Merged
merged 5 commits into from Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/770.bugfix.rst
@@ -0,0 +1 @@
Improve error message for a missing config file.
27 changes: 20 additions & 7 deletions tests/conftest.py
Expand Up @@ -4,27 +4,40 @@
import pytest

from twine import settings
from twine import utils


@pytest.fixture()
def pypirc(tmpdir):
return tmpdir / ".pypirc"
def config_file(tmpdir, monkeypatch):
path = tmpdir / ".pypirc"
# Mimic common case of .pypirc in home directory
monkeypatch.setattr(utils, "DEFAULT_CONFIG_FILE", path)
return path


@pytest.fixture
def write_config_file(config_file):
def _write(config):
config_file.write(textwrap.dedent(config))
return config_file

return _write


@pytest.fixture()
def make_settings(pypirc):
def make_settings(write_config_file):
"""Return a factory function for settings.Settings with defaults."""
default_pypirc = """
default_config = """
[pypi]
username:foo
password:bar
"""

def _settings(pypirc_text=default_pypirc, **settings_kwargs):
pypirc.write(textwrap.dedent(pypirc_text))
def _settings(config=default_config, **settings_kwargs):
config_file = write_config_file(config)

settings_kwargs.setdefault("sign_with", None)
settings_kwargs.setdefault("config_file", str(pypirc))
settings_kwargs.setdefault("config_file", config_file)

return settings.Settings(**settings_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_register.py
Expand Up @@ -19,7 +19,7 @@ def register_settings(make_settings):
repository: https://test.pypi.org/legacy
username:foo
password:bar
"""
"""
)


Expand Down
32 changes: 12 additions & 20 deletions tests/test_settings.py
Expand Up @@ -14,8 +14,6 @@
# limitations under the License.
import argparse
import logging
import os.path
import textwrap

import pytest

Expand All @@ -29,22 +27,18 @@ def test_settings_takes_no_positional_arguments():
settings.Settings("a", "b", "c")


def test_settings_transforms_repository_config(tmpdir):
def test_settings_transforms_repository_config(write_config_file):
"""Set repository config and defaults when .pypirc is provided."""
pypirc = os.path.join(str(tmpdir), ".pypirc")

with open(pypirc, "w") as fp:
fp.write(
textwrap.dedent(
"""
[pypi]
repository: https://upload.pypi.org/legacy/
username:username
password:password
config_file = write_config_file(
"""
)
)
s = settings.Settings(config_file=pypirc)
[pypi]
repository: https://upload.pypi.org/legacy/
username:username
password:password
"""
)

s = settings.Settings(config_file=config_file)
assert s.repository_config["repository"] == "https://upload.pypi.org/legacy/"
assert s.sign is False
assert s.sign_with == "gpg"
Expand Down Expand Up @@ -72,16 +66,14 @@ def test_setup_logging(verbose, log_level):
"verbose",
[True, False],
)
def test_print_config_path_if_verbose(tmpdir, capsys, make_settings, verbose):
def test_print_config_path_if_verbose(config_file, capsys, make_settings, verbose):
"""Print the location of the .pypirc config used by the user."""
pypirc = os.path.join(str(tmpdir), ".pypirc")

make_settings(verbose=verbose)

captured = capsys.readouterr()

if verbose:
assert captured.out == f"Using configuration from {pypirc}\n"
assert captured.out == f"Using configuration from {config_file}\n"
else:
assert captured.out == ""

Expand Down
10 changes: 5 additions & 5 deletions tests/test_upload.py
Expand Up @@ -203,22 +203,22 @@ def test_exception_for_http_status(verbose, upload_settings, stub_response, caps
assert "--verbose" in captured.out


def test_get_config_old_format(make_settings, pypirc):
def test_get_config_old_format(make_settings, config_file):
try:
make_settings(
"""
[server-login]
username:foo
password:bar
"""
"""
)
except KeyError as err:
assert all(
text in err.args[0]
for text in [
"'pypi'",
"--repository-url",
pypirc,
config_file,
"https://docs.python.org/",
]
)
Expand All @@ -232,7 +232,7 @@ def test_deprecated_repo(make_settings):
repository: https://pypi.python.org/pypi/
username:foo
password:bar
"""
"""
)

upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
Expand All @@ -257,7 +257,7 @@ def test_exception_for_redirect(make_settings):
repository: https://test.pypi.org/legacy
username:foo
password:bar
"""
"""
)

stub_response = pretend.stub(
Expand Down