Skip to content

Commit

Permalink
shellspec_get_nth(): Do not expand glob
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Apr 24, 2020
1 parent 9df5ccf commit ca4e240
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/core/modifiers/word.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ shellspec_modifier_word() {
shellspec_syntax_param 1 is number "$1" || return 0

if [ "${SHELLSPEC_SUBJECT+x}" ]; then
shellspec_get_nth SHELLSPEC_SUBJECT "$1" "$SHELLSPEC_IFS" "$SHELLSPEC_SUBJECT"
shellspec_get_nth SHELLSPEC_SUBJECT "$SHELLSPEC_SUBJECT" "$1"
else
unset SHELLSPEC_SUBJECT ||:
fi
Expand Down
6 changes: 0 additions & 6 deletions lib/core/utils.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#shellcheck shell=sh disable=SC2016

shellspec_get_nth() {
shellspec_reset_params '"$1" "$2" $4' "$3"
eval "$SHELLSPEC_RESET_PARAMS"
eval "$1=\${$(($2 + 2)):-}"
}

shellspec_is() {
case $1 in
number) case ${2:-} in ( '' | *[!0-9]* ) return 1; esac ;;
Expand Down
22 changes: 22 additions & 0 deletions lib/general.sh
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,28 @@ shellspec_chomp() {
eval "$SHELLSPEC_EVAL"
}

if [ "${ZSH_VERSION:-}" ]; then
shellspec_get_nth() {
set -- "$1" "$2" "$3" "${4:-}" "$IFS"
IFS=${4:-$SHELLSPEC_IFS}
eval "set -- \"\$@\" \${=2}"
IFS=$5
eval "$1=\${$(($3 + 5))}"
}
else
shellspec_get_nth() {
set -f -u -- "$1" "$2" "$3" "${4:-}" "$IFS" "$-"
IFS=${4:-$SHELLSPEC_IFS}
# shellcheck disable=SC2086
set -- "$@" $2
IFS=$5
[ "${6#*f}" = "$6" ] && set +f
# Workaround for posh 0.10.2: glob does not expand when set -u
[ "${6#*u}" = "$6" ] && set +u
eval "$1=\${$(($3 + 6))}"
}
fi

shellspec_which() {
set -- "$1" "${PATH%:}:"
while [ "${2%:}" ]; do
Expand Down
1 change: 0 additions & 1 deletion lib/libexec/shellspec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ ps_command() {
read_ps() {
# shellcheck disable=SC2015
ps_command 2>/dev/null | {
[ "${ZSH_VERSION:-}" ] && setopt shwordsplit
IFS=" " pid=$1 p=0 c=0 _pid=''
IFS= read -r line
# shellcheck disable=SC2086
Expand Down
4 changes: 2 additions & 2 deletions libexec/shellspec-reporter.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
#shellcheck disable=SC2004,SC2016

set -eu
set -euf

: "${SHELLSPEC_SPEC_FAILURE_CODE:=101}"
: "${SHELLSPEC_FORMATTER:=debug}" "${SHELLSPEC_GENERATORS:=}"
Expand Down Expand Up @@ -56,7 +56,7 @@ parse_lines() {
}

parse_fields() {
OLDIFS=$IFS && IFS=$US && eval "set -- \${${ZSH_VERSION:+=}1}" && IFS=$OLDIFS
OLDIFS=$IFS && IFS=$US && eval "set -- \$1" && IFS=$OLDIFS

# Workaround: Do not merge two 'for'. A bug occurs in variable expansion
# rarely in busybox-1.10.2.
Expand Down
2 changes: 1 addition & 1 deletion shellspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[ "$PPID" ] || { echo "Unsupported shell. (Bourne shell?)" >&2; exit 1; }

set -e -u
set -e -u -f

if [ "${1:-}" = "-" ]; then
echo 'IFS= read -r shebang < "$0"'
Expand Down
12 changes: 0 additions & 12 deletions spec/core/utils_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
Describe "core/utils.sh"
Include "$SHELLSPEC_LIB/core/utils.sh"

Describe 'shellspec_get_nth()'
It 'fetch nth value seperate by ,'
When call shellspec_get_nth var 3 ',' "a,b,c,d,e"
The variable var should equal c
End

It 'fetch nth value seperate by space'
When call shellspec_get_nth var 3 " " "a b c"
The variable var should equal c
End
End

Describe 'shellspec_is()'
Describe 'number'
It 'succeeds with a numeric value'
Expand Down
19 changes: 19 additions & 0 deletions spec/general_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,25 @@ Describe "general.sh"
End
End

Describe 'shellspec_get_nth()'
It 'fetch nth value seperate by IFS'
When call shellspec_get_nth var " a b c " 3
The variable var should equal c
End

Parameters
"a,b,c,d,e" 3 ',' "c"
" a b c " 3 ' ' "c"
",,a,," 3 ',' "a"
" a * c " 2 " " "*"
End

It "fetches nth word ($1 : $2 : $3)" a b
When call shellspec_get_nth var "$1" "$2" "$3"
The variable var should equal "$4"
End
End

Describe "shellspec_which()"
Context 'when PATH=/foo:/bin:/bar'
Before PATH=/foo:/bin:/bar
Expand Down

0 comments on commit ca4e240

Please sign in to comment.