1
0
mirror of https://github.com/albfan/bash-ini-parser.git synced 2025-08-06 07:02:37 +03:00

Replace invalid chars

bash-ini-parser do not support all the valid chars ini key files support.

Allow to replace them with valid ones using an external function defined
with REPLACE_FUNCTION
This commit is contained in:
albfan
2018-02-20 23:46:58 +01:00
parent d73cdd267d
commit aeb08acf9a
6 changed files with 65 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
$(top_srcdir)/build-aux/tap-driver.sh
TESTS = t0001-whitespace.sh t0002-invalid.sh t0003-sections.sh t0004-comments.sh
TESTS = t0001-whitespace.sh t0002-invalid.sh t0003-sections.sh t0004-comments.sh t0005-invalid-chars.sh
EXTRA_DIST = $(TESTS)

34
t/t0005-invalid-chars.sh Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
test_description="replace invalid chars"
. setup.sh
DIR_TEST=$SHARNESS_TEST_DIRECTORY/t0005
#replace function
function replace_chars() {
if [[ $i =~ .*=.* ]]
then
local key=$(echo $1 | sed 's/\(.*\)=.*/\1/;s/-/_/g')
local value=$(echo $1 | sed 's/.*=\(.*\)/\1/')
echo "$key=$value"
else
echo $1
fi
}
#export function to be avaliable on bash-ini-parser script
export -f replace_chars
#define var to use function inside bash-ini-parser
export REPLACE_FUNCTION=replace_chars
test_expect_success "Replace invalid chars" "
export COVERAGE_NAME=comments_parser
cp ../.simplecov .
cfg_parser $DIR_TEST/invalid.ini
cfg_writer > invalid.out
diff $DIR_TEST/invalid.out.correct invalid.out
"
test_done

9
t/t0005/invalid.ini Normal file
View File

@@ -0,0 +1,9 @@
[sec1]
var-1=foo
var2=hoge
var3=hoge#bar
var4="#hello #bye #chao"
[sec2]
var2=foo
var-4="123#456"
var5=234#456

View File

@@ -0,0 +1,9 @@
[sec1]
var_1="foo"
var2="hoge"
var3="hoge#bar"
var4="#hello #bye #chao"
[sec2]
var2="foo"
var_4="123#456"
var5="234#456"