def master(requests): from bandersnatch.master import Master master = Master('https://pypi.example.com') master.rpc = mock.Mock() master.session = mock.Mock() master.session.get = requests return master
def master(package_json: Dict[str, Any]) -> "Master": from bandersnatch.master import Master class FakeReader: async def read(self, *args: Any) -> bytes: return b"" class FakeAiohttpClient: headers = {"X-PYPI-LAST-SERIAL": "1"} async def __aenter__(self) -> "FakeAiohttpClient": return self async def __aexit__(self, *args: Any) -> None: pass @property def content(self) -> "FakeReader": return FakeReader() async def json(self, *args: Any) -> Dict[str, Any]: return package_json master = Master("https://pypi.example.com") master.rpc = mock.Mock() # type: ignore master.session = mock.MagicMock() master.session.get = mock.MagicMock(return_value=FakeAiohttpClient()) master.session.request = mock.MagicMock(return_value=FakeAiohttpClient()) return master
def master(requests): from bandersnatch.master import Master master = Master("https://pypi.example.com") master.rpc = mock.Mock() master.session = mock.Mock() master.session.get = requests return master
def test_latest_releases_keep_stable(self) -> None: mock_config(self.config_contents) mirror = Mirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1, mirror) pkg._metadata = { "info": { "name": "foo", "version": "2.0.0" }, # stable version "releases": { "1.0.0": {}, "1.1.0": {}, "1.1.1": {}, "1.1.2": {}, "1.1.3": {}, "2.0.0": {}, # <= stable version, keep it "2.0.1b1": {}, "2.0.1b2": {}, # <= most recent, keep it }, } pkg._filter_all_releases(mirror.filters.filter_release_plugins()) assert pkg.releases == {"2.0.1b2": {}, "2.0.0": {}}
def test__filter__matches__release(self) -> None: mock_config("""\ [plugins] enabled = allowlist_release [whitelist] packages = foo==1.2.0 """) mirror = Mirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1, mirror) pkg._metadata = { "info": { "name": "foo" }, "releases": { "1.2.0": {}, "1.2.1": {} }, } pkg._filter_all_releases(mirror.filters.filter_release_plugins()) self.assertEqual(pkg.releases, {"1.2.0": {}})
def test__filter__find_files(self) -> None: absolute_file_path = Path(self.tempdir.name) / "requirements.txt" with open(absolute_file_path, "w") as fh: fh.write("""\ # This is needed for workshop 1 # foo==1.2.0 # via -r requirements.in """) mock_config(f"""\ [mirror] storage-backend = filesystem workers = 2 [plugins] enabled = project_requirements [allowlist] requirements = {absolute_file_path} """) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) mirror.packages_to_sync = { "foo": "", "bar": "", "baz": "", } mirror._filter_packages() self.assertEqual({"foo": ""}, mirror.packages_to_sync)
def test_latest_releases_uninitialized(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.info = {"name": "foo", "version": "2.0.0"} pkg.releases = { "1.0.0": {}, "1.1.0": {}, "1.1.1": {}, "1.1.2": {}, "1.1.3": {}, "2.0.0": {}, } pkg._filter_releases() assert pkg.releases == { "1.0.0": {}, "1.1.0": {}, "1.1.1": {}, "1.1.2": {}, "1.1.3": {}, "2.0.0": {}, }
def test__filter__commented__out(self) -> None: mock_config("""\ [mirror] storage-backend = filesystem workers = 2 [plugins] enabled = allowlist_project [allowlist] packages = foo==1.2.3 # inline comment # bar """) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) mirror.packages_to_sync = { "foo": "", "bar": "", "snu": "", } mirror._filter_packages() self.assertEqual({"foo": ""}, mirror.packages_to_sync)
def test__casing__no__affect(self) -> None: mock_config("""\ [mirror] storage-backend = filesystem workers = 2 [plugins] enabled = allowlist_release [allowlist] packages = Foo<=1.2.0 """) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1) pkg._metadata = { "info": { "name": "foo" }, "releases": { "1.2.0": {}, "1.2.1": {} }, } pkg.filter_all_releases(mirror.filters.filter_release_plugins()) self.assertEqual(pkg.releases, {"1.2.0": {}})
def test__filter__varying__specifiers(self) -> None: mock_config( """\ [mirror] storage-backend = filesystem [plugins] enabled = allowlist_project [allowlist] packages = foo==1.2.3 bar~=3.0,<=1.5 """ ) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) mirror.packages_to_sync = { "foo": "", "bar": "", "snu": "", } mirror._filter_packages() self.assertEqual({"foo": "", "bar": ""}, mirror.packages_to_sync)
def test__dont__filter__prereleases(self) -> None: mock_config( """\ [plugins] enabled = allowlist_release [allowlist] packages = foo<=1.2.0 """ ) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1) pkg._metadata = { "info": {"name": "foo"}, "releases": { "1.1.0a2": {}, "1.1.1beta1": {}, "1.2.0": {}, "1.2.1": {}, "1.2.2alpha3": {}, "1.2.3rc1": {}, }, } pkg.filter_all_releases(mirror.filters.filter_release_plugins()) self.assertEqual(pkg.releases, {"1.1.0a2": {}, "1.1.1beta1": {}, "1.2.0": {}})
def test__filter__matches__release(self) -> None: with open(Path(self.tempdir.name) / "requirements.txt", "w") as fh: fh.write( """\ # This is needed for workshop 1 # foo==1.2.0 # via -r requirements.in """ ) mock_config( f"""\ [plugins] enabled = project_requirements project_requirements_pinned [allowlist] requirements_path = {self.tempdir.name} requirements = requirements.txt """ ) mirror = BandersnatchMirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1) pkg._metadata = { "info": {"name": "foo"}, "releases": {"1.2.0": {}, "1.2.1": {}}, } pkg.filter_all_releases(mirror.filters.filter_release_plugins()) self.assertEqual({"1.2.0": {}}, pkg.releases)
def test_latest_releases_uninitialized(self) -> None: mock_config(self.config_contents) mirror = Mirror(Path("."), Master(url="https://foo.bar.com")) pkg = Package("foo", 1, mirror) pkg._metadata = { "info": { "name": "foo", "version": "2.0.0" }, "releases": { "1.0.0": {}, "1.1.0": {}, "1.1.1": {}, "1.1.2": {}, "1.1.3": {}, "2.0.0": {}, }, } pkg._filter_all_releases(mirror.filters.filter_release_plugins()) assert pkg.releases == { "1.0.0": {}, "1.1.0": {}, "1.1.1": {}, "1.1.2": {}, "1.1.3": {}, "2.0.0": {}, }
def test__filter__matches__package(self): with open(TEST_CONF, "w") as testconfig_handle: testconfig_handle.write( """\ [plugins] enabled = whitelist_release_pyversion [whitelist] python_versions = foo """ ) instance = BandersnatchConfig() instance.config_file = TEST_CONF instance.load_configuration() mirror = Mirror(".", Master(url="https://foo.bar.com")) pkg = Package("foo", 1, mirror) pkg.info = {"name": "foo"} pkg.releases = {"1.2.0": [{'python_version': 'foo'}, {'python_version': 'foo2'}], "1.2.1": [{'python_version': 'foo2'}]} pkg._filter_releases() self.assertListEqual(list(pkg.releases.keys()), ["1.2.0"]) self.assertEqual(len(pkg.releases["1.2.0"]), 1) self.assertEqual(pkg.releases["1.2.0"][0], {'python_version': 'foo'})
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"]
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": {}}
def test_rpc_factory(): master = Master('https://pypi.example.com') assert isinstance(master.rpc(), xmlrpc2.client.Client)