mirror of https://github.com/deajan/osync
Update shunit to current master
parent
ecc4c1d396
commit
59bd176e48
@ -0,0 +1,147 @@
|
|||||||
|
Coding Standards
|
||||||
|
================
|
||||||
|
|
||||||
|
shFlags is more than just a simple 20 line shell script. It is a pretty
|
||||||
|
significant library of shell code that at first glance is not that easy to
|
||||||
|
understand. To improve code readability and usability, some guidelines have been
|
||||||
|
set down to make the code more understandable for anyone who wants to read or
|
||||||
|
modify it.
|
||||||
|
|
||||||
|
Function declaration
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Declare functions using the following form:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
doSomething() {
|
||||||
|
echo 'done!'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
One-line functions are allowed if they can fit within the 80 char line limit.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
doSomething() { echo 'done!'; }
|
||||||
|
```
|
||||||
|
|
||||||
|
Function documentation
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
Each function should be preceded by a header that provides the following:
|
||||||
|
|
||||||
|
1. A one-sentence summary of what the function does.
|
||||||
|
|
||||||
|
1. (optional) A longer description of what the function does, and perhaps some
|
||||||
|
special information that helps convey its usage better.
|
||||||
|
|
||||||
|
1. Args: a one-line summary of each argument of the form:
|
||||||
|
|
||||||
|
`name: type: description`
|
||||||
|
|
||||||
|
1. Output: a one-line summary of the output provided. Only output to STDOUT
|
||||||
|
must be documented, unless the output to STDERR is of significance (i.e. not
|
||||||
|
just an error message). The output should be of the form:
|
||||||
|
|
||||||
|
`type: description`
|
||||||
|
|
||||||
|
1. Returns: a one-line summary of the value returned. Returns in shell are
|
||||||
|
always integers, but if the output is a true/false for success (i.e. a
|
||||||
|
boolean), it should be noted. The output should be of the form:
|
||||||
|
|
||||||
|
`type: description`
|
||||||
|
|
||||||
|
Here is a sample header:
|
||||||
|
|
||||||
|
```
|
||||||
|
# Return valid getopt options using currently defined list of long options.
|
||||||
|
#
|
||||||
|
# This function builds a proper getopt option string for short (and long)
|
||||||
|
# options, using the current list of long options for reference.
|
||||||
|
#
|
||||||
|
# Args:
|
||||||
|
# _flags_optStr: integer: option string type (__FLAGS_OPTSTR_*)
|
||||||
|
# Output:
|
||||||
|
# string: generated option string for getopt
|
||||||
|
# Returns:
|
||||||
|
# boolean: success of operation (always returns True)
|
||||||
|
```
|
||||||
|
|
||||||
|
Variable and function names
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
All shFlags specific constants, variables, and functions will be prefixed
|
||||||
|
appropriately with 'flags'. This is to distinguish usage in the shFlags code
|
||||||
|
from users own scripts so that the shell name space remains predictable to
|
||||||
|
users. The exceptions here are the standard `assertEquals`, etc. functions.
|
||||||
|
|
||||||
|
All non built-in constants and variables will be surrounded with squiggle
|
||||||
|
brackets, e.g. `${flags_someVariable}` to improve code readability.
|
||||||
|
|
||||||
|
Due to some shells not supporting local variables in functions, care in the
|
||||||
|
naming and use of variables, both public and private, is very important.
|
||||||
|
Accidental overriding of the variables can occur easily if care is not taken as
|
||||||
|
all variables are technically global variables in some shells.
|
||||||
|
|
||||||
|
Type | Sample
|
||||||
|
---- | ------
|
||||||
|
global public constant | `FLAGS_TRUE`
|
||||||
|
global private constant | `__FLAGS_SHELL_FLAGS`
|
||||||
|
global public variable | `flags_variable`
|
||||||
|
global private variable | `__flags_variable`
|
||||||
|
global macro | `_FLAGS_SOME_MACRO_`
|
||||||
|
public function | `flags_function`
|
||||||
|
public function, local variable | ``flags_variable_`
|
||||||
|
private function | `_flags_function`
|
||||||
|
private function, local variable | `_flags_variable_`
|
||||||
|
|
||||||
|
Where it makes sense to improve readability, variables can have the first
|
||||||
|
letter of the second and later words capitalized. For example, the local
|
||||||
|
variable name for the help string length is `flags_helpStrLen_`.
|
||||||
|
|
||||||
|
There are three special-case global public variables used. They are used due to
|
||||||
|
overcome the limitations of shell scoping or to prevent forking. The three
|
||||||
|
variables are:
|
||||||
|
|
||||||
|
- `flags_error`
|
||||||
|
- `flags_output`
|
||||||
|
- `flags_return`
|
||||||
|
|
||||||
|
Local variable cleanup
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
As many shells do not support local variables, no support for cleanup of
|
||||||
|
variables is present either. As such, all variables local to a function must be
|
||||||
|
cleared up with the `unset` built-in command at the end of each function.
|
||||||
|
|
||||||
|
Indentation
|
||||||
|
-----------
|
||||||
|
|
||||||
|
Code block indentation is two (2) spaces, and tabs may not be used.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
if [ -z 'some string' ]; then
|
||||||
|
someFunction
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
|
Lines of code should be no longer than 80 characters unless absolutely
|
||||||
|
necessary. When lines are wrapped using the backslash character '\', subsequent
|
||||||
|
lines should be indented with four (4) spaces so as to differentiate from the
|
||||||
|
standard spacing of two characters, and tabs may not be used.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
for x in some set of very long set of arguments that make for a very long \
|
||||||
|
that extends much too long for one line
|
||||||
|
do
|
||||||
|
echo ${x}
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
When a conditional expression is written using the built-in [ command, and that
|
||||||
|
line must be wrapped, place the control || or && operators on the same line as
|
||||||
|
the expression where possible, with the list to be executed on its own line.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[ -n 'some really long expression' -a -n 'some other long expr' ] && \
|
||||||
|
echo 'that was actually true!'
|
||||||
|
```
|
@ -0,0 +1,47 @@
|
|||||||
|
#! /bin/sh
|
||||||
|
#
|
||||||
|
# Initialize the local git hooks this repository.
|
||||||
|
# https://git-scm.com/docs/githooks
|
||||||
|
|
||||||
|
topLevel=$(git rev-parse --show-toplevel)
|
||||||
|
if ! cd "${topLevel}"; then
|
||||||
|
echo "filed to cd into topLevel directory '${topLevel}'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
hooksDir="${topLevel}/.githooks"
|
||||||
|
if ! hooksPath=$(git config core.hooksPath); then
|
||||||
|
hooksPath="${topLevel}/.git/hooks"
|
||||||
|
fi
|
||||||
|
|
||||||
|
src="${hooksDir}/generic"
|
||||||
|
echo "linking hooks..."
|
||||||
|
for hook in \
|
||||||
|
applypatch-msg \
|
||||||
|
pre-applypatch \
|
||||||
|
post-applypatch \
|
||||||
|
pre-commit \
|
||||||
|
pre-merge-commit \
|
||||||
|
prepare-commit-msg \
|
||||||
|
commit-msg \
|
||||||
|
post-commit \
|
||||||
|
pre-rebase \
|
||||||
|
post-checkout \
|
||||||
|
post-merge \
|
||||||
|
pre-push \
|
||||||
|
pre-receive \
|
||||||
|
update \
|
||||||
|
post-receive \
|
||||||
|
post-update \
|
||||||
|
push-to-checkout \
|
||||||
|
pre-auto-gc \
|
||||||
|
post-rewrite \
|
||||||
|
sendemail-validate \
|
||||||
|
fsmonitor-watchman \
|
||||||
|
p4-pre-submit \
|
||||||
|
post-index-change
|
||||||
|
do
|
||||||
|
echo " ${hook}"
|
||||||
|
dest="${hooksPath}/${hook}"
|
||||||
|
ln -sf "${src}" "${dest}"
|
||||||
|
done
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,64 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# vim:et:ft=sh:sts=2:sw=2
|
||||||
|
#
|
||||||
|
# shunit2 unit test for running subset(s) of tests based upon command line args.
|
||||||
|
#
|
||||||
|
# Copyright 2008-2021 Kate Ward. All Rights Reserved.
|
||||||
|
# Released under the Apache 2.0 license.
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# https://github.com/kward/shunit2
|
||||||
|
#
|
||||||
|
# Also shows how non-default tests or a arbitrary subset of tests can be run.
|
||||||
|
#
|
||||||
|
# Disable source following.
|
||||||
|
# shellcheck disable=SC1090,SC1091
|
||||||
|
|
||||||
|
# Load test helpers.
|
||||||
|
. ./shunit2_test_helpers
|
||||||
|
|
||||||
|
CUSTOM_TEST_RAN=''
|
||||||
|
|
||||||
|
# This test does not normally run because it does not begin "test*". Will be
|
||||||
|
# run by setting the arguments to the script to include the name of this test.
|
||||||
|
custom_test() {
|
||||||
|
# Arbitrary assert.
|
||||||
|
assertTrue 0
|
||||||
|
# The true intent is to set this variable, which will be tested below.
|
||||||
|
CUSTOM_TEST_RAN='yup, we ran'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verify that `customTest()` ran.
|
||||||
|
testCustomTestRan() {
|
||||||
|
assertNotNull "'custom_test()' did not run" "${CUSTOM_TEST_RAN}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fail if this test runs, which is shouldn't if arguments are set correctly.
|
||||||
|
testShouldFail() {
|
||||||
|
fail 'testShouldFail should not be run if argument parsing works'
|
||||||
|
}
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
th_oneTimeSetUp
|
||||||
|
}
|
||||||
|
|
||||||
|
# If zero/one argument(s) are provided, this test is being run in it's
|
||||||
|
# entirety, and therefore we want to set the arguments to the script to
|
||||||
|
# (simulate and) test the processing of command-line specified tests. If we
|
||||||
|
# don't, then the "test_will_fail" test will run (by default) and the overall
|
||||||
|
# test will fail.
|
||||||
|
#
|
||||||
|
# However, if two or more arguments are provided, then assume this test script
|
||||||
|
# is being run by hand to experiment with command-line test specification, and
|
||||||
|
# then don't override the user provided arguments.
|
||||||
|
if [ "$#" -le 1 ]; then
|
||||||
|
# We set the arguments in a POSIX way, inasmuch as we can;
|
||||||
|
# helpful tip:
|
||||||
|
# https://unix.stackexchange.com/questions/258512/how-to-remove-a-positional-parameter-from
|
||||||
|
set -- '--' 'custom_test' 'testCustomTestRan'
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Load and run shunit2.
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
|
||||||
|
. "${TH_SHUNIT}"
|
@ -0,0 +1,99 @@
|
|||||||
|
#! /bin/sh
|
||||||
|
# vim:et:ft=sh:sts=2:sw=2
|
||||||
|
#
|
||||||
|
# shUnit2 unit tests for general commands.
|
||||||
|
#
|
||||||
|
# Copyright 2008-2021 Kate Ward. All Rights Reserved.
|
||||||
|
# Released under the Apache 2.0 license.
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Author: kate.ward@forestent.com (Kate Ward)
|
||||||
|
# https://github.com/kward/shunit2
|
||||||
|
#
|
||||||
|
# Disable source following.
|
||||||
|
# shellcheck disable=SC1090,SC1091
|
||||||
|
|
||||||
|
# These variables will be overridden by the test helpers.
|
||||||
|
stdoutF="${TMPDIR:-/tmp}/STDOUT"
|
||||||
|
stderrF="${TMPDIR:-/tmp}/STDERR"
|
||||||
|
|
||||||
|
# Load test helpers.
|
||||||
|
. ./shunit2_test_helpers
|
||||||
|
|
||||||
|
testSkipping() {
|
||||||
|
# We shouldn't be skipping to start.
|
||||||
|
if isSkipping; then
|
||||||
|
th_error 'skipping *should not be* enabled'
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
startSkipping
|
||||||
|
was_skipping_started=${SHUNIT_FALSE}
|
||||||
|
if isSkipping; then was_skipping_started=${SHUNIT_TRUE}; fi
|
||||||
|
|
||||||
|
endSkipping
|
||||||
|
was_skipping_ended=${SHUNIT_FALSE}
|
||||||
|
if isSkipping; then was_skipping_ended=${SHUNIT_TRUE}; fi
|
||||||
|
|
||||||
|
assertEquals "skipping wasn't started" "${was_skipping_started}" "${SHUNIT_TRUE}"
|
||||||
|
assertNotEquals "skipping wasn't ended" "${was_skipping_ended}" "${SHUNIT_TRUE}"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
testStartSkippingWithMessage() {
|
||||||
|
unittestF="${SHUNIT_TMPDIR}/unittest"
|
||||||
|
sed 's/^#//' >"${unittestF}" <<\EOF
|
||||||
|
## Start skipping with a message.
|
||||||
|
#testSkipping() {
|
||||||
|
# startSkipping 'SKIP-a-Dee-Doo-Dah'
|
||||||
|
#}
|
||||||
|
#SHUNIT_COLOR='none'
|
||||||
|
#. ${TH_SHUNIT}
|
||||||
|
EOF
|
||||||
|
# Ignoring errors with `|| :` as we only care about `FAILED` in the output.
|
||||||
|
( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || :
|
||||||
|
if ! grep '\[skipping\] SKIP-a-Dee-Doo-Dah' "${stderrF}" >/dev/null; then
|
||||||
|
fail 'skipping message was not generated'
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
testStartSkippingWithoutMessage() {
|
||||||
|
unittestF="${SHUNIT_TMPDIR}/unittest"
|
||||||
|
sed 's/^#//' >"${unittestF}" <<\EOF
|
||||||
|
## Start skipping with a message.
|
||||||
|
#testSkipping() {
|
||||||
|
# startSkipping
|
||||||
|
#}
|
||||||
|
#SHUNIT_COLOR='none'
|
||||||
|
#. ${TH_SHUNIT}
|
||||||
|
EOF
|
||||||
|
# Ignoring errors with `|| :` as we only care about `FAILED` in the output.
|
||||||
|
( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || :
|
||||||
|
if grep '\[skipping\]' "${stderrF}" >/dev/null; then
|
||||||
|
fail 'skipping message was unexpectedly generated'
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
setUp() {
|
||||||
|
for f in "${stdoutF}" "${stderrF}"; do
|
||||||
|
cp /dev/null "${f}"
|
||||||
|
done
|
||||||
|
|
||||||
|
# Reconfigure coloring as some tests override default behavior.
|
||||||
|
_shunit_configureColor "${SHUNIT_COLOR_DEFAULT}"
|
||||||
|
|
||||||
|
# shellcheck disable=SC2034,SC2153
|
||||||
|
SHUNIT_CMD_TPUT=${__SHUNIT_CMD_TPUT}
|
||||||
|
}
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
SHUNIT_COLOR_DEFAULT="${SHUNIT_COLOR}"
|
||||||
|
th_oneTimeSetUp
|
||||||
|
}
|
||||||
|
|
||||||
|
# Load and run shUnit2.
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
|
||||||
|
. "${TH_SHUNIT}"
|
@ -0,0 +1,70 @@
|
|||||||
|
#! /bin/sh
|
||||||
|
# vim:et:ft=sh:sts=2:sw=2
|
||||||
|
#
|
||||||
|
# shUnit2 unit tests for `shopt` support.
|
||||||
|
#
|
||||||
|
# Copyright 2008-2021 Kate Ward. All Rights Reserved.
|
||||||
|
# Released under the Apache 2.0 license.
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Author: kate.ward@forestent.com (Kate Ward)
|
||||||
|
# https://github.com/kward/shunit2
|
||||||
|
#
|
||||||
|
# Disable source following.
|
||||||
|
# shellcheck disable=SC1090,SC1091
|
||||||
|
|
||||||
|
# Load test helpers.
|
||||||
|
. ./shunit2_test_helpers
|
||||||
|
|
||||||
|
# Call shopt from a variable so it can be mocked if it doesn't work.
|
||||||
|
SHOPT_CMD='shopt'
|
||||||
|
|
||||||
|
testNullglob() {
|
||||||
|
isShoptWorking || startSkipping
|
||||||
|
|
||||||
|
nullglob=$(${SHOPT_CMD} nullglob |cut -f2)
|
||||||
|
|
||||||
|
# Test without nullglob.
|
||||||
|
${SHOPT_CMD} -u nullglob
|
||||||
|
assertEquals 'test without nullglob' 0 0
|
||||||
|
|
||||||
|
# Test with nullglob.
|
||||||
|
${SHOPT_CMD} -s nullglob
|
||||||
|
assertEquals 'test with nullglob' 1 1
|
||||||
|
|
||||||
|
# Reset nullglob.
|
||||||
|
if [ "${nullglob}" = "on" ]; then
|
||||||
|
${SHOPT_CMD} -s nullglob
|
||||||
|
else
|
||||||
|
${SHOPT_CMD} -u nullglob
|
||||||
|
fi
|
||||||
|
|
||||||
|
unset nullglob
|
||||||
|
}
|
||||||
|
|
||||||
|
oneTimeSetUp() {
|
||||||
|
th_oneTimeSetUp
|
||||||
|
|
||||||
|
if ! isShoptWorking; then
|
||||||
|
SHOPT_CMD='mock_shopt'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# isShoptWorking returns true if the `shopt` shell command is available.
|
||||||
|
# NOTE: `shopt` is not defined as part of the POSIX standard.
|
||||||
|
isShoptWorking() {
|
||||||
|
# shellcheck disable=SC2039,SC3044
|
||||||
|
( shopt >/dev/null 2>&1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
mock_shopt() {
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
echo "nullglob off"
|
||||||
|
fi
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
# Load and run shUnit2.
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT="$0"
|
||||||
|
. "${TH_SHUNIT}"
|
Loading…
Reference in New Issue