Skip to content

Commit

Permalink
Merge branch 'mr/pmderodat/shell-stdin' into 'master'
Browse files Browse the repository at this point in the history
ClassicTestDriver.shell: bind e3.os.process.Run's input argument

See merge request it/e3-testsuite!41
  • Loading branch information
pmderodat committed Nov 19, 2024
2 parents 6c5a02a + e2c9fa0 commit f0207a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/e3/testsuite/driver/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import os
import re

from pathlib import Path
import traceback
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, IO, List, Optional, TYPE_CHECKING, Union

import e3.collection.dag
from e3.fs import rm, sync_tree
Expand All @@ -29,6 +30,10 @@
from colorama import Fore, Style


if TYPE_CHECKING:
from e3.os.process import DEVNULL_VALUE, PIPE_VALUE


class TestSkip(Exception):
"""
Convenience exception to abort a testcase.
Expand Down Expand Up @@ -165,6 +170,9 @@ def shell(
encoding: Optional[str] = None,
truncate_logs_threshold: Optional[int] = None,
ignore_environ: bool = True,
stdin: (
DEVNULL_VALUE | PIPE_VALUE | str | bytes | Path | IO | None
) = DEVNULL,
) -> ProcessResult:
"""Run a subprocess.
Expand All @@ -191,6 +199,7 @@ def shell(
When True (the default), pass exactly environment variables
in ``env``. When False, pass a copy of ``os.environ`` that is
augmented with variables in ``env``.
:param stdin: Forwarded to ``e3.os.process.Run``'s ``input`` argument.
"""
# By default, run the subprocess in the test working directory
if cwd is None:
Expand Down Expand Up @@ -230,7 +239,7 @@ def format_header(label: str, value: Any) -> str:
cwd=cwd,
output=PIPE,
error=STDOUT,
input=DEVNULL,
input=stdin,
timeout=timeout,
env=env,
ignore_environ=ignore_environ,
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/classic-tests/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def print_line(line):
sys.stdout.buffer.write(b"h\xe9llo")
elif arg.startswith("-i"):
sys.stdout.buffer.write("héllo".encode("iso-8859-1"))
elif arg == "-stdin":
print("From stdin:", repr(sys.stdin.read()))
else:
print("Unknown switch")
sys.exit(2)
22 changes: 22 additions & 0 deletions tests/tests/test_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,25 @@ def set_up(self):
"\n h\\\\xe9llo",
log,
)


def test_shell_stdin():
"""Check passing non-default stdin paramater to ClassicTestDriver.shell."""

class MyDriver(classic.ClassicTestDriver):
def run(self):
self.shell(
[sys.executable, ScriptDriver.helper_script, "-stdin"],
stdin=os.path.join(
os.path.dirname(__file__),
"classic-tests",
"abs-test-dir",
"input.txt",
),
)
self.result.out = self.output

suite = run_testsuite(create_testsuite(["t"], MyDriver), args=["-E"])
assert extract_results(suite) == {"t": Status.PASS}
r = suite.report_index.entries["t"].load()
assert r.out == "From stdin: 'This is the content of input.txt\\n'\n"

0 comments on commit f0207a5

Please sign in to comment.