@@ -16,109 +16,202 @@ def _setup_dirs(monkeypatch, user_filesystem):
1616 return home_dir
1717
1818
19- def _run_tests (inputs , expected ):
20- args = {"username" : inputs [0 ], "email" : inputs [1 ]}
21- expected_username , expected_email = expected
22- config = get_user_info (args )
19+ def _run_tests (cli_inputs , expected ):
20+ user_info = {"username" : cli_inputs ["cli_username" ], "email" : cli_inputs ["cli_email" ]}
21+ config = get_user_info (user_info = user_info , skip_config_creation = cli_inputs ["skip_config_creation" ])
22+ expected_username = expected ["expected_username" ]
23+ expected_email = expected ["expected_email" ]
2324 assert config .get ("username" ) == expected_username
2425 assert config .get ("email" ) == expected_email
2526
2627
2728params_user_info_with_home_conf_file = [
28- ([
"" ,
"" ], [
"home_username" ,
"[email protected] " ]),
29- ([
"cli_username" ,
"" ], [
"cli_username" ,
"[email protected] " ]),
30- ([
"" ,
"[email protected] " ], [
"home_username" ,
"[email protected] " ]),
31- ([
None ,
None ], [
"home_username" ,
"[email protected] " ]),
32- ([
"cli_username" ,
None ], [
"cli_username" ,
"[email protected] " ]),
33- ([
None ,
"[email protected] " ], [
"home_username" ,
"[email protected] " ]),
34- ([
"cli_username" ,
"[email protected] " ], [
"cli_username" ,
"[email protected] " ]),
29+ (
30+ {"skip_config_creation" : False , "cli_username" : None , "cli_email" : None },
31+ {
"expected_username" :
"home_username" ,
"expected_email" :
"[email protected] " },
32+ ),
33+ (
34+ {"skip_config_creation" : False , "cli_username" : "cli_username" , "cli_email" : None },
35+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
36+ ),
37+ (
38+ {
"skip_config_creation" :
False ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
39+ {
"expected_username" :
"home_username" ,
"expected_email" :
"[email protected] " },
40+ ),
41+ (
42+ {
"skip_config_creation" :
False ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
43+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
44+ ),
45+ (
46+ {"skip_config_creation" : True , "cli_username" : None , "cli_email" : None },
47+ {
"expected_username" :
"home_username" ,
"expected_email" :
"[email protected] " },
48+ ),
49+ (
50+ {"skip_config_creation" : True , "cli_username" : "cli_username" , "cli_email" : None },
51+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
52+ ),
53+ (
54+ {
"skip_config_creation" :
True ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
55+ {
"expected_username" :
"home_username" ,
"expected_email" :
"[email protected] " },
56+ ),
57+ (
58+ {
"skip_config_creation" :
True ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
59+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
60+ ),
3561]
3662params_user_info_with_local_conf_file = [
37- ([
"" ,
"" ], [
"cwd_username" ,
"[email protected] " ]),
38- ([
"cli_username" ,
"" ], [
"cli_username" ,
"[email protected] " ]),
39- 40- ([
None ,
None ], [
"cwd_username" ,
"[email protected] " ]),
41- ([
"cli_username" ,
None ], [
"cli_username" ,
"[email protected] " ]),
42- ([
None ,
"[email protected] " ], [
"cwd_username" ,
"[email protected] " ]),
43- ([
"cli_username" ,
"[email protected] " ], [
"cli_username" ,
"[email protected] " ]),
63+ (
64+ {"skip_config_creation" : False , "cli_username" : None , "cli_email" : None },
65+ {
"expected_username" :
"cwd_username" ,
"expected_email" :
"[email protected] " },
66+ ),
67+ (
68+ {"skip_config_creation" : False , "cli_username" : "cli_username" , "cli_email" : None },
69+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
70+ ),
71+ (
72+ {
"skip_config_creation" :
False ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
73+ {
"expected_username" :
"cwd_username" ,
"expected_email" :
"[email protected] " },
74+ ),
75+ (
76+ {
"skip_config_creation" :
False ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
77+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
78+ ),
79+ (
80+ {"skip_config_creation" : True , "cli_username" : None , "cli_email" : None },
81+ {
"expected_username" :
"cwd_username" ,
"expected_email" :
"[email protected] " },
82+ ),
83+ (
84+ {"skip_config_creation" : True , "cli_username" : "cli_username" , "cli_email" : None },
85+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
86+ ),
87+ (
88+ {
"skip_config_creation" :
True ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
89+ {
"expected_username" :
"cwd_username" ,
"expected_email" :
"[email protected] " },
90+ ),
91+ (
92+ {
"skip_config_creation" :
True ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
93+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
94+ ),
4495]
45- params_user_info_with_no_home_conf_file = [
96+ params_user_info_with_no_conf_file = [
97+ # Case 1: no inputs, do not create config files
4698 (
47- [ None , None ] ,
48- [ "input_username" ,
"[email protected] " ] ,
49- [ "input_username" ,
"[email protected] " ] ,
99+ { "skip_config_creation" : False , "cli_username" : None , "cli_email" : None } ,
100+ { "input_username" : "" , "input_email" : "" } ,
101+ { "expected_username" : "" , "expected_email" : "" , "config_file_exists" : False } ,
50102 ),
103+ # Case 2: One input (username / email) and the other is empty, do not create config file
51104 (
52- [ " cli_username", None ] ,
53- 54- [ " cli_username",
"[email protected] " ] ,
105+ { "skip_config_creation" : False , " cli_username": "cli_username" , "cli_email" : None } ,
106+ { "input_username" : "" , "input_email" : "" } ,
107+ { "expected_username" : " cli_username" , "expected_email" : "" , "config_file_exists" : False } ,
55108 ),
56109 (
57- 58- [ "input_username" , "" ] ,
59- [ "input_username" ,
"[email protected] " ] ,
110+ { "skip_config_creation" : False , "cli_username" : None ,
"cli_email" : " [email protected] "} ,
111+ { "input_username" : "" , "input_email" : "" } ,
112+ { "expected_username" : "" ,
"expected_email" : " [email protected] ", "config_file_exists" : False } ,
60113 ),
61114 (
62- [ "" , "" ] ,
63- [ "input_username" ,
"[email protected] " ] ,
64- [ " input_username",
"[email protected] " ] ,
115+ { "skip_config_creation" : False , "cli_username" : None , "cli_email" : None } ,
116+ { "input_username" : "input_username" , "input_email" : "" } ,
117+ { "expected_username" : " input_username" , "expected_email" : "" , "config_file_exists" : False } ,
65118 ),
66119 (
67- [ " cli_username" , "" ] ,
68- 69- [ "cli_username" ,
"[email protected] " ] ,
120+ { "skip_config_creation" : False , " cli_username": None , "cli_email" : None } ,
121+ { "input_username" : "" ,
"input_email" : " [email protected] "} ,
122+ { "expected_username" : "" ,
"expected_email" : " [email protected] ", "config_file_exists" : False } ,
70123 ),
71124 (
72- 73- [ "input_username" , "" ] ,
74- [ " input_username",
"[email protected] " ] ,
125+ { "skip_config_creation" : False , "cli_username" : "cli_username" , "cli_email" : None } ,
126+ { "input_username" : "input_username" , "input_email" : "" } ,
127+ { "expected_username" : " input_username" , "expected_email" : "" , "config_file_exists" : False } ,
75128 ),
76129 (
77- [
"cli_username" ,
"[email protected] " ],
78- [
"input_username" ,
"[email protected] " ],
79- [
"cli_username" ,
"[email protected] " ],
130+ {
"skip_config_creation" :
False ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
131+ {
"input_username" :
"" ,
"input_email" :
"[email protected] " },
132+ {
"expected_username" :
"" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
False },
133+ ),
134+ # Case 2: Both inputs, create global config file
135+ (
136+ {
"skip_config_creation" :
False ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
137+ {"input_username" : "" , "input_email" : "" },
138+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
True },
139+ ),
140+ (
141+ {"skip_config_creation" : False , "cli_username" : "cli_username" , "cli_email" : None },
142+ {
"input_username" :
"" ,
"input_email" :
"[email protected] " },
143+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
True },
144+ ),
145+ (
146+ {
"skip_config_creation" :
False ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
147+ {"input_username" : "input_username" , "input_email" : "" },
148+ {
"expected_username" :
"input_username" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
True },
149+ ),
150+ (
151+ {"skip_config_creation" : False , "cli_username" : None , "cli_email" : None },
152+ {
"input_username" :
"input_username" ,
"input_email" :
"[email protected] " },
153+ {
"expected_username" :
"input_username" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
True },
154+ ),
155+ (
156+ {
"skip_config_creation" :
False ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
157+ {
"input_username" :
"input_username" ,
"input_email" :
"[email protected] " },
158+ {
"expected_username" :
"input_username" ,
"expected_email" :
"[email protected] " ,
"config_file_exists" :
True },
80159 ),
81160]
82161params_user_info_no_conf_file_no_inputs = [
83- ([None , None ], ["" , "" ], ["" , "" ]),
162+ (
163+ {"skip_config_creation" : True , "cli_username" : None , "cli_email" : None },
164+ {"expected_username" : "" , "expected_email" : "" },
165+ ),
166+ (
167+ {"skip_config_creation" : True , "cli_username" : "cli_username" , "cli_email" : None },
168+ {"expected_username" : "cli_username" , "expected_email" : "" },
169+ ),
170+ (
171+ {
"skip_config_creation" :
True ,
"cli_username" :
None ,
"cli_email" :
"[email protected] " },
172+ {
"expected_username" :
"" ,
"expected_email" :
"[email protected] " },
173+ ),
174+ (
175+ {
"skip_config_creation" :
True ,
"cli_username" :
"cli_username" ,
"cli_email" :
"[email protected] " },
176+ {
"expected_username" :
"cli_username" ,
"expected_email" :
"[email protected] " },
177+ ),
84178]
85179
86180
87- @pytest .mark .parametrize ("inputs , expected" , params_user_info_with_home_conf_file )
88- def test_get_user_info_with_home_conf_file (monkeypatch , inputs , expected , user_filesystem ):
181+ @pytest .mark .parametrize ("cli_inputs , expected" , params_user_info_with_home_conf_file )
182+ def test_get_user_info_with_home_conf_file (monkeypatch , cli_inputs , expected , user_filesystem ):
89183 _setup_dirs (monkeypatch , user_filesystem )
90- _run_tests (inputs , expected )
184+ _run_tests (cli_inputs , expected )
91185
92186
93- @pytest .mark .parametrize ("inputs , expected" , params_user_info_with_local_conf_file )
94- def test_get_user_info_with_local_conf_file (monkeypatch , inputs , expected , user_filesystem ):
187+ @pytest .mark .parametrize ("cli_inputs , expected" , params_user_info_with_local_conf_file )
188+ def test_get_user_info_with_local_conf_file (monkeypatch , cli_inputs , expected , user_filesystem ):
95189 _setup_dirs (monkeypatch , user_filesystem )
96190 local_config_data = {
"username" :
"cwd_username" ,
"email" :
"[email protected] " }
97191 with open (Path (user_filesystem ) / "diffpyconfig.json" , "w" ) as f :
98192 json .dump (local_config_data , f )
99- _run_tests (inputs , expected )
193+ _run_tests (cli_inputs , expected )
194+ # Run tests again without global config, results should be the same
100195 os .remove (Path ().home () / "diffpyconfig.json" )
101- _run_tests (inputs , expected )
196+ _run_tests (cli_inputs , expected )
102197
103198
104- @pytest .mark .parametrize ("inputsa, inputsb , expected" , params_user_info_with_no_home_conf_file )
105- def test_get_user_info_with_no_home_conf_file (monkeypatch , inputsa , inputsb , expected , user_filesystem ):
199+ @pytest .mark .parametrize ("cli_inputs, inputs , expected" , params_user_info_with_no_conf_file )
200+ def test_get_user_info_with_no_conf_file (monkeypatch , cli_inputs , inputs , expected , user_filesystem ):
106201 _setup_dirs (monkeypatch , user_filesystem )
107202 os .remove (Path ().home () / "diffpyconfig.json" )
108- inp_iter = iter (inputsb )
203+ inp_iter = iter ([ inputs [ "input_username" ], inputs [ "input_email" ]] )
109204 monkeypatch .setattr ("builtins.input" , lambda _ : next (inp_iter ))
110- _run_tests (inputsa , expected )
205+ _run_tests (cli_inputs , expected )
111206 confile = Path ().home () / "diffpyconfig.json"
112- assert confile .is_file ()
207+ assert confile .is_file () == expected [ "config_file_exists" ]
113208
114209
115- @pytest .mark .parametrize ("inputsa, inputsb , expected" , params_user_info_no_conf_file_no_inputs )
116- def test_get_user_info_no_conf_file_no_inputs (monkeypatch , inputsa , inputsb , expected , user_filesystem ):
210+ @pytest .mark .parametrize ("cli_inputs , expected" , params_user_info_no_conf_file_no_inputs )
211+ def test_get_user_info_no_conf_file_no_inputs (monkeypatch , cli_inputs , expected , user_filesystem ):
117212 _setup_dirs (monkeypatch , user_filesystem )
118213 os .remove (Path ().home () / "diffpyconfig.json" )
119- inp_iter = iter (inputsb )
120- monkeypatch .setattr ("builtins.input" , lambda _ : next (inp_iter ))
121- _run_tests (inputsa , expected )
214+ _run_tests (cli_inputs , expected )
122215 confile = Path ().home () / "diffpyconfig.json"
123216 assert confile .exists () is False
124217
0 commit comments