From 1603b920c9c9799858ab8115e6ee99df87848172 Mon Sep 17 00:00:00 2001 From: allburov Date: Fri, 21 Oct 2022 13:13:32 +0700 Subject: [PATCH 1/4] Split to test classes --- tests/test_rules_keep.py | 425 ++++++++++++++++++++------------------- 1 file changed, 213 insertions(+), 212 deletions(-) diff --git a/tests/test_rules_keep.py b/tests/test_rules_keep.py index d6cb557..fa9657d 100644 --- a/tests/test_rules_keep.py +++ b/tests/test_rules_keep.py @@ -7,215 +7,216 @@ from tests.utils import makeas -def test_KeepLatestNFiles(): - data = [ - { - "path": "2.1.1", - "name": "name.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "0.1.100", - "name": "name.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "0.1.200", - "name": "name.zip", - "created": "2021-01-19T13:53:52.383+02:00", - }, - { - "path": "0.1.99", - "name": "name.zip", - "created": "2021-01-19T13:53:52.383+02:00", - }, - { - "path": "1.3.1", - "name": "name.zip", - "created": "2021-03-20T13:53:52.383+02:00", - }, - ] - - artifacts = ArtifactsList.from_response(data) - - remove_these = KeepLatestNFiles(2).filter(artifacts) - expected = [ - { - "path": "0.1.200", - }, - { - "path": "0.1.99", - }, - { - "path": "0.1.100", - }, - ] - assert makeas(remove_these, expected) == expected - - -def test_KeepLatestNFilesInFolder(): - data = [ - { - "path": "folder1", - "name": "0.0.2.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder1", - "name": "0.0.1.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.1.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.2.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder3", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - ] - - artifacts = ArtifactsList.from_response(data) - - remove_these = KeepLatestNFilesInFolder(1).filter(artifacts) - expected = [ - {"name": "0.0.1.zip", "path": "folder1"}, - {"name": "0.0.1.zip", "path": "folder2"}, - ] - assert makeas(remove_these, expected) == expected - - -def test_KeepLatestVersionNFilesInFolderDefault(): - data = [ - { - "path": "folder1", - "name": "0.0.2.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder1", - "name": "0.0.1.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.2.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder3", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - ] - - artifacts = ArtifactsList.from_response(data) - - remove_these = KeepLatestVersionNFilesInFolder(1).filter(artifacts) - expected = [ - {"name": "0.0.1.zip", "path": "folder1"}, - {"name": "0.0.1.zip", "path": "folder2"}, - ] - assert makeas(remove_these, expected) == expected - - -def test_KeepLatestVersionNFilesInFolderRegexWork(): - data = [ - { - "path": "folder1", - "name": "0.0.2.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder1", - "name": "0.0.1.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.2.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder3", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder3", - "name": "1.0.1.zip", - "created": "2021-03-21T13:54:52.383+02:00", - }, - { - "path": "folder3", - "name": "1.0.2.zip", - "created": "2021-03-21T14:54:52.383+02:00", - }, - ] - - artifacts = ArtifactsList.from_response(data) - - remove_these = KeepLatestVersionNFilesInFolder( - 1, "[\\d]+\\.([\\d]+\\.[\\d]+)" - ).filter(artifacts) - expected = [ - {"name": "0.0.1.zip", "path": "folder1"}, - {"name": "0.0.1.zip", "path": "folder2"}, - {"name": "1.0.1.zip", "path": "folder3"}, - ] - assert makeas(remove_these, expected) == expected - - -def test_KeepLatestVersionNFilesInFolderRegexFail(): - data = [ - { - "path": "folder1", - "name": "0.0.2.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder1", - "name": "0.0.1.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - { - "path": "folder2", - "name": "0.0.2.zip", - "created": "2021-03-19T13:53:52.383+02:00", - }, - { - "path": "folder3", - "name": "0.0.1.zip", - "created": "2021-03-20T13:54:52.383+02:00", - }, - ] - - artifacts = ArtifactsList.from_response(data) - - remove_these = KeepLatestVersionNFilesInFolder( - 1, "[^\\d][\\._]((\\d+\\.)+\\d+)" - ).filter(artifacts) - expected = [] - assert makeas(remove_these, expected) == expected +class TestKeepLatestNFiles: + def test_basic_case(self): + data = [ + { + "path": "2.1.1", + "name": "name.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "0.1.100", + "name": "name.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "0.1.200", + "name": "name.zip", + "created": "2021-01-19T13:53:52.383+02:00", + }, + { + "path": "0.1.99", + "name": "name.zip", + "created": "2021-01-19T13:53:52.383+02:00", + }, + { + "path": "1.3.1", + "name": "name.zip", + "created": "2021-03-20T13:53:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestNFiles(2).filter(artifacts) + expected = [ + { + "path": "0.1.200", + }, + { + "path": "0.1.99", + }, + { + "path": "0.1.100", + }, + ] + assert makeas(remove_these, expected) == expected + + +class TestKeepLatestNFilesInFolder: + def test_basic_case(self): + data = [ + { + "path": "folder1", + "name": "0.0.2.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "0.0.1.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.1.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.2.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder3", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestNFilesInFolder(1).filter(artifacts) + expected = [ + {"name": "0.0.1.zip", "path": "folder1"}, + {"name": "0.0.1.zip", "path": "folder2"}, + ] + assert makeas(remove_these, expected) == expected + + +class TestKeepLatestVersionNFilesInFolderDefault: + def test_default_regexp(self): + data = [ + { + "path": "folder1", + "name": "0.0.2.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "0.0.1.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.2.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder3", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestVersionNFilesInFolder(1).filter(artifacts) + expected = [ + {"name": "0.0.1.zip", "path": "folder1"}, + {"name": "0.0.1.zip", "path": "folder2"}, + ] + assert makeas(remove_these, expected) == expected + + def test_custom_regexp_work(self): + data = [ + { + "path": "folder1", + "name": "0.0.2.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "0.0.1.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.2.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder3", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder3", + "name": "1.0.1.zip", + "created": "2021-03-21T13:54:52.383+02:00", + }, + { + "path": "folder3", + "name": "1.0.2.zip", + "created": "2021-03-21T14:54:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestVersionNFilesInFolder( + 1, "[\\d]+\\.([\\d]+\\.[\\d]+)" + ).filter(artifacts) + expected = [ + {"name": "0.0.1.zip", "path": "folder1"}, + {"name": "0.0.1.zip", "path": "folder2"}, + {"name": "1.0.1.zip", "path": "folder3"}, + ] + assert makeas(remove_these, expected) == expected + + def test_regexp_fail_to_parse__must_keep_unmatched_files(self): + data = [ + { + "path": "folder1", + "name": "0.0.2.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "0.0.1.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder2", + "name": "0.0.2.zip", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder3", + "name": "0.0.1.zip", + "created": "2021-03-20T13:54:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestVersionNFilesInFolder( + 1, "[^\\d][\\._]((\\d+\\.)+\\d+)" + ).filter(artifacts) + expected = [] + assert makeas(remove_these, expected) == expected From 0d61e5fa430f92f422eaa620592fc35caf650749 Mon Sep 17 00:00:00 2001 From: allburov Date: Fri, 21 Oct 2022 13:27:41 +0700 Subject: [PATCH 2/4] Do not split by dot, use version as is fix #83 --- artifactory_cleanup/rules/keep.py | 4 +--- tests/test_rules_keep.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/artifactory_cleanup/rules/keep.py b/artifactory_cleanup/rules/keep.py index a08c4f9..301b160 100644 --- a/artifactory_cleanup/rules/keep.py +++ b/artifactory_cleanup/rules/keep.py @@ -173,9 +173,7 @@ def filter(self, artifacts): artifacts.keep(artifact) for artifactory_with_version in artifacts_by_path_and_name.values(): - artifactory_with_version.sort( - key=lambda x: [int(x) for x in x[0].split(".")] - ) + artifactory_with_version.sort() artifact_count = len(artifactory_with_version) good_artifact_count = artifact_count - self.count diff --git a/tests/test_rules_keep.py b/tests/test_rules_keep.py index fa9657d..9a4d6ca 100644 --- a/tests/test_rules_keep.py +++ b/tests/test_rules_keep.py @@ -219,4 +219,40 @@ def test_regexp_fail_to_parse__must_keep_unmatched_files(self): 1, "[^\\d][\\._]((\\d+\\.)+\\d+)" ).filter(artifacts) expected = [] + assert remove_these == expected + + def test_regexp_with_hyphen(self): + data = [ + # bootstrap-gui + { + "path": "folder1", + "name": "bootstrap-gui-3.10-39.src.rpm", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "bootstrap-gui-3.10-33.src.rpm", + "created": "2021-03-20T13:54:52.383+02:00", + }, + { + "path": "folder1", + "name": "bootstrap-gui-3.11-33.src.rpm", + "created": "2021-03-19T13:53:52.383+02:00", + }, + { + "path": "folder1", + "name": "bootstrap-gui-4.11-33.src.rpm", + "created": "2021-03-20T13:54:52.383+02:00", + }, + ] + + artifacts = ArtifactsList.from_response(data) + + remove_these = KeepLatestVersionNFilesInFolder( + 2, "bootstrap-gui-([\\d]+\\.[\\d]+\\-[\\d]+).*\\.rpm" + ).filter(artifacts) + expected = [ + {"name": "bootstrap-gui-3.10-39.src.rpm", "path": "folder1"}, + {"name": "bootstrap-gui-3.10-33.src.rpm", "path": "folder1"}, + ] assert makeas(remove_these, expected) == expected From eb94abcf4c8816fef2060270fb46fde134cc37cc Mon Sep 17 00:00:00 2001 From: allburov Date: Fri, 21 Oct 2022 13:34:57 +0700 Subject: [PATCH 3/4] Bump version to 1.0.4 --- artifactory_cleanup/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/artifactory_cleanup/__init__.py b/artifactory_cleanup/__init__.py index 2246c34..7279cb8 100644 --- a/artifactory_cleanup/__init__.py +++ b/artifactory_cleanup/__init__.py @@ -7,4 +7,4 @@ def register(rule): registry.register(rule) -__version__ = "1.0.3" +__version__ = "1.0.4" diff --git a/setup.py b/setup.py index 3c964e7..578cab3 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name="artifactory-cleanup", - version="1.0.3", + version="1.0.4", description="Rules and cleanup policies for Artifactory", long_description=long_description, long_description_content_type="text/markdown", From 3f50138c1d616529c5da5191bcdce1c6441610ed Mon Sep 17 00:00:00 2001 From: allburov Date: Thu, 27 Oct 2022 13:18:03 +0700 Subject: [PATCH 4/4] Do not compare dict, compare version --- artifactory_cleanup/rules/keep.py | 5 +++-- tests/test_rules_keep.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/artifactory_cleanup/rules/keep.py b/artifactory_cleanup/rules/keep.py index 301b160..28e989a 100644 --- a/artifactory_cleanup/rules/keep.py +++ b/artifactory_cleanup/rules/keep.py @@ -158,7 +158,7 @@ def filter(self, artifacts): version_str = ( version[0][0] if isinstance(version[0], tuple) else version[0] ) - artifactory_with_version = [version_str, artifact] + artifactory_with_version = (version_str, artifact) name_without_version = artifact["name"][ : artifact["name"].find(version_str) ] @@ -173,7 +173,8 @@ def filter(self, artifacts): artifacts.keep(artifact) for artifactory_with_version in artifacts_by_path_and_name.values(): - artifactory_with_version.sort() + # List of pairs: [version, ArtifactDict] + artifactory_with_version.sort(key=lambda pair: pair[0]) artifact_count = len(artifactory_with_version) good_artifact_count = artifact_count - self.count diff --git a/tests/test_rules_keep.py b/tests/test_rules_keep.py index 9a4d6ca..4840a0a 100644 --- a/tests/test_rules_keep.py +++ b/tests/test_rules_keep.py @@ -94,7 +94,7 @@ def test_basic_case(self): assert makeas(remove_these, expected) == expected -class TestKeepLatestVersionNFilesInFolderDefault: +class TestKeepLatestVersionNFilesInFolder: def test_default_regexp(self): data = [ {