clitest
and stdin don't always work well together.
StackOverflow wasn't much help (shocked pikachu)! Buried deep in the interwebs is the bash advice
to check for a TTY and whether stdin was piped with this: if [ ! -t 0 ] && [ -p /dev/stdin ];
.
Using this, I wanted to see how to do something similar in Python to make clitest work. Even
deeper on the internet was this gem, which led me to
the answer - use stat
in Python to test whether stdin is a FIFO (pipe).
Thus, the answer to this hours long search is simply this function:
def stdin_is_piped():
fileno = sys.stdin.fileno()
mode = os.fstat(fileno).st_mode
return not os.isatty(fileno) and stat.S_ISFIFO(mode)
With this, now we can run clitests on Python utilities that pipe arguments from stdin.
$ echo x y z | ./bin/pipetest a b c
arg: a
arg: b
arg: c
stdin: x y z
$
$ ./bin/pipetest a b c
arg: a
arg: b
arg: c
no piped data!
$
$ ./bin/pipetest
no piped data!
$