-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optionally running `dbt parse` during `lint`. - When a manifest is provided through `cli` (and it exists, because otherwise it will raise an error), `dbt parse` is not executed. - When a manifest exists in the dbt project, on its specified location, `dbt parse` is not executed. --------- Co-authored-by: Matthieu Caneill <[email protected]>
- Loading branch information
1 parent
5f414cd
commit 133ec81
Showing
8 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Lint dbt models metadata.""" | ||
|
||
from pathlib import Path | ||
|
||
|
||
def lint_dbt_project(manifest_path: Path) -> None: | ||
"""Lint dbt manifest.""" | ||
if not manifest_path.exists(): | ||
raise FileNotFoundError(f"Manifest not found at {manifest_path}.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""dbt utilities.""" | ||
|
||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
from dbt.cli.main import dbtRunner, dbtRunnerResult | ||
|
||
|
||
class DbtParseException(Exception): | ||
"""Raised when dbt parse fails.""" | ||
|
||
|
||
def dbt_parse() -> dbtRunnerResult: | ||
"""Parse a dbt project. | ||
Returns: | ||
The dbt parse run result. | ||
Raises: | ||
DbtParseException: dbt parse failed. | ||
""" | ||
dbt_logger_stdout = logging.getLogger("stdout_log") | ||
dbt_logger_stdout.disabled = True | ||
|
||
result: dbtRunnerResult = dbtRunner().invoke(["parse"]) | ||
|
||
dbt_logger_stdout.disabled = False | ||
|
||
if not result.success: | ||
raise DbtParseException("dbt parse failed.") from result.exception | ||
|
||
return result | ||
|
||
|
||
def get_default_manifest_path() -> Path: | ||
"""Get the manifest path.""" | ||
return ( | ||
Path().cwd() | ||
/ os.getenv("DBT_PROJECT_DIR", "") | ||
/ os.getenv("DBT_TARGET_DIR", "target") | ||
/ "manifest.json" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Test the CLI.""" | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
from dbt_score.cli import lint | ||
|
||
|
||
def test_invalid_options(): | ||
"""Test invalid cli options.""" | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
lint, ["--manifest", "fake_manifest.json", "--run-dbt-parse"] | ||
) | ||
assert result.exit_code == 2 # pylint: disable=PLR2004 | ||
|
||
|
||
def test_lint_existing_manifest(manifest_path): | ||
"""Test lint with an existing manifest.""" | ||
runner = CliRunner() | ||
result = runner.invoke(lint, ["--manifest", manifest_path]) | ||
assert result.exit_code == 0 | ||
|
||
|
||
def test_lint_non_existing_manifest(): | ||
"""Test lint with a non-existing manifest.""" | ||
runner = CliRunner() | ||
|
||
# Provide manifest in command line. | ||
with pytest.raises(FileNotFoundError): | ||
runner.invoke( | ||
lint, ["--manifest", "fake_manifest.json"], catch_exceptions=False | ||
) | ||
|
||
# Use default manifest path.. | ||
with pytest.raises(FileNotFoundError): | ||
runner.invoke(lint, catch_exceptions=False) |