Skip to content

Commit

Permalink
✨ implementing io::invokePiped
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 24, 2024
1 parent 541dabf commit 186dd76
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 59 deletions.
6 changes: 6 additions & 0 deletions tests.d/1002-lib-kurl/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ Standard stream from file: ⌜false⌝
Standard stream: ⌜⌝
Extra parameters: ⌜--silent --show-error --location --write-out %{http_code} --output /tmp/valet.d/kurl-test --code 400 --error https://hello.com/bla --otherOpt⌝
DEBUG The command ⌜curl⌝ originally ended with exit code ⌜1⌝.
________
Standard output:
⌜400⌝
________
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet.d/kurl-test --code 400 --error https://hello.com/bla --otherOpt
Returning 1 from curl.
Expand Down Expand Up @@ -142,8 +144,10 @@ Standard stream: ⌜⌝
Extra parameters: ⌜--silent --show-error --location --write-out %{http_code} --output /tmp/valet.d/kurl-test --code 200 http://hello.com⌝
DEBUG The command ⌜curl⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜200⌝
________
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet.d/kurl-test --code 200 http://hello.com
Expand Down Expand Up @@ -217,8 +221,10 @@ Standard stream: ⌜⌝
Extra parameters: ⌜--silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 400 http://hello.com⌝
DEBUG The command ⌜curl⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜400⌝
________
Error output:
⌜▶ called curl --silent --show-error --location --write-out %{http_code} --output /tmp/valet-work --code 400 http://hello.com
Expand Down
13 changes: 0 additions & 13 deletions tests.d/1005-lib-io/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ function outputTextToStdErr() {
echo "This is an error message" 1>&2
}

function testIo::captureOutput() {

echo "→ io::captureOutput echo \"Hello world!\""
io::captureOutput echo "Hello world!" && echo "${LAST_RETURNED_VALUE}"

echo
echo "→ io::captureOutput outputTextToStdErr"
io::captureOutput outputTextToStdErr && echo "${LAST_RETURNED_VALUE2}"

endTest "Testing io::captureOutput" 0
}

