|
| 1 | +import os |
| 2 | +import re |
| 3 | +from collections import defaultdict |
| 4 | +from tempfile import TemporaryDirectory |
| 5 | +from unittest import TestCase |
| 6 | + |
| 7 | +import bandersnatch.filter |
| 8 | +from bandersnatch.configuration import BandersnatchConfig |
| 9 | +from bandersnatch.master import Master |
| 10 | +from bandersnatch.mirror import Mirror |
| 11 | +from bandersnatch.package import Package |
| 12 | +from bandersnatch_filter_plugins import regex_name |
| 13 | + |
| 14 | + |
| 15 | +def _mock_config(contents, filename="test.conf"): |
| 16 | + """ |
| 17 | + Creates a config file with contents and loads them into a |
| 18 | + BandersnatchConfig instance. |
| 19 | + """ |
| 20 | + with open(filename, "w") as fd: |
| 21 | + fd.write(contents) |
| 22 | + |
| 23 | + instance = BandersnatchConfig() |
| 24 | + instance.config_file = filename |
| 25 | + instance.load_configuration() |
| 26 | + return instance |
| 27 | + |
| 28 | + |
| 29 | +class BasePluginTestCase(TestCase): |
| 30 | + |
| 31 | + tempdir = None |
| 32 | + cwd = None |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.cwd = os.getcwd() |
| 36 | + self.tempdir = TemporaryDirectory() |
| 37 | + bandersnatch.filter.loaded_filter_plugins = defaultdict(list) |
| 38 | + os.chdir(self.tempdir.name) |
| 39 | + |
| 40 | + def tearDown(self): |
| 41 | + if self.tempdir: |
| 42 | + os.chdir(self.cwd) |
| 43 | + self.tempdir.cleanup() |
| 44 | + self.tempdir = None |
| 45 | + |
| 46 | + |
| 47 | +class TestRegexReleaseFilter(BasePluginTestCase): |
| 48 | + |
| 49 | + config_contents = """\ |
| 50 | +[blacklist] |
| 51 | +plugins = |
| 52 | + regex_release |
| 53 | +
|
| 54 | +[filter_regex] |
| 55 | +releases = |
| 56 | + .+rc\\d$ |
| 57 | + .+alpha\\d$ |
| 58 | +""" |
| 59 | + |
| 60 | + def test_plugin_compiles_patterns(self): |
| 61 | + _mock_config(self.config_contents) |
| 62 | + |
| 63 | + plugins = bandersnatch.filter.filter_release_plugins() |
| 64 | + |
| 65 | + assert any(type(plugin) == regex_name.RegexReleaseFilter for plugin in plugins) |
| 66 | + plugin = next( |
| 67 | + plugin |
| 68 | + for plugin in plugins |
| 69 | + if type(plugin) == regex_name.RegexReleaseFilter |
| 70 | + ) |
| 71 | + assert plugin.patterns == [re.compile(r".+rc\d$"), re.compile(r".+alpha\d$")] |
| 72 | + |
| 73 | + def test_plugin_check_match(self): |
| 74 | + _mock_config(self.config_contents) |
| 75 | + |
| 76 | + bandersnatch.filter.filter_release_plugins() |
| 77 | + |
| 78 | + mirror = Mirror(".", Master(url="https://foo.bar.com")) |
| 79 | + pkg = Package("foo", 1, mirror) |
| 80 | + pkg.releases = {"foo-1.2.0rc2": {}, "foo-1.2.0": {}, "foo-1.2.0alpha2": {}} |
| 81 | + |
| 82 | + pkg._filter_releases() |
| 83 | + |
| 84 | + assert pkg.releases == {"foo-1.2.0": {}} |
| 85 | + |
| 86 | + |
| 87 | +class TestRegexProjectFilter(BasePluginTestCase): |
| 88 | + |
| 89 | + config_contents = """\ |
| 90 | +[blacklist] |
| 91 | +plugins = |
| 92 | + regex_project |
| 93 | +
|
| 94 | +[filter_regex] |
| 95 | +packages = |
| 96 | + .+-evil$ |
| 97 | + .+-neutral$ |
| 98 | +""" |
| 99 | + |
| 100 | + def test_plugin_compiles_patterns(self): |
| 101 | + _mock_config(self.config_contents) |
| 102 | + |
| 103 | + plugins = bandersnatch.filter.filter_project_plugins() |
| 104 | + |
| 105 | + assert any(type(plugin) == regex_name.RegexProjectFilter for plugin in plugins) |
| 106 | + plugin = next( |
| 107 | + plugin |
| 108 | + for plugin in plugins |
| 109 | + if type(plugin) == regex_name.RegexProjectFilter |
| 110 | + ) |
| 111 | + assert plugin.patterns == [re.compile(r".+-evil$"), re.compile(r".+-neutral$")] |
| 112 | + |
| 113 | + def test_plugin_check_match(self): |
| 114 | + _mock_config(self.config_contents) |
| 115 | + |
| 116 | + bandersnatch.filter.filter_release_plugins() |
| 117 | + |
| 118 | + mirror = Mirror(".", Master(url="https://foo.bar.com")) |
| 119 | + mirror.packages_to_sync = {"foo-good": {}, "foo-evil": {}, "foo-neutral": {}} |
| 120 | + mirror._filter_packages() |
| 121 | + |
| 122 | + assert list(mirror.packages_to_sync.keys()) == ["foo-good"] |
0 commit comments