def test_create_in_symlink(workon_sym_home): invoke('new', 'env', '-d') pip_path = Path(invoke('in', 'env', 'which', 'pip').out) with pip_path.open() as f: pip_shebang = f.readline() # check it is using the symlinked path, not the real path assert str(workon_sym_home) in pip_shebang
def test_source_relocatable(workon_home, testpackageenv): check_call(['virtualenv', '--relocatable', str(workon_home / 'source')]) invoke('cp', 'source', 'destination', '-d') testscript = Path( invoke('workon', 'destination', inp='which testscript.py').out.strip()) assert workon_home / 'destination' / 'bin' / 'testscript.py' == testscript invoke('rm', 'destination')
def test_new_env_activated(workon_home, testpackageenv): invoke('cp', 'source', 'destination', '-d') testscript = Path(invoke('in', 'destination', which_cmd, 'testscript.py').out.strip()) assert ('destination', 'testscript.py') == (testscript.parts[-3], testscript.parts[-1]) with testscript.open() as f: assert str(workon_home / 'destination') in f.read() invoke('rm', 'destination')
def test_source_relocatable(workon_home, testpackageenv): check_call([executable, '-m', 'virtualenv', '--relocatable', str(workon_home / 'source')]) invoke('cp', 'source', 'destination', '-d') testscript = Path(invoke('workon', 'destination', inp='which testscript.py').out.strip()) assert workon_home / 'destination' / 'bin' / 'testscript.py' == testscript invoke('rm', 'destination')
def test_associate(workon_home): project = Path('/dev/null') invoke('new', 'env', '-a', str(project), '-d') dotproject = workon_home / 'env' / '.project' assert dotproject.exists() with dotproject.open() as f: assert f.read() == str(project.absolute()) invoke('rm', 'env')
def test_apply_template(project_home, testtemplate): projname = 'project1' invoke('mkproject', '-t', 'test', projname, '-d') testfile = project_home / projname / 'TEST_FILE' assert testfile.exists() with testfile.open() as f: assert projname in f.read() invoke('rm', projname) rmtree(str(project_home / projname))
def test_getproject(env1): """Check that ``getproject`` prints an environment's project directory.""" with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: invoke('setproject', 'env1', tmpdir) res = invoke('getproject', 'env1') assert not res.err assert res.out == tmpdir
def test_requirements_file(workon_home): with NamedTemporaryFile(delete=False) as temp: temp.write(b'IPy') temp.close() invoke('new', 'env', '-r', temp.name, '-d') freeze = invoke('in', 'env', 'pip', 'freeze').out assert 'IPy' in freeze invoke('rm', 'env') unlink(temp.name)
def test_new_env_activated(workon_home, testpackageenv): invoke('cp', 'source', 'destination', '-d') testscript = Path( invoke('in', 'destination', which_cmd, 'testscript.py').out.strip()) assert ('destination', 'testscript.py') == (testscript.parts[-3], testscript.parts[-1]) with testscript.open() as f: assert str(workon_home / 'destination') in f.read() invoke('rm', 'destination')
def test_restore(workon_home, env1): patterns = ['lib*/*/site.py*', 'Lib/site.py*'] to_be_deleted = set(chain(*((workon_home / 'env1').glob(pat) for pat in patterns))) for site in to_be_deleted: site.unlink() result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' in result.err or 'fail' in result.err invoke('restore', 'env1') result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' not in result.err and 'fail' not in result.err
def test_restore(workon_home, env1): patterns = ['lib*/*/site.py*', 'Lib/site.py*'] to_be_deleted = set( chain(*((workon_home / 'env1').glob(pat) for pat in patterns))) for site in to_be_deleted: site.unlink() result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' in result.err or 'fail' in result.err invoke('restore', 'env1') result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' not in result.err and 'fail' not in result.err
def test_restore(workon_home, env1): patterns = ['lib*/*/site.py*', 'Lib/site.py*'] to_be_deleted = set( chain(*((workon_home / 'env1').glob(pat) for pat in patterns))) for site in to_be_deleted: try: site.unlink() except FileNotFoundError: pass # multiple links to the same file might appear in to_be_deleted result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' in result.err or 'fail' in result.err invoke('restore', 'env1') result = invoke('in', 'env1', 'python', '-vc', '') assert 'Error' not in result.err and 'fail' not in result.err
def test_ignore_environ(workon_home, env1, env2): with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: res = invoke('workon', 'env1', inp='pew setproject env2 ' + tmpdir) assert not (workon_home / 'env1' / '.project').exists() assert check_project_dir(workon_home, 'env2', tmpdir)
def test_setproject(workon_home, env1): with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: res = invoke('setproject', 'env1', tmpdir) assert not res.err assert check_project_dir(workon_home, 'env1', tmpdir)
def sitepackages_dir(env=os.environ.get('VIRTUAL_ENV')): if not env: sys.exit('ERROR: no virtualenv active') else: env_python = workon_home / env / env_bin_dir / 'python' return Path(invoke(str(env_python), '-c', 'import distutils; \ print(distutils.sysconfig.get_python_lib())').out)
def test_setproject(workon_home, env1): with temp_environ(): del os.environ['VIRTUAL_ENV'] with TemporaryDirectory() as tmpdir: res = invoke('setproject', 'env1', tmpdir) assert not res.err assert check_project_dir(workon_home, 'env1', tmpdir)
def test_ignore_environ(workon_home, env1, env2): with temp_environ(): del os.environ['VIRTUAL_ENV'] with TemporaryDirectory() as tmpdir: res = invoke('workon', 'env1', inp='pew setproject env2 ' + tmpdir) assert not (workon_home / 'env1' / '.project').exists() assert check_project_dir(workon_home, 'env2', tmpdir)
def test_implicit_project(workon_home, env1): "use the cwd as project directory" with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: res = invoke('setproject', 'env1', cwd=tmpdir) assert not res.err assert check_project_dir(workon_home, 'env1', tmpdir)
def sitepackages_dir(): if 'VIRTUAL_ENV' not in os.environ: sys.exit('ERROR: no virtualenv active') else: return Path( invoke( 'python', '-c', 'import distutils; \ print(distutils.sysconfig.get_python_lib())').out)
def test_workon_no_arg(env1, env2): result = invoke('workon') out = result.out envs = [ x.strip() for x in out.split() ] assert 0 == result.returncode assert 'env1' in envs assert 'env2' in envs
def test_implicit_project(workon_home, env1): "use the cwd as project directory" with temp_environ(): del os.environ['VIRTUAL_ENV'] with TemporaryDirectory() as tmpdir: res = invoke('setproject', 'env1', cwd=tmpdir) assert not res.err assert check_project_dir(workon_home, 'env1', tmpdir)
def wipeenv_cmd(): """Remove all installed packages from the current env.""" pkgs = map(lambda d: d.split("==")[0], invoke('pip', 'freeze').out.split()) to_remove = [pkg for pkg in pkgs if pkg not in ('distribute', 'wsgiref')] if to_remove: print("Uninstalling packages:\n%s" % "\n".join(to_remove)) check_call(['pip', 'uninstall', '-y'] + to_remove) else: print("Nothing to remove")
def wipeenv_cmd(): """Remove all installed packages from the current env.""" pkgs = map(lambda d: d.split("==")[0], invoke("pip", "freeze").out.split()) to_remove = [pkg for pkg in pkgs if pkg not in ("distribute", "wsgiref")] if to_remove: print("Uninstalling packages:\n%s" % "\n".join(to_remove)) check_call(["pip", "uninstall", "-y"] + to_remove) else: print("Nothing to remove")
def showvirtualenv(env): columns, _ = get_terminal_size() pkgs = sorted(packages(sitepackages_dir(env))) env_python = workon_home / env / env_bin_dir / "python" l = len(env) + 2 version = invoke(str(env_python), "-V") version = " - ".join((version.out + version.err).splitlines()) print(env, ": ", version, sep="") print(textwrap.fill(" ".join(pkgs), width=columns - l, initial_indent=(l * " "), subsequent_indent=(l * " ")), "\n")
def test_call_without_args_outside_active_venv(): """Check the error message if called without args outside a virtualenv. If ``getproject`` is called without additional arguments outside of an active virtualenv, it should print an error message. """ os.environ.pop('VIRTUAL_ENV', None) res = invoke('getproject') assert not res.out assert res.err == "ERROR: no virtualenv active"
def test_unknown_environment(): """Check the error message if passed an unknown environment name. If ``getproject`` is invoked with the name of an environment that does not exist, the call should fail with an appropriate error message. """ name = 'bogus-environment-that-/hopefully/-does-not-exist' res = invoke('getproject', name) assert not res.out assert res.err == "ERROR: Environment '{0}' does not exist.".format(name)
def showvirtualenv(env): columns, _ = get_terminal_size() pkgs = sorted(packages(sitepackages_dir(env))) env_python = workon_home / env / env_bin_dir / 'python' l = len(env) + 2 version = invoke(str(env_python), '-V') version = ' - '.join((version.out + version.err).splitlines()) print(env, ': ', version, sep='') print(textwrap.fill(' '.join(pkgs), width=columns-l, initial_indent=(l * ' '), subsequent_indent=(l * ' ')), '\n')
def showvirtualenv(env): columns, _ = get_terminal_size() pkgs = packages(sitepackages_dir(env)) env_python = workon_home / env / env_bin_dir / 'python' l = len(env) + 2 version = invoke(str(env_python), '-V') version = ' - '.join((version.out + version.err).splitlines()) print(env, ': ', version, sep='') print(textwrap.fill(' '.join(pkgs), width=columns-l, initial_indent=(l * ' '), subsequent_indent=(l * ' ')), '\n')
def sitepackages_dir(): if "VIRTUAL_ENV" not in os.environ: sys.exit("ERROR: no virtualenv active") else: return Path( invoke( "python", "-c", "import distutils; \ print(distutils.sysconfig.get_python_lib())", ).out )
def sitepackages_dir(env=os.environ.get("VIRTUAL_ENV")): if not env: sys.exit("ERROR: no virtualenv active") else: env_python = workon_home / env / env_bin_dir / "python" return Path( invoke( str(env_python), "-c", "import distutils; \ print(distutils.sysconfig.get_python_lib())", ).out )
def test_project_directory_not_set(env1): """Check the error message if no project directory was set. If no project directory has been configured for an environment, ``getproject`` should quit with an error message. """ name = 'env1' with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: res = invoke('getproject', name) assert not res.out assert res.err == ( "ERROR: no project directory set for Environment '{0}'".format( name))
def test_project_directory_not_set(env1): """Check the error message if no project directory was set. If no project directory has been configured for an environment, ``getproject`` should quit with an error message. """ name = 'env1' with temp_environ(): os.environ.pop('VIRTUAL_ENV', None) with TemporaryDirectory() as tmpdir: res = invoke('getproject', name) assert not res.out assert res.err == ( "ERROR: no project directory set for Environment '{0}'" .format(name) )
def wipeenv_cmd(argv): """Remove all installed packages from the current (or supplied) env.""" env = argv[0] if argv else os.environ.get('VIRTUAL_ENV') if not env: sys.exit('ERROR: no virtualenv active') elif not (workon_home / env).exists(): sys.exit("ERROR: Environment '{0}' does not exist.".format(env)) else: env_pip = str(workon_home / env / env_bin_dir / 'pip') all_pkgs = set(invoke(env_pip, 'freeze').out.splitlines()) pkgs = set(p for p in all_pkgs if len(p.split("==")) == 2) ignored = sorted(all_pkgs - pkgs) pkgs = set(p.split("==")[0] for p in pkgs) to_remove = sorted(pkgs - set(['distribute', 'wsgiref'])) if to_remove: print("Ignoring:\n %s" % "\n ".join(ignored)) print("Uninstalling packages:\n %s" % "\n ".join(to_remove)) inve(env, 'pip', 'uninstall', '-y', *to_remove) else: print("Nothing to remove")
def wipeenv_cmd(argv): """Remove all installed packages from the current (or supplied) env.""" env = argv[0] if argv else os.environ.get("VIRTUAL_ENV") if not env: sys.exit("ERROR: no virtualenv active") elif not (workon_home / env).exists(): sys.exit("ERROR: Environment '{0}' does not exist.".format(env)) else: env_pip = str(workon_home / env / env_bin_dir / "pip") all_pkgs = set(invoke(env_pip, "freeze").out.splitlines()) pkgs = set(p for p in all_pkgs if len(p.split("==")) == 2) ignored = sorted(all_pkgs - pkgs) pkgs = set(p.split("==")[0] for p in pkgs) to_remove = sorted(pkgs - set(["distribute", "wsgiref"])) if to_remove: print("Ignoring:\n %s" % "\n ".join(ignored)) print("Uninstalling packages:\n %s" % "\n ".join(to_remove)) inve(env, "pip", "uninstall", "-y", *to_remove) else: print("Nothing to remove")
def test_wipe(env1): assert not invoke('in', 'env1', 'pip', 'install', 'WebTest').err assert 'WebTest' in invoke('in', 'env1', 'pip', 'freeze').out invoke('wipeenv', 'env1') assert 'WebTest' not in invoke('in', 'env1', 'pip', 'freeze').out
def test_no_such_env(workon_home): assert not (workon_home / 'not_here').exists() assert invoke('rm', 'not_here').returncode != 0
def env1(workon_home): invoke('new', 'env1', '-d') yield invoke('rm', 'env1')
def env2(workon_home): invoke('new', 'env2', '-d') yield invoke('rm', 'env2')
def testpackageenv(workon_home): testpackage = str(Path(__file__).parent / 'testpackage') invoke('new', 'source', '-d') invoke('in', 'source', 'python', 'setup.py', 'install', cwd=testpackage) yield invoke('rm', 'source')
def project(workon_home, project_home): projname = 'project1' invoke('mkproject', projname, '-d') yield projname invoke('rm', projname) rmtree(str(project_home / projname))
def test_create(workon_home): envs = set(invoke('ls').out.split()) invoke('new', 'env', '-d') envs2 = set(invoke('in', 'env', 'pew', 'ls').out.split()) invoke('rm', 'env') assert envs < envs2
def test_list_templates(testtemplate): assert 'test' in invoke('mkproject', '-l').out
def test_remove(to_be_deleted): invoke('rm', to_be_deleted)
def test_install_2pkgs(workon_home): invoke('new', 'env', '-i', 'IPy', '-i', 'WebTest', '-d') freeze = invoke('in', 'env', 'pip', 'freeze').out assert 'IPy' in freeze assert 'WebTest' in freeze invoke('rm', 'env')
def test_gsp(workon_home): invoke('new', 'env', '--system-site-packages', '-d') site = invoke('in', 'env', 'pew', 'sitepackages_dir').out assert not (Path(site).parent / 'no-global-site-packages.txt').exists() invoke('rm', 'env')
def to_be_deleted(workon_home): envname = 'to_be_deleted' invoke('new', envname, '-d') yield envname assert not (workon_home / envname).exists()
def test_create_virtualenv(project_home, project): env = Path(invoke('in', project, *check_env).out) assert project == env.name with (env / '.project').open() as f: assert str(project_home.absolute() / project) == f.read().strip()