From 33a76e1f8e386e2bfce1613cc164908f0939bc27 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 2 Oct 2014 16:32:16 -0700 Subject: [PATCH] Update psub for the new --inherit-variable flag Also do some minor formatting cleanup, make psub return 1 when executed outside of a command substitution, and make it respect $TMPDIR. --- share/functions/psub.fish | 18 +++++++++++++----- tests/test9.in | 8 ++++++++ tests/test9.out | 1 + 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/share/functions/psub.fish b/share/functions/psub.fish index 7877aa4ec..67863ad01 100644 --- a/share/functions/psub.fish +++ b/share/functions/psub.fish @@ -42,19 +42,24 @@ function psub --description "Read from stdin into a file and output the filename if not status --is-command-substitution echo psub: Not inside of command substitution >&2 - return + return 1 + end + + set -l TMPDIR $TMPDIR + if test -z "$TMPDIR[1]" + set TMPDIR /tmp end if test use_fifo = 1 # Write output to pipe. This needs to be done in the background so # that the command substitution exits without needing to wait for # all the commands to exit - set dir (mktemp -d /tmp/.psub.XXXXXXXXXX); or return - set filename $dir/psub.fifo + set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return + set filename $dir/psub.fifo mkfifo $filename cat >$filename & else - set filename (mktemp /tmp/.psub.XXXXXXXXXX) + set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX) cat >$filename end @@ -70,6 +75,9 @@ function psub --description "Read from stdin into a file and output the filename end # Make sure we erase file when caller exits - eval function $funcname --on-job-exit caller\; command rm $filename\; functions -e $funcname\; end + function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable funcname + command rm $filename + functions -e $funcname + end end diff --git a/tests/test9.in b/tests/test9.in index e40bc74ec..95a75843e 100644 --- a/tests/test9.in +++ b/tests/test9.in @@ -1,3 +1,4 @@ +# vim: set filetype=fish: # ensure that builtins that produce no output can still truncate files # (bug PCA almost reintroduced!) echo "Testing that builtins can truncate files" @@ -89,4 +90,11 @@ cat (echo foo | psub) cat (echo bar | psub) cat (echo baz | psub) +set -l filename (echo foo | psub) +if test -e $filename + echo 'psub file was not deleted' +else + echo 'psub file was deleted' +end + false diff --git a/tests/test9.out b/tests/test9.out index 7580b1d85..2e8e8bc7e 100644 --- a/tests/test9.out +++ b/tests/test9.out @@ -16,3 +16,4 @@ Testing for loop foo bar baz +psub file was deleted