@@ -21,6 +21,7 @@ class ConfigSectionSchema1(object):
21
21
@matches_section ("hello" )
22
22
class Hello (SectionSchema ):
23
23
name = Param (type = str )
24
+ number = Param (type = int )
24
25
25
26
@matches_section ("hello.more.*" )
26
27
class HelloMore (SectionSchema ):
@@ -68,6 +69,17 @@ class ConfigFileProcessor2(ConfigFileReader):
68
69
]
69
70
70
71
72
+ # -----------------------------------------------------------------------------
73
+ # TEST CANDIDATE 3: With config_searchpath
74
+ # -----------------------------------------------------------------------------
75
+ class ConfigFileProcessor3 (ConfigFileReader ):
76
+ config_files = ["hello3.ini" ]
77
+ config_searchpath = ["." , "config/profile" ]
78
+ config_section_schemas = [
79
+ ConfigSectionSchema1 .Hello ,
80
+ ]
81
+
82
+
71
83
# -----------------------------------------------------------------------------
72
84
# TEST SUITE
73
85
# -----------------------------------------------------------------------------
@@ -252,7 +264,7 @@ def hello2(ctx, name):
252
264
assert result .exit_code == 0
253
265
254
266
255
- class TestCandidate3 (object ):
267
+ class TestCandidate2B (object ):
256
268
257
269
def test_configfile__with_unbound_section (self , cli_runner_isolated ):
258
270
# -- DEFINITION: unbound section
@@ -279,3 +291,78 @@ class ConfigFileProcessorWithUnboundSection(ConfigFileProcessor2):
279
291
280
292
expected = "LookupError: No schema found for: section=unbound.section"
281
293
assert expected in str (e )
294
+
295
+
296
+ class TestCandidate3 (object ):
297
+
298
+ def test_config_searchpath__with_many_items (self , cli_runner_isolated ):
299
+ assert not os .path .exists ("hello3.ini" )
300
+ CONFIG_FILE_CONTENTS1 = """
301
+ [hello]
302
+ name = Alice
303
+ """
304
+ CONFIG_FILE_CONTENTS2 = """
305
+ [hello]
306
+ name = Bob
307
+ number = 2
308
+ """
309
+ config_filename2 = os .path .join ("config" , "profile" , "hello3.ini" )
310
+ config_dirname2 = os .path .dirname (config_filename2 )
311
+ write_configfile_with_contents ("hello3.ini" , CONFIG_FILE_CONTENTS1 )
312
+ write_configfile_with_contents (config_filename2 , CONFIG_FILE_CONTENTS2 )
313
+ assert os .path .exists ("hello3.ini" )
314
+ assert os .path .exists (config_filename2 )
315
+ assert config_dirname2 in ConfigFileProcessor3 .config_searchpath
316
+
317
+ config = ConfigFileProcessor3 .read_config ()
318
+ assert config == dict (name = "Alice" , number = 2 )
319
+ assert config ["name" ] == "Alice" # -- FROM: config_file1 (prefered)
320
+ assert config ["number" ] == 2 # -- FROM: config_file2
321
+
322
+
323
+ def test_config_searchpath__merges_sections (self , cli_runner_isolated ):
324
+ assert not os .path .exists ("hello3.ini" )
325
+ CONFIG_FILE_CONTENTS1 = """
326
+ [hello]
327
+ name = Alice
328
+ """
329
+ CONFIG_FILE_CONTENTS2 = """
330
+ [hello]
331
+ number = 2
332
+ """
333
+ config_filename2 = os .path .join ("config" , "profile" , "hello3.ini" )
334
+ config_dirname2 = os .path .dirname (config_filename2 )
335
+ write_configfile_with_contents ("hello3.ini" , CONFIG_FILE_CONTENTS1 )
336
+ write_configfile_with_contents (config_filename2 , CONFIG_FILE_CONTENTS2 )
337
+ assert os .path .exists ("hello3.ini" )
338
+ assert os .path .exists (config_filename2 )
339
+ assert config_dirname2 in ConfigFileProcessor3 .config_searchpath
340
+
341
+ config = ConfigFileProcessor3 .read_config ()
342
+ assert config == dict (name = "Alice" , number = 2 )
343
+ assert config ["name" ] == "Alice" # -- FROM: config_file1 (prefered)
344
+ assert config ["number" ] == 2 # -- FROM: config_file2
345
+
346
+
347
+ def test_config_searchpath__param_from_primary_file_overrides_secondary (self ,
348
+ cli_runner_isolated ):
349
+ assert not os .path .exists ("hello3.ini" )
350
+ CONFIG_FILE_CONTENTS1 = """
351
+ [hello]
352
+ name = Alice
353
+ """
354
+ CONFIG_FILE_CONTENTS2 = """
355
+ [hello]
356
+ name = Bob # Will be overridden.
357
+ """
358
+ config_filename2 = os .path .join ("config" , "profile" , "hello3.ini" )
359
+ config_dirname2 = os .path .dirname (config_filename2 )
360
+ write_configfile_with_contents ("hello3.ini" , CONFIG_FILE_CONTENTS1 )
361
+ write_configfile_with_contents (config_filename2 , CONFIG_FILE_CONTENTS2 )
362
+ assert os .path .exists ("hello3.ini" )
363
+ assert os .path .exists (config_filename2 )
364
+ assert config_dirname2 in ConfigFileProcessor3 .config_searchpath
365
+
366
+ config = ConfigFileProcessor3 .read_config ()
367
+ assert config == dict (name = "Alice" )
368
+ assert config ["name" ] == "Alice" # -- FROM: config_file1 (prefered)
0 commit comments