diff options
author | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2018-10-02 10:55:20 -0400 |
---|---|---|
committer | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2018-10-02 13:35:06 -0400 |
commit | c675207696ce8086d460ecbb2388caa76866bdbc (patch) | |
tree | 0bd7d59785a531bc300fe15df1fb198681ac957a /build-aux | |
parent | c741b4a84eebe397b89db08c6d4228fdc5afaa63 (diff) | |
download | tame-c675207696ce8086d460ecbb2388caa76866bdbc.tar.gz tame-c675207696ce8086d460ecbb2388caa76866bdbc.tar.bz2 tame-c675207696ce8086d460ecbb2388caa76866bdbc.zip |
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.
Diffstat (limited to 'build-aux')
-rwxr-xr-x | build-aux/csvm2csv | 25 | ||||
-rwxr-xr-x | build-aux/test/test-csvm2csv | 64 |
2 files changed, 85 insertions, 4 deletions
diff --git a/build-aux/csvm2csv b/build-aux/csvm2csv index 9b9dc19..addbe26 100755 --- a/build-aux/csvm2csv +++ b/build-aux/csvm2csv @@ -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 } diff --git a/build-aux/test/test-csvm2csv b/build-aux/test/test-csvm2csv index 19ae9cf..14ef407 100755 --- a/build-aux/test/test-csvm2csv +++ b/build-aux/test/test-csvm2csv @@ -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 } |