Exemplo n.º 1
0
def test_run_stdin(pytester: Pytester) -> None:
    with pytest.raises(pytester.TimeoutExpired):
        pytester.run(
            sys.executable,
            "-c",
            "import sys, time; time.sleep(1); print(sys.stdin.read())",
            stdin=subprocess.PIPE,
            timeout=0.1,
        )

    with pytest.raises(pytester.TimeoutExpired):
        result = pytester.run(
            sys.executable,
            "-c",
            "import sys, time; time.sleep(1); print(sys.stdin.read())",
            stdin=b"input\n2ndline",
            timeout=0.1,
        )

    result = pytester.run(
        sys.executable,
        "-c",
        "import sys; print(sys.stdin.read())",
        stdin=b"input\n2ndline",
    )
    assert result.stdout.lines == ["input", "2ndline"]
    assert result.stderr.str() == ""
    assert result.ret == 0
Exemplo n.º 2
0
def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
    try:
        bash_version = subprocess.run(
            ["bash", "--version"],
            stdout=subprocess.PIPE,
            stderr=subprocess.DEVNULL,
            check=True,
            text=True,
        ).stdout
    except (OSError, subprocess.CalledProcessError):
        pytest.skip("bash is not available")
    if "GNU bash" not in bash_version:
        # See #7518.
        pytest.skip("not a real bash")

    script = str(pytester.path.joinpath("test_argcomplete"))

    with open(str(script), "w") as fp:
        # redirect output from argcomplete to stdin and stderr is not trivial
        # http://stackoverflow.com/q/12589419/1307905
        # so we use bash
        fp.write(
            'COMP_WORDBREAKS="$COMP_WORDBREAKS" {} -m pytest 8>&1 9>&2'.format(
                shlex.quote(sys.executable)
            )
        )
    # alternative would be extended Pytester.{run(),_run(),popen()} to be able
    # to handle a keyword argument env that replaces os.environ in popen or
    # extends the copy, advantage: could not forget to restore
    monkeypatch.setenv("_ARGCOMPLETE", "1")
    monkeypatch.setenv("_ARGCOMPLETE_IFS", "\x0b")
    monkeypatch.setenv("COMP_WORDBREAKS", " \\t\\n\"\\'><=;|&(:")

    arg = "--fu"
    monkeypatch.setenv("COMP_LINE", "pytest " + arg)
    monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg)))
    result = pytester.run("bash", str(script), arg)
    if result.ret == 255:
        # argcomplete not found
        pytest.skip("argcomplete not available")
    elif not result.stdout.str():
        pytest.skip(
            "bash provided no output on stdout, argcomplete not available? (stderr={!r})".format(
                result.stderr.str()
            )
        )
    else:
        result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"])
    os.mkdir("test_argcomplete.d")
    arg = "test_argc"
    monkeypatch.setenv("COMP_LINE", "pytest " + arg)
    monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg)))
    result = pytester.run("bash", str(script), arg)
    result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"])
Exemplo n.º 3
0
def test_already_initialized(faulthandler_timeout: int,
                             pytester: Pytester) -> None:
    """Test for faulthandler being initialized earlier than pytest (#6575)."""
    pytester.makepyfile("""
        def test():
            import faulthandler
            assert faulthandler.is_enabled()
    """)
    result = pytester.run(
        sys.executable,
        "-X",
        "faulthandler",
        "-mpytest",
        pytester.path,
        "-o",
        f"faulthandler_timeout={faulthandler_timeout}",
    )
    # ensure warning is emitted if faulthandler_timeout is configured
    warning_line = "*faulthandler.py*faulthandler module enabled before*"
    if faulthandler_timeout > 0:
        result.stdout.fnmatch_lines(warning_line)
    else:
        result.stdout.no_fnmatch_line(warning_line)
    result.stdout.fnmatch_lines("*1 passed*")
    assert result.ret == 0
Exemplo n.º 4
0
def test_crash_during_shutdown_not_captured(pytester: Pytester) -> None:
    """
    Check that pytest leaves faulthandler disabled if it was not enabled during configure.
    This prevents us from seeing crashes during interpreter shutdown (see #8260).
    """
    setup_crashing_test(pytester)
    args = (sys.executable, "-mpytest")
    result = pytester.run(*args)
    result.stderr.no_fnmatch_line("*Fatal Python error*")
    assert result.ret != 0
Exemplo n.º 5
0
def test_crash_during_shutdown_captured(pytester: Pytester) -> None:
    """
    Re-enable faulthandler if pytest encountered it enabled during configure.
    We should be able to then see crashes during interpreter shutdown.
    """
    setup_crashing_test(pytester)
    args = (sys.executable, "-Xfaulthandler", "-mpytest")
    result = pytester.run(*args)
    result.stderr.fnmatch_lines(["*Fatal Python error*"])
    assert result.ret != 0
Exemplo n.º 6
0
def test_show_fixture_action_with_bytes(pytester: Pytester) -> None:
    # Issue 7126, BytesWarning when using --setup-show with bytes parameter
    test_file = pytester.makepyfile("""
        import pytest

        @pytest.mark.parametrize('data', [b'Hello World'])
        def test_data(data):
            pass
        """)
    result = pytester.run(sys.executable, "-bb", "-m", "pytest",
                          "--setup-show", str(test_file))
    assert result.ret == 0
Exemplo n.º 7
0
def test_already_initialized_crash(pytester: Pytester) -> None:
    """Even if faulthandler is already initialized, we still dump tracebacks on crashes (#8258)."""
    pytester.makepyfile("""
        def test():
            import faulthandler
            faulthandler._sigabrt()
    """)
    result = pytester.run(
        sys.executable,
        "-X",
        "faulthandler",
        "-mpytest",
        pytester.path,
    )
    result.stderr.fnmatch_lines(["*Fatal Python error*"])
    assert result.ret != 0
Exemplo n.º 8
0
 def test_python_pytest_package(self, pytester: Pytester) -> None:
     p1 = pytester.makepyfile("def test_pass(): pass")
     res = pytester.run(sys.executable, "-m", "pytest", str(p1))
     assert res.ret == 0
     res.stdout.fnmatch_lines(["*1 passed*"])
Exemplo n.º 9
0
 def test_python_minus_m_invocation_fail(self, pytester: Pytester) -> None:
     p1 = pytester.makepyfile("def test_fail(): 0/0")
     res = pytester.run(sys.executable, "-m", "pytest", str(p1))
     assert res.ret == 1