Skip to content

Commit

Permalink
[test/spec] More test cases for $(< file).
Browse files Browse the repository at this point in the history
This is a curious construct described in issue #273.  It works in
multiple shells.

Also fix lint error, to fix the build.
  • Loading branch information
Andy Chu committed Apr 7, 2020
1 parent fbf7b79 commit 54944e0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
1 change: 0 additions & 1 deletion osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from core import error
from core import passwd
from core import pyutil
from core import state
from core.util import log, e_die, e_strict
from frontend import consts
from frontend import match
Expand Down
22 changes: 22 additions & 0 deletions spec/process-sub.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ warning: e2
o2
o1
## END

#### $(<file) idiom with process sub
echo FOO >foo

# works in bash and zsh
echo $(<foo)

# this works in zsh, but not in bash
tr A-Z a-z < <(<foo)

cat < <(<foo; echo hi)

## STDOUT:
FOO
hi
## END
## OK zsh STDOUT:
FOO
foo
FOO
hi
## END
39 changes: 32 additions & 7 deletions spec/redirect.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -455,32 +455,57 @@ status=0
echo FOO > myfile
foo=$(< myfile)
echo $foo
## STDOUT:
FOO
## END
## N-I dash/ash/yash stdout-json: "\n"

#### $(< file) with more statements

# note that it doesn't do this without a command sub!
# It's apparently a special case in bash, mksh, and zsh?
foo=$(echo begin; < myfile)
echo $foo
echo ---

foo=$(< myfile; echo end)
echo $foo
echo ---

foo=$(< myfile; <myfile)
echo $foo
echo ---

## STDOUT:
FOO
begin
---
end
## END
## N-I dash/ash/yash STDOUT:
---

begin
end
---
## END

# weird, zsh behaves differently
## OK zsh STDOUT:
FOO
begin
FOO
---
FOO
end
---
FOO
FOO
---
## END


#### < file in pipeline and subshell doesn't work
echo FOO > file2

# This only happens in command subs, which is weird
< file2 | tr A-Z a-z
( < file2 )
echo end
## STDOUT:
end
## END

Expand Down
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ regex() {

process-sub() {
# mksh and dash don't support it
sh-spec spec/process-sub.test.sh \
sh-spec spec/process-sub.test.sh --osh-failures-allowed 1 \
$BASH $ZSH $OSH_LIST "$@"
}

Expand Down

0 comments on commit 54944e0

Please sign in to comment.