Skip to content

Commit 0db0701

Browse files
committed
FIX: Use config_searchpath when generating config_file names.
1 parent 59109d3 commit 0db0701

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# =========================================================================
2-
# COVERAGE CONFIGURATIO FILE: .coveragerc
2+
# COVERAGE CONFIGURATION FILE: .coveragerc
33
# =========================================================================
44
# LANGUAGE: Python
55
# SEE ALSO:

click_configfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def hello(ctx, name):
382382
@classmethod
383383
def read_config(cls):
384384
configfile_names = list(
385-
generate_configfile_names(cls.config_files))
385+
generate_configfile_names(cls.config_files, cls.config_searchpath))
386386
parser = configparser.ConfigParser()
387387
parser.optionxform = str
388388
parser.read(configfile_names)

tests/functional/test_basics.py

+88-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ConfigSectionSchema1(object):
2121
@matches_section("hello")
2222
class Hello(SectionSchema):
2323
name = Param(type=str)
24+
number = Param(type=int)
2425

2526
@matches_section("hello.more.*")
2627
class HelloMore(SectionSchema):
@@ -68,6 +69,17 @@ class ConfigFileProcessor2(ConfigFileReader):
6869
]
6970

7071

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+
7183
# -----------------------------------------------------------------------------
7284
# TEST SUITE
7385
# -----------------------------------------------------------------------------
@@ -252,7 +264,7 @@ def hello2(ctx, name):
252264
assert result.exit_code == 0
253265

254266

255-
class TestCandidate3(object):
267+
class TestCandidate2B(object):
256268

257269
def test_configfile__with_unbound_section(self, cli_runner_isolated):
258270
# -- DEFINITION: unbound section
@@ -279,3 +291,78 @@ class ConfigFileProcessorWithUnboundSection(ConfigFileProcessor2):
279291

280292
expected = "LookupError: No schema found for: section=unbound.section"
281293
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

Comments
 (0)