def test_commandpath_venv_precedence(tmpdir, monkeypatch, mocksession, newconfig): config = newconfig([], """ [testenv:py123] """) envconfig = config.envconfigs['py123'] venv = VirtualEnv(envconfig, session=mocksession) tmpdir.ensure("easy_install") monkeypatch.setenv("PATH", str(tmpdir), prepend=os.pathsep) envconfig.envbindir.ensure("easy_install") p = venv.getcommandpath("easy_install") assert py.path.local(p).relto(envconfig.envbindir), p
def _makevenv(self, name): envconfig = self.config.envconfigs.get(name, None) if envconfig is None: self.report.error("unknown environment %r" % name) raise LookupError(name) elif envconfig.envdir == self.config.toxinidir: self.report.error( "venv %r in %s would delete project" % (name, envconfig.envdir)) raise tox.exception.ConfigError('envdir must not equal toxinidir') venv = VirtualEnv(envconfig=envconfig, session=self) self._name2venv[name] = venv return venv
def test_develop_recreation(self, newconfig, mocksession): config = newconfig([], "") envconfig = config.envconfigs['python'] venv = VirtualEnv(envconfig, session=mocksession) action = mocksession.newaction(venv, "update") venv.update(action) cconfig = venv._getliveconfig() cconfig.usedevelop = True cconfig.writeconfig(venv.path_config) mocksession._clearmocks() action = mocksession.newaction(venv, "update") venv.update(action) mocksession.report.expect("verbosity0", "*recreate*")
def test_matchingdependencies(self, newconfig, mocksession): config = newconfig( [], """ [testenv] deps=abc """, ) envconfig = config.envconfigs["python"] venv = VirtualEnv(envconfig, session=mocksession) cconfig = venv._getliveconfig() config = newconfig( [], """ [testenv] deps=xyz """, ) envconfig = config.envconfigs["python"] venv = VirtualEnv(envconfig, session=mocksession) otherconfig = venv._getliveconfig() assert not cconfig.matches(otherconfig)
def test_dep_recreation(self, newconfig, mocksession): config = newconfig([], "") envconfig = config.envconfigs['python'] venv = VirtualEnv(envconfig, session=mocksession) action = mocksession.newaction(venv, "update") venv.update(action) cconfig = venv._getliveconfig() cconfig.deps[:] = [("1" * 32, "xyz.zip")] cconfig.writeconfig(venv.path_config) mocksession._clearmocks() action = mocksession.newaction(venv, "update") venv.update(action) mocksession.report.expect("*", "*recreate*")
def getvenv(self, name): if name in self.existing_venvs: return self.existing_venvs[name] env_config = self.config.envconfigs.get(name, None) if env_config is None: reporter.error("unknown environment {!r}".format(name)) raise LookupError(name) elif env_config.envdir == self.config.toxinidir: reporter.error("venv {!r} in {} would delete project".format(name, env_config.envdir)) raise tox.exception.ConfigError("envdir must not equal toxinidir") env_log = self.resultlog.get_envlog(name) venv = VirtualEnv(envconfig=env_config, popen=self.popen, env_log=env_log) self.existing_venvs[name] = venv return venv
def test_matchingdependencies_latest(self, newconfig, mocksession): config = newconfig([], """ [tox] distshare={toxworkdir}/distshare [testenv] deps={distshare}/xyz-* """) config.distshare.ensure("xyz-1.2.0.zip") xyz2 = config.distshare.ensure("xyz-1.2.1.zip") envconfig = config.envconfigs['python'] venv = VirtualEnv(envconfig, session=mocksession) cconfig = venv._getliveconfig() md5, path = cconfig.deps[0] assert path == xyz2 assert md5 == path.computehash()
def test_matchingdependencies_file(self, newconfig, mocksession): config = newconfig([], """ [tox] distshare={toxworkdir}/distshare [testenv] deps=abc {distshare}/xyz.zip """) xyz = config.distshare.join("xyz.zip") xyz.ensure() envconfig = config.envconfigs['python'] venv = VirtualEnv(envconfig, session=mocksession) cconfig = venv._getliveconfig() assert cconfig.matches(cconfig) xyz.write("hello") newconfig = venv._getliveconfig() assert not cconfig.matches(newconfig)
def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch): monkeypatch.delenv("PYTHONPATH", raising=False) pkg = tmpdir.ensure("package.tar.gz") monkeypatch.setenv("X123", "123") monkeypatch.setenv("YY", "456") config = newconfig( [], """ [testenv:python] commands=python -V passenv = x123 setenv = ENV_VAR = value PYTHONPATH = value """, ) mocksession._clearmocks() venv = VirtualEnv(config.envconfigs["python"], session=mocksession) mocksession.installpkg(venv, pkg) venv.test() pcalls = mocksession._pcalls assert len(pcalls) == 2 for x in pcalls: env = x.env assert env is not None assert "ENV_VAR" in env assert env["ENV_VAR"] == "value" assert env["VIRTUAL_ENV"] == str(venv.path) assert env["X123"] == "123" assert "PYTHONPATH" in env assert env["PYTHONPATH"] == "value" # all env variables are passed for installation assert pcalls[0].env["YY"] == "456" assert "YY" not in pcalls[1].env assert {"ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"}.issubset(pcalls[1].env) # setenv does not trigger PYTHONPATH warnings mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
def test_conda_create(newconfig, mocksession): config = newconfig( [], """ [testenv:py123] """, ) venv = VirtualEnv(config.envconfigs["py123"], session=mocksession) assert venv.path == config.envconfigs['py123'].envdir action = mocksession.newaction(venv, "getenv") tox_testenv_create(action=action, venv=venv) pcalls = mocksession._pcalls assert len(pcalls) == 1 assert 'conda' in pcalls[0].args[0] assert 'create' == pcalls[0].args[1] assert '--yes' == pcalls[0].args[2] assert '-p' == pcalls[0].args[3] assert venv.path == pcalls[0].args[4] assert pcalls[0].args[5].startswith('python=')
def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch): pkg = tmpdir.ensure("package.tar.gz") monkeypatch.setenv("X123", "123") monkeypatch.setenv("YY", "456") config = newconfig([], """ [testenv:python] commands=python -V passenv = x123 setenv = ENV_VAR = value PYTHONPATH = value """) mocksession._clearmocks() venv = VirtualEnv(config.envconfigs['python'], session=mocksession) mocksession.installpkg(venv, pkg) venv.test() l = mocksession._pcalls assert len(l) == 2 for x in l: env = x.env assert env is not None assert 'ENV_VAR' in env assert env['ENV_VAR'] == 'value' assert env['VIRTUAL_ENV'] == str(venv.path) assert env['X123'] == "123" assert 'PYTHONPATH' in env assert env['PYTHONPATH'] == 'value' # all env variables are passed for installation assert l[0].env["YY"] == "456" assert "YY" not in l[1].env assert set(["ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"])\ .issubset(l[1].env) # setenv does not trigger PYTHONPATH warnings mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
def test_getsupportedinterpreter(monkeypatch, newconfig, mocksession): config = newconfig([], """ [testenv:python] basepython=%s """ % sys.executable) venv = VirtualEnv(config.envconfigs['python'], session=mocksession) interp = venv.getsupportedinterpreter() # realpath needed for debian symlinks assert py.path.local(interp).realpath() == py.path.local(sys.executable).realpath() monkeypatch.setattr(sys, 'platform', "win32") monkeypatch.setattr(venv.envconfig, 'basepython', 'jython') pytest.raises(tox.exception.UnsupportedInterpreter, venv.getsupportedinterpreter) monkeypatch.undo() monkeypatch.setattr(venv.envconfig, "envname", "py1") monkeypatch.setattr(venv.envconfig, 'basepython', 'notexistingpython') pytest.raises(tox.exception.InterpreterNotFound, venv.getsupportedinterpreter) monkeypatch.undo() # check that we properly report when no version_info is present info = NoInterpreterInfo(name=venv.name) info.executable = "something" monkeypatch.setattr(config.interpreters, "get_info", lambda *args, **kw: info) pytest.raises(tox.exception.InvocationError, venv.getsupportedinterpreter)
def test_conda_env(tmpdir, newconfig, mocksession): """Test environment creation when conda_env given""" yml = tmpdir.join("conda-env.yml") yml.write(""" name: tox-conda channels: - conda-forge - nodefaults dependencies: - numpy - astropy - pip: - pytest """) config = newconfig( [], """ [testenv:py123] conda_env={} """.format(str(yml)), ) venv = VirtualEnv(config.envconfigs["py123"]) assert venv.path == config.envconfigs["py123"].envdir venv, action, pcalls = create_test_env(config, mocksession, "py123") assert venv.envconfig.conda_env with mocksession.newaction(venv.name, "getenv") as action: tox_testenv_create(action=action, venv=venv) pcalls = mocksession._pcalls assert len(pcalls) >= 1 call = pcalls[-1] cmd = call.args assert "conda" in os.path.split(cmd[0])[-1] assert cmd[1:4] == ["env", "create", "-p"] assert venv.path == call.args[4] assert call.args[5].startswith("--file") assert call.args[6].endswith("conda-env.yml")
def test_conda_create(newconfig, mocksession): config = newconfig( [], """ [testenv:py123] """, ) venv = VirtualEnv(config.envconfigs["py123"]) assert venv.path == config.envconfigs["py123"].envdir with mocksession.newaction(venv.name, "getenv") as action: tox_testenv_create(action=action, venv=venv) pcalls = mocksession._pcalls assert len(pcalls) >= 1 call = pcalls[-1] assert "conda" in call.args[0] assert "create" == call.args[1] assert "--yes" == call.args[2] assert "-p" == call.args[3] assert venv.path == call.args[4] assert call.args[5].startswith("python=")
def test_create(monkeypatch, mocksession, newconfig): config = newconfig([], """ [testenv:py123] """) envconfig = config.envconfigs['py123'] venv = VirtualEnv(envconfig, session=mocksession) assert venv.path == envconfig.envdir assert not venv.path.check() action = mocksession.newaction(venv, "getenv") tox_testenv_create(action=action, venv=venv) pcalls = mocksession._pcalls assert len(pcalls) >= 1 args = pcalls[0].args assert "virtualenv" == str(args[2]) if sys.platform != "win32": # realpath is needed for stuff like the debian symlinks assert py.path.local(sys.executable).realpath() == py.path.local(args[0]).realpath() # assert Envconfig.toxworkdir in args assert venv.getcommandpath("easy_install", cwd=py.path.local()) interp = venv._getliveconfig().python assert interp == venv.envconfig.python_info.executable assert venv.path_config.check(exists=False)
def test_env_variables_added_to_needs_reinstall(tmpdir, mocksession, newconfig, monkeypatch): tmpdir.ensure("setup.py") monkeypatch.setenv("TEMP_PASS_VAR", "123") monkeypatch.setenv("TEMP_NOPASS_VAR", "456") config = newconfig( [], """ [testenv:python] passenv = temp_pass_var setenv = CUSTOM_VAR = 789 """, ) venv = VirtualEnv(config.envconfigs["python"], session=mocksession) action = mocksession.newaction(venv, "hello") venv._needs_reinstall(tmpdir, action) pcalls = mocksession._pcalls assert len(pcalls) == 2 env = pcalls[0].env # should have access to setenv vars assert "CUSTOM_VAR" in env assert env["CUSTOM_VAR"] == "789" # should have access to passenv vars assert "TEMP_PASS_VAR" in env assert env["TEMP_PASS_VAR"] == "123" # should also have access to full invocation environment, # for backward compatibility, and to match behavior of venv.run_install_command() assert "TEMP_NOPASS_VAR" in env assert env["TEMP_NOPASS_VAR"] == "456"
def getenv(self, name): return VirtualEnv(self.config.envconfigs[name], session=self)