fix templater.sh and associated test

templater.sh now escapes the delimiter it uses for its sed magic within
the environment variable content.

the associated test uses a properly cleaned up temporary file instead of
explicitly rm-ing its own mess.
This commit is contained in:
Manuel Streuhofer 2018-01-23 22:02:57 +01:00
parent 2463cd6cc9
commit f57e37eb68
3 changed files with 16 additions and 15 deletions

2
.gitignore vendored
View File

@ -1,8 +1,6 @@
npm-debug.log
node_modules
test/scenario/templater/break.cfg
website/index.md
website/_site/
website/_vendor/

View File

@ -41,7 +41,7 @@ function templater() {
cp -f "${templateSrc}" "${templateDst}"
for var in $(env |awk -F= '{print $1}' |grep -E '^(_[A-Z0-9_]+|[A-Z0-9][A-Z0-9_]*)$'); do
sed -i.bak -e "s#\${${var}}#${!var}#g" "${templateDst}"
sed -i.bak -e "s#\${${var}}#${!var//#/\\#/}#g" "${templateDst}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${templateDst}.bak"
done

View File

@ -5,12 +5,18 @@ set -o nounset
# set -o xtrace
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename "${__file}" .sh)"
__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)"
__templaterTmpFile=$(mktemp "${TMPDIR:-/tmp}/${__base}.XXXXXX")
function cleanup_before_exit () { rm "${__templaterTmpFile:?}"; }
trap cleanup_before_exit EXIT
echo "--"
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./app.template.cfg ./app.cfg
cat app.cfg
rm -f app.cfg
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./app.template.cfg "${__templaterTmpFile}"
cat "${__templaterTmpFile}"
echo "--"
export TARGET_HOST="127.0.0.1"
@ -18,16 +24,13 @@ export TARGET_HOST="127.0.0.1"
# shellcheck source=src/templater.sh
source "${__root}/src/templater.sh"
templater ./app.template.cfg ./app.cfg
cat app.cfg
rm -f app.cfg
templater ./app.template.cfg "${__templaterTmpFile}"
cat "${__templaterTmpFile}"
echo "--"
env ALLOW_REMAINDERS="1" TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg ./break.cfg
cat break.cfg
rm -f break.cfg
env ALLOW_REMAINDERS="1" TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg "${__templaterTmpFile}"
cat "${__templaterTmpFile}"
echo "--"
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg ./break.cfg
cat break.cfg
rm -f break.cfg
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg "${__templaterTmpFile}"
cat "${__templaterTmpFile}"