Skip to content

Commit

Permalink
Fix upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Jun 21, 2023
1 parent 639abef commit 12a109f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
4 changes: 2 additions & 2 deletions flit/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def get_password(repo: RepoDetails):
return pw


def find_token(repo: RepoDetails, project_name):
def find_token(repo: RepoDetails, project_name: str):
# https://packaging.python.org/en/latest/specifications/name-normalization/
project_name = re.sub(r"[-_.]+", "-", project_name).lower()
candidate_keys = [f"pypi_token:project:{project_name}"]
Expand Down Expand Up @@ -306,7 +306,7 @@ def main(ini_path, repo_name, pypirc_path=None, formats=None, gen_setup_py=True,
srcdir = ini_path.parent
module = Module(ini_info.module, srcdir)
metadata = make_metadata(module, ini_info)
repo = get_repository(pypirc_path, repo_name, metadata.name)
repo = get_repository(pypirc_path, repo_name, project_name=metadata.name)

from . import build
built = build.main(
Expand Down
57 changes: 21 additions & 36 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

samples_dir = pathlib.Path(__file__).parent / 'samples'

repo_settings = {'url': upload.PYPI,
'username': 'user',
'password': 'pw',
'is_warehouse': True,
}
repo_settings = upload.RepoDetails(
url=upload.PYPI, username='user', password='pw'
)

pypirc1 = """
[distutils]
Expand All @@ -34,8 +32,8 @@

@contextmanager
def temp_pypirc(content):
temp_file = NamedTemporaryFile("w+", delete=False)
try:
temp_file = NamedTemporaryFile("w+", delete=False)
temp_file.write(content)
temp_file.close()
yield temp_file.name
Expand All @@ -56,10 +54,10 @@ def test_upload(copy_sample):

def test_get_repository():
with temp_pypirc(pypirc1) as pypirc:
repo = upload.get_repository(pypirc_path=pypirc)
assert repo['url'] == upload.PYPI
assert repo['username'] == 'fred'
assert repo['password'] == 's3cret'
repo = upload.get_repository(pypirc_path=pypirc, project_name='foo')
assert repo.url == upload.PYPI
assert repo.username == 'fred'
assert repo.password == 's3cret'

def test_get_repository_env():
with temp_pypirc(pypirc1) as pypirc, \
Expand All @@ -68,19 +66,19 @@ def test_get_repository_env():
'FLIT_USERNAME': 'alice',
'FLIT_PASSWORD': 'p4ssword', # Also not a real password
}):
repo = upload.get_repository(pypirc_path=pypirc)
repo = upload.get_repository(pypirc_path=pypirc, project_name='foo')
# Because we haven't specified a repo name, environment variables should
# have higher priority than the config file.
assert repo['url'] == 'https://pypi.example.com'
assert repo['username'] == 'alice'
assert repo['password'] == 'p4ssword'
assert repo.url == 'https://pypi.example.com'
assert repo.username == 'alice'
assert repo.password == 'p4ssword'

@contextmanager
def _fake_keyring(pw):
def _fake_keyring(d):
class FakeKeyring:
@staticmethod
def get_password(service_name, username):
return pw
return d.get(service_name, {}).get(username, None)

class FakeKeyringErrMod:
class KeyringError(Exception):
Expand All @@ -102,11 +100,11 @@ class KeyringError(Exception):

def test_get_repository_keyring():
with modified_env({'FLIT_PASSWORD': None}), \
_fake_keyring('tops3cret'):
repo = upload.get_repository(pypirc_path=io.StringIO(pypirc2))
_fake_keyring({upload.PYPI: {'fred': 'tops3cret'}}):
repo = upload.get_repository(pypirc_path=io.StringIO(pypirc2), project_name='foo')

assert repo['username'] == 'fred'
assert repo['password'] == 'tops3cret'
assert repo.username == 'fred'
assert repo.password == 'tops3cret'


pypirc3_repo = "https://invalid-repo.inv"
Expand Down Expand Up @@ -137,9 +135,9 @@ def test_upload_pypirc_file(copy_sample):
)
_, _, repo = upload_file.call_args[0]

assert repo["url"] == pypirc3_repo
assert repo["username"] == pypirc3_user
assert repo["password"] == pypirc3_pass
assert repo.url == pypirc3_repo
assert repo.username == pypirc3_user
assert repo.password == pypirc3_pass


def test_upload_invalid_pypirc_file(copy_sample):
Expand All @@ -153,16 +151,3 @@ def test_upload_invalid_pypirc_file(copy_sample):
repo_name="test123",
pypirc_path="./file.invalid",
)

def test_upload_default_pypirc_file(copy_sample):
with patch("flit.upload.do_upload") as do_upload:
td = copy_sample("module1_toml")
formats = list(ALL_FORMATS)[:1]
upload.main(
td / "pyproject.toml",
formats=set(formats),
repo_name="test123",
)

file = do_upload.call_args[0][2]
assert file == "~/.pypirc"

0 comments on commit 12a109f

Please sign in to comment.