Skip to content

Commit

Permalink
Improve error handling when dbt manifest is not found (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieucan authored May 27, 2024
1 parent 7050680 commit 38be817
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ max-complexity = 10
convention = "google"

[tool.ruff.lint.pylint]
max-args = 6
max-args = 8

[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = [
Expand Down
14 changes: 13 additions & 1 deletion src/dbt_score/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""CLI interface."""

import logging
from pathlib import Path
from typing import Final, Literal

Expand All @@ -12,6 +13,8 @@
from dbt_score.parse import dbt_parse, get_default_manifest_path
from dbt_score.rule_catalog import display_catalog

logger = logging.getLogger(__name__)

BANNER: Final[str] = r"""
__ __ __
____/ // /_ / /_ _____ _____ ____ _____ ___
Expand Down Expand Up @@ -75,7 +78,9 @@ def cli() -> None:
is_flag=True,
default=False,
)
@click.pass_context
def lint(
ctx: click.Context,
format: Literal["plain", "manifest"],
select: tuple[str],
namespace: list[str],
Expand All @@ -101,7 +106,14 @@ def lint(
if run_dbt_parse:
dbt_parse()

lint_dbt_project(manifest_path=manifest, config=config, format=format)
try:
lint_dbt_project(manifest_path=manifest, config=config, format=format)
except FileNotFoundError:
logger.error(
"dbt's manifest.json could not be found. If you're in a dbt project, be "
"sure to run 'dbt parse' first, or use the option '--run-dbt-parse'."
)
ctx.exit(2)


@cli.command(name="list")
Expand Down
2 changes: 1 addition & 1 deletion tests/formatters/test_manifest_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_manifest_formatter_model(
assert stdout == ""


def test_human_readable_formatter_project( # noqa: PLR0913
def test_manifest_formatter_project(
capsys,
manifest_loader,
model1,
Expand Down
22 changes: 12 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from unittest.mock import patch

import pytest
from click.testing import CliRunner
from dbt_score.cli import lint

Expand All @@ -25,18 +24,21 @@ def test_lint_existing_manifest(manifest_path):
assert result.exit_code == 0


def test_lint_non_existing_manifest():
def test_lint_non_existing_manifest(caplog):
"""Test lint with a non-existing manifest."""
runner = CliRunner()

# Provide manifest in command line
with pytest.raises(FileNotFoundError):
with patch("dbt_score.cli.Config._load_toml_file"):
runner.invoke(
lint, ["--manifest", "fake_manifest.json"], catch_exceptions=False
)
with patch("dbt_score.cli.Config._load_toml_file"):
result = runner.invoke(
lint, ["--manifest", "fake_manifest.json"], catch_exceptions=False
)
assert result.exit_code == 2
assert "dbt's manifest.json could not be found" in caplog.text

# Use default manifest path
with pytest.raises(FileNotFoundError):
with patch("dbt_score.cli.Config._load_toml_file"):
runner.invoke(lint, catch_exceptions=False)
with patch("dbt_score.cli.Config._load_toml_file"):
result = runner.invoke(lint, catch_exceptions=False)

assert result.exit_code == 2
assert "dbt's manifest.json could not be found" in caplog.text

0 comments on commit 38be817

Please sign in to comment.