-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow cleaning paths outside current working directory (#8469)
- Loading branch information
Showing
11 changed files
with
143 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Disallow cleaning paths outside current working directory | ||
time: 2023-09-12T15:30:02.089967+01:00 | ||
custom: | ||
Author: aranke | ||
Issue: "8318" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
from dbt.exceptions import DbtRuntimeError | ||
from dbt.tests.util import run_dbt | ||
|
||
|
||
class TestCleanSourcePath: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return "clean-targets: ['models']" | ||
|
||
def test_clean_source_path(self, project): | ||
with pytest.raises(DbtRuntimeError, match="dbt will not clean the following source paths"): | ||
run_dbt(["clean"]) | ||
|
||
|
||
class TestCleanPathOutsideProjectRelative: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return "clean-targets: ['..']" | ||
|
||
def test_clean_path_outside_project(self, project): | ||
with pytest.raises( | ||
DbtRuntimeError, | ||
match="dbt will not clean the following directories outside the project", | ||
): | ||
run_dbt(["clean"]) | ||
|
||
|
||
class TestCleanPathOutsideProjectAbsolute: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return "clean-targets: ['/']" | ||
|
||
def test_clean_path_outside_project(self, project): | ||
with pytest.raises( | ||
DbtRuntimeError, | ||
match="dbt will not clean the following directories outside the project", | ||
): | ||
run_dbt(["clean"]) | ||
|
||
|
||
class TestCleanPathOutsideProjectWithFlag: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return "clean-targets: ['/tmp/foo']" | ||
|
||
def test_clean_path_outside_project(self, project): | ||
# Doesn't fail because flag is set | ||
run_dbt(["clean", "--no-clean-project-files-only"]) | ||
|
||
with pytest.raises( | ||
DbtRuntimeError, | ||
match="dbt will not clean the following directories outside the project", | ||
): | ||
run_dbt(["clean", "--clean-project-files-only"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
|
||
@contextmanager | ||
def up_one(): | ||
current_path = Path.cwd() | ||
os.chdir("../") | ||
try: | ||
yield | ||
finally: | ||
os.chdir(current_path) |