From 0bb6e5716a65e4d4b3e72a5ca78752f0c13c7143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chastanet?= Date: Sun, 7 Jan 2024 23:08:10 +0100 Subject: [PATCH] fixed UT flakiness - using Bash::handlePipelineFailure - removed usage of Embeded binaries in dbScriptAllDatabases and dbQueryAllDatabases --- bin/cli | 2 +- bin/dbImport | 2 +- bin/dbImportProfile | 2 +- bin/dbImportStream | 2 +- bin/dbQueryAllDatabases | 97 ++++++++++++++++++- bin/dbScriptAllDatabases | 97 ++++++++++++++++++- bin/doc | 2 +- bin/gitIsAncestorOf | 2 +- bin/gitIsBranch | 2 +- bin/gitRenameBranch | 2 +- bin/installRequirements | 2 +- bin/mysql2puml | 4 +- bin/postmanCli | 53 +++++++++- bin/upgradeGithubRelease | 4 +- bin/waitForIt | 4 +- bin/waitForMysql | 4 +- install | 2 +- src/Postman/Model/validate.bats | 15 ++- src/Postman/api.bats | 2 + src/Postman/api.sh | 8 +- src/_binaries/Postman/command.postmanCli.tpl | 20 ++-- .../Postman/testsData/postmanCli.help.txt | 8 +- 22 files changed, 288 insertions(+), 48 deletions(-) diff --git a/bin/cli b/bin/cli index f6f44efb..b0abddf5 100755 --- a/bin/cli +++ b/bin/cli @@ -1646,7 +1646,7 @@ cliCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/dbImport b/bin/dbImport index 093839f7..a810f18f 100755 --- a/bin/dbImport +++ b/bin/dbImport @@ -2202,7 +2202,7 @@ dbImportCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/dbImportProfile b/bin/dbImportProfile index be0dc3ce..a9564c9d 100755 --- a/bin/dbImportProfile +++ b/bin/dbImportProfile @@ -1781,7 +1781,7 @@ dbImportProfileCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/dbImportStream b/bin/dbImportStream index e13e9a6b..1e34e5db 100755 --- a/bin/dbImportStream +++ b/bin/dbImportStream @@ -2014,7 +2014,7 @@ dbImportStreamCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/dbQueryAllDatabases b/bin/dbQueryAllDatabases index ae0cb7e8..b26beb15 100755 --- a/bin/dbQueryAllDatabases +++ b/bin/dbQueryAllDatabases @@ -750,6 +750,96 @@ Version::checkMinimal() { } +bashToolsDefaultConfigTemplate="${bashToolsDefaultConfigTemplate:-$( + cat <<'EOF' +# shellcheck disable=SC2034 + +# Default settings +# you can override these settings by creating ${HOME}/.bash-tools/.env file +### +### LOG Level +### minimum level of the messages that will be logged into LOG_FILE +### +### 0: NO LOG +### 1: ERROR +### 2: WARNING +### 3: INFO +### 4: DEBUG +### +BASH_FRAMEWORK_LOG_LEVEL=${BASH_FRAMEWORK_LOG_LEVEL:-0} + +### +### DISPLAY Level +### minimum level of the messages that will be displayed on screen +### +### 0: NO LOG +### 1: ERROR +### 2: WARNING +### 3: INFO +### 4: DEBUG +### +BASH_FRAMEWORK_DISPLAY_LEVEL=${BASH_FRAMEWORK_DISPLAY_LEVEL:-3} + +### +### Log to file +### +### all log messages will be redirected to log file specified +### this same path will be used inside and outside of the container +### +BASH_FRAMEWORK_LOG_FILE=${BASH_FRAMEWORK_LOG_FILE:-${FRAMEWORK_ROOT_DIR}/logs/bash.log} + +# absolute directory containing db import sql dumps +DB_IMPORT_DUMP_DIR=${DB_IMPORT_DUMP_DIR:-${HOME}/.bash-tools/dbImportDumps} + +# garbage collect all files for which modification is greater than eg: 30 days (+30) +# each time an existing file is used by dbImport/dbImportTable +# the file modification time is set to now +DB_IMPORT_GARBAGE_COLLECT_DAYS=${DB_IMPORT_GARBAGE_COLLECT_DAYS:-+30} + +# absolute directory containing dbScripts used by dbScriptAllDatabases +SCRIPTS_FOLDER=${SCRIPTS_FOLDER:-${HOME}/.bash-tools/conf/dbScripts} + +# ----------------------------------------------------- +# AWS Parameters +# ----------------------------------------------------- +S3_BASE_URL=${S3_BASE_URL:-} + +# ----------------------------------------------------- +# Postman Parameters +# ----------------------------------------------------- +POSTMAN_API_KEY= +EOF +)}" + +# @description loads ~/.bash-tools/.env if available +# if not creates it from a default template +# else check if new options need to be added +BashTools::Conf::requireLoad() { + local envFile="${HOME}/.bash-tools/.env" + if [[ ! -f "${envFile}" ]]; then + mkdir -p "${HOME}/.bash-tools" + ( + echo "#!/usr/bin/env bash" + echo "${bashToolsDefaultConfigTemplate}" + ) >"${envFile}" + Log::displayInfo "Configuration file '${envFile}' created" + else + if ! grep -q '^POSTMAN_API_KEY=' "${envFile}"; then + ( + echo '# -----------------------------------------------------' + echo '# Postman Parameters' + echo '# -----------------------------------------------------' + echo 'POSTMAN_API_KEY=' + ) >>"${envFile}" + fi + fi + # shellcheck source=/conf/.env + source "${envFile}" || { + Log::displayError "impossible to load '${envFile}'" + exit 1 + } +} + # @description ensure COMMAND_BIN_DIR env var is set # and PATH correctly prepared # @noargs @@ -1152,6 +1242,7 @@ Env::requireLoad UI::requireTheme Linux::requireRealpathCommand Log::requireLoad +BashTools::Conf::requireLoad Compiler::Facade::requireCommandBinDir # @require Compiler::Facade::requireCommandBinDir @@ -1836,7 +1927,7 @@ dbQueryAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(specify\ the\ number\ of\ db\ to\ query\ in\ parallel) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: 1" + echo ' Default value: 1' echo -e " ${__HELP_OPTION_COLOR}--bar${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-b${__HELP_NORMAL} {single}" local -a helpArray # shellcheck disable=SC2054 @@ -1847,7 +1938,7 @@ dbQueryAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(character\ to\ use\ to\ separate\ mysql\ column) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: |" + echo ' Default value: |' echo echo -e "${__HELP_TITLE_COLOR}QUERY OPTIONS:${__RESET_COLOR}" echo -e " ${__HELP_OPTION_COLOR}--from-dsn${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-f ${__HELP_NORMAL} {single}" @@ -1897,7 +1988,7 @@ dbQueryAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/dbScriptAllDatabases b/bin/dbScriptAllDatabases index 006cb424..99c7e4ac 100755 --- a/bin/dbScriptAllDatabases +++ b/bin/dbScriptAllDatabases @@ -673,6 +673,96 @@ UI::theme() { fi } +bashToolsDefaultConfigTemplate="${bashToolsDefaultConfigTemplate:-$( + cat <<'EOF' +# shellcheck disable=SC2034 + +# Default settings +# you can override these settings by creating ${HOME}/.bash-tools/.env file +### +### LOG Level +### minimum level of the messages that will be logged into LOG_FILE +### +### 0: NO LOG +### 1: ERROR +### 2: WARNING +### 3: INFO +### 4: DEBUG +### +BASH_FRAMEWORK_LOG_LEVEL=${BASH_FRAMEWORK_LOG_LEVEL:-0} + +### +### DISPLAY Level +### minimum level of the messages that will be displayed on screen +### +### 0: NO LOG +### 1: ERROR +### 2: WARNING +### 3: INFO +### 4: DEBUG +### +BASH_FRAMEWORK_DISPLAY_LEVEL=${BASH_FRAMEWORK_DISPLAY_LEVEL:-3} + +### +### Log to file +### +### all log messages will be redirected to log file specified +### this same path will be used inside and outside of the container +### +BASH_FRAMEWORK_LOG_FILE=${BASH_FRAMEWORK_LOG_FILE:-${FRAMEWORK_ROOT_DIR}/logs/bash.log} + +# absolute directory containing db import sql dumps +DB_IMPORT_DUMP_DIR=${DB_IMPORT_DUMP_DIR:-${HOME}/.bash-tools/dbImportDumps} + +# garbage collect all files for which modification is greater than eg: 30 days (+30) +# each time an existing file is used by dbImport/dbImportTable +# the file modification time is set to now +DB_IMPORT_GARBAGE_COLLECT_DAYS=${DB_IMPORT_GARBAGE_COLLECT_DAYS:-+30} + +# absolute directory containing dbScripts used by dbScriptAllDatabases +SCRIPTS_FOLDER=${SCRIPTS_FOLDER:-${HOME}/.bash-tools/conf/dbScripts} + +# ----------------------------------------------------- +# AWS Parameters +# ----------------------------------------------------- +S3_BASE_URL=${S3_BASE_URL:-} + +# ----------------------------------------------------- +# Postman Parameters +# ----------------------------------------------------- +POSTMAN_API_KEY= +EOF +)}" + +# @description loads ~/.bash-tools/.env if available +# if not creates it from a default template +# else check if new options need to be added +BashTools::Conf::requireLoad() { + local envFile="${HOME}/.bash-tools/.env" + if [[ ! -f "${envFile}" ]]; then + mkdir -p "${HOME}/.bash-tools" + ( + echo "#!/usr/bin/env bash" + echo "${bashToolsDefaultConfigTemplate}" + ) >"${envFile}" + Log::displayInfo "Configuration file '${envFile}' created" + else + if ! grep -q '^POSTMAN_API_KEY=' "${envFile}"; then + ( + echo '# -----------------------------------------------------' + echo '# Postman Parameters' + echo '# -----------------------------------------------------' + echo 'POSTMAN_API_KEY=' + ) >>"${envFile}" + fi + fi + # shellcheck source=/conf/.env + source "${envFile}" || { + Log::displayError "impossible to load '${envFile}'" + exit 1 + } +} + # @description ensure COMMAND_BIN_DIR env var is set # and PATH correctly prepared # @noargs @@ -999,6 +1089,7 @@ Env::requireLoad UI::requireTheme Linux::requireRealpathCommand Log::requireLoad +BashTools::Conf::requireLoad Compiler::Facade::requireCommandBinDir Linux::requireExecutedAsUser @@ -1723,7 +1814,7 @@ dbScriptAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(specify\ the\ number\ of\ db\ to\ query\ in\ parallel) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: 1" + echo ' Default value: 1' echo -e " ${__HELP_OPTION_COLOR}--bar${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-b${__HELP_NORMAL} {single}" local -a helpArray # shellcheck disable=SC2054 @@ -1751,7 +1842,7 @@ dbScriptAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(if\ output\ dir\ provided\,\ will\ log\ each\ db\ result\ to\ log\ file) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: none" + echo ' Default value: none' echo ' Possible values: none|log' echo echo -e "${__HELP_TITLE_COLOR}GLOBAL OPTIONS:${__RESET_COLOR}" @@ -1795,7 +1886,7 @@ dbScriptAllDatabasesCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/doc b/bin/doc index aa5a2e9a..17f60051 100755 --- a/bin/doc +++ b/bin/doc @@ -1624,7 +1624,7 @@ docCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/gitIsAncestorOf b/bin/gitIsAncestorOf index e06cffdb..3c659635 100755 --- a/bin/gitIsAncestorOf +++ b/bin/gitIsAncestorOf @@ -1374,7 +1374,7 @@ gitIsAncestorOfCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/gitIsBranch b/bin/gitIsBranch index ca046c09..d0fae218 100755 --- a/bin/gitIsBranch +++ b/bin/gitIsBranch @@ -1352,7 +1352,7 @@ gitIsBranchCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/gitRenameBranch b/bin/gitRenameBranch index 40a9f301..f6471c78 100755 --- a/bin/gitRenameBranch +++ b/bin/gitRenameBranch @@ -1458,7 +1458,7 @@ gitRenameBranchCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/installRequirements b/bin/installRequirements index b5d0f779..1c5bb714 100755 --- a/bin/installRequirements +++ b/bin/installRequirements @@ -1423,7 +1423,7 @@ installRequirementsCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/mysql2puml b/bin/mysql2puml index aaa106cb..c5c6a79f 100755 --- a/bin/mysql2puml +++ b/bin/mysql2puml @@ -1525,7 +1525,7 @@ mysql2pumlCommand() { # shellcheck disable=SC2054 helpArray=(header\ configuration\ of\ the\ plant\ uml\ file) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo echo -e "${__HELP_TITLE_COLOR}GLOBAL OPTIONS:${__RESET_COLOR}" echo -e " ${__HELP_OPTION_COLOR}--bash-framework-config ${__HELP_NORMAL} {single}" @@ -1568,7 +1568,7 @@ mysql2pumlCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/postmanCli b/bin/postmanCli index b360eab6..09f6440d 100755 --- a/bin/postmanCli +++ b/bin/postmanCli @@ -1152,6 +1152,8 @@ Postman::api() { responseFile="$(Framework::createTempFile)" local status=0 + # shellcheck disable=SC2034 + local -a pipeStatus=() getCollectionDataFromFile "${collectionFile}" | curl \ --request POST https://api.getpostman.com/collections \ @@ -1160,7 +1162,7 @@ Postman::api() { --header 'Accept: application/json' \ --header "X-Api-Key: ${POSTMAN_API_KEY}" \ --data @- \ - --fail --silent --show-error || status=$? + --fail --silent --show-error || Bash::handlePipelineFailure status pipeStatus Postman::displayResponse "createCollectionFromFile" "${responseFile}" @@ -1174,6 +1176,8 @@ Postman::api() { responseFile="$(Framework::createTempFile)" local status=0 + # shellcheck disable=SC2034 + local -a pipeStatus=() getCollectionDataFromFile "${collectionFile}" | curl \ --request PUT "https://api.getpostman.com/collections/${collectionId}" \ @@ -1182,7 +1186,7 @@ Postman::api() { --header 'Accept: application/json' \ --header "X-Api-Key: ${POSTMAN_API_KEY}" \ --data @- \ - --fail --silent --show-error || status=$? + --fail --silent --show-error || Bash::handlePipelineFailure status pipeStatus Postman::displayResponse "updateCollectionFromFile" "${responseFile}" @@ -1217,6 +1221,36 @@ Postman::api() { esac } +# @description ignore exit code 141 from simple command pipes +# @example use with: +# local resultingStatus=0 +# local -a originalPipeStatus=() +# cmd1 | cmd2 || Bash::handlePipelineFailure resultingStatus originalPipeStatus || true +# [[ "${resultingStatus}" = "0" ]] +# @arg $1 resultingStatusCode:&int (passed by reference) (optional) resulting status code +# @arg $2 originalStatus:int[] (passed by reference) (optional) copy of original PIPESTATUS array +# @env PIPESTATUS assuming that this function is called like in the example provided +# @see https://unix.stackexchange.com/a/709880/582856 +Bash::handlePipelineFailure() { + local -a pipeStatusBackup=("${PIPESTATUS[@]}") + local -n handlePipelineFailure_resultingStatusCode=$1 + local -n handlePipelineFailure_originalStatus=$2 + # shellcheck disable=SC2034 + handlePipelineFailure_originalStatus=("${pipeStatusBackup[@]}") + handlePipelineFailure_resultingStatusCode=0 + local statusCode + for statusCode in "${pipeStatusBackup[@]}"; do + if ((statusCode == 141)); then + return 0 + elif ((statusCode > 0)); then + # shellcheck disable=SC2034 + handlePipelineFailure_resultingStatusCode="${statusCode}" + break + fi + done + return "${handlePipelineFailure_resultingStatusCode}" +} + # @description log message to file # @arg $1 message:String the message to display Log::logSkipped() { @@ -1588,6 +1622,14 @@ commandOptionParseFinished() { displayConfig fi } +NBSP="\xc2\xa0" +NBSP2="${NBSP}${NBSP}" +argCommandHelp() { + echo -e "${__HELP_OPTION_COLOR}pull${__HELP_NORMAL}" + echo -e "${NBSP2}Pull collections from Postman back to repositories.\r" + echo -e "${__HELP_OPTION_COLOR}push${__HELP_NORMAL}" + echo -e "${NBSP2}Push repositories collections to Postman." +} postmanCliCommand() { local options_parse_cmd="$1" @@ -1921,8 +1963,8 @@ postmanCliCommand() { echo -e "${__HELP_TITLE_COLOR}ARGUMENTS:${__RESET_COLOR}" echo -e " [${__HELP_OPTION_COLOR}command${__HELP_NORMAL} {single}]" local -a helpArray - # shellcheck disable=SC2054 - helpArray=($'\e[1;34mpull\n\n \xc2\xa0\xc2\xa0Pull collections from Postman back to repositories.\n\n \e[1;34mpush\n\n \xc2\xa0\xc2\xa0Push repositories collections to Postman.\n') + # shellcheck disable=SC2054,SC2206 + mapfile -t helpArray < <(argCommandHelp) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" echo -e " [${__HELP_OPTION_COLOR}commandArgs${__HELP_NORMAL} {list} (optional)]" local -a helpArray @@ -1978,7 +2020,7 @@ postmanCliCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray @@ -2032,6 +2074,7 @@ postmanCliCommand() { fi } declare optionPostmanModelConfig="$(pwd -P)/postmanCli.collections.json" +declare copyrightBeginYear="2023" # shellcheck disable=SC2317 # if function is overridden unknownOption() { diff --git a/bin/upgradeGithubRelease b/bin/upgradeGithubRelease index a62e0ce6..fe69d18d 100755 --- a/bin/upgradeGithubRelease +++ b/bin/upgradeGithubRelease @@ -1733,7 +1733,7 @@ upgradeGithubReleaseCommand() { # shellcheck disable=SC2054 helpArray=($'The argument that will be provided to the currently installed binary\n\n to check the version of the software. \n\n See options constraints below.') echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: --version" + echo ' Default value: --version' echo -e " ${__HELP_OPTION_COLOR}--current-version${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-c ${__HELP_NORMAL} {single}" local -a helpArray # shellcheck disable=SC2054 @@ -1791,7 +1791,7 @@ upgradeGithubReleaseCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/waitForIt b/bin/waitForIt index 2f525963..8bb49f99 100755 --- a/bin/waitForIt +++ b/bin/waitForIt @@ -1451,7 +1451,7 @@ waitForItCommand() { # shellcheck disable=SC2054 helpArray=(Timeout\ in\ seconds\,\ zero\ for\ no\ timeout.) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: 15" + echo ' Default value: 15' echo -e " ${__HELP_OPTION_COLOR}--host${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-i ${__HELP_NORMAL} {single} (mandatory)" local -a helpArray # shellcheck disable=SC2054 @@ -1514,7 +1514,7 @@ waitForItCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/bin/waitForMysql b/bin/waitForMysql index 4b7d479a..6f601176 100755 --- a/bin/waitForMysql +++ b/bin/waitForMysql @@ -1427,7 +1427,7 @@ waitForMysqlCommand() { # shellcheck disable=SC2054 helpArray=(Timeout\ in\ seconds\,\ zero\ for\ no\ timeout.) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: 15" + echo ' Default value: 15' echo echo -e "${__HELP_TITLE_COLOR}GLOBAL OPTIONS:${__RESET_COLOR}" echo -e " ${__HELP_OPTION_COLOR}--bash-framework-config ${__HELP_NORMAL} {single}" @@ -1470,7 +1470,7 @@ waitForMysqlCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/install b/install index 2cf9a935..b6784644 100755 --- a/install +++ b/install @@ -1325,7 +1325,7 @@ installCommand() { # shellcheck disable=SC2054 helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped) echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")" - echo " Default value: default" + echo ' Default value: default' echo ' Possible values: default|default-force|noColor' echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}" local -a helpArray diff --git a/src/Postman/Model/validate.bats b/src/Postman/Model/validate.bats index 77255f80..4747f4a3 100755 --- a/src/Postman/Model/validate.bats +++ b/src/Postman/Model/validate.bats @@ -11,6 +11,10 @@ setup() { export BASH_FRAMEWORK_THEME="noColor" } +teardown() { + chmod -R u+w "${BATS_TEST_TMPDIR}/pullMode" 2>/dev/null || true +} + function Postman::Model::validate::missingMode { #@test run Postman::Model::validate \ "fileNotFound.json" "invalidMode" @@ -89,17 +93,20 @@ function Postman::Model::validate::pushModeCollectionsWithErrors { #@test } function Postman::Model::validate::pullModeCollectionsWithErrors { #@test + cp -R "${BATS_TEST_DIRNAME}/testsData/pullMode" "${BATS_TEST_TMPDIR}" + chmod -w "${BATS_TEST_TMPDIR}/pullMode/GithubAPI/notWritableFile.json" + chmod -w "${BATS_TEST_TMPDIR}/pullMode" Postman::Model::getRelativeConfigDirectory() { - echo "${BATS_TEST_DIRNAME}/testsData/pullMode" + echo "${BATS_TEST_TMPDIR}/pullMode" } - local file="${BATS_TEST_DIRNAME}/testsData/pullMode/getCollectionRefs-collectionsWithErrors.json" + local file="${BATS_TEST_TMPDIR}/pullMode/getCollectionRefs-collectionsWithErrors.json" run Postman::Model::validate \ "${file}" "pull" assert_failure 1 assert_line --index 0 "ERROR - File '${file}' - collection 0 - missing file property" - assert_line --index 1 "ERROR - File '${file}' - collection fileNotWritable - collection file ${BATS_TEST_DIRNAME}/testsData/pullMode/GithubAPI/notWritableFile.json is not writable" - assert_line --index 2 "ERROR - File '${file}' - collection dirNotWritable - config directory ${BATS_TEST_DIRNAME}/testsData/pullMode is not writable" + assert_line --index 1 "ERROR - File '${file}' - collection fileNotWritable - collection file ${BATS_TEST_TMPDIR}/pullMode/GithubAPI/notWritableFile.json is not writable" + assert_line --index 2 "ERROR - File '${file}' - collection dirNotWritable - config directory ${BATS_TEST_TMPDIR}/pullMode is not writable" assert_lines_count 3 } diff --git a/src/Postman/api.bats b/src/Postman/api.bats index add09791..43e3b429 100755 --- a/src/Postman/api.bats +++ b/src/Postman/api.bats @@ -4,6 +4,8 @@ source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh" # shellcheck source=src/Postman/api.sh source "${rootDir}/src/Postman/api.sh" +# shellcheck source=vendor/bash-tools-framework/src/Bash/handlePipelineFailure.sh +source "${FRAMEWORK_ROOT_DIR}/src/Bash/handlePipelineFailure.sh" setup() { export BASH_FRAMEWORK_THEME="noColor" diff --git a/src/Postman/api.sh b/src/Postman/api.sh index 817f46bc..f3980587 100755 --- a/src/Postman/api.sh +++ b/src/Postman/api.sh @@ -30,6 +30,8 @@ Postman::api() { responseFile="$(Framework::createTempFile)" local status=0 + # shellcheck disable=SC2034 + local -a pipeStatus=() getCollectionDataFromFile "${collectionFile}" | curl \ --request POST https://api.getpostman.com/collections \ @@ -38,7 +40,7 @@ Postman::api() { --header 'Accept: application/json' \ --header "X-Api-Key: ${POSTMAN_API_KEY}" \ --data @- \ - --fail --silent --show-error || status=$? + --fail --silent --show-error || Bash::handlePipelineFailure status pipeStatus Postman::displayResponse "createCollectionFromFile" "${responseFile}" @@ -52,6 +54,8 @@ Postman::api() { responseFile="$(Framework::createTempFile)" local status=0 + # shellcheck disable=SC2034 + local -a pipeStatus=() getCollectionDataFromFile "${collectionFile}" | curl \ --request PUT "https://api.getpostman.com/collections/${collectionId}" \ @@ -60,7 +64,7 @@ Postman::api() { --header 'Accept: application/json' \ --header "X-Api-Key: ${POSTMAN_API_KEY}" \ --data @- \ - --fail --silent --show-error || status=$? + --fail --silent --show-error || Bash::handlePipelineFailure status pipeStatus Postman::displayResponse "updateCollectionFromFile" "${responseFile}" diff --git a/src/_binaries/Postman/command.postmanCli.tpl b/src/_binaries/Postman/command.postmanCli.tpl index 6f5a9151..eb87dccf 100644 --- a/src/_binaries/Postman/command.postmanCli.tpl +++ b/src/_binaries/Postman/command.postmanCli.tpl @@ -2,15 +2,21 @@ declare versionNumber="1.0" declare commandFunctionName="postmanCliCommand" declare help="Push/Pull postman collections of all the configured repositories" -declare copyrightBeginYear="2023" # shellcheck disable=SC2016 % .INCLUDE "$(dynamicTemplateDir _binaries/options/options.base.tpl)" -% - NBSP="\xc2\xa0" NBSP2="${NBSP}${NBSP}" +argCommandHelp() { + echo -e "${__HELP_OPTION_COLOR}pull${__HELP_NORMAL}" + echo -e "${NBSP2}Pull collections from Postman back to repositories." $'\r' + echo -e "${__HELP_OPTION_COLOR}push${__HELP_NORMAL}" + echo -e "${NBSP2}Push repositories collections to Postman." +} + +% + # shellcheck source=/dev/null source <( Options::generateGroup \ @@ -28,17 +34,14 @@ source <( --variable-name "optionPostmanModelConfig" \ --function-name optionPostmanModelConfigFunction + argCommandHelp() { :; } Options::generateArg \ --variable-name "argCommand" \ --min 0 \ --max 1 \ --name "command" \ --authorized-values 'pull|push' \ - --help \ - $'${__HELP_OPTION_COLOR}pull${__HELP_NORMAL}\n - ${NBSP2}Pull collections from Postman back to repositories.\n - ${__HELP_OPTION_COLOR}push${__HELP_NORMAL}\n - ${NBSP2}Push repositories collections to Postman.\n' \ + --help argCommandHelp \ --function-name argCommandFunction Options::generateArg \ @@ -63,6 +66,7 @@ options+=( Options::generateCommand "${options[@]}" % declare optionPostmanModelConfig="$(pwd -P)/postmanCli.collections.json" +declare copyrightBeginYear="2023" # shellcheck disable=SC2317 # if function is overridden unknownOption() { diff --git a/src/_binaries/Postman/testsData/postmanCli.help.txt b/src/_binaries/Postman/testsData/postmanCli.help.txt index 17c44e0e..04c6028a 100644 --- a/src/_binaries/Postman/testsData/postmanCli.help.txt +++ b/src/_binaries/Postman/testsData/postmanCli.help.txt @@ -10,10 +10,8 @@ Push/Pull postman collections of all the configured repositories ARGUMENTS: [command {single}] - pull -   Pull collections from Postman back to repositories. - push -   Push repositories collections to Postman. + pull   Pull collections from Postman back to repositories. + push   Push repositories collections to Postman. [commandArgs {list} (optional)] list of postman collection's references to pull or push or no argument to pull or push all the collections @@ -70,4 +68,4 @@ https://github.com/fchastanet/bash-tools/tree/master/src/_binaries/Postman/postm LICENSE: MIT License -Copyright (c) 2024-now François Chastanet +Copyright (c) 2023-now François Chastanet