Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable use of env vars for --resource-types and --exclude-resource-types #9779

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .changes/unreleased/Features-20240312-140407.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kind: Features
body: Allow excluding resource types for build, list, and clone commands
body: Allow excluding resource types for build, list, and clone commands, and provide env vars
time: 2024-03-12T14:04:07.086017-04:00
custom:
Author: gshank
Expand Down
7 changes: 5 additions & 2 deletions core/dbt/cli/option_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@
name = "CHOICE_TUPLE"

def convert(self, value, param, ctx):
for value_item in value:
super().convert(value_item, param, ctx)
if not isinstance(value, str):
for value_item in value:
super().convert(value_item, param, ctx)
else:
super().convert(value, param, ctx)

Check warning on line 87 in core/dbt/cli/option_types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/option_types.py#L87

Added line #L87 was not covered by tests

return value
2 changes: 1 addition & 1 deletion core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def resource_types_from_args(
arg_resource_types.remove("default")
arg_resource_types.update(default_resource_values)
# Convert to a set of NodeTypes now that the non-NodeType strings are gone
resource_types = set([NodeType(rt) for rt in arg_resource_types])
resource_types = set([NodeType(rt) for rt in list(arg_resource_types)])

if args.exclude_resource_types:
# Convert from a list of strings to a set of NodeTypes
Expand Down
29 changes: 29 additions & 0 deletions tests/functional/list/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,34 @@ def expect_resource_type_multiple(self):
"test.outer",
}

def expect_resource_type_env_var(self):
"""Expect selected resources when --resource-type given multiple times"""
os.environ["DBT_RESOURCE_TYPES"] = "test model"
results = self.run_dbt_ls()
assert set(results) == {
"test.ephemeral",
"test.incremental",
"test.not_null_outer_id",
"test.outer",
"test.sub.inner",
"test.metricflow_time_spine",
"test.t",
"test.unique_outer_id",
}
del os.environ["DBT_RESOURCE_TYPES"]
os.environ[
"DBT_EXCLUDE_RESOURCE_TYPES"
] = "test saved_query metric source semantic_model snapshot seed"
results = self.run_dbt_ls()
assert set(results) == {
"test.ephemeral",
"test.incremental",
"test.outer",
"test.sub.inner",
"test.metricflow_time_spine",
}
del os.environ["DBT_EXCLUDE_RESOURCE_TYPES"]

def expect_selected_keys(self, project):
"""Expect selected fields of the the selected model"""
expectations = [
Expand Down Expand Up @@ -798,6 +826,7 @@ def test_ls(self, project):
self.expect_test_output()
self.expect_select()
self.expect_resource_type_multiple()
self.expect_resource_type_env_var()
self.expect_all_output()
self.expect_selected_keys(project)

Expand Down
8 changes: 8 additions & 0 deletions tests/functional/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import os
from unittest import mock
from dbt.tests.util import (
run_dbt,
Expand Down Expand Up @@ -66,6 +67,13 @@ def test_basic(self, project):
)
assert len(results) == 1

# Exclude unit tests with environment variable
os.environ["DBT_EXCLUDE_RESOURCE_TYPES"] = "unit_test"
results = run_dbt(["build", "--select", "my_model"], expect_pass=True)
assert len(results) == 1

del os.environ["DBT_EXCLUDE_RESOURCE_TYPES"]

# Test select by test name
results = run_dbt(["test", "--select", "test_name:test_my_model_string_concat"])
assert len(results) == 1
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_anonymous_usage_state(
flags = Flags(run_context)
assert flags.SEND_ANONYMOUS_USAGE_STATS == expected_anonymous_usage_stats

def test_resource_types(self, monkeypatch):
monkeypatch.setenv("DBT_RESOURCE_TYPES", "model")
build_context = self.make_dbt_context("build", ["build"])
# Is there any point to this?
MichelleArk marked this conversation as resolved.
Show resolved Hide resolved
build_context.params["resource_types"] = ("unit_test",)
flags = Flags(build_context)
assert flags.resource_types == ("unit_test",)

def test_empty_project_flags_uses_default(self, run_context, project_flags):
flags = Flags(run_context, project_flags)
assert flags.USE_COLORS == run_context.params["use_colors"]
Expand Down
Loading