Skip to content

Commit

Permalink
Merge pull request #45 from godatadriven/cleaner-args
Browse files Browse the repository at this point in the history
Deprecate dbt-artifacts-dir arg
  • Loading branch information
pgoslatara authored Jul 22, 2024
2 parents 8cc3e5b + 4ffccea commit 204a91e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ jobs:
docker run --rm \
--volume "$PWD/dbt_project/target":/dbt_project/target \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:ci \
/dbt-bouncer.pex --config-file dbt-bouncer-example.yml --dbt-artifacts-dir dbt_project/target
/dbt-bouncer.pex --config-file dbt-bouncer-example.yml
github-action-tests:
needs: [pre-commit]
Expand All @@ -213,7 +213,7 @@ jobs:
- name: Run action
uses: ./
with:
dbt-artifacts-dir: dbt_project/target
config-file: dbt-bouncer-example.yml


pex-tests:
Expand Down Expand Up @@ -258,4 +258,4 @@ jobs:
run: make build-pex

- name: Test pex file
run: ./dist/dbt-bouncer.pex --config-file dbt-bouncer-example.yml --dbt-artifacts-dir dbt_project/target
run: ./dist/dbt-bouncer.pex --config-file dbt-bouncer-example.yml
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Configure and enforce conventions for your dbt project.

# How to use

Generate a `manifest.json` by running `dbt parse`. Once completed, run `dbt-bouncer` to validate that your conventions are being maintained.
1. Generate a `manifest.json` by running `dbt parse`.
1. Create a `dbt-bouncer.yml` config file.
1. Run `dbt-bouncer` to validate that your conventions are being maintained. You can use GitHub Actions, Docker or a `.pex` file to run `dbt-bouncer`.

## GitHub Actions

Expand All @@ -21,7 +23,7 @@ steps:

- uses: godatadriven/dbt-bouncer@v0
with:
dbt-artifacts-dir: ./<PATH_TO_DBT_PROJECT>/target
config-file: ./<PATH_TO_CONFIG_FILE>

...
```
Expand All @@ -36,17 +38,17 @@ docker pull ghcr.io/godatadriven/dbt-bouncer:v0
docker run --rm \
--volume "$PWD/<PATH_TO_DBT_PROJECT>/target":/<PATH_TO_DBT_PROJECT>/target \
ghcr.io/godatadriven/dbt-bouncer:v0 \
/dbt-bouncer.pex --dbt-artifacts-dir <PATH_TO_DBT_PROJECT>/target
/dbt-bouncer.pex --config-file <PATH_TO_CONFIG_FILE>
```

## Pex

You can also run the `.pex` artifact directly once you have a python executable installed:
You can also run the `.pex` ([Python EXecutable](https://docs.pex-tool.org/whatispex.html#whatispex)) artifact directly once you have a python executable installed:

```bash
wget https://github.com/godatadriven/dbt-bouncer/releases/download/vX.X.X/dbt-bouncer.pex -O dbt-bouncer.pex

dbt-bouncer.pex --dbt-artifacts-dir <PATH_TO_DBT_PROJECT>/target
dbt-bouncer.pex --config-file <PATH_TO_CONFIG_FILE>
```

# Development
Expand Down
6 changes: 1 addition & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ inputs:
default: 'dbt-bouncer.yml'
description: 'Location of the YML config file.'
required: false
dbt-artifacts-dir:
default: '.'
description: 'Directory where the dbt artifacts exists, generally the `target` directory inside a dbt project.'
required: false

runs:
using: 'composite'
Expand All @@ -23,4 +19,4 @@ runs:
shell: bash
run: |
wget https://github.com/godatadriven/dbt-bouncer/releases/download/vX.X.X/dbt-bouncer.pex -O ${{ github.action_path }}/dist/dbt-bouncer.pex
${{ github.action_path }}/dist/dbt-bouncer.pex --config-file ${{ inputs.config-file }} --dbt-artifacts-dir ${{ inputs.dbt-artifacts-dir }}
${{ github.action_path }}/dist/dbt-bouncer.pex --config-file ${{ inputs.config-file }}
2 changes: 2 additions & 0 deletions dbt-bouncer-example.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dbt-artifacts-dir: dbt_project/target # [Optional] Directory where the dbt artifacts exists, generally the `target` directory inside a dbt project. Defaults to `./target`.

checks:
- name: check_model_names
include: ^intermediate
Expand Down
12 changes: 4 additions & 8 deletions dbt_bouncer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@
required=False,
type=click.Path(exists=True),
)
@click.option(
"--dbt-artifacts-dir",
help="Directory where the dbt artifacts exists, generally the `target` directory inside a dbt project.",
required=True,
type=click.Path(exists=True),
)
@click.version_option()
def cli(config_file, dbt_artifacts_dir):
def cli(config_file):
logger.info(f"Running dbt-bouncer ({version()})...")

# Load config
Expand All @@ -53,7 +47,9 @@ def cli(config_file, dbt_artifacts_dir):
logger.debug(f"{config=}")

# Load manifest
manifest_json_path = Path(dbt_artifacts_dir) / "manifest.json"
manifest_json_path = (
Path(bouncer_config.get("dbt-artifacts-dir", "./target")) / "manifest.json"
)
logger.info(f"Loading manifest.json from {manifest_json_path}...")
if not manifest_json_path.exists():
raise FileNotFoundError(f"No manifest.json found at {manifest_json_path}.")
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def pytest_configure(config):
config.addinivalue_line("markers", "iterate_over_models: Tests that should run once per model")


import json
from pathlib import Path

import pytest
from dbt_artifacts_parser.parser import parse_manifest


def pytest_configure(config):
config.addinivalue_line("markers", "iterate_over_models: Tests that should run once per model")


@pytest.fixture(scope="session")
def manifest_obj():
manifest_json_path = Path("dbt_project") / "target/manifest.json"
Expand Down
4 changes: 1 addition & 3 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def test_logger_debug() -> None:
[
"--config-file",
"dbt-bouncer-example.yml",
"--dbt-artifacts-dir",
"dbt_project/target",
],
)

