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)
def test_custom_exceptions(): curr_stdout = sys.stdout curr_stderr = sys.stderr try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err excs = [ NotFound, InvalidParameter, InvalidState, RepositoryError, ScriptError ] for Exc in excs: try: with Console(exit_on_error=False): raise Exc("urgh") except: pass out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == len(excs) finally: sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_version_cmp(data): repo = Repository() pkg_mgr = PackageManager(repo=repo, installer=None, console=Console(exit_on_error=False)) for syntax in ["toolkit>=1.0.3"]: pkg = pkg_mgr.search(syntax) assert utils.pkg_comp_version("1.05.0", "1.5.0") < 0 assert utils.pkg_comp_version("1.0.15", "1.0.2") > 0 assert utils.pkg_comp_version("10.0.0", "9.999.999") > 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, overwrite=data["overwrite"]) pkg = PackageBuilder(os.path.abspath(data["file"])).build() c.info("uploading {}...".format(str(pkg))) pkg_mgr.upload(pkg, os.path.abspath(data["file"])) c.badge("uploaded", "success")
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) for syntax in data["syntax"]: pkg = pkg_mgr.search(syntax) if pkg: c.output(pkg)
def handle_push(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)) pkg_mgr.restore(data["zipped_repo"])
def handle_pull(config, data): with Console(verbose=config.get("verbose", False), exit_on_error=True) as c: repo = init_repository(c, config["repository"]) is_python3 = config.get("python3", False) pkg_mgr = PackageManager(repo, console=c, installer=None, is_python3=config.get("python3", False)) pkg_mgr.clone(data["destination"])
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 syntax in data["packages"]: pkg_mgr.uninstall(Package.from_text(syntax))
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)) downloaded = pkg_mgr.download_by_name(data["obj"], data["dir"]) c.info("Downloaded: {0}".format(downloaded))
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 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, mirroring=data["mirror"], install_deps=not data["no_dependencies"]) if data["requirements"]: c.info("installing from requirements file...") for syntax in open(data["requirements"], "r").readlines(): c.info("installing {}".format(syntax)) pkg_mgr.install(syntax, data["type"], data["no_user"]) else: for syntax in data["packages"]: c.info("installing {}".format(syntax)) pkg_mgr.install(syntax, data["type"], data["no_user"])
def test_install_no_user(): repo = Repository() installer = Installer() pm = PackageManager(repo=repo, 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) #install pm.install("test_package==1.0.0", "SOURCE", no_user=False) assert check_if_installed(installer, "test_package-1.0.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) for syntax in data["packages"]: pkg = pkg_mgr.search(syntax) ok = pkg is not None if ok: ok = pkg_mgr.remove(pkg, interactive=not data["non_interactive"]) if ok: c.badge("removed", "success")
def test_remove_with_repo_error(): repo = Repository() def problem(x): raise Exception() repo.delete = problem pm = PackageManager(repo=repo, 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) assert not pm.remove(pkg, interactive=False)
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_without_deps(): repo = Repository() installer = Installer() pm = PackageManager(repo=repo, installer=installer, overwrite=False, mirroring=True, install_deps=False, console=Console(exit_on_error=False)) pkg1 = Package(name="test_package", version="1.0.0", type="SOURCE") filename1 = resource_filename(__name__, "data/test_package-1.0.0.zip") pkg2 = Package(name="other_package", version="1.0.0", type="SOURCE") #depends on test_package filename2 = resource_filename(__name__, "data/other_package-1.0.0.tar.gz") pm.upload(pkg1, filename1) pm.upload(pkg2, filename2) pm.install("other_package==1.0.0", "SOURCE", no_user=False) assert check_if_installed(installer, "other_package-1.0.0")
def test_search(): 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) assert pm.search("test_package") == pkg assert pm.search("test_package==1.0.0") == pkg assert pm.search("test_package>0.9.9") == pkg assert pm.search("test_package<1.9.9") == pkg assert pm.search("fake") == None with pytest.raises(InvalidParameter): #no name pm.search("") with pytest.raises(InvalidParameter): #no ops pm.search("1.0.0")
def test_output(): curr_stdout = sys.stdout curr_stderr = sys.stderr curr_stdin = sys.stdin try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err with Console() as c: c.output("hello") out.seek(0) err.seek(0) assert len(out.readlines()) == 1 assert len(err.readlines()) == 0 finally: sys.stdin = curr_stdin sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_dynamic(): curr_stdout = sys.stdout curr_stderr = sys.stderr try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err with Console() as c: pending = c.info("hello", "\r") c.blank(pending) c.info("world") out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == 1 finally: sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_selection(): curr_stdout = sys.stdout curr_stderr = sys.stderr curr_stdin = sys.stdin try: out = StringIO() err = StringIO() cin = StringIO("y\n") sys.stdout = out sys.stderr = err sys.stdin = cin with Console() as c: assert c.selection("do this", ["y", "n"]) == "y" out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == 1 finally: sys.stdin = curr_stdin sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_input_with_response(): curr_stdout = sys.stdout curr_stderr = sys.stderr curr_stdin = sys.stdin try: out = StringIO() err = StringIO() cin = StringIO("hello\n") sys.stdout = out sys.stderr = err sys.stdin = cin with Console() as c: assert c.input("do this", "default") == "hello" out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == 1 finally: sys.stdin = curr_stdin sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_exception(): curr_stdout = sys.stdout curr_stderr = sys.stderr try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err try: with Console(exit_on_error=False) as c: c.info("hello") raise Exception("urgh") except: pass out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == 2 finally: sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_no_exception(): curr_stdout = sys.stdout curr_stderr = sys.stderr try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err with Console() as c: c.info("hello") c.error("wrong") c.warning("careful") c.badge("hello", "success") c.badge("hello", "warning") c.badge("hello", "danger") out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len(err.readlines()) == 6 finally: sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_exception_verbose(): curr_stdout = sys.stdout curr_stderr = sys.stderr try: out = StringIO() err = StringIO() sys.stdout = out sys.stderr = err try: with Console(exit_on_error=False, verbose=True) as c: c.info("hello") raise Exception("urgh") except: pass out.seek(0) err.seek(0) assert len(out.readlines()) == 0 assert len( err.readlines()) > 1 #expecting more than one line from verbose finally: sys.stdout = curr_stdout sys.stderr = curr_stderr
def test_init(): pm = PackageManager(repo=Repository(), installer=Installer(), console=Console(exit_on_error=False)) assert [x for x in pm.list_items()] == []
def handle_(config, data): with Console(verbose=config.get("verbose", False), exit_on_error=True) as c: print_syntax(c)
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)