2018-08-04 23:13:43 +02:00
|
|
|
#!/usr/bin/env py.test
|
|
|
|
"""Tests for manage.py."""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
try:
|
|
|
|
from unittest.mock import Mock
|
|
|
|
except ImportError:
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pypiserver import manage
|
|
|
|
from pypiserver.core import (
|
|
|
|
PkgFile,
|
|
|
|
guess_pkgname_and_version,
|
|
|
|
parse_version,
|
|
|
|
)
|
|
|
|
from pypiserver.manage import (
|
|
|
|
PipCmd,
|
|
|
|
build_releases,
|
|
|
|
filter_stable_releases,
|
|
|
|
filter_latest_pkgs,
|
|
|
|
is_stable_version,
|
|
|
|
update_package,
|
2020-07-17 06:03:30 +02:00
|
|
|
update_all_packages,
|
2018-08-04 23:13:43 +02:00
|
|
|
)
|
2012-12-28 01:48:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
def touch_files(root, files):
|
2018-08-04 23:13:43 +02:00
|
|
|
root = py.path.local(root) # pylint: disable=no-member
|
2012-12-28 01:48:18 +01:00
|
|
|
for f in files:
|
|
|
|
root.join(f).ensure()
|
|
|
|
|
|
|
|
|
|
|
|
def pkgfile_from_path(fn):
|
|
|
|
pkgname, version = guess_pkgname_and_version(fn)
|
2020-10-06 04:04:22 +02:00
|
|
|
return PkgFile(
|
|
|
|
pkgname=pkgname,
|
|
|
|
version=version,
|
|
|
|
root=py.path.local(fn)
|
|
|
|
.parts()[1]
|
|
|
|
.strpath, # noqa pylint: disable=no-member
|
|
|
|
fn=fn,
|
|
|
|
)
|
2012-12-28 01:48:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("version", "is_stable"),
|
2020-10-06 04:04:22 +02:00
|
|
|
[
|
|
|
|
("1.0", True),
|
|
|
|
("0.0.0", True),
|
|
|
|
("1.1beta1", False),
|
|
|
|
("1.2.10-123", True),
|
|
|
|
("5.5.0-DEV", False),
|
|
|
|
("1.2-rc1", False),
|
|
|
|
("1.0b1", False),
|
|
|
|
],
|
|
|
|
)
|
2012-12-28 01:48:18 +01:00
|
|
|
def test_is_stable_version(version, is_stable):
|
|
|
|
parsed_version = parse_version(version)
|
|
|
|
assert is_stable_version(parsed_version) == is_stable
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_releases():
|
2020-10-06 04:04:22 +02:00
|
|
|
p = pkgfile_from_path("/home/ralf/pypiserver/d/greenlet-0.2.zip")
|
2012-12-28 01:48:18 +01:00
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
expected = dict(
|
|
|
|
parsed_version=("00000000", "00000003", "*final"),
|
|
|
|
pkgname="greenlet",
|
|
|
|
replaces=p,
|
|
|
|
version="0.3.0",
|
|
|
|
)
|
2012-12-28 01:48:18 +01:00
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
(res,) = list(build_releases(p, ["0.3.0"]))
|
2016-01-04 22:42:39 +01:00
|
|
|
for k, v in expected.items():
|
|
|
|
assert getattr(res, k) == v
|
2012-12-28 01:48:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_filter_stable_releases():
|
2020-10-06 04:04:22 +02:00
|
|
|
p = pkgfile_from_path("/home/ralf/pypiserver/d/greenlet-0.2.zip")
|
2012-12-28 01:48:18 +01:00
|
|
|
assert list(filter_stable_releases([p])) == [p]
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
p2 = pkgfile_from_path("/home/ralf/pypiserver/d/greenlet-0.5rc1.zip")
|
2012-12-28 01:48:18 +01:00
|
|
|
assert list(filter_stable_releases([p2])) == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_filter_latest_pkgs():
|
2020-10-06 04:04:22 +02:00
|
|
|
paths = [
|
|
|
|
"/home/ralf/greenlet-0.2.zip",
|
|
|
|
"/home/ralf/foo/baz-1.0.zip" "/home/ralf/bar/greenlet-0.3.zip",
|
|
|
|
]
|
2012-12-28 01:48:18 +01:00
|
|
|
pkgs = [pkgfile_from_path(x) for x in paths]
|
|
|
|
|
|
|
|
assert frozenset(filter_latest_pkgs(pkgs)) == frozenset(pkgs[1:])
|
2012-12-28 01:53:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_filter_latest_pkgs_case_insensitive():
|
2020-10-06 04:04:22 +02:00
|
|
|
paths = [
|
|
|
|
"/home/ralf/greenlet-0.2.zip",
|
|
|
|
"/home/ralf/foo/baz-1.0.zip" "/home/ralf/bar/Greenlet-0.3.zip",
|
|
|
|
]
|
2012-12-28 01:53:10 +01:00
|
|
|
pkgs = [pkgfile_from_path(x) for x in paths]
|
|
|
|
|
|
|
|
assert frozenset(filter_latest_pkgs(pkgs)) == frozenset(pkgs[1:])
|
2018-08-04 23:13:43 +02:00
|
|
|
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"pip_ver, cmd_type",
|
|
|
|
(
|
|
|
|
("10.0.0", "d"),
|
|
|
|
("10.0.0rc10", "d"),
|
|
|
|
("10.0.0b10", "d"),
|
|
|
|
("10.0.0a3", "d"),
|
|
|
|
("10.0.0.dev8", "d"),
|
|
|
|
("10.0.0.dev8", "d"),
|
|
|
|
("18.0", "d"),
|
|
|
|
("9.9.8", "i"),
|
|
|
|
("9.9.8rc10", "i"),
|
|
|
|
("9.9.8b10", "i"),
|
|
|
|
("9.9.8a10", "i"),
|
|
|
|
("9.9.8.dev10", "i"),
|
|
|
|
("9.9", "i"),
|
|
|
|
),
|
|
|
|
)
|
2018-08-04 23:13:43 +02:00
|
|
|
def test_pip_cmd_root(pip_ver, cmd_type):
|
|
|
|
"""Verify correct determination of the command root by pip version."""
|
|
|
|
exp_cmd = (
|
2020-10-06 04:04:22 +02:00
|
|
|
"pip",
|
|
|
|
"-q",
|
|
|
|
"install" if cmd_type == "i" else "download",
|
2018-08-04 23:13:43 +02:00
|
|
|
)
|
|
|
|
assert tuple(PipCmd.update_root(pip_ver)) == exp_cmd
|
|
|
|
|
|
|
|
|
|
|
|
def test_pip_cmd_update():
|
|
|
|
"""Verify the correct determination of a pip command."""
|
2020-10-06 04:04:22 +02:00
|
|
|
index = "https://pypi.org/simple"
|
|
|
|
destdir = "foo/bar"
|
|
|
|
pkg_name = "mypkg"
|
|
|
|
pkg_version = "12.0"
|
|
|
|
cmd_root = ("pip", "-q", "download")
|
2018-08-04 23:13:43 +02:00
|
|
|
exp_cmd = cmd_root + (
|
2020-10-06 04:04:22 +02:00
|
|
|
"--no-deps",
|
|
|
|
"-i",
|
2018-08-04 23:13:43 +02:00
|
|
|
index,
|
2020-10-06 04:04:22 +02:00
|
|
|
"-d",
|
2018-08-04 23:13:43 +02:00
|
|
|
destdir,
|
2020-10-06 04:04:22 +02:00
|
|
|
"{}=={}".format(pkg_name, pkg_version),
|
2018-08-04 23:13:43 +02:00
|
|
|
)
|
|
|
|
assert exp_cmd == tuple(
|
|
|
|
PipCmd.update(cmd_root, destdir, pkg_name, pkg_version)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_pip_cmd_update_index_overridden():
|
|
|
|
"""Verify the correct determination of a pip command."""
|
2020-10-06 04:04:22 +02:00
|
|
|
index = "https://pypi.org/complex"
|
|
|
|
destdir = "foo/bar"
|
|
|
|
pkg_name = "mypkg"
|
|
|
|
pkg_version = "12.0"
|
|
|
|
cmd_root = ("pip", "-q", "download")
|
2018-08-04 23:13:43 +02:00
|
|
|
exp_cmd = cmd_root + (
|
2020-10-06 04:04:22 +02:00
|
|
|
"--no-deps",
|
|
|
|
"-i",
|
|
|
|
index,
|
|
|
|
"-d",
|
|
|
|
destdir,
|
|
|
|
"{}=={}".format(pkg_name, pkg_version),
|
2018-08-04 23:13:43 +02:00
|
|
|
)
|
|
|
|
assert exp_cmd == tuple(
|
|
|
|
PipCmd.update(cmd_root, destdir, pkg_name, pkg_version, index=index)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_package(monkeypatch):
|
|
|
|
"""Test generating an update command for a package."""
|
2020-10-06 04:04:22 +02:00
|
|
|
monkeypatch.setattr(manage, "call", Mock())
|
|
|
|
pkg = PkgFile("mypkg", "1.0", replaces=PkgFile("mypkg", "0.9"))
|
|
|
|
update_package(pkg, ".")
|
|
|
|
manage.call.assert_called_once_with(
|
|
|
|
( # pylint: disable=no-member
|
|
|
|
"pip",
|
|
|
|
"-q",
|
|
|
|
"download",
|
|
|
|
"--no-deps",
|
|
|
|
"-i",
|
|
|
|
"https://pypi.org/simple",
|
|
|
|
"-d",
|
|
|
|
".",
|
|
|
|
"mypkg==1.0",
|
|
|
|
)
|
|
|
|
)
|
2018-08-04 23:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_package_dry_run(monkeypatch):
|
|
|
|
"""Test generating an update command for a package."""
|
2020-10-06 04:04:22 +02:00
|
|
|
monkeypatch.setattr(manage, "call", Mock())
|
|
|
|
pkg = PkgFile("mypkg", "1.0", replaces=PkgFile("mypkg", "0.9"))
|
|
|
|
update_package(pkg, ".", dry_run=True)
|
2018-08-04 23:13:43 +02:00
|
|
|
assert not manage.call.mock_calls # pylint: disable=no-member
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_all_packages(monkeypatch):
|
|
|
|
"""Test calling update_all_packages()"""
|
2020-10-06 04:04:22 +02:00
|
|
|
public_pkg_1 = PkgFile("Flask", "1.0")
|
|
|
|
public_pkg_2 = PkgFile("requests", "1.0")
|
|
|
|
private_pkg_1 = PkgFile("my_private_pkg", "1.0")
|
|
|
|
private_pkg_2 = PkgFile("my_other_private_pkg", "1.0")
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
roots_mock = {
|
2020-10-06 04:04:22 +02:00
|
|
|
"/opt/pypi": [
|
2020-07-17 06:03:30 +02:00
|
|
|
public_pkg_1,
|
|
|
|
private_pkg_1,
|
|
|
|
],
|
2020-10-06 04:04:22 +02:00
|
|
|
"/data/pypi": [public_pkg_2, private_pkg_2],
|
2020-07-17 06:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def core_listdir_mock(directory):
|
|
|
|
return roots_mock.get(directory, [])
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
monkeypatch.setattr(manage.core, "listdir", core_listdir_mock)
|
|
|
|
monkeypatch.setattr(manage.core, "read_lines", Mock(return_value=[]))
|
|
|
|
monkeypatch.setattr(manage, "update", Mock(return_value=None))
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
destdir = None
|
|
|
|
dry_run = False
|
|
|
|
stable_only = True
|
|
|
|
blacklist_file = None
|
|
|
|
|
|
|
|
update_all_packages(
|
|
|
|
roots=list(roots_mock.keys()),
|
|
|
|
destdir=destdir,
|
|
|
|
dry_run=dry_run,
|
|
|
|
stable_only=stable_only,
|
|
|
|
blacklist_file=blacklist_file,
|
|
|
|
)
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
manage.core.read_lines.assert_not_called() # pylint: disable=no-member
|
|
|
|
manage.update.assert_called_once_with( # pylint: disable=no-member
|
2020-07-17 06:03:30 +02:00
|
|
|
frozenset([public_pkg_1, public_pkg_2, private_pkg_1, private_pkg_2]),
|
|
|
|
destdir,
|
|
|
|
dry_run,
|
2020-10-06 04:04:22 +02:00
|
|
|
stable_only,
|
2020-07-17 06:03:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_all_packages_with_blacklist(monkeypatch):
|
|
|
|
"""Test calling update_all_packages()"""
|
2020-10-06 04:04:22 +02:00
|
|
|
public_pkg_1 = PkgFile("Flask", "1.0")
|
|
|
|
public_pkg_2 = PkgFile("requests", "1.0")
|
|
|
|
private_pkg_1 = PkgFile("my_private_pkg", "1.0")
|
|
|
|
private_pkg_2 = PkgFile("my_other_private_pkg", "1.0")
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
roots_mock = {
|
2020-10-06 04:04:22 +02:00
|
|
|
"/opt/pypi": [
|
2020-07-17 06:03:30 +02:00
|
|
|
public_pkg_1,
|
|
|
|
private_pkg_1,
|
|
|
|
],
|
2020-10-06 04:04:22 +02:00
|
|
|
"/data/pypi": [public_pkg_2, private_pkg_2],
|
2020-07-17 06:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def core_listdir_mock(directory):
|
|
|
|
return roots_mock.get(directory, [])
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
monkeypatch.setattr(manage.core, "listdir", core_listdir_mock)
|
|
|
|
monkeypatch.setattr(
|
|
|
|
manage.core,
|
|
|
|
"read_lines",
|
|
|
|
Mock(return_value=["my_private_pkg", "my_other_private_pkg"]),
|
|
|
|
)
|
|
|
|
monkeypatch.setattr(manage, "update", Mock(return_value=None))
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
destdir = None
|
|
|
|
dry_run = False
|
|
|
|
stable_only = True
|
2020-10-06 04:04:22 +02:00
|
|
|
blacklist_file = "/root/pkg_blacklist"
|
2020-07-17 06:03:30 +02:00
|
|
|
|
|
|
|
update_all_packages(
|
|
|
|
roots=list(roots_mock.keys()),
|
|
|
|
destdir=destdir,
|
|
|
|
dry_run=dry_run,
|
|
|
|
stable_only=stable_only,
|
|
|
|
blacklist_file=blacklist_file,
|
|
|
|
)
|
|
|
|
|
2020-10-06 04:04:22 +02:00
|
|
|
manage.update.assert_called_once_with( # pylint: disable=no-member
|
|
|
|
frozenset([public_pkg_1, public_pkg_2]), destdir, dry_run, stable_only
|
2020-07-17 06:03:30 +02:00
|
|
|
)
|
2020-10-06 04:04:22 +02:00
|
|
|
manage.core.read_lines.assert_called_once_with(
|
|
|
|
blacklist_file
|
|
|
|
) # pylint: disable=no-member
|