Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES/300.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug related to qualifier-first versions like final-1.2.3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Fixed bug related to qualifier-first versions like final-1.2.3
Fixed bug related to qualifier-first versions like `final-1.2.3`.

Maybe give the user more details:

Suggested change
Fixed bug related to qualifier-first versions like final-1.2.3
Fixed bug related to qualifier-first versions, so e.g. `final-1.2.3` is considered valid now.

2 changes: 1 addition & 1 deletion pulp_maven/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def group_artifact_version_filename(relative_path):
"""
sub_path, filename = path.split(relative_path)
sub_path, version = path.split(sub_path)
pattern = re.compile(r"\d+(\.\d+)?(\.\d+)?([.-][a-zA-Z0-9]+)*")
pattern = re.compile(r"^((?:[A-Za-z0-9]+[.-])+\d+(?:\.\d+){0,2}|\d+(?:\.\d+){0,2}(?:[.-][A-Za-z0-9]+)*)$")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would 1.2.3.4.5.6 be a valid version?

Can you add some negative test cases too?
Is there an official document about maven versioning we can link here?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@mdellweg @bvladimir Maybe we should remove version regexp-ing at all?
Reasons for do so:

So, if some package in most well-known repo https://repo1.maven.org/maven2/ has "irregular" version - it could fail to load to Pulp...

if pattern.match(version) is None:
artifact_id = version
version = None
Expand Down
102 changes: 96 additions & 6 deletions pulp_maven/tests/unit/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,99 @@
from django.test import TestCase
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We use pytest to run these tests, and pytest-django is available.

Any chance I could ask you to redesign these around the and db, monkeypatch fixtures and the pytest.mark.parametrize decorator?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you want, you can also just test the regex in complete isolation.

"""
Unit tests for MavenContentMixin version parsing.

These tests can run without Django installed by mocking Django dependencies.
They test the actual code from pulp_maven.app.models, not a copy.
"""
import sys
import unittest
from unittest.mock import MagicMock, Mock

class TestNothing(TestCase):
"""Test Nothing (placeholder)."""
# Create a proper mock for Django models with correct metaclass
class MockModel:
"""Mock base class for Django models."""
pass

def test_nothing_at_all(self):
"""Test that the tests are running and that's it."""
self.assertTrue(True)
# Mock Django modules before importing
django_models_mock = MagicMock()
django_models_mock.Model = MockModel
django_models_mock.CharField = MagicMock
django_models_mock.ForeignKey = MagicMock
django_models_mock.PROTECT = MagicMock

pulpcore_models_mock = MagicMock()
pulpcore_models_mock.Content = MockModel
pulpcore_models_mock.Remote = MockModel
pulpcore_models_mock.Repository = MockModel
pulpcore_models_mock.Distribution = MockModel

sys.modules['django'] = MagicMock()
sys.modules['django.db'] = MagicMock()
sys.modules['django.db.models'] = django_models_mock
sys.modules['pulpcore'] = MagicMock()
sys.modules['pulpcore.plugin'] = MagicMock()
sys.modules['pulpcore.plugin.models'] = pulpcore_models_mock
sys.modules['pulpcore.plugin.util'] = MagicMock()

# Mock get_domain_pk function
sys.modules['pulpcore.plugin.util'].get_domain_pk = MagicMock(return_value=1)

# Now import the actual code from the module
from pulp_maven.app.models import MavenContentMixin


class TestMavenContentMixin(unittest.TestCase):
"""Test MavenContentMixin version parsing."""

def test_standard_version_format(self):
"""Test standard semantic version format (1.2.3)."""
relative_path = "org/example/myapp/1.2.3/myapp-1.2.3.jar"
group_id, artifact_id, version, filename = MavenContentMixin.group_artifact_version_filename(
relative_path
)

self.assertEqual(group_id, "org.example")
self.assertEqual(artifact_id, "myapp")
self.assertEqual(version, "1.2.3")
self.assertEqual(filename, "myapp-1.2.3.jar")

def test_version_with_suffix(self):
"""Test version with suffix format (1.2.3-rc, 1.2.3-SNAPSHOT)."""
test_cases = [
("org/example/myapp/1.2.3-rc/myapp-1.2.3-rc.jar", "1.2.3-rc"),
("org/example/myapp/1.2.3-SNAPSHOT/myapp-1.2.3-SNAPSHOT.jar", "1.2.3-SNAPSHOT"),
("org/example/myapp/1.2.3-beta1/myapp-1.2.3-beta1.jar", "1.2.3-beta1"),
]

for relative_path, expected_version in test_cases:
with self.subTest(version=expected_version):
group_id, artifact_id, version, filename = MavenContentMixin.group_artifact_version_filename(
relative_path
)

self.assertEqual(group_id, "org.example")
self.assertEqual(artifact_id, "myapp")
self.assertEqual(version, expected_version)
self.assertEqual(filename, f"myapp-{expected_version}.jar")

def test_version_with_prefix(self):
"""Test version with prefix format (rc-1.2.3, SNAPSHOT-1.2.3) - Artifactory compatibility."""
test_cases = [
("org/example/myapp/rc-1.2.3/myapp-rc-1.2.3.jar", "rc-1.2.3"),
("org/example/myapp/SNAPSHOT-1.2.3/myapp-SNAPSHOT-1.2.3.jar", "SNAPSHOT-1.2.3"),
("org/example/myapp/beta1-1.2.3/myapp-beta1-1.2.3.jar", "beta1-1.2.3"),
("org/example/myapp/v1-1.2/myapp-v1-1.2.jar", "v1-1.2"),
]

for relative_path, expected_version in test_cases:
with self.subTest(version=expected_version):
group_id, artifact_id, version, filename = MavenContentMixin.group_artifact_version_filename(
relative_path
)

self.assertEqual(group_id, "org.example")
self.assertEqual(artifact_id, "myapp")
self.assertEqual(version, expected_version)
self.assertEqual(filename, f"myapp-{expected_version}.jar")

if __name__ == '__main__':
unittest.main()
Loading