Skip to content

Commit

Permalink
Ensures "no pipe" and "terminal" in evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Nov 30, 2019
1 parent 5139e6b commit c298391
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/core/evaluation.sh
Expand Up @@ -4,14 +4,17 @@ SHELLSPEC_STDIN_FILE="$SHELLSPEC_TMPBASE/$$.stdin"
SHELLSPEC_STDOUT_FILE="$SHELLSPEC_TMPBASE/$$.stdout"
SHELLSPEC_STDERR_FILE="$SHELLSPEC_TMPBASE/$$.stderr"

SHELLSPEC_STDIN_DEV=/dev/null
(: < /dev/tty) 2>/dev/null && SHELLSPEC_STDIN_DEV=/dev/tty

shellspec_syntax 'shellspec_evaluation_call'
shellspec_syntax 'shellspec_evaluation_run'

shellspec_proxy 'shellspec_evaluation' 'shellspec_syntax_dispatch evaluation'

shellspec_evaluation_call() {
if [ ! "${SHELLSPEC_DATA:-}" ]; then
shellspec_around_call "$@"
shellspec_around_call "$@" < "$SHELLSPEC_STDIN_DEV"
else
shellspec_data > "$SHELLSPEC_STDIN_FILE"
shellspec_around_call "$@" < "$SHELLSPEC_STDIN_FILE"
Expand Down Expand Up @@ -42,7 +45,7 @@ fi

shellspec_evaluation_run_data() {
if [ ! "${SHELLSPEC_DATA:-}" ]; then
shellspec_evaluation_run_trap_exit_status "$@"
shellspec_evaluation_run_trap_exit_status "$@" < "$SHELLSPEC_STDIN_DEV"
else
shellspec_data > "$SHELLSPEC_STDIN_FILE"
shellspec_evaluation_run_trap_exit_status "$@" < "$SHELLSPEC_STDIN_FILE"
Expand Down
24 changes: 24 additions & 0 deletions spec/core/evaluation_spec.sh
Expand Up @@ -63,6 +63,18 @@ Describe "core/evaluation.sh"
The stdout should equal "1${LF}2"
The status should equal 0
End

It "ensures no pipe and term"
evaluation() {
[ -p /dev/stdin ] && echo "pipe" || echo "no pipe"
[ -t 0 ] && echo "term" || echo "no term"
}
term() { exists_tty && echo "term" || echo "no term"; }

When call evaluation
The line 1 of stdout should equal 'no pipe'
The line 2 of stdout should equal "$(term)"
End
End

Describe 'run evaluation'
Expand Down Expand Up @@ -117,6 +129,18 @@ Describe "core/evaluation.sh"
The stdout should equal 'data'
End

It "ensures no pipe and term"
evaluation() {
[ -p /dev/stdin ] && echo "pipe" || echo "no pipe"
[ -t 0 ] && echo "term" || echo "no term"
}
term() { exists_tty && echo "term" || echo "no term"; }

When run evaluation
The line 1 of stdout should equal 'no pipe'
The line 2 of stdout should equal "$(term)"
End

Describe 'abort test'
Skip if 'shell flag handling broken' [ "$SHELLSPEC_DEFECT_SHELL_FLAG" ]
evaluation() { set -e; echo line1; "$BIN/exit.sh" 12; echo line2; }
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.sh
Expand Up @@ -86,4 +86,9 @@ shellspec_spec_helper_configure() {
shopt -s failglob 2>/dev/null && return 1
return 0
}

exists_tty() {
(: < /dev/tty) 2>/dev/null
}

}

0 comments on commit c298391

Please sign in to comment.