1010
1111from . import helpers
1212
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+
1340
1441@pytest .fixture
1542def user_config_dir (tmp_home : Path ):
@@ -59,6 +86,41 @@ def rc_file_text(rc_file_args):
5986 return yaml .dump (rc_file_args , Dumper = Dumper )
6087
6188
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+
62124@pytest .fixture
63125def rc_file (
64126 request ,
@@ -68,6 +130,7 @@ def rc_file(
68130 tmp_prefix ,
69131 tmp_path ,
70132 user_config_dir ,
133+ monkeypatch ,
71134):
72135 """Parametrizable fixture to create an rc file at the desired location.
73136
@@ -76,33 +139,55 @@ def rc_file(
76139 """
77140 if hasattr (request , "param" ):
78141 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"
96142 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+ )
102156
103157 return rc_file
104158
105159
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+
106191class TestConfig :
107192 def test_config_empty (self , tmp_home ):
108193 assert "Configuration of micromamba" in config ()
@@ -140,34 +225,9 @@ def test_config_sources_empty(self, tmp_prefix, quiet_flag, norc):
140225 res = config ("sources" , quiet_flag )
141226 assert res .startswith ("Configuration files (by precedence order):" )
142227
143- # TODO: test system located sources?
144228 @pytest .mark .parametrize (
145229 "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 ,
171231 indirect = True ,
172232 )
173233 @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):
177237 expected_srcs = f"Configuration files (by precedence order):\n { short_name } " .splitlines ()
178238 assert srcs == expected_srcs
179239
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+
180262 @pytest .mark .parametrize (
181263 "rc_file" ,
182264 [("home" , "somefile.yml" )],
0 commit comments