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

Add support for File Selectors and add file selectors to the default method selector list #5241

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20220512-215748.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Adds file selectors and support for file selectors in the default method selector
time: 2022-05-12T21:57:48.289674-07:00
custom:
Author: jwills
Issue: "5240"
PR: "5241"
12 changes: 11 additions & 1 deletion core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MethodName(StrEnum):
Tag = "tag"
Source = "source"
Path = "path"
File = "file"
Package = "package"
Config = "config"
TestName = "test_name"
Expand Down Expand Up @@ -280,7 +281,7 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu

class PathSelectorMethod(SelectorMethod):
def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]:
"""Yields nodes from inclucded that match the given path."""
"""Yields nodes from included that match the given path."""
# use '.' and not 'root' for easy comparison
root = Path.cwd()
paths = set(p.relative_to(root) for p in root.glob(selector))
Expand All @@ -294,6 +295,14 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu
yield node


class FileSelectorMethod(SelectorMethod):
def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]:
"""Yields nodes from included that match the given file name."""
for node, real_node in self.all_nodes(included_nodes):
if Path(real_node.original_file_path).name == selector:
yield node


class PackageSelectorMethod(SelectorMethod):
def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]:
"""Yields nodes from included that have the specified package"""
Expand Down Expand Up @@ -589,6 +598,7 @@ class MethodManager:
MethodName.Tag: TagSelectorMethod,
MethodName.Source: SourceSelectorMethod,
MethodName.Path: PathSelectorMethod,
MethodName.File: FileSelectorMethod,
MethodName.Package: PackageSelectorMethod,
MethodName.Config: ConfigSelectorMethod,
MethodName.TestName: TestNameSelectorMethod,
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/graph/selector_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __post_init__(self):
def default_method(cls, value: str) -> MethodName:
if _probably_path(value):
return MethodName.Path
elif value.lower().endswith(".sql"):
return MethodName.File
else:
return MethodName.FQN

Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_graph_selector_methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy

import pytest
from unittest import mock

Expand Down Expand Up @@ -31,6 +32,7 @@
TagSelectorMethod,
SourceSelectorMethod,
PathSelectorMethod,
FileSelectorMethod,
PackageSelectorMethod,
ConfigSelectorMethod,
TestNameSelectorMethod,
Expand Down Expand Up @@ -703,6 +705,20 @@ def test_select_path(manifest):
manifest, method, 'models/missing*')


def test_select_file(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('file', [])
assert isinstance(method, FileSelectorMethod)
assert method.arguments == []

assert search_manifest_using_method(
manifest, method, 'table_model.sql') == {'table_model'}
assert search_manifest_using_method(
manifest, method, 'union_model.sql') == {'union_model', 'mynamespace.union_model'}
assert not search_manifest_using_method(
manifest, method, 'missing.sql')


def test_select_package(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('package', [])
Expand Down