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 more package validation #9986

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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-20240422-152244.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Validate against empty strings in package definitions
time: 2024-04-22T15:22:44.575999-05:00
custom:
Author: emmyoop
Issue: "9985"
15 changes: 14 additions & 1 deletion core/dbt/contracts/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,26 @@
@classmethod
def validate(cls, data):
for package in data.get("packages", data):
# This can happen when the target is a variable that is not filled and results in hangs
if isinstance(package, dict):
if package.get("package") == "":
raise ValidationError(

Check warning on line 107 in core/dbt/contracts/project.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/project.py#L107

Added line #L107 was not covered by tests
"A package is missing the value. It is a required property."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some more detailed message here to help folks narrow down the problem since we already know what type is missing value here?

emmyoop marked this conversation as resolved.
Show resolved Hide resolved
)
if package.get("local") == "":
raise ValidationError(

Check warning on line 111 in core/dbt/contracts/project.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/project.py#L111

Added line #L111 was not covered by tests
"A local package is missing the value. It is a required property."
)
if package.get("git") == "":
raise ValidationError(

Check warning on line 115 in core/dbt/contracts/project.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/project.py#L115

Added line #L115 was not covered by tests
"A git package is missing the value. It is a required property."
)
if isinstance(package, dict) and package.get("package"):
if not package["version"]:
raise ValidationError(
f"{package['package']} is missing the version. When installing from the Hub "
"package index, version is a required property"
)

if "/" not in package["package"]:
raise ValidationError(
f"{package['package']} was not found in the package index. Packages on the index "
Expand Down
41 changes: 41 additions & 0 deletions tests/functional/dependencies/test_simple_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,44 @@ def test_malformed_tarball_package_causes_exception(self, project):
) as e:
run_dbt(["deps"])
assert e is not None


class TestEmptyDependency:
def test_empty_package(self, project):
# We have to specify the bad formatted package here because if we do it
# in a `packages` fixture, the test will blow up in the setup phase, meaning
# we can't appropriately catch it with a `pytest.raises`
empty_hub_package = {
"packages": [
{
"package": "",
"version": "1.0.0",
}
]
}
write_config_file(empty_hub_package, "packages.yml")
with pytest.raises(DbtProjectError, match="A package is missing the value"):
run_dbt(["deps"])

empty_git_package = {
"packages": [
{
"git": "",
"revision": "1.0.0",
}
]
}
write_config_file(empty_git_package, "packages.yml")
with pytest.raises(DbtProjectError, match="A git package is missing the value"):
run_dbt(["deps"])

empty_local_package = {
"packages": [
{
"local": "",
}
]
}
write_config_file(empty_local_package, "packages.yml")
with pytest.raises(DbtProjectError, match="A local package is missing the value"):
run_dbt(["deps"])