Skip to content

Commit

Permalink
Refactor package case check
Browse files Browse the repository at this point in the history
Refactor package case check so that logic is directly in package_manifest.py,
rather than hidden in a poorly named util function
Update test to reflect that updating packages that already exist with different
case should still be valid
  • Loading branch information
x753 committed Feb 21, 2025
1 parent e01b725 commit 1f89b6d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
17 changes: 8 additions & 9 deletions django/thunderstore/repository/package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
PackageVersionField,
StrictCharField,
)
from thunderstore.repository.utils import (
does_contain_package,
has_different_case,
has_duplicate_packages,
)
from thunderstore.repository.utils import does_contain_package, has_duplicate_packages


class PackageInstallerSerializer(serializers.Serializer):
Expand Down Expand Up @@ -100,10 +96,13 @@ def validate(self, data):
)
if does_contain_package(result["dependencies"], reference):
raise ValidationError("Package depending on itself is not allowed")
if has_different_case(reference.without_version):
raise ValidationError(
"Package name already exists with different capitalization"
)
if not reference.without_version.exists: # if we're making a new package
if (
reference.without_version.exists_in_any_case
): # if it exists in a different case
raise ValidationError(
"Package name already exists with different capitalization"
)
return result

def update(self, instance, validated_data):
Expand Down
10 changes: 10 additions & 0 deletions django/thunderstore/repository/tests/test_package_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def test_manifest_v1_serializer_package_already_exists_with_different_case(
assert "Package name already exists with different capitalization" in str(
serializer.errors["non_field_errors"][0]
)
# Updating packages that already exist with different case should still be valid
PackageFactory(
owner=package.owner, name=package.name.swapcase(), namespace=package.namespace
)
serializer = ManifestV1Serializer(
user=user,
team=package.owner,
data=manifest_v1_data,
)
assert serializer.is_valid() is True


@pytest.mark.django_db
Expand Down
8 changes: 0 additions & 8 deletions django/thunderstore/repository/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ def has_duplicate_packages(packages: List["PackageReference"]) -> bool:
return False


def has_different_case(reference: "PackageReference") -> bool:
if reference.exists:
return False
elif reference.exists_in_any_case:
return True
return False


def unpack_serializer_errors(field, errors, error_dict=None):
if error_dict is None:
error_dict = {}
Expand Down

0 comments on commit 1f89b6d

Please sign in to comment.