10
10
11
11
from . import helpers
12
12
13
+ all_rc_files_list = [
14
+ # TODO: test system located sources?
15
+ # "/etc/conda/.condarc",
16
+ # "/etc/conda/condarc",
17
+ # "/etc/conda/condarc.d/",
18
+ # "/etc/conda/.mambarc",
19
+ # "/var/lib/conda/.condarc",
20
+ # "/var/lib/conda/condarc",
21
+ # "/var/lib/conda/condarc.d/",
22
+ # "/var/lib/conda/.mambarc",
23
+ ("root_prefix" , ".condarc" ),
24
+ ("root_prefix" , "condarc" ),
25
+ ("root_prefix" , "condarc.d" ),
26
+ ("root_prefix" , ".mambarc" ),
27
+ ("home" , ".conda/.condarc" ),
28
+ ("home" , ".conda/condarc" ),
29
+ ("home" , ".conda/condarc.d" ),
30
+ ("home" , ".condarc" ),
31
+ ("env_set_xdg" , "mambarc" ),
32
+ ("user_config_dir" , "mambarc" ),
33
+ ("home" , ".mambarc" ),
34
+ ("prefix" , ".condarc" ),
35
+ ("prefix" , "condarc" ),
36
+ ("prefix" , "condarc.d" ),
37
+ ("prefix" , ".mambarc" ),
38
+ ]
39
+
13
40
14
41
@pytest .fixture
15
42
def user_config_dir (tmp_home : Path ):
@@ -59,6 +86,41 @@ def rc_file_text(rc_file_args):
59
86
return yaml .dump (rc_file_args , Dumper = Dumper )
60
87
61
88
89
+ def create_rc_file (
90
+ where ,
91
+ rc_filename ,
92
+ rc_file_text ,
93
+ tmp_home ,
94
+ tmp_root_prefix ,
95
+ tmp_prefix ,
96
+ tmp_path ,
97
+ user_config_dir ,
98
+ monkeypatch ,
99
+ ):
100
+ if where == "home" :
101
+ rc_file = tmp_home / rc_filename
102
+ elif where == "root_prefix" :
103
+ rc_file = tmp_root_prefix / rc_filename
104
+ elif where == "prefix" :
105
+ rc_file = tmp_prefix / rc_filename
106
+ elif where == "user_config_dir" :
107
+ rc_file = user_config_dir / rc_filename
108
+ elif where == "env_set_xdg" :
109
+ monkeypatch .setenv ("XDG_CONFIG_HOME" , str (tmp_home / "custom_xdg_config_dir" ))
110
+ rc_file = tmp_home / "custom_xdg_config_dir" / "mamba" / rc_filename
111
+ elif where == "absolute" :
112
+ rc_file = Path (rc_filename )
113
+ else :
114
+ raise ValueError ("Bad rc file location" )
115
+ if rc_file .suffix == ".d" :
116
+ rc_file = rc_file / "test.yaml"
117
+
118
+ rc_file .parent .mkdir (parents = True , exist_ok = True )
119
+ rc_file .write_text (rc_file_text )
120
+
121
+ return rc_file
122
+
123
+
62
124
@pytest .fixture
63
125
def rc_file (
64
126
request ,
@@ -68,6 +130,7 @@ def rc_file(
68
130
tmp_prefix ,
69
131
tmp_path ,
70
132
user_config_dir ,
133
+ monkeypatch ,
71
134
):
72
135
"""Parametrizable fixture to create an rc file at the desired location.
73
136
@@ -76,33 +139,55 @@ def rc_file(
76
139
"""
77
140
if hasattr (request , "param" ):
78
141
where , rc_filename = request .param
79
- if where == "home" :
80
- rc_file = tmp_home / rc_filename
81
- elif where == "root_prefix" :
82
- rc_file = tmp_root_prefix / rc_filename
83
- elif where == "prefix" :
84
- rc_file = tmp_prefix / rc_filename
85
- elif where == "user_config_dir" :
86
- rc_file = user_config_dir / rc_filename
87
- elif where == "env_set_xdg" :
88
- os .environ ["XDG_CONFIG_HOME" ] = str (tmp_home / "custom_xdg_config_dir" )
89
- rc_file = tmp_home / "custom_xdg_config_dir" / "mamba" / rc_filename
90
- elif where == "absolute" :
91
- rc_file = Path (rc_filename )
92
- else :
93
- raise ValueError ("Bad rc file location" )
94
- if rc_file .suffix == ".d" :
95
- rc_file = rc_file / "test.yaml"
96
142
else :
97
- rc_file = tmp_path / "umamba/config.yaml"
98
-
99
- rc_file .parent .mkdir (parents = True , exist_ok = True )
100
- with open (rc_file , "w+" ) as f :
101
- f .write (rc_file_text )
143
+ where , rc_filename = ("absolute" , tmp_path / "umamba/config.yaml" )
144
+
145
+ rc_file = create_rc_file (
146
+ where ,
147
+ rc_filename ,
148
+ rc_file_text ,
149
+ tmp_home ,
150
+ tmp_root_prefix ,
151
+ tmp_prefix ,
152
+ tmp_path ,
153
+ user_config_dir ,
154
+ monkeypatch ,
155
+ )
102
156
103
157
return rc_file
104
158
105
159
160
+ @pytest .fixture
161
+ def all_rc_files (
162
+ rc_file_text , tmp_home , tmp_root_prefix , tmp_prefix , tmp_path , user_config_dir , monkeypatch
163
+ ):
164
+ """Fixture to create all rc files
165
+
166
+ The files are created in isolated folders and set as the prefix, root prefix, and
167
+ home folder.
168
+
169
+ """
170
+
171
+ files = []
172
+ for where , filename in all_rc_files_list :
173
+ if where == "user_config_dir" :
174
+ continue # redundant with XDG_HOME_DIR
175
+ f = create_rc_file (
176
+ where ,
177
+ filename ,
178
+ rc_file_text ,
179
+ tmp_home ,
180
+ tmp_root_prefix ,
181
+ tmp_prefix ,
182
+ tmp_path ,
183
+ user_config_dir ,
184
+ monkeypatch ,
185
+ )
186
+
187
+ files .append (f )
188
+ return files
189
+
190
+
106
191
class TestConfig :
107
192
def test_config_empty (self , tmp_home ):
108
193
assert "Configuration of micromamba" in config ()
@@ -140,34 +225,9 @@ def test_config_sources_empty(self, tmp_prefix, quiet_flag, norc):
140
225
res = config ("sources" , quiet_flag )
141
226
assert res .startswith ("Configuration files (by precedence order):" )
142
227
143
- # TODO: test system located sources?
144
228
@pytest .mark .parametrize (
145
229
"rc_file" ,
146
- (
147
- # "/etc/conda/.condarc",
148
- # "/etc/conda/condarc",
149
- # "/etc/conda/condarc.d/",
150
- # "/etc/conda/.mambarc",
151
- # "/var/lib/conda/.condarc",
152
- # "/var/lib/conda/condarc",
153
- # "/var/lib/conda/condarc.d/",
154
- # "/var/lib/conda/.mambarc",
155
- ("user_config_dir" , "mambarc" ),
156
- ("env_set_xdg" , "mambarc" ),
157
- ("home" , ".conda/.condarc" ),
158
- ("home" , ".conda/condarc" ),
159
- ("home" , ".conda/condarc.d" ),
160
- ("home" , ".condarc" ),
161
- ("home" , ".mambarc" ),
162
- ("root_prefix" , ".condarc" ),
163
- ("root_prefix" , "condarc" ),
164
- ("root_prefix" , "condarc.d" ),
165
- ("root_prefix" , ".mambarc" ),
166
- ("prefix" , ".condarc" ),
167
- ("prefix" , "condarc" ),
168
- ("prefix" , "condarc.d" ),
169
- ("prefix" , ".mambarc" ),
170
- ),
230
+ all_rc_files_list ,
171
231
indirect = True ,
172
232
)
173
233
@pytest .mark .parametrize ("rc_file_args" , ({"override_channels_enabled" : True },), indirect = True )
@@ -177,6 +237,28 @@ def test_config_rc_file(self, rc_file, tmp_env_name):
177
237
expected_srcs = f"Configuration files (by precedence order):\n { short_name } " .splitlines ()
178
238
assert srcs == expected_srcs
179
239
240
+ @pytest .mark .parametrize ("rc_file_args" , ({"override_channels_enabled" : True },), indirect = True )
241
+ def test_rc_file_precedence (
242
+ self , rc_file_text , all_rc_files , tmp_env_name , tmp_home , monkeypatch
243
+ ):
244
+ env_filenames = []
245
+ for x in ["conda" , "mamba" ]:
246
+ env_x_rc = tmp_home / f"env-{ x } rc.yaml"
247
+ monkeypatch .setenv (f"{ x .upper ()} RC" , f"{ env_x_rc } " )
248
+ env_x_rc .write_text (rc_file_text )
249
+ env_filenames .append (str (env_x_rc ).replace (os .path .expanduser ("~" ), "~" ))
250
+
251
+ srcs = config ("sources" , "-vvvv" , "-n" , tmp_env_name ).strip ().splitlines ()
252
+ for rc_file in all_rc_files :
253
+ short_names = [
254
+ str (rc_file ).replace (os .path .expanduser ("~" ), "~" ) for rc_file in all_rc_files
255
+ ]
256
+
257
+ short_names .extend (env_filenames )
258
+ short_names .reverse ()
259
+
260
+ assert srcs [1 :] == short_names
261
+
180
262
@pytest .mark .parametrize (
181
263
"rc_file" ,
182
264
[("home" , "somefile.yml" )],
0 commit comments