Skip to content

Commit

Permalink
Improve temporary dir creation
Browse files Browse the repository at this point in the history
Use mktemp. If not available, create a random dir manually, using awk's
rand() and $$.

Not a pretty solution, but it's portable.

Fixes aureliojargas#12
  • Loading branch information
aureliojargas authored and pvital committed Oct 1, 2018
1 parent f384957 commit 6802284
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions clitest
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ Customization options:
--prefix PREFIX Set command line prefix (default: '$tt_prefix')
--prompt STRING Set prompt string (default: '$tt_prompt')"

# Temporary files (using files because <(...) is not portable)
tt_temp_dir="${TMPDIR:-/tmp}/clitest.$$"
tt_temp_file="$tt_temp_dir/temp.txt"
tt_test_ok_file="$tt_temp_dir/ok.txt"
tt_test_output_file="$tt_temp_dir/output.txt"

# Flags (0=off, 1=on), most can be altered by command line options
tt_debug=0
tt_use_colors=0
Expand Down Expand Up @@ -604,10 +598,29 @@ tt_process_test_file ()
# Run pending tests
test -n "$tt_test_command" && tt_run_test
}
tt_make_temp_dir ()
{
# Create private temporary dir and sets global $tt_temp_dir.
# http://mywiki.wooledge.org/BashFAQ/062

# Prefer mktemp when available
tt_temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/clitest.XXXXXX" 2> /dev/null) && return 0

# No mktemp, let's create the dir manually
tt_temp_dir="${TMPDIR:-/tmp}/clitest.$(awk 'BEGIN { srand(); print rand() }').$$" &&
mkdir -m 700 "$tt_temp_dir" || tt_error "cannot create temporary dir: $tt_temp_dir"
}


### Init process

# Temporary files (using files because <(...) is not portable)
tt_temp_dir=
tt_make_temp_dir # sets global $tt_temp_dir
tt_temp_file="$tt_temp_dir/temp.txt"
tt_test_ok_file="$tt_temp_dir/ok.txt"
tt_test_output_file="$tt_temp_dir/output.txt"

# Handle command line options
while test "${1#-}" != "$1"
do
Expand Down Expand Up @@ -757,9 +770,6 @@ then
tt_error "invalid argument for -s or --skip: $tt_skip_range"
fi

# Create temp dir, protected from others
umask 077 && mkdir "$tt_temp_dir" || tt_error "cannot create temporary dir: $tt_temp_dir"


### Real execution begins here

Expand Down
4 changes: 2 additions & 2 deletions test.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ $ echo $not_exported #→ --regex ^1$
$ TMPDIR___SAVE="$TMPDIR"
$ TMPDIR=/XXnotfoundXX
$ export TMPDIR
$ ./clitest test/ok-1.sh 2>&1 | grep ^clitest | sed 's/clitest\.[0-9]*$/clitest.NNN/'
clitest: Error: cannot create temporary dir: /XXnotfoundXX/clitest.NNN
$ ./clitest test/ok-1.sh 2>&1 | grep ^clitest | sed 's/clitest\..*$/clitest.XXXXXX/'
clitest: Error: cannot create temporary dir: /XXnotfoundXX/clitest.XXXXXX
$ TMPDIR="$TMPDIR___SAVE"
$
```
Expand Down

0 comments on commit 6802284

Please sign in to comment.