Skip to content

Commit

Permalink
tests: Accommodate tests run on Windows
Browse files Browse the repository at this point in the history
PR-21: #21
  • Loading branch information
blairconrad authored Oct 25, 2024
1 parent e53506b commit 807a234
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
6 changes: 4 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import sys

import pytest

from failprint import cli, debug
Expand All @@ -27,7 +29,7 @@ def test_show_help(capsys: pytest.CaptureFixture) -> None:

def test_run_command() -> None:
"""Run a simple command."""
assert cli.main(["echo", "hello"]) == 0
assert cli.main(["--", sys.executable, "-c", "print('hello')"]) == 0


def test_accept_custom_format(capsys: pytest.CaptureFixture) -> None:
Expand All @@ -36,7 +38,7 @@ def test_accept_custom_format(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
assert cli.main(["--no-progress", "-f", "custom={{output}}", "echo", "custom"]) == 0
assert cli.main(["--no-progress", "-f", "custom={{output}}", "--", sys.executable, "-c", "print('custom')"]) == 0
outerr = capsys.readouterr()
assert "custom" in outerr.out

Expand Down
7 changes: 4 additions & 3 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Callable

import pytest
Expand Down Expand Up @@ -104,10 +105,10 @@ def test_tap_format(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
run(["true"], fmt="tap")
run([sys.executable, "-c", "import sys; sys.exit(0)"], fmt="tap")
outerr = capsys.readouterr()
assert "ok" in outerr.out
run(["false"], fmt="tap")
run([sys.executable, "-c", "import sys; sys.exit(1)"], fmt="tap")
outerr = capsys.readouterr()
assert "not ok" in outerr.out

Expand Down Expand Up @@ -146,7 +147,7 @@ def test_escaping_and_unescaping_command_and_output(capsys: pytest.CaptureFixtur
Arguments:
capsys: Pytest fixture to capture output.
"""
run(["test", "-z", "<l num=0>hello</l>"], fmt="pretty")
run([sys.executable, "-c", "print('<l num=0>hello</l>')"], fmt="pretty")
outerr = capsys.readouterr()
assert "<l num=0>hello</l>" in outerr.out
assert LT not in outerr.out
Expand Down
5 changes: 4 additions & 1 deletion tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import sys

import pytest
from hypothesis import given, settings
from hypothesis.strategies import characters, text
Expand Down Expand Up @@ -46,13 +48,14 @@ def test_run_pty_subprocess_capture_none(capsys: pytest.CaptureFixture) -> None:


@given(text(alphabet=characters(blacklist_categories=["C"])))
@settings(deadline=None)
def test_pass_stdin_to_subprocess(stdin: str) -> None:
"""Pass input to a normal subprocess.
Arguments:
stdin: Text sample generated by Hypothesis.
"""
code, output = run_subprocess(["cat"], stdin=stdin)
code, output = run_subprocess([sys.executable, "-c", "import sys; print(sys.stdin.read(), end='')"], stdin=stdin)
assert code == 0
assert output == stdin

Expand Down
36 changes: 24 additions & 12 deletions tests/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_run_silent_command_silently(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
run(["true"], silent=True)
run([sys.executable, "-c", "import sys; sys.exit(0))"], silent=True)
outerr = capsys.readouterr()
assert not outerr.out
assert not outerr.err
Expand All @@ -33,7 +33,7 @@ def test_run_verbose_command_silently(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
run("echo VERBS", silent=True)
run([sys.executable, "-c", "print('VERBS')"], silent=True)
outerr = capsys.readouterr()
assert not outerr.out
assert not outerr.err
Expand All @@ -45,9 +45,9 @@ def test_run_silent_command_verbosely(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
run(["true"])
run([sys.executable, "-c", "import sys; sys.exit(0)"])
outerr = capsys.readouterr()
assert "true" in outerr.out
assert sys.executable in outerr.out
assert not outerr.err


Expand All @@ -57,9 +57,9 @@ def test_run_failing_silent_command_verbosely(capsys: pytest.CaptureFixture) ->
Arguments:
capsys: Pytest fixture to capture output.
"""
run(["false"])
run([sys.executable, "-c", "import sys; sys.exit(1)"])
outerr = capsys.readouterr()
assert "false" in outerr.out
assert sys.executable in outerr.out
assert not outerr.err


Expand All @@ -69,20 +69,20 @@ def test_run_verbose_command_verbosely(capsys: pytest.CaptureFixture) -> None:
Arguments:
capsys: Pytest fixture to capture output.
"""
assert run("echo VERBS").code == 0
assert run([sys.executable, "-c", "print('VERBS')"]).code == 0
outerr = capsys.readouterr()
assert "VERBS" in outerr.out
assert not outerr.err


def test_return_success_code() -> None:
"""Check the return code of a successful command."""
assert run(["true"]).code == 0
assert run([sys.executable, "-c", "import sys; sys.exit(0)"]).code == 0


def test_return_failure_code() -> None:
"""Check the return code of a failing command."""
assert run(["false"]).code == 1
assert run([sys.executable, "-c", "import sys; sys.exit(1)"]).code == 1


def test_return_shell_custom_code() -> None:
Expand Down Expand Up @@ -221,7 +221,11 @@ def test_process_capture_both() -> None:
msg_stdout = "out"
msg_stderr = "err"
result = run(
["bash", "-c", f"echo {msg_stdout}; echo {msg_stderr} >&2"],
[
sys.executable,
"-c",
f"import sys; print('{msg_stdout}', file=sys.stdout); print('{msg_stderr}', file=sys.stderr)",
],
capture=True,
fmt="custom={{output}}",
)
Expand All @@ -234,7 +238,11 @@ def test_process_capture_stdout() -> None:
msg_stdout = "out"
msg_stderr = "err"
result = run(
["bash", "-c", f"echo {msg_stdout}; echo {msg_stderr} >&2"],
[
sys.executable,
"-c",
f"import sys; print('{msg_stdout}', file=sys.stdout); print('{msg_stderr}', file=sys.stderr)",
],
capture="stdout",
fmt="custom={{output}}",
)
Expand All @@ -247,7 +255,11 @@ def test_process_capture_stderr() -> None:
msg_stdout = "out"
msg_stderr = "err"
result = run(
["bash", "-c", f"echo {msg_stdout}; echo {msg_stderr} >&2"],
[
sys.executable,
"-c",
f"import sys; print('{msg_stdout}', file=sys.stdout); print('{msg_stderr}', file=sys.stderr)",
],
capture="stderr",
fmt="custom={{output}}",
)
Expand Down

0 comments on commit 807a234

Please sign in to comment.