Skip to content

Commit

Permalink
[spec/redirect-glob] New tests for redirects with globs
Browse files Browse the repository at this point in the history
This is a bash/zsh feature.  It's arguably weird, but I did use it, and
others may run into it.

Part of issue #1521
  • Loading branch information
Andy C committed Aug 31, 2023
1 parent 8f0e83b commit c597157
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
5 changes: 4 additions & 1 deletion osh/cmd_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ def _EvalRedirect(self, r):
r.op.id) # could be static in the LST?

if redir_type == redir_arg_type_e.Path:
# NOTES
# Note: Only bash and zsh allow globbing a path. If there
# are multiple files, zsh opens BOTH, but bash exits with
# error 1.

# - no globbing. You can write to a file called '*.py'.
# - set -o strict-array prevents joining by spaces
val = self.word_ev.EvalWordToString(arg_word)
Expand Down
137 changes: 137 additions & 0 deletions spec/redirect-glob.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
## compare_shells: bash mksh zsh ash
## oils_failures_allowed: 3

#### file redirects with glob args (bash and zsh only)

touch one-bar

echo hi > one-*

cat one-bar

echo escaped > one-\*

cat one-\*

## STDOUT:
hi
escaped
## END
## N-I dash/mksh/ash STDOUT:
escaped
## END

#### file redirect to $var with glob char

touch two-bar

star='*'

echo hi > two-$star
echo status=$?

head two-bar two-\*

## status: 0
## STDOUT:
status=0
==> two-bar <==

==> two-* <==
hi
## END

## BUG bash status: 1
## BUG bash STDOUT:
status=0
==> two-bar <==
hi
## END

#### file redirect that globs to more than one file (bash and zsh only)

touch foo-bar
touch foo-spam

echo hi > foo-*
echo status=$?

head foo-bar foo-spam

## STDOUT:
status=1
==> foo-bar <==

==> foo-spam <==
## END

## N-I dash/mksh/ash STDOUT:
status=0
==> foo-bar <==

==> foo-spam <==
## END

## BUG zsh STDOUT:
status=0
==> foo-bar <==
hi

==> foo-spam <==
hi
## END

#### file redirect with extended glob (bash only)

shopt -s extglob

touch foo-bar

echo hi > @(*-bar|other)
echo status=$?

cat foo-bar

## status: 0
## STDOUT:
status=0
hi
## END

## N-I zsh status: 1
## N-I dash/ash status: 2

## N-I dash/zsh/ash STDOUT:
## END

## BUG mksh status: 0
## BUG mksh STDOUT:
status=0
## END

#### other redirects with glob args

touch 10

exec 10>&1 # open stdout as descriptor 10

# Does this go to stdout? ONLY bash respects it, not zsh
echo should-not-be-on-stdout >& 1*

echo stdout
echo stderr >&2

## status: 0

## STDOUT:
stdout
## END

## BUG bash STDOUT:
should-not-be-on-stdout
stdout
## END

## N-I dash/zsh status: 127
## N-I dash/zsh STDOUT:
## END
2 changes: 1 addition & 1 deletion spec/redirect.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ echo XX >| $TMP/c.txt

set -o noclobber

echo YY > $TMP/c.txt # not globber
echo YY > $TMP/c.txt # not clobber
echo status=$?

cat $TMP/c.txt
Expand Down
4 changes: 4 additions & 0 deletions test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ redirect() {
${REF_SHELLS[@]} $OSH_LIST "$@"
}

redirect-glob() {
run-file redirect-glob "$@"
}

posix() {
sh-spec spec/posix.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
Expand Down

0 comments on commit c597157

Please sign in to comment.