Skip to content

Commit

Permalink
Add more package validation (#9986)
Browse files Browse the repository at this point in the history
* validate packages are not empty strings

* add test

* changelog

* Update core/dbt/contracts/project.py

* fix test message
  • Loading branch information
emmyoop authored Apr 24, 2024
1 parent 2d33655 commit 0dc2a2f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
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 @@ class PackageConfig(dbtClassMixin):
@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(
"A hub package is missing the value. It is a required property."
)
if package.get("local") == "":
raise ValidationError(
"A local package is missing the value. It is a required property."
)
if package.get("git") == "":
raise ValidationError(
"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 hub 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"])

0 comments on commit 0dc2a2f

Please sign in to comment.