Skip to content

Commit

Permalink
fix: Crash with --hypothesis-deadline=0 CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Feb 9, 2020
1 parent 790350a commit 9a385f5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -14,6 +14,7 @@ Fixed
- Crash on invalid value in ``header`` CLI options. `#405`_
- Crash on some invalid URLs in ``schema`` CLI option. `#406`_
- Validation of ``--request-timeout`` parameter. `#407`_
- Crash with ``--hypothesis-deadline=0`` CLI option. `#410`_

`0.24.1`_ - 2020-02-08
----------------------
Expand Down Expand Up @@ -696,6 +697,7 @@ Fixed
.. _0.3.0: https://github.com/kiwicom/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/kiwicom/schemathesis/compare/v0.1.0...v0.2.0

.. _#410: https://github.com/kiwicom/schemathesis/issues/410
.. _#407: https://github.com/kiwicom/schemathesis/issues/407
.. _#406: https://github.com/kiwicom/schemathesis/issues/406
.. _#405: https://github.com/kiwicom/schemathesis/issues/405
Expand Down
2 changes: 1 addition & 1 deletion src/schemathesis/cli/__init__.py
Expand Up @@ -123,7 +123,7 @@ def schemathesis(pre_run: Optional[str] = None) -> None:
@click.option(
"--hypothesis-deadline",
help="Duration in milliseconds that each individual example with a test is not allowed to exceed.",
type=OptionalInt(),
type=OptionalInt(1),
)
@click.option("--hypothesis-derandomize", help="Use Hypothesis's deterministic mode.", is_flag=True, default=None)
@click.option(
Expand Down
5 changes: 3 additions & 2 deletions src/schemathesis/cli/options.py
Expand Up @@ -29,13 +29,14 @@ class NotSet:
not_set = NotSet()


class OptionalInt(click.types.IntParamType):
class OptionalInt(click.types.IntRange):
def convert( # type: ignore
self, value: str, param: Optional[click.core.Parameter], ctx: Optional[click.core.Context]
) -> Union[int, NotSet]:
if value == "None":
return not_set
try:
return int(value)
int(value)
return super().convert(value, param, ctx)
except (ValueError, UnicodeError):
self.fail("%s is not a valid integer or None" % value, param, ctx)
7 changes: 6 additions & 1 deletion test/cli/test_commands.py
Expand Up @@ -112,6 +112,10 @@ def test_commands_version(cli):
("run", "http://127.0.0.1", "--hypothesis-deadline=wrong"),
'Error: Invalid value for "--hypothesis-deadline": wrong is not a valid integer or None',
),
(
("run", "http://127.0.0.1", "--hypothesis-deadline=0"),
'Error: Invalid value for "--hypothesis-deadline": 0 is smaller than the minimum valid value 1.',
),
(
("run", "http://127.0.0.1", "--header=тест:test"),
'Error: Invalid value for "--header" / "-H": Header name should be latin-1 encodable',
Expand Down Expand Up @@ -170,7 +174,8 @@ def test_commands_run_help(cli):
" during the test run.",
" --validate-schema BOOLEAN Enable or disable validation of input schema.",
" --show-errors-tracebacks Show full tracebacks for internal errors.",
" --hypothesis-deadline INTEGER Duration in milliseconds that each individual",
" --hypothesis-deadline INTEGER RANGE",
" Duration in milliseconds that each individual",
" example with a test is not allowed to exceed.",
" --hypothesis-derandomize Use Hypothesis's deterministic mode.",
" --hypothesis-max-examples INTEGER",
Expand Down
2 changes: 2 additions & 0 deletions test/cli/test_crashes.py
Expand Up @@ -57,6 +57,7 @@ def paths(draw):
"workers": st.integers(min_value=1, max_value=64),
"request-timeout": st.integers(),
"validate-schema": st.booleans(),
"hypothesis-deadline": st.integers() | st.none(),
},
).map(lambda params: [f"--{key}={value}" for key, value in params.items()]),
flags=st.fixed_dictionaries(
Expand All @@ -74,6 +75,7 @@ def paths(draw):
).map(lambda params: [f"--{key}={value}" for key, values in params.items() for value in values]),
)
@example(params=[], flags=[], multiple_params=["--header=0:0\r"])
@example(params=["--hypothesis-deadline=0"], flags=[], multiple_params=[])
@pytest.mark.usefixtures("mocked_schema")
def test_valid_parameters_combos(cli, schema_url, params, flags, multiple_params):
result = cli.run(schema_url, *params, *multiple_params, *flags)
Expand Down

0 comments on commit 9a385f5

Please sign in to comment.