diff options
author | Daniel Hahler <git@thequod.de> | 2019-01-17 22:06:45 +0100 |
---|---|---|
committer | Daniel Hahler <git@thequod.de> | 2019-02-11 15:49:48 +0100 |
commit | f13935da53241cd53900df758e4853c07e23a9dc (patch) | |
tree | 3c4d7f5e777e4e0c5e779dddf45b33f042abd31d /testing/test_config.py | |
parent | ed01dc6567e2ec91e0abdb8817a7423be3a930fc (diff) | |
download | pytest-f13935da53241cd53900df758e4853c07e23a9dc.tar.gz |
Display --help/--version with ArgumentErrors
Diffstat (limited to 'testing/test_config.py')
-rw-r--r-- | testing/test_config.py | 76 |
1 files changed, 73 insertions, 3 deletions
diff --git a/testing/test_config.py b/testing/test_config.py index b0b09f44a..f9f22a63e 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -8,10 +8,12 @@ import textwrap import _pytest._code import pytest from _pytest.config import _iter_rewritable_modules +from _pytest.config.exceptions import UsageError from _pytest.config.findpaths import determine_setup from _pytest.config.findpaths import get_common_ancestor from _pytest.config.findpaths import getcfg from _pytest.main import EXIT_NOTESTSCOLLECTED +from _pytest.main import EXIT_USAGEERROR class TestParseIni(object): @@ -1031,9 +1033,12 @@ class TestOverrideIniArgs(object): monkeypatch.setenv("PYTEST_ADDOPTS", "-o") config = get_config() - with pytest.raises(SystemExit) as excinfo: + with pytest.raises(UsageError) as excinfo: config._preparse(["cache_dir=ignored"], addopts=True) - assert excinfo.value.args[0] == _pytest.main.EXIT_USAGEERROR + assert ( + "error: argument -o/--override-ini: expected one argument (via PYTEST_ADDOPTS)" + in excinfo.value.args[0] + ) def test_addopts_from_ini_not_concatenated(self, testdir): """addopts from ini should not take values from normal args (#4265).""" @@ -1046,7 +1051,7 @@ class TestOverrideIniArgs(object): result = testdir.runpytest("cache_dir=ignored") result.stderr.fnmatch_lines( [ - "%s: error: argument -o/--override-ini: expected one argument" + "%s: error: argument -o/--override-ini: expected one argument (via addopts config)" % (testdir.request.config._parser.optparser.prog,) ] ) @@ -1083,3 +1088,68 @@ class TestOverrideIniArgs(object): result = testdir.runpytest("-o", "foo=1", "-o", "bar=0", "test_foo.py") assert "ERROR:" not in result.stderr.str() result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="]) + + +def test_help_via_addopts(testdir): + testdir.makeini( + """ + [pytest] + addopts = --unknown-option-should-allow-for-help --help + """ + ) + result = testdir.runpytest() + assert result.ret == 0 + result.stdout.fnmatch_lines( + [ + "usage: *", + "positional arguments:", + # Displays full/default help. + "to see available markers type: pytest --markers", + ] + ) + + +def test_help_and_version_after_argument_error(testdir): + testdir.makeconftest( + """ + def validate(arg): + raise argparse.ArgumentTypeError("argerror") + + def pytest_addoption(parser): + group = parser.getgroup('cov') + group.addoption( + "--invalid-option-should-allow-for-help", + type=validate, + ) + """ + ) + testdir.makeini( + """ + [pytest] + addopts = --invalid-option-should-allow-for-help + """ + ) + result = testdir.runpytest("--help") + result.stdout.fnmatch_lines( + [ + "usage: *", + "positional arguments:", + "NOTE: displaying only minimal help due to UsageError.", + ] + ) + result.stderr.fnmatch_lines( + [ + "ERROR: usage: *", + "%s: error: argument --invalid-option-should-allow-for-help: expected one argument" + % (testdir.request.config._parser.optparser.prog,), + ] + ) + # Does not display full/default help. + assert "to see available markers type: pytest --markers" not in result.stdout.lines + assert result.ret == EXIT_USAGEERROR + + result = testdir.runpytest("--version") + result.stderr.fnmatch_lines( + ["*pytest*{}*imported from*".format(pytest.__version__)] + ) + assert result.ret == EXIT_USAGEERROR |