Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fengzch-das
multibuild
Commits
444a6e5f
Commit
444a6e5f
authored
Aug 30, 2020
by
robbuckley
Browse files
add utility functions
parent
a01ddf72
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
0 deletions
+44
-0
common_utils.sh
common_utils.sh
+33
-0
tests/test_common_utils.sh
tests/test_common_utils.sh
+11
-0
No files found.
common_utils.sh
View file @
444a6e5f
...
@@ -141,6 +141,39 @@ function suppress {
...
@@ -141,6 +141,39 @@ function suppress {
return "
$ret
"
return "
$ret
"
}
}
function expect_return {
# Run a command, succeeding (returning 0) only if the commend returns a specified code
# Parameters
# retcode expected return code (which may be zero)
# command the command called
#
# any further arguments are passed to the called command
#
# Returns 1 if called with less than 2 arguments
((
$#
< 2 )) && echo "
Must have at least 2 arguments
" && return 1
local retcode=
$1
local retval
( "
${
@
:2
}
" ) || retval=
$?
[[
$retcode
==
${
retval
:-
0
}
]] && return 0
return
${
retval
:-
1
}
}
function cmd_notexit {
# wraps a command, capturing its return code and preventing it
# from exiting the shell. Handles -e / +e modes.
# Parameters
# cmd - command
# any further parameters are passed to the wrapped command
# If called without an argument, it will exit the shell with an error
local cmd=
$1
if [ -z "
$cmd
" ];then echo "
no
command
"; exit 1; fi
if [[
$-
= *e* ]]; then errexit_set=true; fi
set +e
("
${
@
:1
}
") ; retval=
$?
[[ -n
$errexit_set
]] && set -e
return
$retval
}
function rm_mkdir {
function rm_mkdir {
# Remove directory if present, then make directory
# Remove directory if present, then make directory
local path=
$1
local path=
$1
...
...
tests/test_common_utils.sh
View file @
444a6e5f
...
@@ -88,6 +88,17 @@ actual="$(set -e; suppress bad_mid_cmd)"
...
@@ -88,6 +88,17 @@ actual="$(set -e; suppress bad_mid_cmd)"
# Reset options
# Reset options
set_opts
$ORIG_OPTS
set_opts
$ORIG_OPTS
!
expect_return 1
||
ingest
"Too few arguments"
!
expect_return 1 good_cmd
||
ingest
"unexpected success"
!
expect_return 0 bad_cmd
||
ingest
"unexpected failure"
expect_return 1 bad_cmd
||
ingest
"fail with expected error 1"
!
expect_return 2 bad_cmd
||
ingest
"fail with unexpected error"
expect_return 0 good_cmd
||
ingest
"succeed as expected"
cmd_notexit good_cmd
||
ingest
!
cmd_notexit bad_cmd
||
ingest
!
cmd_notexit
exit
1
||
ingest
# On Linux docker containers in travis, can be x86_64, i686, s390x, ppc64le, or
# On Linux docker containers in travis, can be x86_64, i686, s390x, ppc64le, or
# aarch64
# aarch64
[
"
$(
get_platform
)
"
==
x86_64
]
||
\
[
"
$(
get_platform
)
"
==
x86_64
]
||
\
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment