-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pre-release filter Grouped and renamed filter tests * Fix uninitialized `patterns` * Fix linting * Added `RegexProjectFilter` * Use `[filter_*]` config convention * Add `PreReleaseFilter` * Additional test and documentation * Use "bandersnatch" logger * Add test for blacklist release Use proper release naming
- Loading branch information
1 parent
151674b
commit 8493a99
Showing
11 changed files
with
418 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import re | ||
from collections import defaultdict | ||
from tempfile import TemporaryDirectory | ||
from unittest import TestCase | ||
|
||
import bandersnatch.filter | ||
from bandersnatch.configuration import BandersnatchConfig | ||
from bandersnatch.master import Master | ||
from bandersnatch.mirror import Mirror | ||
from bandersnatch.package import Package | ||
from bandersnatch_filter_plugins import prerelease_name | ||
|
||
|
||
def _mock_config(contents, filename="test.conf"): | ||
""" | ||
Creates a config file with contents and loads them into a | ||
BandersnatchConfig instance. | ||
""" | ||
with open(filename, "w") as fd: | ||
fd.write(contents) | ||
|
||
instance = BandersnatchConfig() | ||
instance.config_file = filename | ||
instance.load_configuration() | ||
return instance | ||
|
||
|
||
class BasePluginTestCase(TestCase): | ||
|
||
tempdir = None | ||
cwd = None | ||
|
||
def setUp(self): | ||
self.cwd = os.getcwd() | ||
self.tempdir = TemporaryDirectory() | ||
bandersnatch.filter.loaded_filter_plugins = defaultdict(list) | ||
os.chdir(self.tempdir.name) | ||
|
||
def tearDown(self): | ||
if self.tempdir: | ||
os.chdir(self.cwd) | ||
self.tempdir.cleanup() | ||
self.tempdir = None | ||
|
||
|
||
class TestRegexReleaseFilter(BasePluginTestCase): | ||
|
||
config_contents = """\ | ||
[blacklist] | ||
plugins = | ||
prerelease_release | ||
""" | ||
|
||
def test_plugin_includes_predefined_patterns(self): | ||
_mock_config(self.config_contents) | ||
|
||
plugins = bandersnatch.filter.filter_release_plugins() | ||
|
||
assert any( | ||
type(plugin) == prerelease_name.PreReleaseFilter for plugin in plugins | ||
) | ||
plugin = next( | ||
plugin | ||
for plugin in plugins | ||
if type(plugin) == prerelease_name.PreReleaseFilter | ||
) | ||
expected_patterns = [ | ||
re.compile(pattern_string) for pattern_string in plugin.PRERELEASE_PATTERNS | ||
] | ||
assert plugin.patterns == expected_patterns | ||
|
||
def test_plugin_check_match(self): | ||
_mock_config(self.config_contents) | ||
|
||
bandersnatch.filter.filter_release_plugins() | ||
|
||
mirror = Mirror(".", Master(url="https://foo.bar.com")) | ||
pkg = Package("foo", 1, mirror) | ||
pkg.releases = { | ||
"1.2.0alpha1": {}, | ||
"1.2.0a2": {}, | ||
"1.2.0beta1": {}, | ||
"1.2.0b2": {}, | ||
"1.2.0rc1": {}, | ||
"1.2.0": {}, | ||
} | ||
|
||
pkg._filter_releases() | ||
|
||
assert pkg.releases == {"1.2.0": {}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import os | ||
import re | ||
from collections import defaultdict | ||
from tempfile import TemporaryDirectory | ||
from unittest import TestCase | ||
|
||
import bandersnatch.filter | ||
from bandersnatch.configuration import BandersnatchConfig | ||
from bandersnatch.master import Master | ||
from bandersnatch.mirror import Mirror | ||
from bandersnatch.package import Package | ||
from bandersnatch_filter_plugins import regex_name | ||
|
||
|
||
def _mock_config(contents, filename="test.conf"): | ||
""" | ||
Creates a config file with contents and loads them into a | ||
BandersnatchConfig instance. | ||
""" | ||
with open(filename, "w") as fd: | ||
fd.write(contents) | ||
|
||
instance = BandersnatchConfig() | ||
instance.config_file = filename | ||
instance.load_configuration() | ||
return instance | ||
|
||
|
||
class BasePluginTestCase(TestCase): | ||
|
||
tempdir = None | ||
cwd = None | ||
|
||
def setUp(self): | ||
self.cwd = os.getcwd() | ||
self.tempdir = TemporaryDirectory() | ||
bandersnatch.filter.loaded_filter_plugins = defaultdict(list) | ||
os.chdir(self.tempdir.name) | ||
|
||
def tearDown(self): | ||
if self.tempdir: | ||
os.chdir(self.cwd) | ||
self.tempdir.cleanup() | ||
self.tempdir = None | ||
|
||
|
||
class TestRegexReleaseFilter(BasePluginTestCase): | ||
|
||
config_contents = """\ | ||
[blacklist] | ||
plugins = | ||
regex_release | ||
[filter_regex] | ||
releases = | ||
.+rc\\d$ | ||
.+alpha\\d$ | ||
""" | ||
|
||
def test_plugin_compiles_patterns(self): | ||
_mock_config(self.config_contents) | ||
|
||
plugins = bandersnatch.filter.filter_release_plugins() | ||
|
||
assert any(type(plugin) == regex_name.RegexReleaseFilter for plugin in plugins) | ||
plugin = next( | ||
plugin | ||
for plugin in plugins | ||
if type(plugin) == regex_name.RegexReleaseFilter | ||
) | ||
assert plugin.patterns == [re.compile(r".+rc\d$"), re.compile(r".+alpha\d$")] | ||
|
||
def test_plugin_check_match(self): | ||
_mock_config(self.config_contents) | ||
|
||
bandersnatch.filter.filter_release_plugins() | ||
|
||
mirror = Mirror(".", Master(url="https://foo.bar.com")) | ||
pkg = Package("foo", 1, mirror) | ||
pkg.releases = {"foo-1.2.0rc2": {}, "foo-1.2.0": {}, "foo-1.2.0alpha2": {}} | ||
|
||
pkg._filter_releases() | ||
|
||
assert pkg.releases == {"foo-1.2.0": {}} | ||
|
||
|
||
class TestRegexProjectFilter(BasePluginTestCase): | ||
|
||
config_contents = """\ | ||
[blacklist] | ||
plugins = | ||
regex_project | ||
[filter_regex] | ||
packages = | ||
.+-evil$ | ||
.+-neutral$ | ||
""" | ||
|
||
def test_plugin_compiles_patterns(self): | ||
_mock_config(self.config_contents) | ||
|
||
plugins = bandersnatch.filter.filter_project_plugins() | ||
|
||
assert any(type(plugin) == regex_name.RegexProjectFilter for plugin in plugins) | ||
plugin = next( | ||
plugin | ||
for plugin in plugins | ||
if type(plugin) == regex_name.RegexProjectFilter | ||
) | ||
assert plugin.patterns == [re.compile(r".+-evil$"), re.compile(r".+-neutral$")] | ||
|
||
def test_plugin_check_match(self): | ||
_mock_config(self.config_contents) | ||
|
||
bandersnatch.filter.filter_release_plugins() | ||
|
||
mirror = Mirror(".", Master(url="https://foo.bar.com")) | ||
mirror.packages_to_sync = {"foo-good": {}, "foo-evil": {}, "foo-neutral": {}} | ||
mirror._filter_packages() | ||
|
||
assert list(mirror.packages_to_sync.keys()) == ["foo-good"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import re | ||
|
||
from bandersnatch.filter import FilterReleasePlugin | ||
|
||
logger = logging.getLogger("bandersnatch") | ||
|
||
|
||
class PreReleaseFilter(FilterReleasePlugin): | ||
""" | ||
Filters releases considered pre-releases. | ||
""" | ||
|
||
name = "prerelease_release" | ||
PRERELEASE_PATTERNS = (r".+rc\d$", r".+a(lpha)?\d$", r".+b(eta)?\d$") | ||
|
||
def initialize_plugin(self): | ||
""" | ||
Initialize the plugin reading patterns from the config. | ||
""" | ||
self.patterns = [ | ||
re.compile(pattern_string) for pattern_string in self.PRERELEASE_PATTERNS | ||
] | ||
|
||
logger.info(f"Initialized prerelease plugin with {self.patterns}") | ||
|
||
def check_match(self, name, version): | ||
""" | ||
Check if a release version matches any of the specificed patterns. | ||
Parameters | ||
========== | ||
name: str | ||
Release name | ||
version: str | ||
Release version | ||
Returns | ||
======= | ||
bool: | ||
True if it matches, False otherwise. | ||
""" | ||
return any(pattern.match(version) for pattern in self.patterns) |
Oops, something went wrong.