How to use getvenv method in tox

Best Python code snippet using tox_python

test_venv.py

Source:test_venv.py Github

copy

Full Screen

...27 sys.executable28 ),29 )30 mocksession.new_config(config)31 venv = mocksession.getvenv("python")32 interp = venv.getsupportedinterpreter()33 # realpath needed for debian symlinks34 assert py.path.local(interp).realpath() == py.path.local(sys.executable).realpath()35 monkeypatch.setattr(tox.INFO, "IS_WIN", True)36 monkeypatch.setattr(venv.envconfig, "basepython", "jython")37 with pytest.raises(tox.exception.UnsupportedInterpreter):38 venv.getsupportedinterpreter()39 monkeypatch.undo()40 monkeypatch.setattr(venv.envconfig, "envname", "py1")41 monkeypatch.setattr(venv.envconfig, "basepython", "notexisting")42 with pytest.raises(tox.exception.InterpreterNotFound):43 venv.getsupportedinterpreter()44 monkeypatch.undo()45 # check that we properly report when no version_info is present46 info = NoInterpreterInfo(name=venv.name)47 info.executable = "something"48 monkeypatch.setattr(config.interpreters, "get_info", lambda *args, **kw: info)49 with pytest.raises(tox.exception.InvocationError):50 venv.getsupportedinterpreter()51def test_create(mocksession, newconfig):52 config = newconfig(53 [],54 """\55 [testenv:py123]56 """,57 )58 envconfig = config.envconfigs["py123"]59 mocksession.new_config(config)60 venv = mocksession.getvenv("py123")61 assert venv.path == envconfig.envdir62 assert not venv.path.check()63 with mocksession.newaction(venv.name, "getenv") as action:64 tox_testenv_create(action=action, venv=venv)65 pcalls = mocksession._pcalls66 assert len(pcalls) >= 167 args = pcalls[0].args68 module = "venv" if use_builtin_venv(venv) else "virtualenv"69 assert module == str(args[2])70 if not tox.INFO.IS_WIN:71 executable = sys.executable72 if use_builtin_venv(venv) and hasattr(sys, "real_prefix"):73 # workaround virtualenv prefixing issue w/ venv on python374 executable = "python{}.{}".format(*sys.version_info)75 executable = os.path.join(sys.real_prefix, "bin", executable)76 # realpath is needed for stuff like the debian symlinks77 our_sys_path = py.path.local(executable).realpath()78 assert our_sys_path == py.path.local(args[0]).realpath()79 # assert Envconfig.toxworkdir in args80 assert venv.getcommandpath("easy_install", cwd=py.path.local())81 interp = venv._getliveconfig().base_resolved_python_path82 assert interp == venv.envconfig.python_info.executable83 assert venv.path_config.check(exists=False)84def test_create_KeyboardInterrupt(mocksession, newconfig, mocker):85 config = newconfig(86 [],87 """\88 [testenv:py123]89 """,90 )91 mocksession.new_config(config)92 venv = mocksession.getvenv("py123")93 mocker.patch.object(venv, "_pcall", side_effect=KeyboardInterrupt)94 with pytest.raises(KeyboardInterrupt):95 venv.setupenv()96 assert venv.status == "keyboardinterrupt"97def test_commandpath_venv_precedence(tmpdir, monkeypatch, mocksession, newconfig):98 config = newconfig(99 [],100 """\101 [testenv:py123]102 """,103 )104 mocksession.new_config(config)105 venv = mocksession.getvenv("py123")106 envconfig = venv.envconfig107 tmpdir.ensure("easy_install")108 monkeypatch.setenv("PATH", str(tmpdir), prepend=os.pathsep)109 envconfig.envbindir.ensure("easy_install")110 p = venv.getcommandpath("easy_install")111 assert py.path.local(p).relto(envconfig.envbindir), p112def test_create_sitepackages(mocksession, newconfig):113 config = newconfig(114 [],115 """\116 [testenv:site]117 sitepackages=True118 [testenv:nosite]119 sitepackages=False120 """,121 )122 mocksession.new_config(config)123 venv = mocksession.getvenv("site")124 with mocksession.newaction(venv.name, "getenv") as action:125 tox_testenv_create(action=action, venv=venv)126 pcalls = mocksession._pcalls127 assert len(pcalls) >= 1128 args = pcalls[0].args129 assert "--system-site-packages" in map(str, args)130 mocksession._clearmocks()131 venv = mocksession.getvenv("nosite")132 with mocksession.newaction(venv.name, "getenv") as action:133 tox_testenv_create(action=action, venv=venv)134 pcalls = mocksession._pcalls135 assert len(pcalls) >= 1136 args = pcalls[0].args137 assert "--system-site-packages" not in map(str, args)138 assert "--no-site-packages" not in map(str, args)139def test_install_deps_wildcard(newmocksession):140 mocksession = newmocksession(141 [],142 """\143 [tox]144 distshare = {toxworkdir}/distshare145 [testenv:py123]146 deps=147 {distshare}/dep1-*148 """,149 )150 venv = mocksession.getvenv("py123")151 with mocksession.newaction(venv.name, "getenv") as action:152 tox_testenv_create(action=action, venv=venv)153 pcalls = mocksession._pcalls154 assert len(pcalls) == 1155 distshare = venv.envconfig.config.distshare156 distshare.ensure("dep1-1.0.zip")157 distshare.ensure("dep1-1.1.zip")158 tox_testenv_install_deps(action=action, venv=venv)159 assert len(pcalls) == 2160 args = pcalls[-1].args161 assert pcalls[-1].cwd == venv.envconfig.config.toxinidir162 assert py.path.local.sysfind("python") == args[0]163 assert ["-m", "pip"] == args[1:3]164 assert args[3] == "install"165 args = [arg for arg in args if str(arg).endswith("dep1-1.1.zip")]166 assert len(args) == 1167def test_install_deps_indexserver(newmocksession):168 mocksession = newmocksession(169 [],170 """\171 [tox]172 indexserver =173 abc = ABC174 abc2 = ABC175 [testenv:py123]176 deps=177 dep1178 :abc:dep2179 :abc2:dep3180 """,181 )182 venv = mocksession.getvenv("py123")183 with mocksession.newaction(venv.name, "getenv") as action:184 tox_testenv_create(action=action, venv=venv)185 pcalls = mocksession._pcalls186 assert len(pcalls) == 1187 pcalls[:] = []188 tox_testenv_install_deps(action=action, venv=venv)189 # two different index servers, two calls190 assert len(pcalls) == 3191 args = " ".join(pcalls[0].args)192 assert "-i " not in args193 assert "dep1" in args194 args = " ".join(pcalls[1].args)195 assert "-i ABC" in args196 assert "dep2" in args197 args = " ".join(pcalls[2].args)198 assert "-i ABC" in args199 assert "dep3" in args200def test_install_deps_pre(newmocksession):201 mocksession = newmocksession(202 [],203 """\204 [testenv]205 pip_pre=true206 deps=207 dep1208 """,209 )210 venv = mocksession.getvenv("python")211 with mocksession.newaction(venv.name, "getenv") as action:212 tox_testenv_create(action=action, venv=venv)213 pcalls = mocksession._pcalls214 assert len(pcalls) == 1215 pcalls[:] = []216 tox_testenv_install_deps(action=action, venv=venv)217 assert len(pcalls) == 1218 args = " ".join(pcalls[0].args)219 assert "--pre " in args220 assert "dep1" in args221def test_installpkg_indexserver(newmocksession, tmpdir):222 mocksession = newmocksession(223 [],224 """\225 [tox]226 indexserver =227 default = ABC228 """,229 )230 venv = mocksession.getvenv("python")231 pcalls = mocksession._pcalls232 p = tmpdir.ensure("distfile.tar.gz")233 installpkg(venv, p)234 # two different index servers, two calls235 assert len(pcalls) == 1236 args = " ".join(pcalls[0].args)237 assert "-i ABC" in args238def test_install_recreate(newmocksession, tmpdir):239 pkg = tmpdir.ensure("package.tar.gz")240 mocksession = newmocksession(241 ["--recreate"],242 """\243 [testenv]244 deps=xyz245 """,246 )247 venv = mocksession.getvenv("python")248 with mocksession.newaction(venv.name, "update") as action:249 venv.update(action)250 installpkg(venv, pkg)251 mocksession.report.expect("verbosity0", "*create*")252 venv.update(action)253 mocksession.report.expect("verbosity0", "*recreate*")254def test_install_sdist_extras(newmocksession):255 mocksession = newmocksession(256 [],257 """\258 [testenv]259 extras = testing260 development261 """,262 )263 venv = mocksession.getvenv("python")264 with mocksession.newaction(venv.name, "getenv") as action:265 tox_testenv_create(action=action, venv=venv)266 pcalls = mocksession._pcalls267 assert len(pcalls) == 1268 pcalls[:] = []269 venv.installpkg("distfile.tar.gz", action=action)270 assert "distfile.tar.gz[testing,development]" in pcalls[-1].args271def test_develop_extras(newmocksession, tmpdir):272 mocksession = newmocksession(273 [],274 """\275 [testenv]276 extras = testing277 development278 """,279 )280 venv = mocksession.getvenv("python")281 with mocksession.newaction(venv.name, "getenv") as action:282 tox_testenv_create(action=action, venv=venv)283 pcalls = mocksession._pcalls284 assert len(pcalls) == 1285 pcalls[:] = []286 venv.developpkg(tmpdir, action=action)287 expected = "{}[testing,development]".format(tmpdir.strpath)288 assert expected in pcalls[-1].args289def test_env_variables_added_to_needs_reinstall(tmpdir, mocksession, newconfig, monkeypatch):290 tmpdir.ensure("setup.py")291 monkeypatch.setenv("TEMP_PASS_VAR", "123")292 monkeypatch.setenv("TEMP_NOPASS_VAR", "456")293 config = newconfig(294 [],295 """\296 [testenv:python]297 passenv = temp_pass_var298 setenv =299 CUSTOM_VAR = 789300 """,301 )302 mocksession.new_config(config)303 venv = mocksession.getvenv("python")304 with mocksession.newaction(venv.name, "hello") as action:305 venv._needs_reinstall(tmpdir, action)306 pcalls = mocksession._pcalls307 assert len(pcalls) == 2308 env = pcalls[0].env309 # should have access to setenv vars310 assert "CUSTOM_VAR" in env311 assert env["CUSTOM_VAR"] == "789"312 # should have access to passenv vars313 assert "TEMP_PASS_VAR" in env314 assert env["TEMP_PASS_VAR"] == "123"315 # should also have access to full invocation environment,316 # for backward compatibility, and to match behavior of venv.run_install_command()317 assert "TEMP_NOPASS_VAR" in env318 assert env["TEMP_NOPASS_VAR"] == "456"319def test_test_hashseed_is_in_output(newmocksession, monkeypatch):320 seed = "123456789"321 monkeypatch.setattr("tox.config.make_hashseed", lambda: seed)322 mocksession = newmocksession([], "")323 venv = mocksession.getvenv("python")324 with mocksession.newaction(venv.name, "update") as action:325 venv.update(action)326 tox.venv.tox_runtest_pre(venv)327 mocksession.report.expect("verbosity0", "run-test-pre: PYTHONHASHSEED='{}'".format(seed))328def test_test_runtests_action_command_is_in_output(newmocksession):329 mocksession = newmocksession(330 [],331 """\332 [testenv]333 commands = echo foo bar334 """,335 )336 venv = mocksession.getvenv("python")337 with mocksession.newaction(venv.name, "update") as action:338 venv.update(action)339 venv.test()340 mocksession.report.expect("verbosity0", "*run-test:*commands?0? | echo foo bar")341def test_install_error(newmocksession):342 mocksession = newmocksession(343 ["--recreate"],344 """\345 [testenv]346 deps=xyz347 commands=348 qwelkqw349 """,350 )351 venv = mocksession.getvenv("python")352 venv.test()353 mocksession.report.expect("error", "*not find*qwelkqw*")354 assert venv.status == "commands failed"355def test_install_command_not_installed(newmocksession):356 mocksession = newmocksession(357 ["--recreate"],358 """\359 [testenv]360 commands=361 pytest362 """,363 )364 venv = mocksession.getvenv("python")365 venv.status = 0366 venv.test()367 mocksession.report.expect("warning", "*test command found but not*")368 assert venv.status == 0369def test_install_command_whitelisted(newmocksession):370 mocksession = newmocksession(371 ["--recreate"],372 """\373 [testenv]374 whitelist_externals = pytest375 xy*376 commands=377 pytest378 xyz379 """,380 )381 venv = mocksession.getvenv("python")382 venv.test()383 mocksession.report.expect("warning", "*test command found but not*", invert=True)384 assert venv.status == "commands failed"385def test_install_command_not_installed_bash(newmocksession):386 mocksession = newmocksession(387 ["--recreate"],388 """\389 [testenv]390 commands=391 bash392 """,393 )394 venv = mocksession.getvenv("python")395 venv.test()396 mocksession.report.expect("warning", "*test command found but not*")397def test_install_python3(newmocksession):398 if not py.path.local.sysfind("python3") or tox.INFO.IS_PYPY:399 pytest.skip("needs cpython3")400 mocksession = newmocksession(401 [],402 """\403 [testenv:py123]404 basepython=python3405 deps=406 dep1407 dep2408 """,409 )410 venv = mocksession.getvenv("py123")411 with mocksession.newaction(venv.name, "getenv") as action:412 tox_testenv_create(action=action, venv=venv)413 pcalls = mocksession._pcalls414 assert len(pcalls) == 1415 args = pcalls[0].args416 assert str(args[2]) == "venv"417 pcalls[:] = []418 with mocksession.newaction(venv.name, "hello") as action:419 venv._install(["hello"], action=action)420 assert len(pcalls) == 1421 args = pcalls[0].args422 assert py.path.local.sysfind("python") == args[0]423 assert ["-m", "pip"] == args[1:3]424 for _ in args:425 assert "--download-cache" not in args, args426class TestCreationConfig:427 def test_basic(self, newconfig, mocksession, tmpdir):428 config = newconfig([], "")429 mocksession.new_config(config)430 venv = mocksession.getvenv("python")431 cconfig = venv._getliveconfig()432 assert cconfig.matches(cconfig)433 path = tmpdir.join("configdump")434 cconfig.writeconfig(path)435 newconfig = CreationConfig.readconfig(path)436 assert newconfig.matches(cconfig)437 assert cconfig.matches(newconfig)438 def test_matchingdependencies(self, newconfig, mocksession):439 config = newconfig(440 [],441 """\442 [testenv]443 deps=abc444 """,445 )446 mocksession.new_config(config)447 venv = mocksession.getvenv("python")448 cconfig = venv._getliveconfig()449 config = newconfig(450 [],451 """\452 [testenv]453 deps=xyz454 """,455 )456 mocksession.new_config(config)457 venv = mocksession.getvenv("python")458 otherconfig = venv._getliveconfig()459 assert not cconfig.matches(otherconfig)460 def test_matchingdependencies_file(self, newconfig, mocksession):461 config = newconfig(462 [],463 """\464 [tox]465 distshare={toxworkdir}/distshare466 [testenv]467 deps=abc468 {distshare}/xyz.zip469 """,470 )471 xyz = config.distshare.join("xyz.zip")472 xyz.ensure()473 mocksession.new_config(config)474 venv = mocksession.getvenv("python")475 cconfig = venv._getliveconfig()476 assert cconfig.matches(cconfig)477 xyz.write("hello")478 newconfig = venv._getliveconfig()479 assert not cconfig.matches(newconfig)480 def test_matchingdependencies_latest(self, newconfig, mocksession):481 config = newconfig(482 [],483 """\484 [tox]485 distshare={toxworkdir}/distshare486 [testenv]487 deps={distshare}/xyz-*488 """,489 )490 config.distshare.ensure("xyz-1.2.0.zip")491 xyz2 = config.distshare.ensure("xyz-1.2.1.zip")492 mocksession.new_config(config)493 venv = mocksession.getvenv("python")494 cconfig = venv._getliveconfig()495 sha256, path = cconfig.deps[0]496 assert path == xyz2497 assert sha256 == path.computehash("sha256")498 def test_python_recreation(self, tmpdir, newconfig, mocksession):499 pkg = tmpdir.ensure("package.tar.gz")500 config = newconfig(["-v"], "")501 mocksession.new_config(config)502 venv = mocksession.getvenv("python")503 create_config = venv._getliveconfig()504 with mocksession.newaction(venv.name, "update") as action:505 venv.update(action)506 assert not venv.path_config.check()507 installpkg(venv, pkg)508 assert venv.path_config.check()509 assert mocksession._pcalls510 args1 = map(str, mocksession._pcalls[0].args)511 module = "venv" if use_builtin_venv(venv) else "virtualenv"512 assert module in " ".join(args1)513 mocksession.report.expect("*", "*create*")514 # modify config and check that recreation happens515 mocksession._clearmocks()516 with mocksession.newaction(venv.name, "update") as action:517 venv.update(action)518 mocksession.report.expect("*", "*reusing*")519 mocksession._clearmocks()520 with mocksession.newaction(venv.name, "update") as action:521 create_config.base_resolved_python_path = py.path.local("balla")522 create_config.writeconfig(venv.path_config)523 venv.update(action)524 mocksession.report.expect("verbosity0", "*recreate*")525 def test_dep_recreation(self, newconfig, mocksession):526 config = newconfig([], "")527 mocksession.new_config(config)528 venv = mocksession.getvenv("python")529 with mocksession.newaction(venv.name, "update") as action:530 venv.update(action)531 cconfig = venv._getliveconfig()532 cconfig.deps[:] = [("1" * 32, "xyz.zip")]533 cconfig.writeconfig(venv.path_config)534 mocksession._clearmocks()535 with mocksession.newaction(venv.name, "update") as action:536 venv.update(action)537 mocksession.report.expect("*", "*recreate*")538 def test_develop_recreation(self, newconfig, mocksession):539 config = newconfig([], "")540 mocksession.new_config(config)541 venv = mocksession.getvenv("python")542 with mocksession.newaction(venv.name, "update") as action:543 venv.update(action)544 cconfig = venv._getliveconfig()545 cconfig.usedevelop = True546 cconfig.writeconfig(venv.path_config)547 mocksession._clearmocks()548 with mocksession.newaction(venv.name, "update") as action:549 venv.update(action)550 mocksession.report.expect("verbosity0", "*recreate*")551class TestVenvTest:552 def test_envbindir_path(self, newmocksession, monkeypatch):553 monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")554 mocksession = newmocksession(555 [],556 """\557 [testenv:python]558 commands=abc559 """,560 )561 venv = mocksession.getvenv("python")562 with mocksession.newaction(venv.name, "getenv") as action:563 monkeypatch.setenv("PATH", "xyz")564 sysfind_calls = []565 monkeypatch.setattr(566 "py.path.local.sysfind",567 classmethod(lambda *args, **kwargs: sysfind_calls.append(kwargs) or 0 / 0),568 )569 with pytest.raises(ZeroDivisionError):570 venv._install(list("123"), action=action)571 assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]572 with pytest.raises(ZeroDivisionError):573 venv.test(action)574 assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]575 with pytest.raises(ZeroDivisionError):576 venv.run_install_command(["qwe"], action=action)577 assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]578 monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")579 monkeypatch.setenv("PIP_REQUIRE_VIRTUALENV", "1")580 monkeypatch.setenv("__PYVENV_LAUNCHER__", "1")581 prev_pcall = venv._pcall582 def collect(*args, **kwargs):583 env = kwargs["env"]584 assert "PIP_RESPECT_VIRTUALENV" not in env585 assert "PIP_REQUIRE_VIRTUALENV" not in env586 assert "__PYVENV_LAUNCHER__" not in env587 assert env["PIP_USER"] == "0"588 assert env["PIP_NO_DEPS"] == "0"589 return prev_pcall(*args, **kwargs)590 monkeypatch.setattr(venv, "_pcall", collect)591 with pytest.raises(ZeroDivisionError):592 venv.run_install_command(["qwe"], action=action)593 def test_pythonpath_remove(self, newmocksession, monkeypatch, caplog):594 monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")595 mocksession = newmocksession(596 [],597 """\598 [testenv:python]599 commands=abc600 """,601 )602 venv = mocksession.getvenv("python")603 with mocksession.newaction(venv.name, "getenv") as action:604 venv.run_install_command(["qwe"], action=action)605 mocksession.report.expect("warning", "*Discarding $PYTHONPATH from environment*")606 pcalls = mocksession._pcalls607 assert len(pcalls) == 1608 assert "PYTHONPATH" not in pcalls[0].env609 def test_pythonpath_keep(self, newmocksession, monkeypatch, caplog):610 # passenv = PYTHONPATH allows PYTHONPATH to stay in environment611 monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")612 mocksession = newmocksession(613 [],614 """\615 [testenv:python]616 commands=abc617 passenv = PYTHONPATH618 """,619 )620 venv = mocksession.getvenv("python")621 with mocksession.newaction(venv.name, "getenv") as action:622 venv.run_install_command(["qwe"], action=action)623 mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")624 assert "PYTHONPATH" in os.environ625 pcalls = mocksession._pcalls626 assert len(pcalls) == 1627 assert pcalls[0].env["PYTHONPATH"] == "/my/awesome/library"628 def test_pythonpath_empty(self, newmocksession, monkeypatch, caplog):629 monkeypatch.setenv("PYTHONPATH", "")630 mocksession = newmocksession(631 [],632 """\633 [testenv:python]634 commands=abc635 """,636 )637 venv = mocksession.getvenv("python")638 with mocksession.newaction(venv.name, "getenv") as action:639 venv.run_install_command(["qwe"], action=action)640 if sys.version_info < (3, 4):641 mocksession.report.expect("warning", "*Discarding $PYTHONPATH from environment*")642 else:643 with pytest.raises(AssertionError):644 mocksession.report.expect("warning", "*Discarding $PYTHONPATH from environment*")645 pcalls = mocksession._pcalls646 assert len(pcalls) == 1647 assert "PYTHONPATH" not in pcalls[0].env648def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch):649 monkeypatch.delenv("PYTHONPATH", raising=False)650 pkg = tmpdir.ensure("package.tar.gz")651 monkeypatch.setenv("X123", "123")652 monkeypatch.setenv("YY", "456")653 config = newconfig(654 [],655 """\656 [testenv:python]657 commands=python -V658 passenv = x123659 setenv =660 ENV_VAR = value661 PYTHONPATH = value662 """,663 )664 mocksession._clearmocks()665 mocksession.new_config(config)666 venv = mocksession.getvenv("python")667 installpkg(venv, pkg)668 venv.test()669 pcalls = mocksession._pcalls670 assert len(pcalls) == 2671 for x in pcalls:672 env = x.env673 assert env is not None674 assert "ENV_VAR" in env675 assert env["ENV_VAR"] == "value"676 assert env["VIRTUAL_ENV"] == str(venv.path)677 assert env["X123"] == "123"678 assert "PYTHONPATH" in env679 assert env["PYTHONPATH"] == "value"680 # all env variables are passed for installation681 assert pcalls[0].env["YY"] == "456"682 assert "YY" not in pcalls[1].env683 assert {"ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"}.issubset(pcalls[1].env)684 # setenv does not trigger PYTHONPATH warnings685 mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")686 # for e in os.environ:687 # assert e in env688def test_installpkg_no_upgrade(tmpdir, newmocksession):689 pkg = tmpdir.ensure("package.tar.gz")690 mocksession = newmocksession([], "")691 venv = mocksession.getvenv("python")692 venv.just_created = True693 venv.envconfig.envdir.ensure(dir=1)694 installpkg(venv, pkg)695 pcalls = mocksession._pcalls696 assert len(pcalls) == 1697 assert pcalls[0].args[1:-1] == ["-m", "pip", "install", "--exists-action", "w"]698@pytest.mark.parametrize("count, level", [(0, 0), (1, 0), (2, 0), (3, 1), (4, 2), (5, 3), (6, 3)])699def test_install_command_verbosity(tmpdir, newmocksession, count, level):700 pkg = tmpdir.ensure("package.tar.gz")701 mock_session = newmocksession(["-{}".format("v" * count)], "")702 env = mock_session.getvenv("python")703 env.just_created = True704 env.envconfig.envdir.ensure(dir=1)705 installpkg(env, pkg)706 pcalls = mock_session._pcalls707 assert len(pcalls) == 1708 expected = ["-m", "pip", "install", "--exists-action", "w"] + (["-v"] * level)709 assert pcalls[0].args[1:-1] == expected710def test_installpkg_upgrade(newmocksession, tmpdir):711 pkg = tmpdir.ensure("package.tar.gz")712 mocksession = newmocksession([], "")713 venv = mocksession.getvenv("python")714 assert not hasattr(venv, "just_created")715 installpkg(venv, pkg)716 pcalls = mocksession._pcalls717 assert len(pcalls) == 1718 index = pcalls[0].args.index(pkg.basename)719 assert index >= 0720 assert "-U" in pcalls[0].args[:index]721 assert "--no-deps" in pcalls[0].args[:index]722def test_run_install_command(newmocksession):723 mocksession = newmocksession([], "")724 venv = mocksession.getvenv("python")725 venv.just_created = True726 venv.envconfig.envdir.ensure(dir=1)727 with mocksession.newaction(venv.name, "hello") as action:728 venv.run_install_command(packages=["whatever"], action=action)729 pcalls = mocksession._pcalls730 assert len(pcalls) == 1731 args = pcalls[0].args732 assert py.path.local.sysfind("python") == args[0]733 assert ["-m", "pip"] == args[1:3]734 assert "install" in args735 env = pcalls[0].env736 assert env is not None737def test_run_custom_install_command(newmocksession):738 mocksession = newmocksession(739 [],740 """\741 [testenv]742 install_command=easy_install {opts} {packages}743 """,744 )745 venv = mocksession.getvenv("python")746 venv.just_created = True747 venv.envconfig.envdir.ensure(dir=1)748 with mocksession.newaction(venv.name, "hello") as action:749 venv.run_install_command(packages=["whatever"], action=action)750 pcalls = mocksession._pcalls751 assert len(pcalls) == 1752 assert "easy_install" in pcalls[0].args[0]753 assert pcalls[0].args[1:] == ["whatever"]754def test_command_relative_issue36(newmocksession, tmpdir, monkeypatch):755 mocksession = newmocksession(756 [],757 """\758 [testenv]759 """,760 )761 x = tmpdir.ensure("x")762 venv = mocksession.getvenv("python")763 x2 = venv.getcommandpath("./x", cwd=tmpdir)764 assert x == x2765 mocksession.report.not_expect("warning", "*test command found but not*")766 x3 = venv.getcommandpath("/bin/bash", cwd=tmpdir)767 assert x3 == "/bin/bash"768 mocksession.report.not_expect("warning", "*test command found but not*")769 monkeypatch.setenv("PATH", str(tmpdir))770 x4 = venv.getcommandpath("x", cwd=tmpdir)771 assert x4.endswith(os.sep + "x")772 mocksession.report.expect("warning", "*test command found but not*")773def test_ignore_outcome_failing_cmd(newmocksession):774 mocksession = newmocksession(775 [],776 """\777 [testenv]778 commands=testenv_fail779 ignore_outcome=True780 """,781 )782 venv = mocksession.getvenv("python")783 venv.test()784 assert venv.status == "ignored failed command"785 mocksession.report.expect("warning", "*command failed but result from testenv is ignored*")786def test_tox_testenv_create(newmocksession):787 log = []788 class Plugin:789 @tox.hookimpl790 def tox_testenv_create(self, action, venv):791 assert isinstance(action, tox.session.Action)792 assert isinstance(venv, VirtualEnv)793 log.append(1)794 @tox.hookimpl795 def tox_testenv_install_deps(self, action, venv):796 assert isinstance(action, tox.session.Action)797 assert isinstance(venv, VirtualEnv)798 log.append(2)799 mocksession = newmocksession(800 [],801 """\802 [testenv]803 commands=testenv_fail804 ignore_outcome=True805 """,806 plugins=[Plugin()],807 )808 venv = mocksession.getvenv("python")809 with mocksession.newaction(venv.name, "getenv") as action:810 venv.update(action=action)811 assert log == [1, 2]812def test_tox_testenv_pre_post(newmocksession):813 log = []814 class Plugin:815 @tox.hookimpl816 def tox_runtest_pre(self):817 log.append("started")818 @tox.hookimpl819 def tox_runtest_post(self):820 log.append("finished")821 mocksession = newmocksession(822 [],823 """\824 [testenv]825 commands=testenv_fail826 """,827 plugins=[Plugin()],828 )829 venv = mocksession.getvenv("python")830 venv.status = None831 assert log == []832 runtestenv(venv, venv.envconfig.config)833 assert log == ["started", "finished"]834@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")835def test_tox_testenv_interpret_shebang_empty_instance(tmpdir):836 testfile = tmpdir.join("check_shebang_empty_instance.py")837 base_args = [str(testfile), "arg1", "arg2", "arg3"]838 # empty instance839 testfile.write("")840 args = prepend_shebang_interpreter(base_args)841 assert args == base_args842@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")843def test_tox_testenv_interpret_shebang_empty_interpreter(tmpdir):844 testfile = tmpdir.join("check_shebang_empty_interpreter.py")845 base_args = [str(testfile), "arg1", "arg2", "arg3"]846 # empty interpreter847 testfile.write("#!")848 args = prepend_shebang_interpreter(base_args)849 assert args == base_args850@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")851def test_tox_testenv_interpret_shebang_empty_interpreter_ws(tmpdir):852 testfile = tmpdir.join("check_shebang_empty_interpreter_ws.py")853 base_args = [str(testfile), "arg1", "arg2", "arg3"]854 # empty interpreter (whitespaces)855 testfile.write("#! \n")856 args = prepend_shebang_interpreter(base_args)857 assert args == base_args858@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")859def test_tox_testenv_interpret_shebang_non_utf8(tmpdir):860 testfile = tmpdir.join("check_non_utf8.py")861 base_args = [str(testfile), "arg1", "arg2", "arg3"]862 testfile.write_binary(b"#!\x9a\xef\x12\xaf\n")863 args = prepend_shebang_interpreter(base_args)864 assert args == base_args865@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")866def test_tox_testenv_interpret_shebang_interpreter_simple(tmpdir):867 testfile = tmpdir.join("check_shebang_interpreter_simple.py")868 base_args = [str(testfile), "arg1", "arg2", "arg3"]869 # interpreter (simple)870 testfile.write("#!interpreter")871 args = prepend_shebang_interpreter(base_args)872 assert args == ["interpreter"] + base_args873@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")874def test_tox_testenv_interpret_shebang_interpreter_ws(tmpdir):875 testfile = tmpdir.join("check_shebang_interpreter_ws.py")876 base_args = [str(testfile), "arg1", "arg2", "arg3"]877 # interpreter (whitespaces)878 testfile.write("#! interpreter \n\n")879 args = prepend_shebang_interpreter(base_args)880 assert args == ["interpreter"] + base_args881@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")882def test_tox_testenv_interpret_shebang_interpreter_arg(tmpdir):883 testfile = tmpdir.join("check_shebang_interpreter_arg.py")884 base_args = [str(testfile), "arg1", "arg2", "arg3"]885 # interpreter with argument886 testfile.write("#!interpreter argx\n")887 args = prepend_shebang_interpreter(base_args)888 assert args == ["interpreter", "argx"] + base_args889@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")890def test_tox_testenv_interpret_shebang_interpreter_args(tmpdir):891 testfile = tmpdir.join("check_shebang_interpreter_args.py")892 base_args = [str(testfile), "arg1", "arg2", "arg3"]893 # interpreter with argument (ensure single argument)894 testfile.write("#!interpreter argx argx-part2\n")895 args = prepend_shebang_interpreter(base_args)896 assert args == ["interpreter", "argx argx-part2"] + base_args897@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")898def test_tox_testenv_interpret_shebang_real(tmpdir):899 testfile = tmpdir.join("check_shebang_real.py")900 base_args = [str(testfile), "arg1", "arg2", "arg3"]901 # interpreter (real example)902 testfile.write("#!/usr/bin/env python\n")903 args = prepend_shebang_interpreter(base_args)904 assert args == ["/usr/bin/env", "python"] + base_args905@pytest.mark.skipif("sys.platform == 'win32'", reason="no shebang on Windows")906def test_tox_testenv_interpret_shebang_long_example(tmpdir):907 testfile = tmpdir.join("check_shebang_long_example.py")908 base_args = [str(testfile), "arg1", "arg2", "arg3"]909 # interpreter (long example)910 testfile.write(911 "#!this-is-an-example-of-a-very-long-interpret-directive-what-should-"912 "be-directly-invoked-when-tox-needs-to-invoked-the-provided-script-"913 "name-in-the-argument-list"914 )915 args = prepend_shebang_interpreter(base_args)916 expected = [917 "this-is-an-example-of-a-very-long-interpret-directive-what-should-be-"918 "directly-invoked-when-tox-needs-to-invoked-the-provided-script-name-"919 "in-the-argument-list"920 ]921 assert args == expected + base_args922@pytest.mark.parametrize("download", [True, False, None])923def test_create_download(mocksession, newconfig, download):924 config = newconfig(925 [],926 """\927 [testenv:env]928 {}929 """.format(930 "download={}".format(download) if download else ""931 ),932 )933 mocksession.new_config(config)934 venv = mocksession.getvenv("env")935 with mocksession.newaction(venv.name, "getenv") as action:936 tox_testenv_create(action=action, venv=venv)937 pcalls = mocksession._pcalls938 assert len(pcalls) >= 1939 args = pcalls[0].args940 # builtin venv does not support --no-download941 if download or use_builtin_venv(venv):942 assert "--no-download" not in map(str, args)943 else:944 assert "--no-download" in map(str, args)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful