Skip to content

Commit

Permalink
fix: CLI crash on Windows and Python < 3.8 when the schema path conta…
Browse files Browse the repository at this point in the history
…ins characters unrepresentable at the OS level.
  • Loading branch information
chr1st1ank authored and Stranger6667 committed Feb 8, 2020
1 parent f2e160d commit 974e1d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -6,6 +6,11 @@ Changelog
`Unreleased`_
-------------

Fixed
~~~~~

- CLI crash on Windows and Python < 3.8 when the schema path contains characters unrepresentable at the OS level. `#400`_

`0.24.0`_ - 2020-02-07
----------------------

Expand Down Expand Up @@ -678,6 +683,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

.. _#400: https://github.com/kiwicom/schemathesis/issues/400
.. _#394: https://github.com/kiwicom/schemathesis/issues/394
.. _#391: https://github.com/kiwicom/schemathesis/issues/391
.. _#386: https://github.com/kiwicom/schemathesis/issues/386
Expand Down
3 changes: 1 addition & 2 deletions src/schemathesis/cli/__init__.py
@@ -1,4 +1,3 @@
import pathlib
import traceback
from contextlib import contextmanager
from enum import Enum
Expand Down Expand Up @@ -193,7 +192,7 @@ def run( # pylint: disable=too-many-arguments

with abort_on_network_errors():
options.update({"checks": selected_checks, "workers_num": workers_num})
if pathlib.Path(schema).is_file():
if utils.file_exists(schema):
options["loader"] = from_path
elif app is not None and not urlparse(schema).netloc:
# If `schema` is not an existing filesystem path or an URL then it is considered as an endpoint with
Expand Down
11 changes: 1 addition & 10 deletions src/schemathesis/cli/callbacks.py
@@ -1,4 +1,3 @@
import pathlib
import re
import sys
from contextlib import contextmanager
Expand All @@ -13,21 +12,13 @@

def validate_schema(ctx: click.core.Context, param: click.core.Parameter, raw_value: str) -> str:
if "app" not in ctx.params and not urlparse(raw_value).netloc:
if "\x00" in raw_value or not _verify_path(raw_value):
if "\x00" in raw_value or not utils.file_exists(raw_value):
raise click.UsageError("Invalid SCHEMA, must be a valid URL or file path.")
if "base_url" not in ctx.params:
raise click.UsageError('Missing argument, "--base-url" is required for SCHEMA specified by file.')
return raw_value


def _verify_path(path: str) -> bool:
try:
return pathlib.Path(path).is_file()
except OSError:
# For example, path could be too long
return False


def validate_base_url(ctx: click.core.Context, param: click.core.Parameter, raw_value: str) -> str:
if raw_value and not urlparse(raw_value).netloc:
raise click.UsageError("Invalid base URL")
Expand Down
9 changes: 9 additions & 0 deletions src/schemathesis/utils.py
@@ -1,4 +1,5 @@
import cgi
import pathlib
import traceback
import warnings
from contextlib import contextmanager
Expand All @@ -16,6 +17,14 @@
NOT_SET = NotSet()


def file_exists(path: str) -> bool:
try:
return pathlib.Path(path).is_file()
except OSError:
# For example, path could be too long
return False


def deprecated(func: Callable, message: str) -> Callable:
"""Emit a warning if the given function is used."""

Expand Down

0 comments on commit 974e1d7

Please sign in to comment.