Changeset View
Changeset View
Standalone View
Standalone View
bin/sh/t_redir.sh
Property | Old Value | New Value |
---|---|---|
File Mode | 100755 | 100644 |
# $NetBSD: t_redir.sh,v 1.9 2016/05/14 00:33:02 kre Exp $ | # $NetBSD: t_redir.sh,v 1.11 2021/05/19 22:43:18 kre Exp $ | ||||
# | # | ||||
# Copyright (c) 2016 The NetBSD Foundation, Inc. | # Copyright (c) 2016 The NetBSD Foundation, Inc. | ||||
# All rights reserved. | # All rights reserved. | ||||
# | # | ||||
# Redistribution and use in source and binary forms, with or without | # Redistribution and use in source and binary forms, with or without | ||||
# modification, are permitted provided that the following conditions | # modification, are permitted provided that the following conditions | ||||
# are met: | # are met: | ||||
# 1. Redistributions of source code must retain the above copyright | # 1. Redistributions of source code must retain the above copyright | ||||
▲ Show 20 Lines • Show All 311 Lines • ▼ Show 20 Lines | #12 | ||||
EOF | EOF | ||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} | atf_check -s exit:0 -o empty -e empty ${TEST_SH} | ||||
test "$(cat Out1)" = "line 1${nl}line 2${nl}line 3" || atf_fail \ | test "$(cat Out1)" = "line 1${nl}line 2${nl}line 3" || atf_fail \ | ||||
"#$T: Incorrect Out1: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out1)'" | "#$T: Incorrect Out1: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out1)'" | ||||
test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \ | test "$(cat Out2)" = "line 1${nl}line 2${nl}line 3" || atf_fail \ | ||||
"#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'" | "#$T: Incorrect Out2: Should be 'line 1\\nline 2\\nline 3' is '$(cat Out2)'" | ||||
} | } | ||||
atf_test_case do_redirect_input_output | |||||
do_redirect_input_output_head() | |||||
{ | |||||
atf_set "descr" "Test Input+Output (BiDir) redirections" | |||||
} | |||||
do_redirect_input_output_body() | |||||
{ | |||||
nl=' | |||||
' | |||||
T=0 | |||||
i() { T=$(expr "$T" + 1); } | |||||
rm -f Output 2>/dev/null || : | |||||
test -f Output && atf_fail "Unable to remove Output file" | |||||
#1 | |||||
i; atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c '<> Output' | |||||
test -f Output || atf_fail "#$T: Did not make Output file" | |||||
#2 | |||||
echo data >Output 2>/dev/null || : | |||||
i | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ | |||||
'<>Output' | |||||
test -f Output || atf_fail "#$T: Removed Output file" | |||||
test -s Output || atf_fail "#$T: Did not keep data in Output file" | |||||
test "$(cat Output)" = "data" || | |||||
atf_fail "#$T: Incorrect Output: Should be 'data' is '$(cat Output)'" | |||||
#3 | |||||
rm -f Output 2>/dev/null || : | |||||
i | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ | |||||
'echo Hello 1<>Output' | |||||
test -s Output || atf_fail "#$T: Did not keep non-empty Output file" | |||||
test "$(cat Output)" = "Hello" || | |||||
atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'" | |||||
#4 | |||||
printf data >Output 2>/dev/null || : | |||||
i | |||||
atf_check -s exit:0 -o inline:'data' -e empty ${TEST_SH} -c \ | |||||
'cat <>Output' | |||||
test -f Output || atf_fail "#$T: Removed Output file" | |||||
test -s Output || atf_fail "#$T: Did not keep data in Output file" | |||||
test "$(cat Output)" = "data" || | |||||
atf_fail "#$T: Incorrect Output: Should be 'data' is '$(cat Output)'" | |||||
#5 | |||||
echo data >Output 2>/dev/null || : | |||||
i | |||||
atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ | |||||
'echo Hello 1<>Output' | |||||
test -s Output || atf_fail "#$T: Did not make non-empty Output file" | |||||
test "$(cat Output)" = "Hello" || | |||||
atf_fail "#$T: Incorrect Output: Should be 'Hello' is '$(cat Output)'" | |||||
#6 | |||||
printf data >Output 2>/dev/null || : | |||||
i | |||||
atf_check -s exit:0 -o inline:data -e empty ${TEST_SH} -c \ | |||||
'{ cat >&3; printf file; } <>Output 3>&1 >&0' | |||||
test -f Output || atf_fail "#$T: Removed Output file" | |||||
test -s Output || atf_fail "#$T: Did not keep data in Output file" | |||||
test "$(cat Output)" = "datafile" || | |||||
atf_fail \ | |||||
"#$T: Incorrect Output: Should be 'datafile' is '$(cat Output)'" | |||||
} | |||||
atf_test_case fd_redirections | atf_test_case fd_redirections | ||||
fd_redirections_head() | fd_redirections_head() | ||||
{ | { | ||||
atf_set "descr" "Tests redirections to/from specific descriptors" | atf_set "descr" "Tests redirections to/from specific descriptors" | ||||
} | } | ||||
fd_redirections_body() | fd_redirections_body() | ||||
{ | { | ||||
atf_require_prog /bin/echo | atf_require_prog /bin/echo | ||||
▲ Show 20 Lines • Show All 241 Lines • ▼ Show 20 Lines | incorrect_redirections_body() { | ||||
rm -f Output \> 2>/dev/null || : | rm -f Output \> 2>/dev/null || : | ||||
atf_check -s exit:0 -e empty -o empty \ | atf_check -s exit:0 -e empty -o empty \ | ||||
${TEST_SH} -c 'echo A Line >\> Output' | ${TEST_SH} -c 'echo A Line >\> Output' | ||||
test -f Output && atf_file "File 'Output' appeared and should not have" | test -f Output && atf_file "File 'Output' appeared and should not have" | ||||
test -f '>' || atf_file "File '>' not created when it should" | test -f '>' || atf_file "File '>' not created when it should" | ||||
test "$(cat '>')" = 'A Line Output' || atf_fail \ | test "$(cat '>')" = 'A Line Output' || atf_fail \ | ||||
"Output file ('>') contains '$(cat '>')' instead of 'A Line Output'" | "Output file ('>') contains '$(cat '>')' instead of 'A Line Output'" | ||||
rm -fr OutDir | |||||
atf-check -s not-exit:0 -o empty -e not-empty \ | |||||
${TEST_SH} -c ': > OutDir/stdout; printf foo' | |||||
atf-check -s not-exit:0 -o empty -e not-empty \ | |||||
${TEST_SH} -c ': > OutDir/stdout || printf foo; printf bar' | |||||
atf-check -s exit:0 -o inline:bar -e not-empty \ | |||||
${TEST_SH} -c '> OutDir/stdout; printf bar' | |||||
atf-check -s exit:0 -o inline:foobar -e not-empty \ | |||||
${TEST_SH} -c '> OutDir/stdout || printf foo; printf bar' | |||||
atf-check -s exit:0 -o inline:bar -e not-empty \ | |||||
${TEST_SH} -c 'command : > OutDir/stdout; printf bar' | |||||
atf-check -s exit:0 -o inline:foobar -e not-empty ${TEST_SH} -c \ | |||||
'command : > OutDir/stdout || printf foo; printf bar' | |||||
atf-check -s not-exit:0 -o empty -e not-empty \ | |||||
${TEST_SH} -c ': <> OutDir/stdout; printf foo' | |||||
atf-check -s not-exit:0 -o empty -e not-empty \ | |||||
${TEST_SH} -c ': >&8 ; printf foo' | |||||
atf-check -s not-exit:0 -o empty -e not-empty \ | |||||
${TEST_SH} -c ': >&8 || printf foo; printf bar' | |||||
atf-check -s exit:0 -o inline:bar -e not-empty \ | |||||
${TEST_SH} -c '>&8 ; printf bar' | |||||
atf-check -s exit:0 -o inline:foobar -e not-empty \ | |||||
${TEST_SH} -c '>&8 || printf foo; printf bar' | |||||
atf-check -s exit:0 -o inline:bar -e not-empty \ | |||||
${TEST_SH} -c 'command : >&7; printf bar' | |||||
atf-check -s exit:0 -o inline:foobar -e not-empty ${TEST_SH} -c \ | |||||
'command : >&7 || printf foo; printf bar' | |||||
return 0 | |||||
} | } | ||||
# Many more tests in t_here, so here we have just rudimentary checks | # Many more tests in t_here, so here we have just rudimentary checks | ||||
atf_test_case redir_here_doc | atf_test_case redir_here_doc | ||||
redir_here_doc_head() | redir_here_doc_head() | ||||
{ | { | ||||
atf_set "descr" "Tests that sh(1) correctly processes 'here' doc " \ | atf_set "descr" "Tests that sh(1) correctly processes 'here' doc " \ | ||||
"input redirections" | "input redirections" | ||||
▲ Show 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | f() { | ||||
printf '%s\n' In-Func | printf '%s\n' In-Func | ||||
} | } | ||||
DONE | DONE | ||||
atf_check -s exit:0 -o inline:'In-Func\nsuccess1\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nsuccess1\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; f ; printf '%s\n' success1" | ${TEST_SH} -c ". ./f-def; f ; printf '%s\n' success1" | ||||
atf_check -s exit:0 -o inline:'success2\n' -e empty \ | atf_check -s exit:0 -o inline:'success2\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; f >/dev/null; printf '%s\n' success2" | ${TEST_SH} -c ". ./f-def; f >/dev/null; printf '%s\n' success2" | ||||
atf_check -s exit:0 -o inline:'success3\n' -e empty \ | atf_check -s exit:0 -o inline:'success3\n' -e not-empty \ | ||||
${TEST_SH} -c ". ./f-def; f >&- ; printf '%s\n' success3" | ${TEST_SH} -c ". ./f-def; f >&- ; printf '%s\n' success3" | ||||
atf_check -s exit:0 -o inline:'In-Func\nsuccess4\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nsuccess4\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; f & wait; printf '%s\n' success4" | ${TEST_SH} -c ". ./f-def; f & wait; printf '%s\n' success4" | ||||
atf_check -s exit:0 -o inline:'success5\n' -e empty \ | atf_check -s exit:0 -o inline:'success5\n' -e not-empty \ | ||||
${TEST_SH} -c ". ./f-def; f >&- & wait; printf '%s\n' success5" | ${TEST_SH} -c ". ./f-def; f >&- & wait; printf '%s\n' success5" | ||||
atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess6\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess6\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; f;f; printf '%s\n' success6" | ${TEST_SH} -c ". ./f-def; f;f; printf '%s\n' success6" | ||||
atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess7\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess7\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; { f;f;}; printf '%s\n' success7" | ${TEST_SH} -c ". ./f-def; { f;f;}; printf '%s\n' success7" | ||||
atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess8\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nIn-Func\nsuccess8\n' -e empty \ | ||||
${TEST_SH} -c ". ./f-def; { f;f;}& wait; printf '%s\n' success8" | ${TEST_SH} -c ". ./f-def; { f;f;}& wait; printf '%s\n' success8" | ||||
atf_check -s exit:0 -o inline:'In-Func\nsuccess9\n' -e empty \ | atf_check -s exit:0 -o inline:'In-Func\nsuccess9\n' -e empty \ | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | echo ' | ||||
exec 4>&1 || echo FD_FAIL | exec 4>&1 || echo FD_FAIL | ||||
' | atf_check -s exit:0 -o inline:'fghX Y\nGF' -e empty ${TEST_SH} | ' | atf_check -s exit:0 -o inline:'fghX Y\nGF' -e empty ${TEST_SH} | ||||
} | } | ||||
atf_init_test_cases() { | atf_init_test_cases() { | ||||
atf_add_test_case basic_test_method_test | atf_add_test_case basic_test_method_test | ||||
atf_add_test_case do_input_redirections | atf_add_test_case do_input_redirections | ||||
atf_add_test_case do_output_redirections | atf_add_test_case do_output_redirections | ||||
atf_add_test_case do_redirect_input_output | |||||
atf_add_test_case fd_redirections | atf_add_test_case fd_redirections | ||||
atf_add_test_case local_redirections | atf_add_test_case local_redirections | ||||
atf_add_test_case incorrect_redirections | atf_add_test_case incorrect_redirections | ||||
atf_add_test_case named_fd_redirections | atf_add_test_case named_fd_redirections | ||||
atf_add_test_case redir_here_doc | atf_add_test_case redir_here_doc | ||||
atf_add_test_case redir_in_case | atf_add_test_case redir_in_case | ||||
atf_add_test_case subshell_redirections | atf_add_test_case subshell_redirections | ||||
atf_add_test_case ulimit_redirection_interaction | atf_add_test_case ulimit_redirection_interaction | ||||
atf_add_test_case validate_fn_redirects | atf_add_test_case validate_fn_redirects | ||||
} | } |