scripts: introduce _log to sedfile (#2507)

This commit is contained in:
Georg Lauterbach 2022-04-02 15:52:30 +02:00 committed by GitHub
parent 05f680b472
commit a1ecd781c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

View file

@ -85,6 +85,7 @@ RUN \
rm -rf /var/lib/apt/lists/* && \
c_rehash 2>&1
COPY ./target/scripts/helpers/log.sh /usr/local/bin/helpers/log.sh
COPY ./target/bin/sedfile /usr/local/bin/sedfile
RUN chmod +x /usr/local/bin/sedfile

View file

@ -9,6 +9,17 @@
# Is a file change expected? --> use 'sedfile --strict -i'
# Is a file change only on the first container run expected? --> use 'sedfile -i'
if [[ -e /usr/local/bin/helpers/log.sh ]]
then
# shellcheck source=../scripts/helpers/log.sh
source /usr/local/bin/helpers/log.sh
else
# when running BATS (unit tests), this file is not located
# inside a container; as a consequence, we need to source
# from a different location
source target/scripts/helpers/log.sh
fi
set -ueo pipefail
function __usage { echo "Usage: ${0} -i <replace/delete operation> <file>" ; }
@ -18,9 +29,8 @@ SKIP_ERROR=0
if [[ ${#} -lt 3 ]]
then
echo 'Error: At least three parameters must be given'
_log 'error' 'At least three parameters must be given'
__usage
echo
exit 1
fi >&2
@ -41,7 +51,7 @@ NEW=$(${HASHTOOL} "${FILE}")
# fail if file was not modified
if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]]
then
echo "Error: No difference after call to 'sed' in 'sedfile' (sed ${*})" >&2
_log 'error' "No difference after call to 'sed' in 'sedfile' (sed ${*})" >&2
exit 1
fi

View file

@ -18,7 +18,7 @@ function setup_file() {
@test "checking sedfile parameter count" {
run ${SEDFILE}
assert_failure
assert_output --partial 'Error: At least three parameters must be given'
assert_output --partial 'At least three parameters must be given'
}
@test "checking sedfile substitute success" {
@ -35,7 +35,7 @@ function setup_file() {
@test "checking sedfile substitute failure" {
run ${SEDFILE} -i 's|bar|baz|' "${FILE}"
assert_failure
assert_output --partial "Error: No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test"
assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test"
# file unchanged?
run test "$(< "${FILE}")" == 'foo baz'
@ -58,7 +58,7 @@ function setup_file() {
@test "checking sedfile substitude failure (strict)" {
run ${SEDFILE} --strict -i 's|bar|baz|' "${FILE}"
assert_failure
assert_output --partial "Error: No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test"
assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|bar|baz| /tmp/sedfile-test"
# file unchanged?
run test "$(< "${FILE}")" == 'foo baz'