1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
"""Test suite for prance.cli ."""
__author__ = "Jens Finkhaeuser"
__copyright__ = "Copyright (c) 2016-2021 Jens Finkhaeuser"
__license__ = "MIT"
__all__ = ()
import pytest
from . import none_of
@pytest.fixture
def runner():
from click.testing import CliRunner
return CliRunner()
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_validate_defaults(runner):
from prance import cli
# Good example
result = runner.invoke(cli.validate, ["tests/specs/petstore.yaml"])
assert result.exit_code == 0
expected = """Processing "tests/specs/petstore.yaml"...
-> Resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
"""
assert result.output == expected
# Bad example
result = runner.invoke(cli.validate, ["tests/specs/definitions.yaml"])
assert result.exit_code == 1
assert "ValidationError" in result.output
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_validate_multiple(runner):
from prance import cli
result = runner.invoke(
cli.validate, ["tests/specs/petstore.yaml", "tests/specs/petstore.yaml"]
)
assert result.exit_code == 0
expected = """Processing "tests/specs/petstore.yaml"...
-> Resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
Processing "tests/specs/petstore.yaml"...
-> Resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
"""
assert result.output == expected
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_validate_no_resolve(runner):
from prance import cli
# Good example
result = runner.invoke(cli.validate, ["--no-resolve", "tests/specs/petstore.yaml"])
assert result.exit_code == 0
expected = """Processing "tests/specs/petstore.yaml"...
-> Not resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
"""
assert result.output == expected
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_validate_output_too_many_inputs(runner):
from prance import cli
result = runner.invoke(
cli.validate,
["-o", "foo", "tests/specs/petstore.yaml", "tests/specs/petstore.yaml"],
)
assert result.exit_code == 2
assert "If --output-file is given," in result.output
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_validate_output(runner):
from prance import cli
import os, os.path
curdir = os.getcwd()
outnames = ["foo.json", "foo.yaml"]
for outname in outnames:
with runner.isolated_filesystem():
result = runner.invoke(
cli.validate,
["-o", outname, os.path.join(curdir, "tests/specs/petstore.yaml")],
)
assert result.exit_code == 0
# There also must be a 'foo' file now.
files = [f for f in os.listdir(".") if os.path.isfile(f)]
assert outname in files
# Ensure a UTF-8 file encoding.
from prance.util import fs
assert "utf-8" in fs.detect_encoding(
outname, default_to_utf8=False, read_all=True
)
# Now do a full encoding detection, too
# The 'foo' file must be a valid swagger spec.
result = runner.invoke(cli.validate, [outname])
assert result.exit_code == 0
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_compile_defaults(runner):
from prance import cli
# Good example
result = runner.invoke(cli.compile, ["tests/specs/petstore.yaml"])
assert result.exit_code == 0
expected = """Processing "tests/specs/petstore.yaml"...
-> Resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
"""
assert result.output.startswith(expected)
# Bad example
result = runner.invoke(cli.validate, ["tests/specs/definitions.yaml"])
assert result.exit_code == 1
assert "ValidationError" in result.output
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
def test_compile_output(runner):
from prance import cli
import os, os.path
curdir = os.getcwd()
outnames = ["foo.json", "foo.yaml"]
for outname in outnames:
with runner.isolated_filesystem():
result = runner.invoke(
cli.compile,
[os.path.join(curdir, "tests/specs/petstore.yaml"), outname],
)
assert result.exit_code == 0
# There also must be a 'foo' file now.
files = [f for f in os.listdir(".") if os.path.isfile(f)]
assert outname in files
# Ensure a UTF-8 file encoding.
from prance.util import fs
assert "utf-8" in fs.detect_encoding(
outname, default_to_utf8=False, read_all=True
)
# Now do a full encoding detection, too
# The 'foo' file must be a valid swagger spec.
result = runner.invoke(cli.validate, [outname])
assert result.exit_code == 0
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
@pytest.mark.requires_network()
def test_convert_defaults(runner):
from prance import cli
# Good example
result = runner.invoke(cli.convert, ["tests/specs/petstore.yaml"])
assert result.exit_code == 0
assert "openapi" in result.output
assert "3." in result.output
# Bad example
result = runner.invoke(cli.validate, ["tests/specs/definitions.yaml"])
assert result.exit_code == 1
assert "ValidationError" in result.output
@pytest.mark.skipif(none_of("click"), reason="Click does not exist")
@pytest.mark.requires_network()
def test_convert_output(runner):
from prance import cli
import os, os.path
curdir = os.getcwd()
outnames = ["foo.json", "foo.yaml"]
for outname in outnames:
with runner.isolated_filesystem():
result = runner.invoke(
cli.convert,
[os.path.join(curdir, "tests/specs/petstore.yaml"), outname],
)
assert result.exit_code == 0
# There also must be a 'foo' file now.
files = [f for f in os.listdir(".") if os.path.isfile(f)]
assert outname in files
# Ensure a UTF-8 file encoding.
from prance.util import fs
assert "utf-8" in fs.detect_encoding(
outname, default_to_utf8=False, read_all=True
)
# Now do a full encoding detection, too
contents = fs.read_file(outname)
assert "openapi" in contents
assert "3." in contents
|