Add mockapi source files and tests
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
2026-03-22 21:59:08 +00:00
parent 448d51a762
commit 40220881e0

View File

@@ -51,7 +51,7 @@ def validate(spec_file: str, fmt: Optional[str]):
click.echo(f" - {error}", err=True)
sys.exit(1)
else:
click.echo(" Specification is valid!")
click.echo("\u2713 Specification is valid!")
click.echo(f" Paths: {len(spec.get('paths', {}))}")
click.echo(f" Schemas: {len(spec.get('components', {}).get('schemas', {}))}")
@@ -62,53 +62,13 @@ def validate(spec_file: str, fmt: Optional[str]):
@cli.command()
@click.argument("spec_file", type=click.Path(exists=True))
@click.option(
"--port",
"-p",
type=int,
default=None,
help="Port to run the mock server on (default: from config or 8080)",
)
@click.option(
"--host",
"-h",
default=None,
help="Host to bind to (default: from config or 0.0.0.0)",
)
@click.option(
"--delay",
"-d",
type=int,
default=None,
help="Fixed response delay in milliseconds",
)
@click.option(
"--random-delay",
is_flag=True,
default=None,
help="Use random delays instead of fixed",
)
@click.option(
"--config",
"-c",
type=click.Path(exists=True),
default=None,
help="Path to mockapi.yaml configuration file",
)
@click.option(
"--watch",
"-w",
is_flag=True,
default=False,
help="Enable hot-reload on spec file changes",
)
@click.option(
"--verbose",
"-v",
is_flag=True,
default=False,
help="Enable verbose output",
)
@click.option("--port", "-p", type=int, default=None, help="Port to run the mock server on")
@click.option("--host", "-h", default=None, help="Host to bind to")
@click.option("--delay", "-d", type=int, default=None, help="Fixed response delay in milliseconds")
@click.option("--random-delay", is_flag=True, default=None, help="Use random delays")
@click.option("--config", "-c", type=click.Path(exists=True), default=None, help="Path to mockapi.yaml")
@click.option("--watch", "-w", is_flag=True, default=False, help="Enable hot-reload")
@click.option("--verbose", "-v", is_flag=True, default=False, help="Enable verbose output")
def start(
spec_file: str,
port: Optional[int],
@@ -119,10 +79,7 @@ def start(
watch: bool,
verbose: bool,
):
"""Start a mock API server from an OpenAPI specification.
SPEC_FILE: Path to the OpenAPI spec file (YAML or JSON)
"""
"""Start a mock API server from an OpenAPI specification."""
try:
cfg = Config.load(config_path=config)
@@ -148,7 +105,6 @@ def start(
if verbose:
click.echo(f"Starting mock server on {cfg.host}:{cfg.port}")
click.echo(f"Spec file: {spec_file}")
generator = MockServerGenerator(spec, cfg)
app = generator.generate()
@@ -159,12 +115,7 @@ def start(
reloader.start_watching()
else:
import uvicorn
uvicorn.run(
app,
host=cfg.host,
port=cfg.port,
log_level="info" if verbose else "warning",
)
uvicorn.run(app, host=cfg.host, port=cfg.port, log_level="info" if verbose else "warning")
except Exception as e:
click.echo(f"Error: {e}", err=True)
@@ -176,18 +127,9 @@ def start(
@cli.command()
@click.argument("spec_file", type=click.Path(exists=True))
@click.option(
"--output",
"-o",
type=click.Path(),
default=None,
help="Output file path (default: stdout)",
)
@click.option("--output", "-o", type=click.Path(), default=None, help="Output file path")
def generate(spec_file: str, output: Optional[str]):
"""Generate code/structure from an OpenAPI spec (dry-run mode).
SPEC_FILE: Path to the OpenAPI spec file (YAML or JSON)
"""
"""Generate code from an OpenAPI spec (dry-run mode)."""
try:
loader = SpecLoader(spec_file)
spec = loader.load()
@@ -238,15 +180,9 @@ def generate(spec_file: str, output: Optional[str]):
@cli.command()
@click.option(
"--config",
"-c",
type=click.Path(exists=True),
default=None,
help="Path to mockapi.yaml configuration file",
)
@click.option("--config", "-c", type=click.Path(exists=True), default=None, help="Path to mockapi.yaml")
def show_config(config: Optional[str]):
"""Show the current configuration (from file and defaults)."""
"""Show the current configuration."""
try:
cfg = Config.load(config_path=config)
click.echo("Current MockAPI Configuration:")
@@ -262,4 +198,4 @@ def show_config(config: Optional[str]):
if __name__ == "__main__":
cli()
cli()