Skip to content

type annotate plugin tests #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 28, 2020
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ repos:
rev: v0.770
hooks:
- id: mypy
exclude: (docs/.*|src/bandersnatch/tests/plugins/.*)
exclude: (docs/.*)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ atomic = true
not_skip = __init__.py
line_length = 88
multi_line_output = 3
known_third_party = _pytest,aiohttp,aiohttp_xmlrpc,asynctest,filelock,freezegun,keystoneauth1,packaging,pkg_resources,pytest,setuptools,swiftclient
known_third_party = _pytest,aiohttp,aiohttp_xmlrpc,asynctest,filelock,freezegun,keystoneauth1,mock_config,packaging,pkg_resources,pytest,setuptools,swiftclient
known_first_party = bandersnatch,bandersnatch_filter_plugins,bandersnatch_storage_plugins
force_grid_wrap = 0
use_parentheses=True
Expand Down
18 changes: 9 additions & 9 deletions src/bandersnatch/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Blacklist management
"""
from collections import defaultdict
from typing import Any, Dict, Iterable, List
from typing import Any, Dict, List

import pkg_resources

Expand Down Expand Up @@ -111,7 +111,7 @@ class FilterReleaseFilePlugin(Filter):
name = "release_file_plugin"


def load_filter_plugins(entrypoint_group: str) -> Iterable[Filter]:
def load_filter_plugins(entrypoint_group: str) -> List[Filter]:
"""
Load all blacklist plugins that are registered with importlib.resources

Expand Down Expand Up @@ -153,12 +153,12 @@ def load_filter_plugins(entrypoint_group: str) -> Iterable[Filter]:
if "all" in enabled_plugins or plugin_instance.name in enabled_plugins:
plugins.add(plugin_instance)

loaded_filter_plugins[entrypoint_group] = list(plugins)
plugins_list = list(plugins)
loaded_filter_plugins[entrypoint_group] = plugins_list
return plugins_list

return plugins


def filter_project_plugins() -> Iterable[Filter]:
def filter_project_plugins() -> List[Filter]:
"""
Load and return the release filtering plugin objects

Expand All @@ -170,7 +170,7 @@ def filter_project_plugins() -> Iterable[Filter]:
return load_filter_plugins(PROJECT_PLUGIN_RESOURCE)


def filter_metadata_plugins() -> Iterable[Filter]:
def filter_metadata_plugins() -> List[Filter]:
"""
Load and return the release filtering plugin objects

Expand All @@ -182,7 +182,7 @@ def filter_metadata_plugins() -> Iterable[Filter]:
return load_filter_plugins(METADATA_PLUGIN_RESOURCE)


def filter_release_plugins() -> Iterable[Filter]:
def filter_release_plugins() -> List[Filter]:
"""
Load and return the release filtering plugin objects

Expand All @@ -194,7 +194,7 @@ def filter_release_plugins() -> Iterable[Filter]:
return load_filter_plugins(RELEASE_PLUGIN_RESOURCE)


def filter_release_file_plugins() -> Iterable[Filter]:
def filter_release_file_plugins() -> List[Filter]:
"""
Load and return the release file filtering plugin objects

Expand Down
2 changes: 1 addition & 1 deletion src/bandersnatch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def symlink(self, source: PATH_TYPES, dest: PATH_TYPES) -> None:
assert isinstance(dest, pathlib.Path)
dest.symlink_to(source)

def get_hash(self, path: str, function: str = "sha256") -> str:
def get_hash(self, path: PATH_TYPES, function: str = "sha256") -> str:
"""Get the sha256sum of a given **path**"""
raise NotImplementedError

Expand Down
15 changes: 15 additions & 0 deletions src/bandersnatch/tests/mock_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from bandersnatch.configuration import BandersnatchConfig


def mock_config(contents: str, filename: str = "test.conf") -> BandersnatchConfig:
"""
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
122 changes: 46 additions & 76 deletions src/bandersnatch/tests/plugins/test_blacklist_name.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import os
from collections import defaultdict
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase

from mock_config import mock_config

import bandersnatch.filter
from bandersnatch.configuration import BandersnatchConfig
from bandersnatch.master import Master
from bandersnatch.mirror import Mirror
from bandersnatch.package import Package

TEST_CONF = "test.conf"


class TestBlacklistProject(TestCase):
"""
Expand All @@ -20,107 +20,88 @@ class TestBlacklistProject(TestCase):
tempdir = None
cwd = None

