Skip to content

Commit

Permalink
Merge pull request #118 from shellspec/add_tmpdir_option
Browse files Browse the repository at this point in the history
Add --tmpdir option
  • Loading branch information
ko1nksm committed Nov 10, 2020
2 parents f736a4c + d75e858 commit 9bedb1a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `BeforeEach` / `AfterEach` as synonym for `Before` / `After`.
- Added `FORCE_COLOR` environment variable.
- Added `--tmpdir` option.

### Changed

- Replaced `--keep-tempdir` with `--keep-tmpdir`.

### Removed

Expand Down
2 changes: 1 addition & 1 deletion lib/core/dsl.sh
Expand Up @@ -435,7 +435,7 @@ shellspec_logger() {

shellspec_deprecated() {
set -- "${SHELLSPEC_SPECFILE:-}:${SHELLSPEC_LINENO:-$SHELLSPEC_LINENO_BEGIN}" "$@"
shellspec_putsn "$@" >>"$SHELLSPEC_TMPBASE/$SHELLSPEC_DEPRECATION_LOGFILE"
shellspec_putsn "$@" >>"$SHELLSPEC_DEPRECATION_LOGFILE"
}

shellspec_intercept() {
Expand Down
15 changes: 13 additions & 2 deletions lib/libexec/optparser.sh
Expand Up @@ -7,7 +7,7 @@
# shellcheck source=lib/getoptions_abbr.sh
. "$SHELLSPEC_LIB/getoptions_abbr.sh"

# shellcheck disable=SC1083
# shellcheck disable=SC1083,SC2016
parser_definition() {
extension "$@"
set -- "$1" "$2" "error_handler ${3:-echo}"
Expand Down Expand Up @@ -78,8 +78,12 @@ parser_definition() {
param LOGFILE --log-file init:='/dev/tty' -- \
'Log file for %logger directive and trace [default: /dev/tty]'

flag KEEP_TEMPDIR --keep-tempdir -- \
param TMPDIR --tmpdir init:="${TMPDIR:-${TMP:-/tmp}}" -- \
'Specify temporary directory [default: $TMPDIR, $TMP or /tmp]'

flag KEEP_TMPDIR --keep-tmpdir -- \
'Do not cleanup temporary directory [default: disabled]'
flag KEEP_TMPDIR --keep-tempdir validate:'deprecated $1' abbr: hidden:true

msg -- '' ' **** Execution ****' ''

Expand Down Expand Up @@ -410,3 +414,10 @@ error_handler() {
"$1" "$2"
return 1
}

deprecated() {
case $1 in
--keep-tempdir)
warn "--keep-tempdir is deprecated. replace with --keep-tmpdir."
esac
}
6 changes: 3 additions & 3 deletions libexec/shellspec-reporter.sh
Expand Up @@ -7,7 +7,7 @@ interrupt=''
"$SHELLSPEC_TRAP" 'interrupt=1' INT
"$SHELLSPEC_TRAP" '' TERM

echo $$ > "$SHELLSPEC_TMPBASE/$SHELLSPEC_REPORTER_PID"
echo $$ > "$SHELLSPEC_REPORTER_PID"

# shellcheck source=lib/libexec/reporter.sh
. "${SHELLSPEC_LIB:-./lib}/libexec/reporter.sh"
Expand Down Expand Up @@ -178,11 +178,11 @@ if [ -e "$SHELLSPEC_QUICK_FILE" ] && [ ! "$interrupt" ]; then
puts "$quick_file_data${quick_file_data:+"$LF"}" | sort > "$quick_file"
fi

if [ -e "$SHELLSPEC_TMPBASE/$SHELLSPEC_DEPRECATION_LOGFILE" ]; then
if [ -e "$SHELLSPEC_DEPRECATION_LOGFILE" ]; then
count=0 found='Found '
while IFS= read -r line && inc count; do
[ "$SHELLSPEC_DEPRECATION_LOG" ] && info "$line"
done < "$SHELLSPEC_TMPBASE/$SHELLSPEC_DEPRECATION_LOGFILE"
done < "$SHELLSPEC_DEPRECATION_LOGFILE"
pluralize found "$count deprecation"
if [ "$SHELLSPEC_DEPRECATION_LOG" ]; then
info "$found. Use --hide-deprecations to hide the details."
Expand Down
8 changes: 3 additions & 5 deletions libexec/shellspec-runner.sh
Expand Up @@ -3,8 +3,6 @@

set -eu

export SHELLSPEC_PROFILER_SIGNAL="$SHELLSPEC_TMPBASE/profiler.signal"

# shellcheck source=lib/libexec/runner.sh
. "${SHELLSPEC_LIB:-./lib}/libexec/runner.sh"

Expand All @@ -23,7 +21,7 @@ stop_profiler() {
cleanup() {
"$SHELLSPEC_TRAP" '' INT
set -- "$SHELLSPEC_TMPBASE" && SHELLSPEC_TMPBASE=''
[ "$SHELLSPEC_KEEP_TEMPDIR" ] && return 0
[ "$SHELLSPEC_KEEP_TMPDIR" ] && return 0
[ "$1" ] || return 0
{ rmtempdir "$1" & } 2>/dev/null
}
Expand All @@ -32,7 +30,7 @@ interrupt() {
"$SHELLSPEC_TRAP" '' TERM # Workaround for posh: Prevent display 'Terminated'.
stop_profiler
reporter_pid=''
read_pid_file reporter_pid "$SHELLSPEC_TMPBASE/$SHELLSPEC_REPORTER_PID" 0
read_pid_file reporter_pid "$SHELLSPEC_REPORTER_PID" 0
[ "$reporter_pid" ] && sleep_wait signal 0 "$reporter_pid" 2>/dev/null
signal TERM 0 2>/dev/null &&:
cleanup
Expand Down Expand Up @@ -118,7 +116,7 @@ SHELLSPEC_INFO="${quick_mode}${info}${info_extra:+ }${info_extra}"

mktempdir "$SHELLSPEC_TMPBASE"

if [ "$SHELLSPEC_KEEP_TEMPDIR" ]; then
if [ "$SHELLSPEC_KEEP_TMPDIR" ]; then
warn "Keeping temporary directory. "
warn "Manually delete: rm -rf \"$SHELLSPEC_TMPBASE\""
fi
Expand Down
28 changes: 14 additions & 14 deletions shellspec
Expand Up @@ -35,28 +35,19 @@ export SHELLSPEC_BUILTIN_PRINTF=''
export SHELLSPEC_BUILTIN_PRINT=''
export SHELLSPEC_BUILTIN_TYPESETF=''
export SHELLSPEC_TIME=''
export SHELLSPEC_TMPBASE=''
export SHELLSPEC_TIME_LOG=''
export SHELLSPEC_REPORTDIR=''
export SHELLSPEC_BANNER_FILE=''
export SHELLSPEC_LIST=''
export SHELLSPEC_COUNT_FILE=''
export SHELLSPEC_UNIXTIME=''
export SHELLSPEC_DEBUG_TRAP=''
export SHELLSPEC_TMPDIR=${TMPDIR:-${TMP:-/tmp}}
export SHELLSPEC_DEPRECATION_LOGFILE=".shellspec-deprecation.log"
export SHELLSPEC_INFILE=file
export SHELLSPEC_COVERAGE_SETUP=''
export SHELLSPEC_COVERAGE_SHELL_OPTIONS=''
export SHELLSPEC_COVERAGE_DIR=''
export SHELLSPEC_KCOV_COMMON_OPTS='' # TODO: remove
export SHELLSPEC_KCOV_COMPATIBLE_SHELL=''
export SHELLSPEC_KCOV_VERSION=''
export SHELLSPEC_KCOV_FILENAME=''
export SHELLSPEC_KCOV_IN_FILE=''
export SHELLSPEC_OUTPUT_FD=9
export SHELLSPEC_REPORTER_PID='.shellspec-reporter.pid'
export SHELLSPEC_PROFILER_LOG=''
export SHELLSPEC_PROFILER_REPORT=".shellspec-profiler.log"
export SHELLSPEC_DEFECT_READONLY=''
export SHELLSPEC_DEFECT_BUILTIN=''
Expand Down Expand Up @@ -138,13 +129,11 @@ SHELLSPEC_SPECDIR="$SHELLSPEC_PROJECT_ROOT/spec"
SHELLSPEC_REPORTDIR="$SHELLSPEC_PROJECT_ROOT/report"
SHELLSPEC_COVERAGE_DIR="$SHELLSPEC_PROJECT_ROOT/coverage"
SHELLSPEC_LOAD_PATH="$SHELLSPEC_SPECDIR:$SHELLSPEC_LIB:$SHELLSPEC_REPORTERLIB"
SHELLSPEC_TMPBASE="${SHELLSPEC_TMPDIR%/}/shellspec.$SHELLSPEC_UNIXTIME.$$"
SHELLSPEC_SUPPORT_BIN="$SHELLSPEC_LIB/support-bin.sh"
SHELLSPEC_SUPPORT_BINDIR="$SHELLSPEC_SPECDIR/support/bin"
SHELLSPEC_TIME_LOG="$SHELLSPEC_TMPBASE/.shellspec-time.log"
SHELLSPEC_PROFILER_LOG="$SHELLSPEC_TMPBASE/.shellspec-profiler.log"
SHELLSPEC_BANNER_FILE="$SHELLSPEC_SPECDIR/banner"

export SHELLSPEC_KCOV_COMMON_OPTS=''
while IFS= read -r option; do
SHELLSPEC_KCOV_COMMON_OPTS="$SHELLSPEC_KCOV_COMMON_OPTS$option "
done <<HERE
Expand All @@ -153,8 +142,7 @@ done <<HERE
--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/
--path-strip-level=1
HERE
SHELLSPEC_KCOV_FILENAME="$SHELLSPEC_PROJECT_NAME [specfiles]"
SHELLSPEC_KCOV_IN_FILE="$SHELLSPEC_TMPBASE/kcov/$SHELLSPEC_KCOV_FILENAME"
export SHELLSPEC_KCOV_FILENAME="$SHELLSPEC_PROJECT_NAME [specfiles]"

eval "$(getoptions parser_definition parse_options SHELLSPEC error_message)"

Expand Down Expand Up @@ -184,6 +172,18 @@ if [ "${SHELLSPEC_DOCKER_IMAGE#:}" ]; then
fi
eval "set -- $params"

export SHELLSPEC_TMPDIR=${SHELLSPEC_TMPDIR%/}
if [ ! -d "$SHELLSPEC_TMPDIR" ]; then
abort "Temporary directory '$SHELLSPEC_TMPDIR' does not exist or is not a directory"
fi
export SHELLSPEC_TMPBASE="$SHELLSPEC_TMPDIR/shellspec.$SHELLSPEC_UNIXTIME.$$"
export SHELLSPEC_TIME_LOG="$SHELLSPEC_TMPBASE/.shellspec-time.log"
export SHELLSPEC_PROFILER_LOG="$SHELLSPEC_TMPBASE/.shellspec-profiler.log"
export SHELLSPEC_DEPRECATION_LOGFILE="$SHELLSPEC_TMPBASE/.shellspec-deprecation.log"
export SHELLSPEC_PROFILER_SIGNAL="$SHELLSPEC_TMPBASE/.shellspec-profiler.signal"
export SHELLSPEC_REPORTER_PID="$SHELLSPEC_TMPBASE/.shellspec-reporter.pid"
export SHELLSPEC_KCOV_IN_FILE="$SHELLSPEC_TMPBASE/kcov/$SHELLSPEC_KCOV_FILENAME"

if [ "${SHELLSPEC_SHELL:-auto}" = "auto" ]; then
# shellcheck disable=SC2039,SC3047
if [ "$SHELLSPEC_KCOV" ] && ! (trap '' DEBUG) 2>/dev/null; then
Expand Down
9 changes: 7 additions & 2 deletions spec/core/dsl_spec.sh
Expand Up @@ -1053,10 +1053,15 @@ Describe "core/dsl.sh"
End

Describe "shellspec_deprecated()"
# shellcheck disable=SC2034
setup() {
SHELLSPEC_SPECFILE=spec.sh
SHELLSPEC_LINENO=10
SHELLSPEC_DEPRECATION_LOGFILE="$SHELLSPEC_TMPBASE/test-deprecation.log"
}
It 'outputs to logfile'
Path log="$SHELLSPEC_TMPBASE/test-deprecation.log"
BeforeRun SHELLSPEC_SPECFILE=spec.sh SHELLSPEC_LINENO=10
BeforeRun SHELLSPEC_DEPRECATION_LOGFILE=test-deprecation.log
BeforeRun "setup"
When run shellspec_deprecated "deprecated test"
The contents of file log should eq "spec.sh:10 deprecated test"
End
Expand Down

0 comments on commit 9bedb1a

Please sign in to comment.