Skip to content

Commit b4a196e

Browse files
authored
Improve handling of missing config file (#770)
* Simplify and clarify existing logic * Reindent multiline strings * Extract write_config_file fixture * Raise exception for missing config file * Add changelog entry
1 parent e07ce0e commit b4a196e

File tree

8 files changed

+158
-171
lines changed

8 files changed

+158
-171
lines changed

changelog/770.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error message for a missing config file.

tests/conftest.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,40 @@
44
import pytest
55

66
from twine import settings
7+
from twine import utils
78

89

910
@pytest.fixture()
10-
def pypirc(tmpdir):
11-
return tmpdir / ".pypirc"
11+
def config_file(tmpdir, monkeypatch):
12+
path = tmpdir / ".pypirc"
13+
# Mimic common case of .pypirc in home directory
14+
monkeypatch.setattr(utils, "DEFAULT_CONFIG_FILE", path)
15+
return path
16+
17+
18+
@pytest.fixture
19+
def write_config_file(config_file):
20+
def _write(config):
21+
config_file.write(textwrap.dedent(config))
22+
return config_file
23+
24+
return _write
1225

1326

1427
@pytest.fixture()
15-
def make_settings(pypirc):
28+
def make_settings(write_config_file):
1629
"""Return a factory function for settings.Settings with defaults."""
17-
default_pypirc = """
30+
default_config = """
1831
[pypi]
1932
username:foo
2033
password:bar
2134
"""
2235

23-
def _settings(pypirc_text=default_pypirc, **settings_kwargs):
24-
pypirc.write(textwrap.dedent(pypirc_text))
36+
def _settings(config=default_config, **settings_kwargs):
37+
config_file = write_config_file(config)
2538

2639
settings_kwargs.setdefault("sign_with", None)
27-
settings_kwargs.setdefault("config_file", str(pypirc))
40+
settings_kwargs.setdefault("config_file", config_file)
2841

2942
return settings.Settings(**settings_kwargs)
3043

tests/test_register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def register_settings(make_settings):
1919
repository: https://test.pypi.org/legacy
2020
username:foo
2121
password:bar
22-
"""
22+
"""
2323
)
2424

2525

tests/test_settings.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
# limitations under the License.
1515
import argparse
1616
import logging
17-
import os.path
18-
import textwrap
1917

2018
import pytest
2119

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

3129

32-
def test_settings_transforms_repository_config(tmpdir):
30+
def test_settings_transforms_repository_config(write_config_file):
3331
"""Set repository config and defaults when .pypirc is provided."""
34-
pypirc = os.path.join(str(tmpdir), ".pypirc")
35-
36-
with open(pypirc, "w") as fp:
37-
fp.write(
38-
textwrap.dedent(
39-
"""
40-
[pypi]
41-
repository: https://upload.pypi.org/legacy/
42-
username:username
43-
password:password
32+
config_file = write_config_file(
4433
"""
45-
)
46-
)
47-
s = settings.Settings(config_file=pypirc)
34+
[pypi]
35+
repository: https://upload.pypi.org/legacy/
36+
username:username
37+
password:password
38+
"""
39+
)
40+
41+
s = settings.Settings(config_file=config_file)
4842
assert s.repository_config["repository"] == "https://upload.pypi.org/legacy/"
4943
assert s.sign is False
5044
assert s.sign_with == "gpg"
@@ -72,16 +66,14 @@ def test_setup_logging(verbose, log_level):
7266
"verbose",
7367
[True, False],
7468
)
75-
def test_print_config_path_if_verbose(tmpdir, capsys, make_settings, verbose):
69+
def test_print_config_path_if_verbose(config_file, capsys, make_settings, verbose):
7670
"""Print the location of the .pypirc config used by the user."""
77-
pypirc = os.path.join(str(tmpdir), ".pypirc")
78-
7971
make_settings(verbose=verbose)
8072

8173
captured = capsys.readouterr()
8274

8375
if verbose:
84-
assert captured.out == f"Using configuration from {pypirc}\n"
76+
assert captured.out == f"Using configuration from {config_file}\n"
8577
else:
8678
assert captured.out == ""
8779

tests/test_upload.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,22 @@ def test_exception_for_http_status(verbose, upload_settings, stub_response, caps
203203
assert "--verbose" in captured.out
204204

205205

206-
def test_get_config_old_format(make_settings, pypirc):
206+
def test_get_config_old_format(make_settings, config_file):
207207
try:
208208
make_settings(
209209
"""
210210
[server-login]
211211
username:foo
212212
password:bar
213-
"""
213+
"""
214214
)
215215
except KeyError as err:
216216
assert all(
217217
text in err.args[0]
218218
for text in [
219219
"'pypi'",
220220
"--repository-url",
221-
pypirc,
221+
config_file,
222222
"https://docs.python.org/",
223223
]
224224
)
@@ -232,7 +232,7 @@ def test_deprecated_repo(make_settings):
232232
repository: https://pypi.python.org/pypi/
233233
username:foo
234234
password:bar
235-
"""
235+
"""
236236
)
237237

238238
upload.upload(upload_settings, [helpers.WHEEL_FIXTURE])
@@ -257,7 +257,7 @@ def test_exception_for_redirect(make_settings):
257257
repository: https://test.pypi.org/legacy
258258
username:foo
259259
password:bar
260-
"""
260+
"""
261261
)
262262

263263
stub_response = pretend.stub(

0 commit comments

Comments
 (0)