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

Fix using project-dir with list command and path selector #8388

Merged
merged 2 commits into from
Aug 16, 2023
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230814-145702.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix using list command with path selector and project-dir
time: 2023-08-14T14:57:02.02816-04:00
custom:
Author: gshank
Issue: "8385"
30 changes: 17 additions & 13 deletions core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ListCmdOut,
)
from dbt.exceptions import DbtRuntimeError, DbtInternalError
from dbt.events.contextvars import task_contextvars


class ListTask(GraphRunnableTask):
Expand Down Expand Up @@ -123,20 +124,23 @@ def generate_paths(self):
yield node.original_file_path

def run(self):
self.compile_manifest()
output = self.args.output
if output == "selector":
generator = self.generate_selectors
elif output == "name":
generator = self.generate_names
elif output == "json":
generator = self.generate_json
elif output == "path":
generator = self.generate_paths
else:
raise DbtInternalError("Invalid output {}".format(output))
# We set up a context manager here with "task_contextvars" because we
# we need the project_root in compile_manifest.
with task_contextvars(project_root=self.config.project_root):
self.compile_manifest()
output = self.args.output
if output == "selector":
generator = self.generate_selectors
elif output == "name":
generator = self.generate_names
elif output == "json":
generator = self.generate_json
elif output == "path":
generator = self.generate_paths
else:
raise DbtInternalError("Invalid output {}".format(output))

return self.output_results(generator())
return self.output_results(generator())

def output_results(self, results):
"""Log, or output a plain, newline-delimited, and ready-to-pipe list of nodes found."""
Expand Down
37 changes: 37 additions & 0 deletions tests/functional/graph_selection/test_graph_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ def test_locally_qualified_name(self, project):
check_result_nodes_by_name(results, ["subdir"])
assert_correct_schemas(project)

# Check that list command works
os.chdir(
project.profiles_dir
) # Change to random directory to test that Path selector works with project-dir
results = run_dbt(
[
"-q",
"ls",
"-s",
"path:models/test/subdir.sql",
"--project-dir",
str(project.project_root),
]
# ["list", "--project-dir", str(project.project_root), "--select", "models/test/subdir*"]
)
print(f"--- results: {results}")
assert len(results) == 1

def test_locally_qualified_name_model_with_dots(self, project):
results = run_dbt(["run", "--select", "alternative.users"], expect_pass=False)
check_result_nodes_by_name(results, ["alternative.users"])
Expand Down Expand Up @@ -268,3 +286,22 @@ def test_exposure_parents(self, project):
"users",
],
)


class TestListPathGraphSelection(SelectionFixtures):
def test_list_select_with_project_dir(self, project):
# Check that list command works
os.chdir(
project.profiles_dir
) # Change to random directory to test that Path selector works with project-dir
results = run_dbt(
[
"-q",
"ls",
"-s",
"path:models/test/subdir.sql",
"--project-dir",
str(project.project_root),
]
)
assert results == ["test.test.subdir"]