Expand All @@ -36,6 +34,6 @@ def test_logger_info(caplog) -> None:
runner = CliRunner()
runner.invoke(
cli,
["--config-file", "dbt-bouncer-example.yml", "--dbt-artifacts-dir", "dbt_project/target"],
["--config-file", "dbt-bouncer-example.yml"],
)
assert "Running dbt-bouncer (0.0.0)..." in caplog.text
44 changes: 18 additions & 26 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path

import pytest
import yaml
from click.testing import CliRunner

from dbt_bouncer.main import cli
Expand All @@ -7,7 +10,7 @@
@pytest.mark.parametrize(
"cli_args",
[
("--config-file dbt-bouncer-example.yml --dbt-artifacts-dir dbt_project/target"),
("--config-file dbt-bouncer-example.yml"),
],
)
def test_cli_happy_path(caplog, cli_args):
Expand All @@ -29,10 +32,7 @@ def test_cli_happy_path(caplog, cli_args):
"cli_args",
[
(""),
("--config-file dbt-bouncer-example.yml"),
("--dbt-artifacts-dir dbt_project/target"),
("--config-file non-existing.yml --dbt-artifacts-dir dbt_project/target"),
("--config-file dbt-bouncer-example.yml --dbt-artifacts-dir dbt_project"),
("--config-file non-existing.yml"),
],
)
def test_cli_unhappy_path(caplog, cli_args):
Expand All @@ -48,47 +48,39 @@ def test_cli_unhappy_path(caplog, cli_args):
assert result.exit_code != 0


def test_cli_dbt_dir_doesnt_exist():
runner = CliRunner()
result = runner.invoke(
cli,
[
"--config-file",
"dbt-bouncer-example.yml",
"--dbt-artifacts-dir",
"non-existent-directory/target",
],
)
assert type(result.exception) in [SystemExit]
assert result.exit_code != 0


def test_cli_config_doesnt_exist(tmp_path):
runner = CliRunner()
result = runner.invoke(
cli,
[
"--config-file",
"non-existent-file.yml",
"--dbt-artifacts-dir",
"dbt_project/target",
],
)
assert type(result.exception) in [SystemExit]
assert result.exit_code != 0


def test_cli_manifest_doesnt_exist(tmp_path):
with Path.open(Path("dbt-bouncer-example.yml"), "r") as f:
bouncer_config = yaml.safe_load(f)

bouncer_config["dbt-artifacts-dir"] = "non-existent-dir/target"

with Path(tmp_path / "dbt-bouncer-example.yml").open("w") as f:
yaml.dump(bouncer_config, f)

runner = CliRunner()
result = runner.invoke(
cli,
[
"--config-file",
"dbt-bouncer-example.yml",
"--dbt-artifacts-dir",
tmp_path,
Path(tmp_path / "dbt-bouncer-example.yml").__str__(),
],
)
assert type(result.exception) in [FileNotFoundError]
assert result.exception.args[0] == f"No manifest.json found at {tmp_path / 'manifest.json'}."
assert (
result.exception.args[0]
== "No manifest.json found at non-existent-dir/target/manifest.json."
)
assert result.exit_code != 0

0 comments on commit 204a91e

Please sign in to comment.