Support binary comparisons by `expect'

* src/spec.sh
  (__spec_result): Remove variable.
  (__spec_outfile): Add variable.
  (expect): Write stdout to temporary file rather than storing in variable,
    which strips trailing whitespace and does not handle null bytes.
  (shspec::__handle-to): Use `$__spec_outpath'.
  Update copyright years.

* test/test-spec: Add tests for trailing whitespace and binary comparison.
master
Mike Gerwitz 2017-04-19 01:52:19 -04:00
parent 5a18560d7c
commit a368e809a3
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
2 changed files with 37 additions and 7 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
# Specification language
#
# Copyright (C) 2014 Mike Gerwitz
# Copyright (C) 2014, 2017 Mike Gerwitz
#
# This file is part of shspec.
#
@ -45,14 +45,14 @@ shspec::__mktemp-shm()
}
# stderr file
# std{out,err} file
readonly __spec_outpath="$(shspec::__mktemp-shm)"
readonly __spec_errpath="$(shspec::__mktemp-shm)"
# env dump file
readonly __spec_envpath="$(shspec::__mktemp-shm)"
# most recent expect result and its exit code
declare __spec_result=
# most recent expect result exit code
declare -i __spec_rexit=0
# most recent caller for expectations
@ -146,7 +146,7 @@ end()
expect()
{
shspec::stack::_assert-within it expect $(caller)
__spec_result="$("$@" 2>"$__spec_errpath")"
( "$@" >"$__spec_outpath" 2>"$__spec_errpath" )
__spec_rexit=$?
shspec::stack::_push :expect $(caller) "$@"
}
@ -204,7 +204,7 @@ shspec::__handle-to()
# shift argument allows the implementation to vary without breaking BC so
# long as the meaning of the shifted arguments do not change
$assert $rexit $__SHIFTN "$errpath" "$envpath" "$@" \
< <( echo -n "$__spec_result" )
< "$__spec_outpath"
}

View File

@ -1,7 +1,7 @@
#!/bin/bash
# Specification DSL test
#
# Copyright (C) 2014 Mike Gerwitz
# Copyright (C) 2014, 2017 Mike Gerwitz
#
# This file is part of shspec.
#
@ -141,6 +141,36 @@ describe expect
end
'; to succeed
end
# if result data are stored in a variable, the shell will remove
# trailing whitespace, which is certainly _not_ what we want
it retains trailing newlines
expect test-run <<< '
chk() { test $( wc -l ) -eq 3; }
describe foo
it retains trailing whitespace
expect chk <<< "foomoo
"
to succeed
end
end
'; to succeed
end
# null bytes must be properly handled when comparing binary data
it handles binary comparison with null bytes
expect test-run <<< '
input=007a0000
chk() { test $( xxd -p ) == "$input"; }
describe foo
it handles null bytes
expect chk < <( xxd -r -p <<< "$input" )
to succeed
end
end
'; to succeed
end
end