Implement the "-c" command line switch that allows to explicitly specifiy the config file to load.

This feature was requested in issue #174.

--HG--
branch : explicit-ini-filename
This commit is contained in:
christian@christian-linux.sarrazin.local 2014-06-27 17:42:37 +02:00
parent 54c88a6cf3
commit c8264385ea
3 changed files with 26 additions and 0 deletions

View File

@ -833,6 +833,14 @@ def exists(path, ignore=EnvironmentError):
return False
def getcfg(args, inibasenames):
if "-c" in args:
n = len(args)
for i in range(1, n):
if args[i - 1] == "-c" and not str(args[i]).startswith("-"):
iniconfig = py.iniconfig.IniConfig(args[i])
if 'pytest' in iniconfig.sections:
return iniconfig['pytest']
return {}
args = [x for x in args if not str(x).startswith("-")]
if not args:
args = [py.path.local()]

View File

@ -38,6 +38,9 @@ def pytest_addoption(parser):
help="exit after first num failures or errors.")
group._addoption('--strict', action="store_true",
help="run pytest in strict mode, warnings become errors.")
# This option is never used as such, see config.getcfg().
group._addoption("-c", metavar="file", type=str, dest="_inifilename",
help="load configuration from `file` instead of trying to locate one of the implicit configuration files.")
group = parser.getgroup("collect", "collection")
group.addoption('--collectonly', '--collect-only', action="store_true",

View File

@ -79,6 +79,21 @@ class TestConfigCmdlineParsing:
config = testdir.parseconfig()
pytest.raises(AssertionError, lambda: config.parse([]))
def test_explicitly_specified_config_file_is_loaded(self, testdir):
testdir.makeconftest("""
def pytest_addoption(parser):
parser.addini("custom", "")
""")
testdir.makeini("""
[pytest]
custom = 0
""")
testdir.makefile(".cfg", custom = """
[pytest]
custom = 1
""")
config = testdir.parseconfig("-c", "custom.cfg")
assert config.getini("custom") == "1"
class TestConfigAPI:
def test_config_trace(self, testdir):