function testIo::readFile() {

echo "→ io::readFile 'resources/file-to-read' 100"
Expand Down Expand Up @@ -85,7 +73,6 @@ function testIo::cat() {

function main() {
testIo::toAbsolutePath
testIo::captureOutput
testIo::readFile
testIo::createFilePathIfNeeded
testIo::sleep
Expand Down
10 changes: 10 additions & 0 deletions tests.d/1005-lib-io/01.invoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function testIo::invoke() {
endTest "Testing io::invoke, output to var" ${exitCode}
}

function testIo::invokePiped() {
local -i exitCode

echo "→ io::invokePiped 'this is an stdin' fakeexec --std-in --option argument1 argument2"
io::invokePiped 'this is an stdin' fakeexec --std-in --option argument1 argument2 && exitCode=0 || exitCode=$?
echoio::invokeOutput ${exitCode} false
endTest "Testing io::invokePiped, stdin as string, output to var" ${exitCode}
}

function echoio::invokeOutput() {
local exitCode areFiles
exitCode="${1}"
Expand Down Expand Up @@ -104,6 +113,7 @@ function main() {
testIo::invoke5
testIo::invoke3
testIo::invoke
testIo::invokePiped
}

main
71 changes: 55 additions & 16 deletions tests.d/1005-lib-io/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh
$GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh
```

### Testing io::captureOutput

Exit code: `0`

**Standard** output:

```plaintext
→ io::captureOutput echo "Hello world!"
Hello world!
→ io::captureOutput outputTextToStdErr
This is an error message
```

### Testing io::readFile limited to x chars

Exit code: `0`
Expand Down Expand Up @@ -177,11 +161,13 @@ exitcode=1

```log
ERROR The command ⌜fakeexec⌝ originally ended with exit code ⌜1⌝.
________
Standard output:
⌜▶ called fakeexec --std-in --error
▶ fakeexec input stream was:
⌈inputStreamValue⌉
________
Error output:
⌜This is an error output from fakeexec
returning 1 from fakeexec
Expand Down Expand Up @@ -258,11 +244,13 @@ Standard stream from file: ⌜false⌝
Standard stream: ⌜inputStreamValue⌝
Extra parameters: ⌜--std-in --error⌝
DEBUG The command ⌜fakeexec⌝ originally ended with exit code ⌜1⌝.
________
Standard output:
⌜▶ called fakeexec --std-in --error
▶ fakeexec input stream was:
⌈inputStreamValue⌉
________
Error output:
⌜This is an error output from fakeexec
returning 1 from fakeexec
Expand Down Expand Up @@ -298,11 +286,13 @@ Standard stream: ⌜⌝
Extra parameters: ⌜--option argument1 argument2⌝
DEBUG The command ⌜fakeexec⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜▶ called fakeexec --option argument1 argument2
▶ fakeexec input stream was:
⌈⌉
________
Error output:
⌜This is an error output from fakeexec
Expand Down Expand Up @@ -339,11 +329,13 @@ Standard stream: ⌜⌝
Extra parameters: ⌜--option argument1 argument2⌝
DEBUG The command ⌜fakeexec⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜▶ called fakeexec --option argument1 argument2
▶ fakeexec input stream was:
⌈⌉
________
Error output:
⌜This is an error output from fakeexec
Expand All @@ -369,11 +361,13 @@ Standard stream from file: ⌜⌝
Standard stream: ⌜⌝
Extra parameters: ⌜--error⌝
ERROR The command ⌜fakeexec⌝ originally ended with exit code ⌜1⌝.
________
Standard output:
⌜▶ called fakeexec --error
▶ fakeexec input stream was:
⌈⌉
________
Error output:
⌜This is an error output from fakeexec
returning 1 from fakeexec
Expand Down Expand Up @@ -427,11 +421,56 @@ Standard stream: ⌜⌝
Extra parameters: ⌜--option argument1 argument2⌝
DEBUG The command ⌜fakeexec⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜▶ called fakeexec --option argument1 argument2
▶ fakeexec input stream was:
⌈⌉
________
Error output:
⌜This is an error output from fakeexec
```

### Testing io::invokePiped, stdin as string, output to var

Exit code: `0`

**Standard** output:

```plaintext
→ io::invokePiped 'this is an stdin' fakeexec --std-in --option argument1 argument2
io::invoke function ended with exit code ⌈0⌉.
stdout from var:
⌈▶ called fakeexec --std-in --option argument1 argument2
▶ fakeexec input stream was:
⌈this is an stdin⌉
stderr from var:
⌈This is an error output from fakeexec
```

**Error** output:

```log
DEBUG Executing the command ⌜fakeexec⌝.
Fail if it fails: ⌜true⌝
Acceptable error codes: ⌜0⌝
Standard stream from file: ⌜false⌝
Standard stream: ⌜this is an stdin⌝
Extra parameters: ⌜--std-in --option argument1 argument2⌝
DEBUG The command ⌜fakeexec⌝ originally ended with exit code ⌜0⌝.
The error code ⌜0⌝ is acceptable and has been reset to 0.
________
Standard output:
⌜▶ called fakeexec --std-in --option argument1 argument2
▶ fakeexec input stream was:
⌈this is an stdin⌉
________
Error output:
⌜This is an error output from fakeexec
Expand Down
2 changes: 1 addition & 1 deletion valet.d/commands.d/self-build-utils
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ _CMD_INCLUDED=1
# CMD_ARGS_NB_OPTIONAL_xxx = integer to indicate the number of optional arguments (contains ?)
"
io::captureOutput declare -p ${!CMD_*}
io::invoke declare -p ${!CMD_*}
local IFS=$'\n'
local line
local IFS=':'
Expand Down
2 changes: 1 addition & 1 deletion valet.d/commands.d/self-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function extractCommandDefinitionsToVariables() {
array::makeArraysSameSize TEMP_CMD_BUILD_examples_name TEMP_CMD_BUILD_examples_description

if log::isDebugEnabled; then
io::captureOutput declare -p ${!TEMP_CMD_BUILD_*}
io::invoke declare -p ${!TEMP_CMD_BUILD_*}
log::debug "Declared variables for this command:"$'\n'"${LAST_RETURNED_VALUE}"
fi

Expand Down
13 changes: 8 additions & 5 deletions valet.d/commands.d/self-test-utils
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,28 @@ function onExitTestInternal() {
log::getCallStack 4 && local stack="${LAST_RETURNED_VALUE}"

local message="The program has exit during a test with code ${rc}, stack:${stack}"$'\n'
message+="________"$'\n'
if [[ -s "${_TEST_REPORT_FILE}" ]]; then
io::readFile "${_TEST_REPORT_FILE}"
message+="Current test report:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
message+="Current test report:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
else
message+="No test report yet for the current test."$'\n'
fi
message+="________"$'\n'
if [[ -s "${_TEST_STANDARD_OUTPUT_FILE}" ]]; then
io::readFile "${_TEST_STANDARD_OUTPUT_FILE}"
message+="Current test standard output:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
message+="Current test standard output:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
else
message+="No standard output for the current test."$'\n'
fi
message+="________"$'\n'
if [[ -s "${_TEST_STANDARD_ERROR_FILE}" ]]; then
io::readFile "${_TEST_STANDARD_ERROR_FILE}"
message+="Current test error output:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
message+="Current test error output:"$'\n'"${LAST_RETURNED_VALUE}"$'\n'
else
message+="No error output for the current test."$'\n'
fi
message+="If you expect the tested function/program to exit/fail, then run it in a subshell like that:"$'\n'"(myFunctionThatFails)"
message+="If you expect the tested function/program to exit or fail, then run it in a subshell like that:"$'\n'"(myFunctionThatFails)"

log::error "${message}"

Expand Down Expand Up @@ -413,7 +416,7 @@ function resetFdRedirection() {
# valet config and global variables to their original values.
# As well as the log line function.
function setOriginalImportantVariables() {
io::captureOutput declare -p ${!VALET_CONFIG_*} ${!GLOBAL_*}
io::invoke declare -p ${!VALET_CONFIG_*} ${!GLOBAL_*}
ORIGINAL_IMPORTANT_VARIABLES="${GLOBAL_LOG_PRINT_FUNCTION}"$'\n'"${LAST_RETURNED_VALUE//declare -? /}"
}

Expand Down
4 changes: 3 additions & 1 deletion valet.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ function log::createPrintFunction() {
icon="${2}"
severity="${3}"
shift 3
LAST_RETURNED_VALUE="$*"'
local IFS='"$'\n'"'
LAST_RETURNED_VALUE="$*"
unset IFS'

local cDefault=""
local cTimestamp=""
Expand Down
2 changes: 1 addition & 1 deletion valet.d/lib-fsfs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function fsfs::itemSelector() {

# save the original traps so we can restore them later
local originalTraps
io::captureOutput trap -p SIGWINCH EXIT SIGINT SIGQUIT
io::invoke trap -p SIGWINCH EXIT SIGINT SIGQUIT
originalTraps="${LAST_RETURNED_VALUE}"

# we still need to export the terminal size but in addition, we need to fsfsDrawScreen.
Expand Down
Loading

0 comments on commit 186dd76

Please sign in to comment.