From a1ecd781c812f0c62295320223b1122d2fcbb70d Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Sat, 2 Apr 2022 15:52:30 +0200 Subject: [PATCH] scripts: introduce `_log` to `sedfile` (#2507) --- Dockerfile | 1 + target/bin/sedfile | 16 +++++++++++++--- test/sedfile.bats | 6 +++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index a0fc4d29..79d09353 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/target/bin/sedfile b/target/bin/sedfile index 3b849e07..6b550bff 100755 --- a/target/bin/sedfile +++ b/target/bin/sedfile @@ -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 " ; } @@ -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 diff --git a/test/sedfile.bats b/test/sedfile.bats index 61b49ccf..4cc3ee89 100644 --- a/test/sedfile.bats +++ b/test/sedfile.bats @@ -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'