Improve output for missing config keys (#7572)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
		
							parent
							
								
									9bfd14a443
								
							
						
					
					
						commit
						89305e7b09
					
				|  | @ -1 +1 @@ | ||||||
| When a plugin listed in ``required_plugins`` is missing, a simple error message is now shown instead of a stacktrace. | When a plugin listed in ``required_plugins`` is missing or an unknown config key is used with ``--strict-config``, a simple error message is now shown instead of a stacktrace. | ||||||
|  |  | ||||||
|  | @ -1252,7 +1252,7 @@ class Config: | ||||||
| 
 | 
 | ||||||
|     def _warn_or_fail_if_strict(self, message: str) -> None: |     def _warn_or_fail_if_strict(self, message: str) -> None: | ||||||
|         if self.known_args_namespace.strict_config: |         if self.known_args_namespace.strict_config: | ||||||
|             fail(message, pytrace=False) |             raise UsageError(message) | ||||||
| 
 | 
 | ||||||
|         self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3) |         self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -181,7 +181,7 @@ class TestParseIni: | ||||||
|     @pytest.mark.parametrize( |     @pytest.mark.parametrize( | ||||||
|         "ini_file_text, invalid_keys, warning_output, exception_text", |         "ini_file_text, invalid_keys, warning_output, exception_text", | ||||||
|         [ |         [ | ||||||
|             ( |             pytest.param( | ||||||
|                 """ |                 """ | ||||||
|                 [pytest] |                 [pytest] | ||||||
|                 unknown_ini = value1 |                 unknown_ini = value1 | ||||||
|  | @ -194,8 +194,9 @@ class TestParseIni: | ||||||
|                     "*PytestConfigWarning:*Unknown config option: unknown_ini", |                     "*PytestConfigWarning:*Unknown config option: unknown_ini", | ||||||
|                 ], |                 ], | ||||||
|                 "Unknown config option: another_unknown_ini", |                 "Unknown config option: another_unknown_ini", | ||||||
|  |                 id="2-unknowns", | ||||||
|             ), |             ), | ||||||
|             ( |             pytest.param( | ||||||
|                 """ |                 """ | ||||||
|                 [pytest] |                 [pytest] | ||||||
|                 unknown_ini = value1 |                 unknown_ini = value1 | ||||||
|  | @ -207,8 +208,9 @@ class TestParseIni: | ||||||
|                     "*PytestConfigWarning:*Unknown config option: unknown_ini", |                     "*PytestConfigWarning:*Unknown config option: unknown_ini", | ||||||
|                 ], |                 ], | ||||||
|                 "Unknown config option: unknown_ini", |                 "Unknown config option: unknown_ini", | ||||||
|  |                 id="1-unknown", | ||||||
|             ), |             ), | ||||||
|             ( |             pytest.param( | ||||||
|                 """ |                 """ | ||||||
|                 [some_other_header] |                 [some_other_header] | ||||||
|                 unknown_ini = value1 |                 unknown_ini = value1 | ||||||
|  | @ -218,8 +220,9 @@ class TestParseIni: | ||||||
|                 [], |                 [], | ||||||
|                 [], |                 [], | ||||||
|                 "", |                 "", | ||||||
|  |                 id="unknown-in-other-header", | ||||||
|             ), |             ), | ||||||
|             ( |             pytest.param( | ||||||
|                 """ |                 """ | ||||||
|                 [pytest] |                 [pytest] | ||||||
|                 minversion = 5.0.0 |                 minversion = 5.0.0 | ||||||
|  | @ -227,8 +230,9 @@ class TestParseIni: | ||||||
|                 [], |                 [], | ||||||
|                 [], |                 [], | ||||||
|                 "", |                 "", | ||||||
|  |                 id="no-unknowns", | ||||||
|             ), |             ), | ||||||
|             ( |             pytest.param( | ||||||
|                 """ |                 """ | ||||||
|                 [pytest] |                 [pytest] | ||||||
|                 conftest_ini_key = 1 |                 conftest_ini_key = 1 | ||||||
|  | @ -236,6 +240,7 @@ class TestParseIni: | ||||||
|                 [], |                 [], | ||||||
|                 [], |                 [], | ||||||
|                 "", |                 "", | ||||||
|  |                 id="1-known", | ||||||
|             ), |             ), | ||||||
|         ], |         ], | ||||||
|     ) |     ) | ||||||
|  | @ -249,7 +254,8 @@ class TestParseIni: | ||||||
|                 parser.addini("conftest_ini_key", "") |                 parser.addini("conftest_ini_key", "") | ||||||
|             """ |             """ | ||||||
|         ) |         ) | ||||||
|         testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text)) |         testdir.makepyfile("def test(): pass") | ||||||
|  |         testdir.makeini(ini_file_text) | ||||||
| 
 | 
 | ||||||
|         config = testdir.parseconfig() |         config = testdir.parseconfig() | ||||||
|         assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys) |         assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys) | ||||||
|  | @ -257,9 +263,13 @@ class TestParseIni: | ||||||
|         result = testdir.runpytest() |         result = testdir.runpytest() | ||||||
|         result.stdout.fnmatch_lines(warning_output) |         result.stdout.fnmatch_lines(warning_output) | ||||||
| 
 | 
 | ||||||
|         if exception_text: |  | ||||||
|         result = testdir.runpytest("--strict-config") |         result = testdir.runpytest("--strict-config") | ||||||
|             result.stdout.fnmatch_lines("INTERNALERROR>*" + exception_text) |         if exception_text: | ||||||
|  |             result.stderr.fnmatch_lines("ERROR: " + exception_text) | ||||||
|  |             assert result.ret == pytest.ExitCode.USAGE_ERROR | ||||||
|  |         else: | ||||||
|  |             result.stderr.no_fnmatch_line(exception_text) | ||||||
|  |             assert result.ret == pytest.ExitCode.OK | ||||||
| 
 | 
 | ||||||
|     @pytest.mark.filterwarnings("default") |     @pytest.mark.filterwarnings("default") | ||||||
|     def test_silence_unknown_key_warning(self, testdir: Testdir) -> None: |     def test_silence_unknown_key_warning(self, testdir: Testdir) -> None: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue