Skip to content

Commit

Permalink
Respect --resource-type and --exclude-resource-type CLI flags and…
Browse files Browse the repository at this point in the history
… associated environment variables
  • Loading branch information
dbeatty10 committed Sep 12, 2024
1 parent 4914cc9 commit 9ab1c0d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from click.exceptions import BadOptionUsage
from click.exceptions import Exit as ClickExit
from click.exceptions import NoSuchOption, UsageError

from dbt.artifacts.schemas.catalog import CatalogArtifact
from dbt.artifacts.schemas.run import RunExecutionResult
from dbt.cli import params as p
Expand Down Expand Up @@ -783,6 +782,7 @@ def freshness(ctx, **kwargs):
@click.pass_context
@global_flags
@p.exclude
@p.resource_type
@p.exclude_resource_type
@p.profiles_dir
@p.project_dir
Expand Down
5 changes: 5 additions & 0 deletions core/dbt/node_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
NodeType.Snapshot,
]

TEST_NODE_TYPES: List["NodeType"] = [
NodeType.Test,
NodeType.Unit,
]

VERSIONED_NODE_TYPES: List["NodeType"] = [
NodeType.Model,
]
41 changes: 25 additions & 16 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
import re
import threading
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
from typing import (
TYPE_CHECKING,
Any,
Collection,
Dict,
List,
Optional,
Tuple,
Type,
Union,
)

import daff

Expand All @@ -25,9 +35,9 @@
from dbt.exceptions import BooleanError, DbtInternalError
from dbt.flags import get_flags
from dbt.graph import ResourceTypeSelector
from dbt.node_types import NodeType
from dbt.node_types import TEST_NODE_TYPES, NodeType
from dbt.parser.unit_tests import UnitTestManifestLoader
from dbt.task.base import BaseRunner
from dbt.task.base import BaseRunner, resource_types_from_args
from dbt.utils import _coerce_decimal, strtobool
from dbt_common.dataclass_schema import dbtClassMixin
from dbt_common.events.format import pluralize
Expand Down Expand Up @@ -376,14 +386,7 @@ def _render_daff_diff(self, daff_diff: daff.TableDiff) -> str:


class TestSelector(ResourceTypeSelector):
def __init__(self, graph, manifest, previous_state, excluded_resource_types=None) -> None:
# Filter out NodeType.Unit if "unit_test" is specified in excluded_resource_types
if excluded_resource_types and "unit_test" in excluded_resource_types:
resource_types = [NodeType.Test]
else:
# Default case where no exclusions affect the resource types
resource_types = [NodeType.Test, NodeType.Unit]

def __init__(self, graph, manifest, previous_state, resource_types: List[NodeType]) -> None:
super().__init__(
graph=graph,
manifest=manifest,
Expand All @@ -404,18 +407,24 @@ class TestTask(RunTask):
def raise_on_first_error(self) -> bool:
return False

@property
def resource_types(self) -> List[NodeType]:
resource_types: Collection[NodeType] = resource_types_from_args(

Check warning on line 412 in core/dbt/task/test.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/test.py#L412

Added line #L412 was not covered by tests
self.args, set(TEST_NODE_TYPES), set(TEST_NODE_TYPES)
)

# filter out any non-test node types
resource_types = [rt for rt in resource_types if rt in TEST_NODE_TYPES]
return list(resource_types)

Check warning on line 418 in core/dbt/task/test.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/test.py#L417-L418

Added lines #L417 - L418 were not covered by tests

def get_node_selector(self) -> TestSelector:
if self.manifest is None or self.graph is None:
raise DbtInternalError("manifest and graph must be set to get perform node selection")

# Fetch excluded resource types from command line arguments or configuration
excluded_resource_types = self.config.args.exclude_resource_types

return TestSelector(
graph=self.graph,
manifest=self.manifest,
previous_state=self.previous_state,
excluded_resource_types=excluded_resource_types,
resource_types=self.resource_types,
)

def get_runner_type(self, _) -> Optional[Type[BaseRunner]]:
Expand Down

0 comments on commit 9ab1c0d

Please sign in to comment.