88from diffpy .utils .tools import get_package_info , get_user_info
99
1010
11- def _setup_dirs (monkeypatch , user_filesystem ):
12- cwd = Path (user_filesystem )
13- home_dir = cwd / "home_dir"
14- monkeypatch .setattr ("pathlib.Path.home" , lambda _ : home_dir )
15- os .chdir (cwd )
11+ def _setup_dirs (user_filesystem ):
12+ home_dir , cwd_dir = user_filesystem .home_dir , user_filesystem .cwd_dir
13+ os .chdir (cwd_dir )
1614 return home_dir
1715
1816
@@ -24,15 +22,6 @@ def _run_tests(inputs, expected):
2422 assert config .get ("email" ) == expected_email
2523
2624
27- params_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] " ]),
35- ]
3625params_user_info_with_local_conf_file = [
3726 ([
"" ,
"" ], [
"cwd_username" ,
"[email protected] " ]),
3827 ([
"cli_username" ,
"" ], [
"cli_username" ,
"[email protected] " ]),
@@ -84,21 +73,80 @@ def _run_tests(inputs, expected):
8473]
8574
8675
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 ):
89- _setup_dirs (monkeypatch , user_filesystem )
90- _run_tests (inputs , expected )
91-
92-
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 ):
95- _setup_dirs (monkeypatch , user_filesystem )
96- local_config_data = {
"username" :
"cwd_username" ,
"email" :
"[email protected] " }
97- with open (Path (user_filesystem ) / "diffpyconfig.json" , "w" ) as f :
76+ @pytest .mark .parametrize (
77+ "runtime_inputs, expected" ,
78+ [ # config file in home is present, no config in cwd. various runtime values passed
79+ # C1: nothing passed in, expect uname, email, orcid from home_config
80+ ({}, {"owner_name" : "home_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }), 81+ # C2: empty strings passed in, expect uname, email, orcid from home_config
82+ (
83+ {"owner_name" : "" , "owner_email" : "" , "owner_orcid" : "" },
84+ {"owner_name" : "home_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 85+ ),
86+ # C3: just owner name passed in at runtime. expect runtime_oname but others from config
87+ (
88+ {"owner_name" : "runtime_ownername" },
89+ {"owner_name" : "runtime_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 90+ ),
91+ # C4: just owner email passed in at runtime. expect runtime_email but others from config
92+ (
93+ {"owner_email" : "[email protected] " }, 94+ {"owner_name" : "home_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 95+ ),
96+ # C5: just owner ci passed in at runtime. expect runtime_orcid but others from config
97+ (
98+ {"owner_orcid" : "runtime_orcid" },
99+ {"owner_name" : "home_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "runtime_orcid" }, 100+ ),
101+ ],
102+ )
103+ def test_get_user_info_with_home_conf_file (runtime_inputs , expected , user_filesystem , mocker ):
104+ # user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1]
105+ # is tmp_dir/cwd_dir
106+ mocker .patch .object (Path , "home" , return_value = user_filesystem [0 ])
107+ os .chdir (user_filesystem [1 ])
108+ actual = get_user_info (** runtime_inputs )
109+ assert actual == expected
110+
111+
112+ @pytest .mark .parametrize (
113+ "runtime_inputs, expected" ,
114+ [ # tests as before but now config file present in cwd and home but orcid
115+ # missing in the cwd config
116+ # C1: nothing passed in, expect uname, email from local config, orcid from home_config
117+ ({}, {"owner_name" : "cwd_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }), 118+ # C2: empty strings passed in, expect uname, email, orcid from home_config
119+ (
120+ {"owner_name" : "" , "owner_email" : "" , "owner_orcid" : "" },
121+ {"owner_name" : "cwd_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 122+ ),
123+ # C3: just owner name passed in at runtime. expect runtime_oname but others from config
124+ (
125+ {"owner_name" : "runtime_ownername" },
126+ {"owner_name" : "runtime_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 127+ ),
128+ # C4: just owner email passed in at runtime. expect runtime_email but others from config
129+ (
130+ {"owner_email" : "[email protected] " }, 131+ {"owner_name" : "cwd_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "home_orcid" }, 132+ ),
133+ # C5: just owner ci passed in at runtime. expect runtime_orcid but others from config
134+ (
135+ {"owner_orcid" : "runtime_orcid" },
136+ {"owner_name" : "cwd_ownername" , "owner_email" : "[email protected] " , "owner_orcid" : "runtime_orcid" }, 137+ ),
138+ ],
139+ )
140+ def test_get_user_info_with_local_conf_file (runtime_inputs , expected , user_filesystem , mocker ):
141+ # user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1] i
142+ # s tmp_dir/cwd_dir
143+ mocker .patch .object (Path , "home" , return_value = user_filesystem [0 ])
144+ os .chdir (user_filesystem [1 ])
145+ local_config_data = {
"owner_name" :
"cwd_ownername" ,
"owner_email" :
"[email protected] " }
146+ with open (user_filesystem [1 ] / "diffpyconfig.json" , "w" ) as f :
98147 json .dump (local_config_data , f )
99- _run_tests (inputs , expected )
100- os .remove (Path ().home () / "diffpyconfig.json" )
101- _run_tests (inputs , expected )
148+ actual = get_user_info (** runtime_inputs )
149+ assert actual == expected
102150
103151
104152@pytest .mark .parametrize ("inputsa, inputsb, expected" , params_user_info_with_no_home_conf_file )
0 commit comments