def test_registered_local_popt(testdir, source, ext_fake): # Without -p option behaves identical to importable, non-repo repo = new_repo(testdir) pyexe = get_pyexe("base") localfile = repo / "pytest_pdb_break.py" localfile.write_binary(ext_fake.join("pytest_pdb_break.py").read_binary()) rc, out, errs = run_with_input([pyexe, "-", "-p", "pytest_pdb_break"], source, repo) assert rc == 0 and out and not errs assert json.loads(out) == {"rootdir": repo, "registered": True} # Plugin shows up in help rc, out, errs = run_with_input( [pyexe, "-mpytest", "-p", "pytest_pdb_break", "--help"], "", repo, "pt_help") assert rc == 0 and out and not errs LineMatcher(out.splitlines()).fnmatch_lines("*--break=*") # Plugin not mentioned in in report but still runs rc, out, errs = run_with_input( [pyexe, "-mpytest", "-p", "pytest_pdb_break", "--break=99"], "", repo, "pt_opt") assert rc == 0 and out and not errs assert "plugins: pdb-break" not in out LineMatcher(out.splitlines()).fnmatch_lines("*fake-pdb-break*99*")
def test_local(self, m_ev, m_cc, m_be, *m_args): from helpers.ensure_venv import get_pyexe from helpers import common wd = make_workdir("GetPyExe.local") m_cc.side_effect = self.fake_check_call m_ev.return_value = wd m_be.return_value = dict(PATH="foo:bar") # result = get_pyexe("self") m_ev.assert_called_once_with() versioned = "python%d.%d" % sys.version_info[:2] self.assertEqual(result, wd / "self" / "bin" / versioned) from unittest.mock import call sysexe = ensure_venv.get_base_pyexe() pyexe = wd.joinpath("self", "bin", versioned) m_cc.assert_has_calls([ call([str(sysexe), "-mvenv", str(wd / "self")], env={"PATH": "foo:bar"}, stdout=common.SUBOUT, stderr=common.SUBERR), call([str(pyexe), "-mpip", "install", "<proj root>"], stdout=common.SUBOUT, stderr=common.SUBERR) ])
def test_pypi(self, m_cc, ensure_venvdir, get_base_env, is_pyenv_shim): from helpers.ensure_venv import get_pyexe from helpers import common wd = make_workdir("GetPyExe.pypi") m_cc.side_effect = self.fake_check_call ensure_venvdir.return_value = wd get_base_env.return_value = dict(PATH="foo:bar") is_pyenv_shim.return_value = False # result = get_pyexe("base") ensure_venvdir.assert_called_once_with() # 3.5 compat try: is_pyenv_shim.assert_called_once() # CI uses shims except AttributeError: pass versioned = "python%d.%d" % sys.version_info[:2] self.assertEqual(result, wd / "base" / "bin" / versioned) from unittest.mock import call sysexe = ensure_venv.get_base_pyexe() pyexe = wd.joinpath("base", "bin", versioned) m_cc.assert_has_calls([ call([str(sysexe), "-mvenv", str(wd / "base")], env={"PATH": "foo:bar"}, stdout=common.SUBOUT, stderr=common.SUBERR), call([str(pyexe), "-mpip", "install", "pytest"], stdout=common.SUBOUT, stderr=common.SUBERR) ])
def test_registered_not_found(testdir, source): # Not importable, requested repo = new_repo(testdir) pyexe = get_pyexe("base") rc, out, errs = run_with_input([pyexe, "-", "-p", "pytest_pdb_break"], source, repo) assert rc != 0 and not out and errs lines = LineMatcher(errs.splitlines()) lines.fnmatch_lines("Traceback*") lines.fnmatch_lines("ModuleNotFoundError*pytest_pdb_break*")
def test_unregistered(testdir, source, subdir, wantcd, ext_plugin): rootdir = testdir.tmpdir pyexe = get_pyexe("base") testdir.makeini("[pytest]\n") relfile = os.path.join(subdir, "test_file") subdir = subdir and rootdir.mkdir(subdir) or rootdir testdir.makepyfile( ** {relfile: """ def test_foo(): assert True """}) if wantcd: subdir.chdir() invocation_dir = type(rootdir)() testfile = subdir.join("test_file.py") # No node-id rc, out, errs = rv = run_with_input([pyexe], source, invocation_dir, "no_nodeid") assert rc == 0 and out and not errs json.loads(out) == {"rootdir": rootdir, "registered": False} # Real node-id assert rv == run_with_input([pyexe, "-", f"{testfile}::test_foo"], source, invocation_dir, "real_nodeid") # Fake node-id assert rv == run_with_input([pyexe, "-", "/tmp/fake__/fake.py::test_fake"], source, invocation_dir, "fake_nodeid") # Importable, not installed, not requested assert rv == run_with_input([pyexe], source, invocation_dir, prefix="nopip_nopopt", env_vars=dict(PYTHONPATH=ext_plugin)) # Explicit rootdir as option rc, out, errs = run_with_input([pyexe, "-", f"--rootdir={str(subdir)}"], source, invocation_dir, "rootdir_opt") assert rc == 0 and out and not errs assert json.loads(out) == {"rootdir": subdir, "registered": False} # Bad option rc, out, errs = run_with_input([pyexe, "-", "--fake"], source, invocation_dir, "bad_opt") assert rc != 0 and not out and errs with pytest.raises(json.JSONDecodeError): json.loads(out) lines = LineMatcher(errs.splitlines()) lines.fnmatch_lines("*unrecognized argument*")
def test_exists(self, m_ev, m_grp, m_sys): from helpers.ensure_venv import get_pyexe wd = make_workdir("GetPyExe.exists") # bare = make_dummy_venv_tree("bare", version=3.42) bexe = bare / "bin" / "python3.42" try: bexe = bexe.resolve(True) except TypeError: bexe = bexe.resolve() m_ev.return_value = wd result = get_pyexe("bare") m_ev.assert_called_once_with() m_grp.assert_not_called() self.assertEqual(result, bexe)
def test_registered_pip_nopopt(testdir, source, ext_egg): # External installation, not requested via -p option repo = new_repo(testdir) pyexe = get_pyexe("base") rc, out, errs = run_with_input([pyexe], source, repo, env_vars=dict(PYTHONPATH=ext_egg)) assert rc == 0 and out and not errs assert json.loads(out) == {"rootdir": repo, "registered": True} rc, out, errs = run_with_input([pyexe, "-mpytest", "--break=1"], "", repo, "pt", env_vars=dict(PYTHONPATH=ext_egg)) assert rc == 0 and out and not errs LineMatcher(out.splitlines()).fnmatch_lines("plugins: pdb-break*")
def test_baseline(testdir, request, ext_plugin): instr = dedent(""" import sys print(*sys.path, sep="\\n") """) pyexe = get_pyexe("bare") # As stdin path_check_dir = testdir.tmpdir.mkdir("path_check") path_check_dir.chdir() result = run_with_input([pyexe], instr, path_check_dir) # As exec string arg path_check_c_dir = testdir.tmpdir.mkdir("path_check_c") path_check_c_dir.chdir() assert result == run_with_input([pyexe, "-c", instr], "", path_check_c_dir) rc, out, errs = result assert rc == 0 and out and not errs outlines = out.splitlines() assert str(testdir.tmpdir) not in outlines assert str(request.config.rootdir) not in outlines assert str(request.config.invocation_dir) not in outlines # With modified path instr = dedent(""" import os import sys print("PYTHONPATH:", os.getenv("PYTHONPATH")) print(*sys.path, sep="\\n") """) libdir = ext_plugin env_dir = testdir.tmpdir.mkdir("env_dir") env_dir.chdir() rc, out, errs = run_with_input([pyexe], instr, env_dir, env_vars=dict(PYTHONPATH=libdir)) assert rc == 0 and out and not errs lines = LineMatcher(out.splitlines()) lines.fnmatch_lines(f"PYTHONPATH*{libdir}*") lines.fnmatch_lines(libdir.strpath)
def test_registered_nopip_popt(testdir, source, ext_plugin, ext_fake): # Importable, not installed, requested repo = new_repo(testdir) pyexe = get_pyexe("base") rc, out, errs = run_with_input([pyexe, "-", "-p", "pytest_pdb_break"], source, repo, env_vars=dict(PYTHONPATH=ext_plugin)) assert rc == 0 and out and not errs assert json.loads(out) == {"rootdir": repo, "registered": True} # Plugin shows up in help rc, out, errs = run_with_input( [pyexe, "-mpytest", "-p", "pytest_pdb_break", "--help"], "", repo, "pt_help", env_vars=dict(PYTHONPATH=ext_plugin)) assert rc == 0 and out and not errs LineMatcher(out.splitlines()).fnmatch_lines("*--break=*") # Plugin not mentioned in in report rc, out, errs = run_with_input( [pyexe, "-mpytest", "-p", "pytest_pdb_break", "--break=1"], "", repo, "pt_opt", env_vars=dict(PYTHONPATH=ext_plugin)) assert rc == 0 and out and not errs assert "plugins: pdb-break" not in out # Plugin runs rc, out, errs = run_with_input( [pyexe, "-mpytest", "-p", "pytest_pdb_break", "--break=99"], "", repo, "pt_fake", env_vars=dict(PYTHONPATH=ext_fake)) assert rc == 0 and out and not errs LineMatcher(out.splitlines()).fnmatch_lines("*fake-pdb-break*99*")
def ext_egg(module_tmpdir): """Like ext_plugin but with egg-/dist-info installed by 'bare' """ path = make_libpath(module_tmpdir / "with_egg") install_plugin(path, pyexe=get_pyexe("bare")) return path
def test_bad_name(self): from helpers.ensure_venv import get_pyexe with self.assertRaises(ValueError): get_pyexe("fake")