Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve temporary dir creation #14

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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