Skip to content

Commit 675325a

Browse files
committed
Add return_handler function
- Call disable_verbosity before returning early from a public function. Otherwise verbosity would be left in whatever state was set by the enable_verbosity call at the start of the function. - Automatically take the correct action regarding errexit. - If returning success and errexit is enabled then disable it. - If returning failure then don't disable errexit. errexit must be left enabled when returning failure from a function called from any "build lifecycle stage" other than "install", otherwise Travis CI will continue merrily along without failing the build.
1 parent 5c35fba commit 675325a

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

arduino-ci-script.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ function disable_verbosity()
130130
}
131131

132132

133+
# Verbosity and, in some cases, errexit must be disabled before an early return from a public function, this allows it to be done in a single line instead of two
134+
function return_handler()
135+
{
136+
local -r exitStatus="$1"
137+
138+
# If exit status is success and errexit is enabled then it must be disabled before exiting the script because errexit must be disabled by default and only enabled in the functions that specifically require it.
139+
# If exit status is not success then errexit should not be disabled, otherwise Travis CI won't fail the build even though the exit status was failure.
140+
if [[ "$exitStatus" == "$ARDUINO_CI_SCRIPT_SUCCESS_EXIT_STATUS" ]] && shopt -q -o errexit; then
141+
set +o errexit
142+
fi
143+
144+
disable_verbosity
145+
146+
return "$exitStatus"
147+
}
148+
149+
133150
function set_application_folder()
134151
{
135152
enable_verbosity
@@ -209,7 +226,7 @@ function install_ide()
209226

210227
if [[ "$ARDUINO_CI_SCRIPT_APPLICATION_FOLDER" == "" ]]; then
211228
echo "ERROR: Application folder was not set. Please use the set_application_folder function to define the location of the application folder."
212-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
229+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
213230
fi
214231
create_folder "$ARDUINO_CI_SCRIPT_APPLICATION_FOLDER"
215232

@@ -459,15 +476,15 @@ function install_package()
459476
# Check if Arduino IDE is installed
460477
if [[ "$INSTALLED_IDE_VERSION_LIST_ARRAY" == "" ]]; then
461478
echo "ERROR: Installing a hardware package via Boards Manager requires the Arduino IDE to be installed. Please call install_ide before this command."
462-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
479+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
463480
fi
464481

465482
# Check if the newest installed IDE version supports --install-boards
466483
local -r unsupportedInstallBoardsOptionVersionsRange1regex="1.5.[0-9]"
467484
local -r unsupportedInstallBoardsOptionVersionsRange2regex="1.6.[0-3]"
468485
if [[ "$NEWEST_INSTALLED_IDE_VERSION" =~ $unsupportedInstallBoardsOptionVersionsRange1regex || "$NEWEST_INSTALLED_IDE_VERSION" =~ $unsupportedInstallBoardsOptionVersionsRange2regex ]]; then
469486
echo "ERROR: --install-boards option is not supported by the newest version of the Arduino IDE you have installed. You must have Arduino IDE 1.6.4 or newer installed to use this function."
470-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
487+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
471488
else
472489
# Temporarily install the latest IDE version to use for the package installation
473490
install_ide_version "$NEWEST_INSTALLED_IDE_VERSION"
@@ -555,15 +572,15 @@ function install_library()
555572
# Check if Arduino IDE is installed
556573
if [[ "$INSTALLED_IDE_VERSION_LIST_ARRAY" == "" ]]; then
557574
echo "ERROR: Installing a library via Library Manager requires the Arduino IDE to be installed. Please call install_ide before this command."
558-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
575+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
559576
fi
560577

561578
# Check if the newest installed IDE version supports --install-library
562579
local -r unsupportedInstallLibraryOptionVersionsRange1regex="1.5.[0-9]"
563580
local -r unsupportedInstallLibraryOptionVersionsRange2regex="1.6.[0-3]"
564581
if [[ "$NEWEST_INSTALLED_IDE_VERSION" =~ $unsupportedInstallLibraryOptionVersionsRange1regex || "$NEWEST_INSTALLED_IDE_VERSION" =~ $unsupportedInstallLibraryOptionVersionsRange2regex ]]; then
565582
echo "ERROR: --install-library option is not supported by the newest version of the Arduino IDE you have installed. You must have Arduino IDE 1.6.4 or newer installed to use this function."
566-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
583+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
567584
else
568585
local -r libraryName="$1"
569586

@@ -1007,11 +1024,11 @@ function publish_report_to_repository()
10071024
fi
10081025
else
10091026
echo "ERROR: Failed to push to remote branch."
1010-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
1027+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
10111028
fi
10121029
else
10131030
echo "ERROR: Failed to clone branch ${reportBranch} of repository URL ${repositoryURL}. Do they exist?"
1014-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
1031+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
10151032
fi
10161033
else
10171034
echo "No report file available for this job"
@@ -1026,7 +1043,7 @@ function publish_report_to_repository()
10261043
if [[ "$reportBranch" == "" ]]; then
10271044
echo "ERROR: Repository branch not specified. Failed to publish build report."
10281045
fi
1029-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
1046+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
10301047
fi
10311048

10321049
disable_verbosity
@@ -1077,7 +1094,7 @@ curlDataHere
10771094
if [[ "$gistURL" == "" ]]; then
10781095
echo "ERROR: Gist URL not specified. Failed to publish build report. See https://github.com/per1234/arduino-ci-script#publishing-job-reports for instructions."
10791096
fi
1080-
return "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
1097+
return_handler "$ARDUINO_CI_SCRIPT_FAILURE_EXIT_STATUS"
10811098
fi
10821099

10831100
disable_verbosity

0 commit comments

Comments
 (0)