Skip to content

Commit

Permalink
Fix using project-dir with list command and path selector (#8388) (#8427
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 048553d)

Co-authored-by: Gerda Shank <[email protected]>
  • Loading branch information
github-actions[bot] and gshank authored Aug 16, 2023
1 parent 0f9f5dd commit b003e7d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
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"]

0 comments on commit b003e7d

Please sign in to comment.