csvm2csv: Add some error checks
* build-aux/csvm2csv: Fail on invalid var definition. (expand_vars): Fail in invalid reference. (parseline): Fail on non-numeric range. * build-aux/test/test-csvm2csv (test-fail-unknown-var-ref, test-fail-non-numeric-range) (test-fail-invalid-var-dfn): New tests.master
parent
c741b4a84e
commit
c675207696
|
@ -46,12 +46,20 @@
|
|||
|
||||
|
||||
# Expand variable with its value, if any
|
||||
function expand_vars( s )
|
||||
function expand_vars( s, value )
|
||||
{
|
||||
# attempt to parse variable (may expand into a range)
|
||||
if ( match( s, /^\$([a-zA-Z_-]+)$/, m ) )
|
||||
{
|
||||
return vars[ m[1] ];
|
||||
value = vars[ m[1] ];
|
||||
|
||||
if ( value == "" )
|
||||
{
|
||||
print "error: unknown variable reference: `$" m[1] "'" > "/dev/stderr"
|
||||
exit 1
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
return s
|
||||
|
@ -102,6 +110,12 @@ function parseline( i, m, j, me, orig )
|
|||
j = expand_vars( m[1] )
|
||||
me = expand_vars( m[2] )
|
||||
|
||||
if ( !match( j, /^[0-9]+$/ ) || !match( me, /^[0-9]+$/ ) )
|
||||
{
|
||||
print "error: invalid range: `" $i "'" > "/dev/stderr"
|
||||
exit 1
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
$i = j
|
||||
|
@ -130,7 +144,12 @@ BEGIN {
|
|||
|
||||
# lines that begin with a colon are variable definitions
|
||||
/^:/ {
|
||||
match( $0, /^:([a-zA-Z_-]+)=(.*?)$/, m )
|
||||
if ( !match( $0, /^:([a-zA-Z_-]+)=(.*?)$/, m ) )
|
||||
{
|
||||
print "error: invalid variable definition: `" $0 "'" > "/dev/stderr"
|
||||
exit 1
|
||||
}
|
||||
|
||||
vars[ m[1] ] = m[2]
|
||||
next
|
||||
}
|
||||
|
|
|
@ -35,6 +35,8 @@ run-test()
|
|||
# SUT invocation
|
||||
declare -r given=$( ../csvm2csv < <( cat <<< "$input" ) )
|
||||
|
||||
test $? -eq 0 || return 1
|
||||
|
||||
# expected output
|
||||
diff <( cat <<< "$expected" ) <( cat <<< "$given" )
|
||||
}
|
||||
|
@ -187,6 +189,62 @@ $baz, 5'
|
|||
}
|
||||
|
||||
|
||||
# :foo=0 should be considered to be defined
|
||||
test-var-zero-ref()
|
||||
{
|
||||
declare -r input='header, line
|
||||
:foo=0
|
||||
$foo'
|
||||
|
||||
declare -r expected='header,line
|
||||
0'
|
||||
|
||||
run-test "$input" "$expected"
|
||||
}
|
||||
|
||||
|
||||
test-fail-unknown-var-ref()
|
||||
{
|
||||
((testsum++))
|
||||
|
||||
local -r result=$(
|
||||
../csvm2csv 2>&1 <<< '$undefined' \
|
||||
&& echo '(test failure: expected failure)'
|
||||
)
|
||||
|
||||
grep -q 'unknown.*\$undefined' <<< "$result" \
|
||||
|| return 1
|
||||
}
|
||||
|
||||
|
||||
test-fail-non-numeric-range()
|
||||
{
|
||||
((testsum++))
|
||||
|
||||
local -r result=$(
|
||||
../csvm2csv 2>&1 <<< 'A--Z' \
|
||||
&& echo '(test failure: expected failure)'
|
||||
)
|
||||
|
||||
grep -q 'invalid range.*A--Z' <<< "$result" \
|
||||
|| return 1
|
||||
}
|
||||
|
||||
|
||||
test-fail-invalid-var-dfn()
|
||||
{
|
||||
((testsum++))
|
||||
|
||||
local -r result=$(
|
||||
../csvm2csv 2>&1 <<< ':BAD@#=var' \
|
||||
&& echo '(test failure: expected failure)'
|
||||
)
|
||||
|
||||
grep -q 'invalid variable definition.*:BAD@#=var' <<< "$result" \
|
||||
|| return 1
|
||||
}
|
||||
|
||||
|
||||
test-comment \
|
||||
&& test-range \
|
||||
&& test-delim \
|
||||
|
@ -195,13 +253,17 @@ test-comment \
|
|||
&& test-var-in-range-delim \
|
||||
&& test-var-with-range-delim \
|
||||
&& test-var-with-var \
|
||||
&& test-var-zero-ref \
|
||||
&& test-fail-unknown-var-ref \
|
||||
&& test-fail-non-numeric-range \
|
||||
&& test-fail-invalid-var-dfn \
|
||||
|| {
|
||||
echo 'csvm2csv test failed' >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# safety check
|
||||
test "$testsum" -eq 8 || {
|
||||
test "$testsum" -eq 12 || {
|
||||
echo 'error: did not run all csvm2csv tests!' >&2
|
||||
exit 1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue