2014-05-09 01:13:26 -04:00
|
|
|
#!/bin/bash
|
|
|
|
# Core expectation tests
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Mike Gewitz
|
|
|
|
#
|
|
|
|
# This file is part of shspec.
|
|
|
|
#
|
|
|
|
# shspec is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2014-05-13 21:46:00 -04:00
|
|
|
describe be
|
|
|
|
it cannot stand alone
|
|
|
|
expect _expect--be 0 2
|
|
|
|
to fail
|
|
|
|
end
|
|
|
|
|
|
|
|
it processes remainder clause as if it did not exist
|
|
|
|
(
|
|
|
|
expected="foo bar baz"
|
|
|
|
_expect--awesome() { shift "$2"; test "$*" == "$expected"; }
|
|
|
|
|
|
|
|
expect _expect--be 0 2 awesome $expected
|
|
|
|
to succeed
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-09 01:13:26 -04:00
|
|
|
|
|
|
|
describe succeed
|
|
|
|
it will succeed when command exits with status code 0
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--succeed 0 2
|
2014-05-09 01:13:26 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it will fail when command exits with non-zero status code
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--succeed 1 2
|
2014-05-09 01:13:26 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
describe fail
|
|
|
|
it will succeed when command exits with non-zero status code
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--fail 1 2
|
2014-05-09 01:13:26 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it will fail when command exits with status code 0
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--fail 0 2
|
2014-05-09 01:13:26 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-09 02:04:09 -04:00
|
|
|
|
2014-05-15 00:45:49 -04:00
|
|
|
describe not
|
|
|
|
it will invert the result of an expectation
|
|
|
|
# exit code of 1, so normally `succeed' would fail
|
|
|
|
expect _expect--not 1 2 succeed
|
|
|
|
to succeed
|
|
|
|
|
|
|
|
# exit code of 0, so normally `succeed' would succeed
|
|
|
|
expect _expect--not 0 2 succeed
|
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-09 02:04:09 -04:00
|
|
|
describe output
|
|
|
|
it will default to asserting against stdout
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--output 0 2 "test string" <<< "test string"
|
2014-05-09 02:04:09 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
2014-05-12 21:39:26 -04:00
|
|
|
# by convention, stderr file passed as third argument
|
|
|
|
it can assert against stderr output
|
|
|
|
{
|
|
|
|
expect _expect--output 0 3 /dev/fd/3 \
|
|
|
|
"test stderr" on stderr
|
|
|
|
to succeed
|
|
|
|
} 3< <( echo "test stderr" )
|
2014-05-09 02:04:09 -04:00
|
|
|
end
|
|
|
|
|
2014-05-12 21:39:26 -04:00
|
|
|
it fails on stdout mismatch
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--output 0 2 "foo" <<< "bar"
|
2014-05-09 02:04:09 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
|
2014-05-12 21:39:26 -04:00
|
|
|
it fails on stderr mismatch
|
|
|
|
{
|
|
|
|
expect _expect--output 0 3 /dev/fd/3 "foo" on stderr
|
|
|
|
to fail
|
|
|
|
} 3< <( echo bar )
|
|
|
|
end
|
|
|
|
|
|
|
|
it ignores exit code
|
|
|
|
expect _expect--output 1 2 "foo" <<< "foo"
|
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
2014-05-09 02:04:09 -04:00
|
|
|
# as is good practice for Unix utilities
|
|
|
|
it expects trailing newline by default
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--output 0 2 "foo" < <( echo -n foo )
|
2014-05-09 02:04:09 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
|
|
|
|
it can disable trailing newline check with "'without newline'" clause
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--output 0 2 "foo" without newline < <( echo -n foo )
|
2014-05-09 02:04:09 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it errors on any unrecognized clause
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--output 0 2 "foo" fluffy bunnies <<< "foo"
|
2014-05-09 02:04:09 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-09 23:31:47 -04:00
|
|
|
|
|
|
|
describe match
|
|
|
|
it will default to asserting against stdout
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--match 0 2 "foo" <<< "foo"
|
2014-05-09 23:31:47 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
2014-05-13 23:25:27 -04:00
|
|
|
# by convention, stderr file passed as third argument
|
|
|
|
it can assert against stderr output
|
|
|
|
{
|
|
|
|
expect _expect--match 0 3 /dev/fd/3 "stderr" on stderr
|
|
|
|
to succeed
|
|
|
|
} 3< <( echo "test stderr" )
|
|
|
|
end
|
|
|
|
|
2014-05-09 23:31:47 -04:00
|
|
|
it ignores exit code
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--match 1 2 "foo" <<< "foo"
|
2014-05-09 23:31:47 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it will perform partial match
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--match 0 2 "foo" <<< "contains foo"
|
2014-05-09 23:31:47 -04:00
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it will match against a pattern
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--match 0 2 "^foo.*baz" <<< "foo bar baz"
|
2014-05-09 23:31:47 -04:00
|
|
|
to succeed
|
|
|
|
|
2014-05-10 00:42:58 -04:00
|
|
|
expect _expect--match 0 2 "^foo.*baz" <<< "bar foo bar baz"
|
2014-05-09 23:31:47 -04:00
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-13 00:20:24 -04:00
|
|
|
|
|
|
|
describe silent
|
|
|
|
it succeeds when both stdout and stderr are empty
|
|
|
|
# omitting stderr arg
|
|
|
|
expect _expect--silent 0 2 < <(:)
|
|
|
|
to succeed
|
|
|
|
|
|
|
|
# empty stderr
|
|
|
|
expect _expect--silent 0 3 <(:) < <(:)
|
|
|
|
to succeed
|
|
|
|
end
|
|
|
|
|
|
|
|
it fails when stdout is empty but stderr is not
|
|
|
|
expect _expect--silent 0 3 <( echo ) < <(:)
|
|
|
|
to fail
|
|
|
|
end
|
|
|
|
|
|
|
|
it fails when stderr is empty but stdout is not
|
|
|
|
# omitting stderr arg
|
|
|
|
expect _expect--silent 0 2 <<< ""
|
|
|
|
to fail
|
|
|
|
|
|
|
|
# empty stderr
|
|
|
|
expect _expect--silent 0 3 <(:) <<< ""
|
|
|
|
to fail
|
|
|
|
end
|
|
|
|
|
|
|
|
# no arguments within context of the DSL, that is
|
|
|
|
it accepts no arguments
|
|
|
|
expect _expect--silent 0 2 foo < <(:)
|
|
|
|
to fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|