Skip to content

Commit

Permalink
Change the structure of the temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed May 3, 2020
1 parent e645970 commit 5d417c3
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 21 deletions.
8 changes: 8 additions & 0 deletions lib/core/dsl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ shellspec_example() {
shellspec_invoke_example() {
shellspec_output EXAMPLE

# shellcheck disable=SC2034
{
SHELLSPEC_WORKDIR="$SHELLSPEC_TMPBASE/$SHELLSPEC_SPEC_NO"
SHELLSPEC_STDIN_FILE="$SHELLSPEC_WORKDIR/$SHELLSPEC_EXAMPLE_NO.stdin"
SHELLSPEC_STDOUT_FILE="$SHELLSPEC_WORKDIR/$SHELLSPEC_EXAMPLE_NO.stdout"
SHELLSPEC_STDERR_FILE="$SHELLSPEC_WORKDIR/$SHELLSPEC_EXAMPLE_NO.stderr"
}

shellspec_on NOT_IMPLEMENTED
shellspec_off FAILED WARNED EXPECTATION
shellspec_off UNHANDLED_STATUS UNHANDLED_STDOUT UNHANDLED_STDERR
Expand Down
3 changes: 0 additions & 3 deletions lib/core/evaluation.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#shellcheck shell=sh

SHELLSPEC_STDIN_FILE="$SHELLSPEC_TMPBASE/$$.stdin"
SHELLSPEC_STDOUT_FILE="$SHELLSPEC_TMPBASE/$$.stdout"
SHELLSPEC_STDERR_FILE="$SHELLSPEC_TMPBASE/$$.stderr"
SHELLSPEC_SHELL_OPTIONS=''

SHELLSPEC_STDIN_DEV=/dev/null
Expand Down
5 changes: 3 additions & 2 deletions lib/core/matchers/satisfy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

shellspec_syntax 'shellspec_matcher_satisfy'

SHELLSPEC_SATISFY_STDOUT_FILE="$SHELLSPEC_TMPBASE/$$.satisfy.stdout"
SHELLSPEC_SATISFY_STDERR_FILE="$SHELLSPEC_TMPBASE/$$.satisfy.stderr"

shellspec_matcher_satisfy() {
shellspec_matcher__match() {
SHELLSPEC_SATISFY_STDOUT_FILE="$SHELLSPEC_WORKDIR/satisfy.stdout"
SHELLSPEC_SATISFY_STDERR_FILE="$SHELLSPEC_WORKDIR/satisfy.stderr"

if ! shellspec_is_function "${1:-}"; then
shellspec_output SYNTAX_ERROR "'$1' is not function name"
return 0
Expand Down
6 changes: 3 additions & 3 deletions lib/core/modifiers/result.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

shellspec_syntax 'shellspec_modifier_result'

SHELLSPEC_RESULT_STDOUT_FILE="$SHELLSPEC_TMPBASE/$$.result.stdout"
SHELLSPEC_RESULT_STDERR_FILE="$SHELLSPEC_TMPBASE/$$.result.stderr"

shellspec_modifier_result() {
SHELLSPEC_RESULT_STDOUT_FILE="$SHELLSPEC_WORKDIR/result.stdout"
SHELLSPEC_RESULT_STDERR_FILE="$SHELLSPEC_WORKDIR/result.stderr"

if [ "${SHELLSPEC_SUBJECT+x}" ]; then
if ! shellspec_is_function "$SHELLSPEC_SUBJECT"; then
shellspec_output SYNTAX_ERROR "'$SHELLSPEC_SUBJECT' is not function name"
Expand Down
16 changes: 16 additions & 0 deletions lib/libexec/executor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#shellcheck shell=sh disable=SC2004

count_specfiles() {
eval "$1=0; found_specfile() { $1=\$((\$$1 + 1)); }"
shift
eval find_specfiles found_specfile ${1+'"$@"'}
}

create_workdirs() {
i=0
while [ "$i" -lt "$1" ] && i=$(($i + 1)); do
set -- "$@" "$SHELLSPEC_TMPBASE/$i"
done
shift
[ $# -eq 0 ] || mkdir "$@"
}
18 changes: 11 additions & 7 deletions lib/libexec/kcov-executor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

use includes replace_all

kcov_preprocess() {
[ -d "$SHELLSPEC_COVERAGE_DIR" ] || return 0

# Cleanup previous coverage data
rm -rf "${SHELLSPEC_COVERAGE_DIR:?}/${SHELLSPEC_KCOV_FILENAME:?}"*
}

executor() {
#shellcheck disable=SC2039
[ "$(ulimit -n)" -lt 1024 ] && ulimit -n 1024

count=0
eval count_specfiles count ${1+'"$@"'}
create_workdirs "$count"

#shellcheck disable=SC2034
SHELLSPEC_COVERAGE_SETUP="$SHELLSPEC_LIB/cov/kcov-setup.sh"
#shellcheck disable=SC2034
Expand All @@ -41,6 +38,13 @@ executor() {
eval "kcov_postprocess; return $?"
}

kcov_preprocess() {
[ -d "$SHELLSPEC_COVERAGE_DIR" ] || return 0

# Cleanup previous coverage data
rm -rf "${SHELLSPEC_COVERAGE_DIR:?}/${SHELLSPEC_KCOV_FILENAME:?}"*
}

kcov_postprocess() {
[ -d "$SHELLSPEC_COVERAGE_DIR" ] || return 0

Expand Down
1 change: 1 addition & 0 deletions lib/libexec/parallel-executor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ executor() {
putsn "$1" > "$SHELLSPEC_JOBDIR/$jobs.job"
}
eval find_specfiles specfile ${1+'"$@"'}
create_workdirs "$jobs"

translator --no-finished | $SHELLSPEC_SHELL # output only metadata
callback() { worker "$1" "$jobs" & }
Expand Down
3 changes: 3 additions & 0 deletions lib/libexec/serial-executor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
. "${SHELLSPEC_LIB:-./lib}/libexec.sh"

executor() {
count=0
eval count_specfiles count ${1+'"$@"'}
create_workdirs "$count"
translator "$@" | $SHELLSPEC_SHELL
}
3 changes: 3 additions & 0 deletions libexec/shellspec-executor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

set -eu

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

if [ "$SHELLSPEC_KCOV" -gt 0 ]; then
# shellcheck source=lib/libexec/kcov-executor.sh
. "${SHELLSPEC_LIB:-./lib}/libexec/kcov-executor.sh"
Expand Down
2 changes: 1 addition & 1 deletion libexec/shellspec-reporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interrupt=''
if (trap - INT) 2>/dev/null; then trap 'interrupt=1' INT; fi
if (trap - TERM) 2>/dev/null; then trap '' TERM; fi

[ "${SHELLSPEC_TMPBASE:-}" ] && echo $$ > "$SHELLSPEC_TMPBASE/reporter.pid"
echo $$ > "$SHELLSPEC_TMPBASE/$SHELLSPEC_REPORTER_PID"

# shellcheck source=lib/libexec/reporter.sh
. "${SHELLSPEC_LIB:-./lib}/libexec/reporter.sh"
Expand Down
9 changes: 5 additions & 4 deletions libexec/shellspec-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ stop_profiler() {

cleanup() {
if (trap - INT) 2>/dev/null; then trap '' INT; fi
[ "$SHELLSPEC_TMPBASE" ] || return 0
tmpbase="$SHELLSPEC_TMPBASE" && SHELLSPEC_TMPBASE=''
[ "$SHELLSPEC_KEEP_TEMPDIR" ] || rmtempdir "$tmpbase"
set -- "$SHELLSPEC_TMPBASE" && SHELLSPEC_TMPBASE=''
[ "$SHELLSPEC_KEEP_TEMPDIR" ] && return 0
[ "$1" ] || return 0
rmtempdir "$1" &
}

interrupt() {
trap '' TERM # Workaround for posh: Prevent display 'Terminated'.
stop_profiler
reporter_pid=''
read_pid_file reporter_pid "$SHELLSPEC_TMPBASE/reporter.pid" 0
read_pid_file reporter_pid "$SHELLSPEC_TMPBASE/$SHELLSPEC_REPORTER_PID" 0
[ "$reporter_pid" ] && sleep_wait sigchk "$reporter_pid"
sigterm 0 &&:
cleanup
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"lib/general.sh",
"lib/libexec.sh",
"lib/libexec/binary.sh",
"lib/libexec/executor.sh",
"lib/libexec/grammar.sh",
"lib/libexec/grammar/blocks",
"lib/libexec/grammar/directives",
Expand Down
3 changes: 2 additions & 1 deletion shellspec
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ export SHELLSPEC_KCOV_COMMON_OPTS=''
export SHELLSPEC_KCOV_OPTS=''
export SHELLSPEC_KCOV_FILENAME=''
export SHELLSPEC_KCOV_IN_FILE=''
export SHELLSPEC_REPORTER_PID='.shellspec-reporter.pid'
export SHELLSPEC_PROFILER=''
export SHELLSPEC_PROFILER_LOG=''
export SHELLSPEC_PROFILER_LIMIT=10
export SHELLSPEC_PROFILER_REPORT="profiler.log"
export SHELLSPEC_PROFILER_REPORT=".shellspec-profiler.log"
export SHELLSPEC_DEFECT_READONLY=''
export SHELLSPEC_DEFECT_BUILTIN=''
export SHELLSPEC_DEFECT_REDEFINE=''
Expand Down
32 changes: 32 additions & 0 deletions spec/libexec/executor_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#shellcheck shell=sh

Describe "libexec/executor.sh"
Include "$SHELLSPEC_LIB/libexec/executor.sh"

Describe "count_specfiles()"
Before count=''

find_specfiles() {
_callback=$1 && shift
while [ $# -gt 0 ]; do "$_callback"; shift ; done
}

It 'counts specfiles'
When call count_specfiles count file1 file3 file3
The variable count should eq 3
End
End

Describe "create_workdirs()"
mkdir() { printf '%s\n' "$@"; }

It 'creates workdirs'
When call create_workdirs 5
The line 1 of stdout should eq "$SHELLSPEC_TMPBASE/1"
The line 2 of stdout should eq "$SHELLSPEC_TMPBASE/2"
The line 3 of stdout should eq "$SHELLSPEC_TMPBASE/3"
The line 4 of stdout should eq "$SHELLSPEC_TMPBASE/4"
The line 5 of stdout should eq "$SHELLSPEC_TMPBASE/5"
End
End
End

0 comments on commit 5d417c3

Please sign in to comment.