8000 Controllable `subprocess` wrapper by tony · Pull Request #336 · vcs-python/libvcs · GitHub
[go: up one dir, main page]

Skip to content

Controllable subprocess wrapper #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests: libvcs.utils.subprocess
  • Loading branch information
tony committed May 1, 2022
commit 8551a44cc215f8d2c22627f9c366e8e5924c39bb
129 changes: 129 additions & 0 deletions tests/utils/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import pathlib
import subprocess
from typing import Any

import pytest

from libvcs.utils.subprocess import SubprocessCommand


@pytest.fixture(autouse=True)
def cwd_default(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
monkeypatch.chdir(tmp_path)


@pytest.mark.parametrize(
"args,kwargs,expected_result",
[
[["ls"], {}, SubprocessCommand("ls")],
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
[[], {"args": ["ls", "-l"]}, SubprocessCommand(["ls", "-l"])],
[["ls -l"], {"shell": True}, SubprocessCommand("ls -l", shell=True)],
[[], {"args": "ls -l", "shell": True}, SubprocessCommand("ls -l", shell=True)],
[
[],
{"args": ["ls", "-l"], "shell": True},
SubprocessCommand(["ls", "-l"], shell=True),
],
],
)
def test_init(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand via list + kwargs, assert attributes"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result

# Attributes in cmd should match whats passed in
for k, v in kwargs.items():
assert getattr(cmd, k) == v

proc = cmd.Popen()
proc.communicate()
assert proc.returncode == 0


FIXTURES = [
[["ls"], {}, SubprocessCommand("ls")],
[[["ls", "-l"]], {}, SubprocessCommand(["ls", "-l"])],
]


@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
)
def test_init_and_Popen(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result

cmd_proc = cmd.Popen()
cmd_proc.communicate()
assert cmd_proc.returncode == 0

proc = subprocess.Popen(*args, **kwargs)
proc.communicate()
assert proc.returncode == 0


@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
)
def test_init_and_Popen_run(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with run"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result

cmd_proc = cmd.Popen()
cmd_proc.communicate()
assert cmd_proc.returncode == 0

proc = subprocess.run(*args, **kwargs)
assert proc.returncode == 0


@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
)
def test_init_and_check_call(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen.check_call"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result

return_code = cmd.check_call()
assert return_code == 0

proc = subprocess.check_call(*args, **kwargs)
assert proc == return_code


@pytest.mark.parametrize(
"args,kwargs,expected_result",
FIXTURES,
)
def test_init_and_check_output(args: list, kwargs: dict, expected_result: Any):
"""Test SubprocessCommand with Popen.check_output"""
cmd = SubprocessCommand(*args, **kwargs)
assert cmd == expected_result

return_output = cmd.check_output()
assert isinstance(return_output, bytes)

proc = subprocess.check_output(*args, **kwargs)
assert proc == return_output


@pytest.mark.parametrize(
"args,kwargs,run_kwargs",
[
[["ls"], {}, {}],
[[["ls", "-l"]], {}, {}],
[[["ls", "-al"]], {}, {"stdout": subprocess.DEVNULL}],
],
)
def test_run(tmp_path: pathlib.Path, args: list, kwargs: dict, run_kwargs: dict):
cmd = SubprocessCommand(*args, cwd=tmp_path, **kwargs)
response = cmd.run(**run_kwargs)

assert response.returncode == 0
0