def test_cloning_no_overwrite():
    repo1 = Repository()
    pm = PackageManager(repo=repo1,
                        installer=Installer(),
                        overwrite=False,
                        console=Console(exit_on_error=False))
    pkg = Package(name="test_package", version="1.0.0", type="SOURCE")
    filename = resource_filename(__name__, "data/test_package-1.0.0.zip")
    pm.upload(pkg, filename)
    try:
        tdir = tempfile.mkdtemp()
        pm.clone(tdir)
        assert os.listdir(tdir)
        cloned_zip = os.path.join(tdir, os.listdir(tdir)[0])

        #tryint to write back in the same repository if not in overwrite mode
        #is not allowed
        assert not pm.restore(cloned_zip, interactive=False)

        dest_pm = PackageManager(repo=Repository(),
                                 installer=Installer(),
                                 console=Console(exit_on_error=False))
        assert not dest_pm.list_items()
        dest_pm.restore(cloned_zip, interactive=False)
        assert dest_pm.list_items() == pm.list_items()
    finally:
        shutil.rmtree(tdir)
Beispiel #2
0
def handle_(config, data):
    with Console(verbose=config.get("verbose", False),
                 exit_on_error=True) as c:
        repo = init_repository(c, config["repository"])
        pkg_mgr = PackageManager(repo,
                                 console=c,
                                 installer=None,
                                 is_python3=config.get("python3", False))
        for path in sorted(pkg_mgr.list_items(data["package"],
                                              from_cache=True)):
            c.output(path.split("/")[-1])
def test_remove():
    pm = PackageManager(repo=Repository(),
                        installer=Installer(),
                        overwrite=False,
                        console=Console(exit_on_error=False))
    pkg = Package(name="test_package", version="1.0.0", type="SOURCE")
    filename = resource_filename(__name__, "data/test_package-1.0.0.zip")
    pm.upload(pkg, filename)
    pm.list_items() == [Package.repo_name(pkg, filename)]
    assert pm.remove(pkg, interactive=False)
    pm.list_items() == []
    #expecting cache to be updated
    pm.list_items(from_cache=True) == []
    #removing second time
    assert not pm.remove(pkg, interactive=False)
    #removing non existing
    assert not pm.remove(Package("some", "1.0.0"), interactive=False)
def test_upload_download():
    pm = PackageManager(repo=Repository(),
                        installer=Installer(),
                        overwrite=False,
                        console=Console(exit_on_error=False))
    pkg = Package(name="test_package", version="1.0.0", type="SOURCE")
    wrong = Package(name="test_package", version="")
    filename = resource_filename(__name__, "data/test_package-1.0.0.zip")
    with pytest.raises(InvalidParameter):
        pm.upload(wrong, filename)
    pm.upload(pkg, filename)
    pm.list_items() == [Package.repo_name(pkg, filename)]
    #expecting cache to be updated
    pm.list_items(from_cache=True) == [Package.repo_name(pkg, filename)]
    #uploading again without overwrite mode
    with pytest.raises(InvalidState):
        pm.upload(pkg, filename)
    #downloading by name
    try:
        tdir = tempfile.mkdtemp()
        #non existing
        assert not pm.download_by_name("fake", tdir)
        assert not pm.download_by_name(
            Package.repo_name(
                Package(name="test_package", version="2.0.0", type="SOURCE"),
                filename), tdir)
        #valid
        dest = pm.download_by_name(Package.repo_name(pkg, filename), tdir)
        source_md5 = hashlib.md5()
        with open(filename, "rb") as f:
            source_md5.update(f.read())
        dest_md5 = hashlib.md5()
        with open(dest, "rb") as f:
            dest_md5.update(f.read())
        assert source_md5.hexdigest() == dest_md5.hexdigest()
    finally:
        shutil.rmtree(tdir)
    #downloading by package with version specified
    try:
        tdir = tempfile.mkdtemp()
        #non existing
        assert not pm.download(Package(name="fake", version="", type="SOURCE"),
                               tdir,
                               preferred_type="SOURCE")
        assert not pm.download(Package(
            name="test_package", version="2.0.0", type="SOURCE"),
                               tdir,
                               preferred_type="SOURCE")
        dest = pm.download(pkg, tdir, preferred_type="SOURCE")
        source_md5 = hashlib.md5()
        with open(filename, "rb") as f:
            source_md5.update(f.read())
        dest_md5 = hashlib.md5()
        with open(dest, "rb") as f:
            dest_md5.update(f.read())
        assert source_md5.hexdigest() == dest_md5.hexdigest()
    finally:
        shutil.rmtree(tdir)
    #downloading by package without version specified (gets last)
    try:
        tdir = tempfile.mkdtemp()
        dest = pm.download(Package(name="test_package",
                                   version="",
                                   type="SOURCE"),
                           tdir,
                           preferred_type="SOURCE")
        source_md5 = hashlib.md5()
        with open(filename, "rb") as f:
            source_md5.update(f.read())
        dest_md5 = hashlib.md5()
        with open(dest, "rb") as f:
            dest_md5.update(f.read())
        assert source_md5.hexdigest() == dest_md5.hexdigest()
    finally:
        shutil.rmtree(tdir)
def test_init():
    pm = PackageManager(repo=Repository(),
                        installer=Installer(),
                        console=Console(exit_on_error=False))
    assert [x for x in pm.list_items()] == []