Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __pycache__
dist
*.egg-info
.coverage
poetry.lock
45 changes: 41 additions & 4 deletions aurornis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def __repr__(self) -> str:


def run(
command: [str], environment: {str: str} = None, remove_colors: bool = False
command: [str],
environment: {str: str} = None,
remove_colors: bool = False,
stdin: [str] = None,
) -> CommandResult:
"""Execute the given command and return an object ready to check its result.

Expand Down Expand Up @@ -122,6 +125,25 @@ def run(
When `remove_color` is set to True, the `NO_COLOR` environment variable is defined to tell your command it should not output colors.
It is recommended to take this environment variable in account, as it is becoming a standard.
For more information, see https://no-color.org.

If your command reads the standard input, you can give it with the `stdin` argument.
It is a list of strings, which are joined with the end-of-line character ("\n") at execution.
Remember that the text written in standard input does not appear in the standard output.
>>> c = run(["python3", "-c", "who = input('Who are you? '); print(f'Hello {who}!')"], stdin=["World"])
>>> c.is_successful()
True
>>> c.stdout
'Who are you? Hello World!\\n'

The number of elements given in `stdin` is not verified by Aurornis, it is up to you to verify that the command
has the expected behavior.
>>> c = run(["python3", "-c", "who = input('Who are you? '); print(f'Hello {who}!')"])
>>> c.is_successful()
False
>>> c.stdout
'Who are you? '
>>> c.stderr
'Traceback (most recent call last):\\n File "<string>", line 1, in <module>\\nEOFError: EOF when reading a line\\n'
"""
if environment is None:
environment = {"LANG": "C"}
Expand All @@ -134,11 +156,26 @@ def run(
# See: https://no-color.org/
environment["NO_COLOR"] = "1"

if stdin is not None and len(stdin) > 0:
input = "\n".join(stdin).encode("utf-8")
else:
input = None

start_time = datetime.now()
process = subprocess.run(command, capture_output=True, check=False, env=environment)
exec_time = (datetime.now() - start_time).microseconds

stdout, stderr = process.stdout.decode(), process.stderr.decode()
process = subprocess.Popen(
command,
env=environment,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
)
stdout, stderr = process.communicate(input)

stdout = stdout.decode()
stderr = stderr.decode()

exec_time = (datetime.now() - start_time).microseconds

if remove_colors:
stdout, stderr = _remove_colors(stdout), _remove_colors(stderr)
Expand Down