def setUp(self):
def setUp(self) -> None:
self.cwd = os.getcwd()
self.tempdir = TemporaryDirectory()
bandersnatch.filter.loaded_filter_plugins = defaultdict(list)
os.chdir(self.tempdir.name)

def tearDown(self):
def tearDown(self) -> None:
if self.tempdir:
assert self.cwd
os.chdir(self.cwd)
self.tempdir.cleanup()
self.tempdir = None

def test__plugin__loads__explicitly_enabled(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__plugin__loads__explicitly_enabled(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_project
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

plugins = bandersnatch.filter.filter_project_plugins()
names = [plugin.name for plugin in plugins]
self.assertListEqual(names, ["blacklist_project"])
self.assertEqual(len(plugins), 1)

def test__plugin__doesnt_load__explicitly__disabled(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__plugin__doesnt_load__explicitly__disabled(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_release
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

plugins = bandersnatch.filter.filter_project_plugins()
names = [plugin.name for plugin in plugins]
self.assertNotIn("blacklist_project", names)

def test__plugin__loads__default(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__plugin__loads__default(self) -> None:
mock_config(
"""\
[blacklist]
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

plugins = bandersnatch.filter.filter_project_plugins()
names = [plugin.name for plugin in plugins]
self.assertNotIn("blacklist_project", names)

def test__filter__matches__package(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__filter__matches__package(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_project
[blacklist]
packages =
foo
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

mirror = Mirror(".", Master(url="https://foo.bar.com"))
mirror.packages_to_sync = {"foo": {}}
mirror = Mirror(Path("."), Master(url="https://foo.bar.com"))
mirror.packages_to_sync = {"foo": ""}
mirror._filter_packages()

self.assertNotIn("foo", mirror.packages_to_sync.keys())

def test__filter__nomatch_package(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__filter__nomatch_package(self) -> None:
mock_config(
"""\
[blacklist]
plugins =
blacklist_project
packages =
foo
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

mirror = Mirror(".", Master(url="https://foo.bar.com"))
mirror.packages_to_sync = {"foo2": {}}
mirror = Mirror(Path("."), Master(url="https://foo.bar.com"))
mirror.packages_to_sync = {"foo2": ""}
mirror._filter_packages()

self.assertIn("foo2", mirror.packages_to_sync.keys())
Expand All @@ -134,70 +115,59 @@ class TestBlacklistRelease(TestCase):
tempdir = None
cwd = None

def setUp(self):
def setUp(self) -> None:
self.cwd = os.getcwd()
self.tempdir = TemporaryDirectory()
bandersnatch.filter.loaded_filter_plugins = defaultdict(list)
os.chdir(self.tempdir.name)

def tearDown(self):
def tearDown(self) -> None:
if self.tempdir:
assert self.cwd
os.chdir(self.cwd)
self.tempdir.cleanup()
self.tempdir = None

def test__plugin__loads__explicitly_enabled(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__plugin__loads__explicitly_enabled(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_release
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

plugins = bandersnatch.filter.filter_release_plugins()
names = [plugin.name for plugin in plugins]
self.assertListEqual(names, ["blacklist_release"])
self.assertEqual(len(plugins), 1)

def test__plugin__doesnt_load__explicitly__disabled(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__plugin__doesnt_load__explicitly__disabled(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_package
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

plugins = bandersnatch.filter.filter_release_plugins()
names = [plugin.name for plugin in plugins]
self.assertNotIn("blacklist_release", names)

def test__filter__matches__release(self):
with open(TEST_CONF, "w") as testconfig_handle:
testconfig_handle.write(
"""\
def test__filter__matches__release(self) -> None:
mock_config(
"""\
[plugins]
enabled =
blacklist_release
[blacklist]
packages =
foo==1.2.0
"""
)
instance = BandersnatchConfig()
instance.config_file = TEST_CONF
instance.load_configuration()
)

mirror = Mirror(".", Master(url="https://foo.bar.com"))
mirror = Mirror(Path("."), Master(url="https://foo.bar.com"))
pkg = Package("foo", 1, mirror)
pkg.info = {"name": "foo"}
pkg.releases = {"1.2.0": {}, "1.2.1": {}}
Expand Down
Loading