Skip to content

Commit

Permalink
Fix regression involving command builtin
Browse files Browse the repository at this point in the history
Fixes #1402
  • Loading branch information
krader1961 committed Sep 16, 2019
1 parent 9484249 commit d9491d4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/cmd/ksh93/bltins/command.c
Expand Up @@ -57,7 +57,7 @@ int b_command(int argc, char *argv[], Shbltin_t *context) {
while ((opt = getopt_long(true_argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 1: {
builtin_print_help(shp, cmd);
if (argc != 0) builtin_print_help(shp, cmd);
return 0;
}
case 'p': {
Expand Down Expand Up @@ -86,6 +86,7 @@ int b_command(int argc, char *argv[], Shbltin_t *context) {
return 2;
}
case '?': {
if (argc == 0) return 0;
builtin_unknown_option(shp, cmd, argv[opterr]);
return 2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/functions/_ksh_print_help
Expand Up @@ -50,5 +50,5 @@ function _ksh_print_help {
/^[^ ]/b
p
}' |
uniq | "$ul"
uniq | "$ul" >&2
}
9 changes: 9 additions & 0 deletions src/cmd/ksh93/tests/b_command.sh
Expand Up @@ -82,3 +82,12 @@ actual=$(
)
[[ $actual == "$expect" ]] ||
log_error 'assignments to "command special_built-in" leaving side effects' "$expect" "$actual"

# ========
# Regression test for https://github.com/att/ast/issues/1402.
#
# We throw away stderr because we only want the value of `$t` not the error text from running
# `command` with an invalid flag.
expect='good'
actual=$($SHELL -c 't=good; t=bad command -@; print $t' 2>/dev/null)
[[ $expect == $actual ]] || log_error 'temp var assignment with `command`' "$expect" "$actual"
54 changes: 54 additions & 0 deletions src/cmd/ksh93/tests/b_exit.sh
@@ -0,0 +1,54 @@
#
# Verify the `exit` command behaves as expected.
#

# =======
actual=$($SHELL -c 'exit' 2>&1)
status=$?
expect=0
[[ -z $actual ]] || log_error 'bare exit' "" "$actual"
[[ $expect == $status ]] || log_error 'bare exit' "$expect" "$status"

# =======
actual=$($SHELL -c 'exit 0' 2>&1)
status=$?
expect=0
[[ -z $actual ]] || log_error 'exit 0' "" "$actual"
[[ $expect == $status ]] || log_error 'exit 0' "$expect" "$status"

# =======
actual=$($SHELL -c 'exit 1' 2>&1)
status=$?
expect=1
[[ -z $actual ]] || log_error 'exit 1' "" "$actual"
[[ $expect == $status ]] || log_error 'exit 1' "$expect" "$status"

# =======
actual=$($SHELL -c 'function e37 { return 37; } ; e37' 2>&1)
status=$?
expect=37
[[ -z $actual ]] || log_error 'exit 37' "" "$actual"
[[ $expect == $status ]] || log_error 'exit 37' "$expect" "$status"

# =======
actual=$($SHELL -c 'exit -1' 2>&1)
status=$?
expect=255
[[ -z $actual ]] || log_error 'exit -1' "" "$actual"
[[ $expect == $status ]] || log_error 'exit -1' "$expect" "$status"

# =======
actual=$($SHELL -c 'exit -2' 2>&1)
status=$?
expect=254
[[ -z $actual ]] || log_error 'exit -2' "" "$actual"
[[ $expect == $status ]] || log_error 'exit -2' "$expect" "$status"

# =======
$SHELL +E -i <<- \! && log_error 'interactive shell should not exit 0 after false'
false
exit
!
status=$?
expect=1
[[ $expect == $status ]] || log_error 'bare exit after false' "$expect" "$status"
5 changes: 0 additions & 5 deletions src/cmd/ksh93/tests/builtins.sh
Expand Up @@ -380,11 +380,6 @@ mkdir foo .bar
cd foo
cd ../.bar 2> /dev/null || log_error 'cd ../.bar when ../.bar exists should not fail'
$SHELL +E -i <<- \! && log_error 'interactive shell should not exit 0 after false'
false
exit
!
unset ENV
: ~root
Expand Down
1 change: 1 addition & 0 deletions src/cmd/ksh93/tests/meson.build
Expand Up @@ -16,6 +16,7 @@ all_tests = [
['b_dirname'],
['b_echo.exp'],
['b_enum'],
['b_exit'],
['b_grep'],
['b_head'],
['b_hist.exp'],
Expand Down

0 comments on commit d9491d4

Please sign in to comment.