Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/hookrelay/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def _read_data(spec: str | None) -> str:


def cmd_run(args: argparse.Namespace) -> int:
config = load_config(args.config)
try:
config = load_config(args.config)
except ConfigError as e:
print(f"config error: {e}", file=sys.stderr)
return 1
logging.basicConfig(
level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s"
)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json

import hookrelay.cli as cli

BASE_CONFIG = {
"routes": [
{
"name": "r1",
"match": {"path": "/a"},
"sinks": [{"type": "generic", "url": "http://example.invalid"}],
}
]
}


def _write_config(tmp_path, data):
p = tmp_path / "config.json"
p.write_text(json.dumps(data), encoding="utf-8")
return str(p)


def test_run_missing_config_prints_clean_error(tmp_path, capsys):
missing = str(tmp_path / "does-not-exist.json")
rc = cli.main(["run", "-c", missing])
assert rc == 1
err = capsys.readouterr().err
assert "config error:" in err
assert "Traceback" not in err


def test_run_invalid_config_prints_clean_error(tmp_path, capsys):
# missing required 'routes' key
path = _write_config(tmp_path, {})
rc = cli.main(["run", "-c", path])
assert rc == 1
err = capsys.readouterr().err
assert "config error:" in err
assert "Traceback" not in err


def test_run_valid_config_starts_server(tmp_path, monkeypatch):
path = _write_config(tmp_path, BASE_CONFIG)
calls = []
monkeypatch.setattr(cli, "serve", lambda config: calls.append(config))
rc = cli.main(["run", "-c", path])
assert rc == 0
assert len(calls) == 1
assert calls[0].routes[0].name == "r1"