Skip to content

Commit

Permalink
Add support for File Selectors and add file selectors to the default …
Browse files Browse the repository at this point in the history
…method selector list (#5241)

* Add a new selector method for files and add it to the default method selection criteria if the given selector has a . in it but no path separators

* Add a file: selector method to the default selector methods because it will make Pedram happy

* changie stuff
  • Loading branch information
Josh Wills authored May 13, 2022
1 parent fc1fc2d commit 03b17ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
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

0 comments on commit 03b17ff

Please sign in to comment.