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:
		
							parent
							
								
									54c88a6cf3
								
							
						
					
					
						commit
						c8264385ea
					
				|  | @ -833,6 +833,14 @@ def exists(path, ignore=EnvironmentError): | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| def getcfg(args, inibasenames): | 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("-")] |     args = [x for x in args if not str(x).startswith("-")] | ||||||
|     if not args: |     if not args: | ||||||
|         args = [py.path.local()] |         args = [py.path.local()] | ||||||
|  |  | ||||||
|  | @ -38,6 +38,9 @@ def pytest_addoption(parser): | ||||||
|                help="exit after first num failures or errors.") |                help="exit after first num failures or errors.") | ||||||
|     group._addoption('--strict', action="store_true", |     group._addoption('--strict', action="store_true", | ||||||
|                help="run pytest in strict mode, warnings become errors.") |                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 = parser.getgroup("collect", "collection") | ||||||
|     group.addoption('--collectonly', '--collect-only', action="store_true", |     group.addoption('--collectonly', '--collect-only', action="store_true", | ||||||
|  |  | ||||||
|  | @ -79,6 +79,21 @@ class TestConfigCmdlineParsing: | ||||||
|         config = testdir.parseconfig() |         config = testdir.parseconfig() | ||||||
|         pytest.raises(AssertionError, lambda: config.parse([])) |         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: | class TestConfigAPI: | ||||||
|     def test_config_trace(self, testdir): |     def test_config_trace(self, testdir): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue