Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few parse_ini_config_file() fixes #127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions cqfd
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,23 @@ parse_ini_config_file() {
die "Can't find $1 - create it or pick one using 'cqfd -f'"
fi

ini="$(<"$1")" # read the file
ini="${ini//[/\\[}" # escape [
ini="${ini//]/\\]}" # escape ]
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments with ;
ini=( ${ini[*]/\ =/=} ) # remove tabs before =
ini=( ${ini[*]/=\ /=} ) # remove tabs be =
ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around =
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/%\(/ \( \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
ini="$(<"$1")" # read the file
ini="${ini//[/\\[}" # escape [
ini="${ini//]/\\]}" # escape ]
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments with ;
ini=( ${ini[*]/$'\t'=/=} ) # remove tabs before =
ini=( ${ini[*]/=$'\t'/=} ) # remove tabs after =
ini=( ${ini[*]/\ =/=} ) # remove space before =
ini=( ${ini[*]/=\ /=} ) # remove space after =
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/%\(/ \( \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
if ! eval "$(echo "${ini[*]}")" 2>/dev/null; then # eval the result
die "$1: Invalid ini-file!"
fi
Expand Down
51 changes: 51 additions & 0 deletions tests/03-cqfdrc_error
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
#
# validate the .cqfdrc syntax

. "$(dirname "$0")"/jtest.inc "$1"
cqfd="$TDIR/.cqfd/cqfd"

cd $TDIR/

cqfdrc_old=$(mktemp)
cp -f .cqfdrc "$cqfdrc_old"
echo "foo =bar" >>.cqfdrc

jtest_prepare "cqfdrc with tabulation before should pass"
if $cqfd run true; then
jtest_result pass
else
jtest_result fail
fi

cp -f "$cqfdrc_old" .cqfdrc
echo "foo= bar" >>.cqfdrc

jtest_prepare "cqfdrc with tabulation after should pass"
if $cqfd run true; then
jtest_result pass
else
jtest_result fail
fi

cp -f "$cqfdrc_old" .cqfdrc
echo "foo =bar" >>.cqfdrc

jtest_prepare "cqfdrc with space before should pass"
if $cqfd run true; then
jtest_result pass
else
jtest_result fail
fi

cp -f "$cqfdrc_old" .cqfdrc
echo "foo= bar" >>.cqfdrc

jtest_prepare "cqfdrc with space after should pass"
if $cqfd run true; then
jtest_result pass
else
jtest_result fail
fi

mv -f "$cqfdrc_old" .cqfdrc