Changeset - 902c313b4dfb
[Not reviewed]
0 2 0
Brett Smith - 3 years ago 2021-01-09 15:09:08
brettcsmith@brettcsmith.org
cliutil: New function can_run.
2 files changed with 30 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -22,2 +22,3 @@ import re
 
import signal
 
import subprocess
 
import sys
...
 
@@ -39,2 +40,3 @@ from typing import (
 
    Callable,
 
    Container,
 
    IO,
...
 
@@ -281,2 +283,18 @@ def add_version_argument(parser: argparse.ArgumentParser) -> argparse.Action:
 

	
 
def can_run(
 
        cmd: Sequence[str],
 
        stdout: Optional[int]=subprocess.DEVNULL,
 
        stderr: Optional[int]=None,
 
        ok_returncodes: Container[int]=frozenset([0]),
 
) -> bool:
 
    try:
 
        with subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr) as proc:
 
            # Typing says this can be None, but I don't think that's true
 
            # given that we passed stdin=PIPE.
 
            proc.stdin.close()  # type:ignore[union-attr]
 
    except (OSError, subprocess.SubprocessError):
 
        return False
 
    else:
 
        return proc.returncode in ok_returncodes
 

	
 
def date_arg(arg: str) -> datetime.date:
tests/test_cliutil.py
Show inline comments
...
 
@@ -22,2 +22,4 @@ from pathlib import Path
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import cliutil
...
 
@@ -229 +231,11 @@ def test_diff_year(date, diff, expected):
 
    assert cliutil.diff_year(date, diff) == expected
 

	
 
@pytest.mark.parametrize('cmd,expected', [
 
    (['true'], True),
 
    (['true', '--version'], True),
 
    (['false'], False),
 
    (['false', '--version'], False),
 
    ([str(testutil.TESTS_DIR)], False),
 
])
 
def test_can_run(cmd, expected):
 
    assert cliutil.can_run(cmd) == expected
0 comments (0 inline, 0 general)