# vim: set filetype=expect:
#
# Verify `$pipestatus` is preserved to next prompt.

spawn $fish

expect_prompt

# no pipe
send_line "true"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "0 : 0" {}
send_line "false"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 : 1" {}

# no pipe but negated
send_line "! true"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "0 : 1" {}
send_line "! false"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 : 0" {}

# pipe
send_line "false | true | false"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 0 1 : 1" {}

# pipe negated
send_line "! false | true | false"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 0 1 : 0" {}

# pipe mixed with builtins and external
send_line "command true | false | true | true"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "0 1 0 0 : 0" {}
send_line "command true | command false | command false"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "0 1 1 : 1" {}
send_line "sh -c 'exit 3' | command false | sh -c 'exit 5'"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "3 1 5 : 5" {}

# negated pipe with mixed builtin and external
send_line "! sh -c 'exit 3' | command false | sh -c 'exit 5'"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "3 1 5 : 0" {}

# block
send_line "command false | begin; command true; end | true"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 0 0 : 0" {}
send_line "command false | begin; ! true; end | true"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "1 1 0 : 0" {}

# syntax error
send_line "syntax terror"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "127 : 127" {}
send_line "syntax | terror"
expect_prompt
send_line "echo \$pipestatus : \$status"
expect_prompt "127 : 127" {}