Skip to content
Open
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
7 changes: 7 additions & 0 deletions promptlens/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def run(
with open(config, "r") as f:
config_data = yaml.safe_load(f)

if config_data is None:
console.print("[red]Invalid configuration: configuration file is empty[/red]")
sys.exit(1)
if not isinstance(config_data, dict):
console.print("[red]Invalid configuration: top-level YAML must be a mapping/object[/red]")
sys.exit(1)

# Override with CLI options
if golden_set:
config_data["golden_set"] = golden_set
Expand Down
27 changes: 27 additions & 0 deletions tests/test_cli_config_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import Path

from click.testing import CliRunner

from promptlens.cli import cli


def test_run_rejects_empty_config_file(tmp_path: Path) -> None:
config = tmp_path / "empty.yaml"
config.write_text("", encoding="utf-8")

runner = CliRunner()
result = runner.invoke(cli, ["run", str(config), "--dry-run"])

assert result.exit_code == 1
assert "configuration file is empty" in result.output


def test_run_rejects_non_mapping_top_level_config(tmp_path: Path) -> None:
config = tmp_path / "list.yaml"
config.write_text("- a\n- b\n", encoding="utf-8")

runner = CliRunner()
result = runner.invoke(cli, ["run", str(config), "--dry-run"])

assert result.exit_code == 1
assert "top-level YAML must be a mapping/object" in result.output