11from contextlib import contextmanager
2+ from tempfile import NamedTemporaryFile
3+ import os
24import io
35import pathlib
46import sys
57
8+ import pytest
69import responses
710from testpath import modified_env
811from unittest .mock import patch
912
1013from flit import upload
14+ from flit .build import ALL_FORMATS
1115
1216samples_dir = pathlib .Path (__file__ ).parent / 'samples'
1317
1721 'is_warehouse' : True ,
1822 }
1923
20- @responses .activate
21- def test_upload (copy_sample ):
22- responses .add (responses .POST , upload .PYPI , status = 200 )
23- td = copy_sample ('module1_toml' )
24-
25- with patch ('flit.upload.get_repository' , return_value = repo_settings ):
26- upload .main (td / 'pyproject.toml' , repo_name = 'pypi' )
27-
28- assert len (responses .calls ) == 2
29-
3024pypirc1 = """
3125[distutils]
3226index-servers =
@@ -38,19 +32,43 @@ def test_upload(copy_sample):
3832"""
3933# That's not a real password. Well, hopefully not.
4034
35+ @contextmanager
36+ def temp_pypirc (content ):
37+ try :
38+ temp_file = NamedTemporaryFile ("w+" , delete = False )
39+ temp_file .write (content )
40+ temp_file .close ()
41+ yield temp_file .name
42+ finally :
43+ os .unlink (temp_file .name )
44+
45+
46+ @responses .activate
47+ def test_upload (copy_sample ):
48+ responses .add (responses .POST , upload .PYPI , status = 200 )
49+ td = copy_sample ('module1_toml' )
50+
51+ with temp_pypirc (pypirc1 ) as pypirc , \
52+ patch ('flit.upload.get_repository' , return_value = repo_settings ):
53+ upload .main (td / 'pyproject.toml' , repo_name = 'pypi' , pypirc_path = pypirc )
54+
55+ assert len (responses .calls ) == 2
56+
4157def test_get_repository ():
42- repo = upload .get_repository (cfg_file = io .StringIO (pypirc1 ))
43- assert repo ['url' ] == upload .PYPI
44- assert repo ['username' ] == 'fred'
45- assert repo ['password' ] == 's3cret'
58+ with temp_pypirc (pypirc1 ) as pypirc :
59+ repo = upload .get_repository (pypirc_path = pypirc )
60+ assert repo ['url' ] == upload .PYPI
61+ assert repo ['username' ] == 'fred'
62+ assert repo ['password' ] == 's3cret'
4663
4764def test_get_repository_env ():
48- with modified_env ({
65+ with temp_pypirc (pypirc1 ) as pypirc , \
66+ modified_env ({
4967 'FLIT_INDEX_URL' : 'https://pypi.example.com' ,
5068 'FLIT_USERNAME' : 'alice' ,
5169 'FLIT_PASSWORD' : 'p4ssword' , # Also not a real password
5270 }):
53- repo = upload .get_repository (cfg_file = io . StringIO ( pypirc1 ) )
71+ repo = upload .get_repository (pypirc_path = pypirc )
5472 # Because we haven't specified a repo name, environment variables should
5573 # have higher priority than the config file.
5674 assert repo ['url' ] == 'https://pypi.example.com'
@@ -87,7 +105,66 @@ def get_password(service_name, username):
87105def test_get_repository_keyring ():
88106 with modified_env ({'FLIT_PASSWORD' : None }), \
89107 _fake_keyring ('tops3cret' ):
90- repo = upload .get_repository (cfg_file = io .StringIO (pypirc2 ))
108+ repo = upload .get_repository (pypirc_path = io .StringIO (pypirc2 ))
91109
92110 assert repo ['username' ] == 'fred'
93111 assert repo ['password' ] == 'tops3cret'
112+
113+
114+ pypirc3_repo = "https://invalid-repo.inv"
115+ pypirc3_user = "test"
116+ pypirc3_pass = "not_a_real_password"
117+ pypirc3 = f"""
118+ [distutils] =
119+ index-servers =
120+ test123
121+
122+ [test123]
123+ repository: { pypirc3_repo }
124+ username: { pypirc3_user }
125+ password: { pypirc3_pass }
126+ """
127+
128+
129+ def test_upload_pypirc_file (copy_sample ):
130+ with temp_pypirc (pypirc3 ) as pypirc , \
131+ patch ("flit.upload.upload_file" ) as upload_file :
132+ td = copy_sample ("module1_toml" )
133+ formats = list (ALL_FORMATS )[:1 ]
134+ upload .main (
135+ td / "pyproject.toml" ,
136+ formats = set (formats ),
137+ repo_name = "test123" ,
138+ pypirc_path = pypirc ,
139+ )
140+ _ , _ , repo = upload_file .call_args [0 ]
141+
142+ assert repo ["url" ] == pypirc3_repo
143+ assert repo ["username" ] == pypirc3_user
144+ assert repo ["password" ] == pypirc3_pass
145+
146+
147+ def test_upload_invalid_pypirc_file (copy_sample ):
148+ with patch ("flit.upload.upload_file" ):
149+ td = copy_sample ("module1_toml" )
150+ formats = list (ALL_FORMATS )[:1 ]
151+ with pytest .raises (FileNotFoundError ):
152+ upload .main (
153+ td / "pyproject.toml" ,
154+ formats = set (formats ),
155+ repo_name = "test123" ,
156+ pypirc_path = "./file.invalid" ,
157+ )
158+
159+ def test_upload_default_pypirc_file (copy_sample ):
160+ with patch ("flit.upload.do_upload" ) as do_upload :
161+ td = copy_sample ("module1_toml" )
162+ formats = list (ALL_FORMATS )[:1 ]
163+ upload .main (
164+ td / "pyproject.toml" ,
165+ formats = set (formats ),
166+ repo_name = "test123" ,
167+ )
168+
169+ file = do_upload .call_args [0 ][2 ]
170+ assert file == "~/.pypirc"
0 commit comments