mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de104f7cd5 | |||
| 58e1b8dfa7 | |||
| e8388b911b | |||
| d9e3e14c76 | |||
|
|
f46506d00e | ||
|
|
59a69b883f | ||
| 1f6dc46f3f | |||
| 332cfb8d28 | |||
| f3e0f666cb | |||
| 47ce28bba6 | |||
| 6e8e9fcfcf | |||
|
|
0ea845751e | ||
| 3b02e60207 | |||
| 78f31d9bc9 | |||
| e5382ea2dc | |||
| 9a34d8fa90 | |||
| 8c18d4075c | |||
| 407fefcb34 | |||
| ef607e8eec | |||
| 49ec034829 | |||
| 5ac835a822 | |||
| 51f6c8776e | |||
| 7f77ef159e | |||
| 023e3090dd | |||
| 4b46a98fda | |||
| 807d1ba73d | |||
| a6741d903c | |||
| 2869bf2f93 | |||
| 3b68d7a3c7 | |||
| f077f46036 | |||
| d1f658f173 | |||
| 007d6296d3 | |||
| 721226618c | |||
| ce67b6c3a5 | |||
| 13a571b9a3 | |||
| 32af661b2f | |||
| 5af71ab2fd | |||
| cd5eeda4c9 | |||
| d90fd505e9 | |||
| 844c41b4ae | |||
| d6cbe395ea | |||
| 156f1a3657 | |||
| efb9140329 | |||
| 0c4a5d11ee | |||
| ef231e420a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
test/assert.sh
|
test/assert.sh
|
||||||
|
test-runner.sh
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
language: node_js
|
language: c
|
||||||
script: make test
|
before_install:
|
||||||
|
- sudo apt-get install bc
|
||||||
|
script: make test
|
||||||
|
|||||||
44
Makefile
44
Makefile
@@ -1,12 +1,38 @@
|
|||||||
test: prepare
|
test: bootstrap
|
||||||
./test.sh
|
./test-runner.sh
|
||||||
|
|
||||||
prepare:
|
bootstrap: test-runner.sh test/assert.sh
|
||||||
test -f "test/assert.sh" || ( \
|
clean: remove_test-runner.sh remove_assert.sh
|
||||||
echo "fetching assert.sh..." && \
|
update: update_test-runner.sh update_assert.sh
|
||||||
curl -s -L -o test/assert.sh \
|
|
||||||
https://raw.github.com/lehmannro/assert.sh/master/assert.sh \
|
test/assert.sh:
|
||||||
)
|
echo "fetching assert.sh..." && \
|
||||||
|
curl -s -L -o test/assert.sh \
|
||||||
|
https://raw.github.com/lehmannro/assert.sh/v1.0.2/assert.sh
|
||||||
|
|
||||||
|
remove_assert.sh:
|
||||||
|
( \
|
||||||
|
test -f "test/assert.sh" && rm "test/assert.sh" && \
|
||||||
|
echo "removed test/assert.sh" \
|
||||||
|
) || exit 0
|
||||||
|
|
||||||
|
update_assert.sh: remove_assert.sh test/assert.sh
|
||||||
|
|
||||||
|
test-runner.sh:
|
||||||
|
echo "fetching test-runner.sh..." && \
|
||||||
|
curl -s -L -o test-runner.sh \
|
||||||
|
https://github.com/jimeh/test-runner.sh/raw/master/test-runner.sh && \
|
||||||
|
chmod +x test-runner.sh
|
||||||
|
|
||||||
|
remove_test-runner.sh:
|
||||||
|
( \
|
||||||
|
test -f "test-runner.sh" && rm "test-runner.sh" && \
|
||||||
|
echo "removed test-runner.sh" \
|
||||||
|
) || exit 0
|
||||||
|
|
||||||
|
update_test-runner.sh: remove_test-runner.sh test-runner.sh
|
||||||
|
|
||||||
.SILENT:
|
.SILENT:
|
||||||
.PHONY: test prepare
|
.PHONY: test bootstrap clean update \
|
||||||
|
remove_test-runner.sh update_test-runner.sh \
|
||||||
|
remove_assert.sh update_assert.sh
|
||||||
|
|||||||
166
README.md
166
README.md
@@ -1,7 +1,8 @@
|
|||||||
# stub.sh [](https://travis-ci.org/jimeh/stub.sh)
|
# stub.sh [](https://travis-ci.org/jimeh/stub.sh)
|
||||||
|
|
||||||
A set of stubbing helpers for use in bash script tests. Supports stubbing and
|
Helpers for bash script testing to stub/fake binaries and functions. Includes
|
||||||
restoring both binaries and bash functions.
|
support for validating number of stub calls, and/or if stub has been called
|
||||||
|
with specific arguments.
|
||||||
|
|
||||||
Particularly useful when used in combination with the simple and elegant
|
Particularly useful when used in combination with the simple and elegant
|
||||||
[assert.sh](https://github.com/lehmannro/assert.sh) test framework.
|
[assert.sh](https://github.com/lehmannro/assert.sh) test framework.
|
||||||
@@ -20,12 +21,14 @@ Stubbing binaries:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
uname #=> Darwin
|
uname #=> Darwin
|
||||||
stub uname
|
stub uname # silent stub
|
||||||
uname #=> uname stub:
|
uname #=>
|
||||||
uname -r #=> uname stub: -r
|
stub uname STDOUT # stub prints to STDOUT
|
||||||
restore uname
|
uname #=> uname stub:
|
||||||
uname #=> Darwin
|
uname -r #=> uname stub: -r
|
||||||
|
restore uname # remove stub
|
||||||
|
uname #=> Darwin
|
||||||
```
|
```
|
||||||
|
|
||||||
Stubbing bash functions:
|
Stubbing bash functions:
|
||||||
@@ -34,32 +37,155 @@ Stubbing bash functions:
|
|||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
my-name-is() { echo "My name is $@."; }
|
my-name-is() { echo "My name is $@."; }
|
||||||
my-name-is Edward Elric #=> My name is Edward Elric.
|
my-name-is Edward Elric #=> My name is Edward Elric.
|
||||||
stub my-name-is
|
stub my-name-is # silent stub
|
||||||
|
my-name-is Edward Elric #=>
|
||||||
|
stub my-name-is STDOUT # stub prints to STDOUT
|
||||||
my-name-is Edward Elric #=> my-name-is stub: Edward Elric
|
my-name-is Edward Elric #=> my-name-is stub: Edward Elric
|
||||||
restore my-name-is
|
restore my-name-is # remove stub
|
||||||
my-name-is Edward Elric #=> My name is Edward Elric.
|
my-name-is Edward Elric #=> My name is Edward Elric.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Asserting stub has been called:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source "stub.sh"
|
||||||
|
my-uname() { uname; }
|
||||||
|
stub_and_echo uname "FooBar"
|
||||||
|
stub_called uname # returns 1 (error)
|
||||||
|
my-uname #=> FooBar
|
||||||
|
stub_called uname # returns 0 (success)
|
||||||
|
restore uname
|
||||||
|
```
|
||||||
|
|
||||||
|
Asserting stub has been called X times:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source "stub.sh"
|
||||||
|
my-uname() { uname; }
|
||||||
|
stub_and_echo uname "FooBar"
|
||||||
|
stub_called_times uname #=> 0
|
||||||
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
|
my-uname #=> FooBar
|
||||||
|
stub_called_times uname #=> 1
|
||||||
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
|
my-uname #=> FooBar
|
||||||
|
stub_called_times uname #=> 2
|
||||||
|
stub_called_exactly_times uname 2 # returns 0 (success)
|
||||||
|
restore uname
|
||||||
|
```
|
||||||
|
|
||||||
|
Asserting stub has been called with specific arguments:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source "stub.sh"
|
||||||
|
my-uname() { uname $@; }
|
||||||
|
stub_and_echo uname "FooBar"
|
||||||
|
stub_called_with uname -r -a # returns 1 (error)
|
||||||
|
stub_called_with uname -r # returns 1 (error)
|
||||||
|
my-uname -r -a #=> FooBar
|
||||||
|
stub_called_with uname -r -a # returns 0 (success)
|
||||||
|
stub_called_with uname -r # returns 1 (error)
|
||||||
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with uname -r -a # returns 0 (success)
|
||||||
|
stub_called_with uname -r # returns 0 (success)
|
||||||
|
restore uname
|
||||||
|
```
|
||||||
|
|
||||||
|
Asserting stub has been called X number of times with specific arguments:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source "stub.sh"
|
||||||
|
my-uname() { uname $@; }
|
||||||
|
stub_and_echo uname "FooBar"
|
||||||
|
stub_called_with_times uname -r #=> 0
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 1 (error)
|
||||||
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with_times uname -r #=> 1
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 1 (error)
|
||||||
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with_times uname -r #=> 2
|
||||||
|
stub_called_with_exactly_times uname 2 -r # returns 0 (success)
|
||||||
|
stub_called_with_times uname -r -a #=> 0
|
||||||
|
restore uname
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Function Reference
|
## Function Reference
|
||||||
|
|
||||||
- `stub`: Basic stubbing command. Will echo a default message to STDOUT.
|
### Stubbing and Restoring
|
||||||
Arguments:
|
|
||||||
|
- **`stub`**: Basic stubbing command.
|
||||||
- `$1`: Name of command to stub
|
- `$1`: Name of command to stub
|
||||||
- `$2`: When set to "STDERR", echo to STDERR instead of STDOUT.
|
- `$2`: (optional) When set to "STDERR" or "STDOUT", will echo a default
|
||||||
- `stub_and_echo`: Stub given command and echo a custom string to STDOUT.
|
message to specified output. If no output is specified, nothing is
|
||||||
Arguments:
|
echoed.
|
||||||
|
- **`stub_and_echo`**: Stub given command and echo a custom string to STDOUT.
|
||||||
- `$1`: Name of command to stub.
|
- `$1`: Name of command to stub.
|
||||||
- `$2`: String to echo when stub is called.
|
- `$2`: String to echo when stub is called.
|
||||||
- `$3`: When set to "STDERR", echo to STDERR instead of STDOUT.
|
- `$3`: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||||
- `stub_and_eval`: Stub given command and execute custom commands via eval.
|
- **`stub_and_eval`**: Stub given command and execute custom commands via
|
||||||
Arguments:
|
eval.
|
||||||
- `$1`: Name of command to stub.
|
- `$1`: Name of command to stub.
|
||||||
- `$2`: String to eval when stub is called.
|
- `$2`: String to eval when stub is called.
|
||||||
- `restore`: Restores use of original binary/function that was stubbed.
|
- **`restore`**: Restores use of original binary/function that was stubbed.
|
||||||
Arguments:
|
|
||||||
- `$1`: Name of command to restore.
|
- `$1`: Name of command to restore.
|
||||||
|
|
||||||
|
### Stub Called Validation
|
||||||
|
|
||||||
|
- **`stub_called`**: Check if given stub has been called. Gives a `0` return
|
||||||
|
value when true, and `1` when false.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- **`stub_called_times`**: Find out how many times a stub has been called.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- **`stub_called_exactly_times`**: Validate that stub has been called exactly
|
||||||
|
given number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Exact number of times stub should have been called.
|
||||||
|
- **`stub_called_at_least_times`**: Validate that stub has been called at
|
||||||
|
least X number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Minimum number of times stub should have been called.
|
||||||
|
- **`stub_called_at_most_times`**: Validate that stub has been called no more
|
||||||
|
than X number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Maximum number of times stub should have been called.
|
||||||
|
|
||||||
|
### Stub Called With Validation
|
||||||
|
|
||||||
|
- **`stub_called_with`**: Check if given stub has been called with specified
|
||||||
|
arguments.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
- **`stub_called_with_times`**: Find out how many times a stub has been
|
||||||
|
called with specified arguments.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
- **`stub_called_with_exactly_times`**: Validate that stub has been called
|
||||||
|
with specified arguments exactly given number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Exact number of times stub should have been called.
|
||||||
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
- **`stub_called_with_at_least_times`**: Validate that stub has been called
|
||||||
|
with specified arguments at least X number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Minimum number of times stub should have been called.
|
||||||
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
- **`stub_called_with_at_most_times`**: Validate that stub has been called
|
||||||
|
with specified arguments no more than X number of times.
|
||||||
|
- `$1`: Name of stub to check.
|
||||||
|
- `$2`: Maximum number of times stub should have been called.
|
||||||
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
|
||||||
|
|
||||||
|
## Todo
|
||||||
|
|
||||||
|
- Better ReadMe/Documentation.
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
360
stub.sh
360
stub.sh
@@ -1,9 +1,12 @@
|
|||||||
# !/usr/bin/env bash
|
# !/usr/bin/env bash
|
||||||
# stub.sh 0.1.0 - stubbing helpers for simplifying bash script tests.
|
|
||||||
# Copyright (c) 2014 Jim Myhrberg.
|
|
||||||
#
|
#
|
||||||
|
# stub.sh 1.0.2 - stubbing helpers for simplifying bash script tests.
|
||||||
# https://github.com/jimeh/stub.sh
|
# https://github.com/jimeh/stub.sh
|
||||||
#
|
#
|
||||||
|
# (The MIT License)
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Jim Myhrberg.
|
||||||
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to
|
# of this software and associated documentation files (the "Software"), to
|
||||||
# deal in the Software without restriction, including without limitation the
|
# deal in the Software without restriction, including without limitation the
|
||||||
@@ -24,78 +27,322 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
# Stub given command, echoing a default stub message.
|
# Public: Stub given command.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to stub.
|
# - $1: Name of command to stub.
|
||||||
# - $2: When set to "STDERR", echo to STDERR instead of STDOUT.
|
# - $2: (optional) When set to "STDOUT", echo a default message to STDOUT.
|
||||||
#
|
# When set to "STDERR", echo default message to STDERR.
|
||||||
#
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
stub() {
|
stub() {
|
||||||
stub_and_echo "$1" "$1 stub: \$@" "$2"
|
local redirect="null"
|
||||||
|
if [ "$2" == "stdout" ] || [ "$2" == "STDOUT" ]; then redirect=""; fi
|
||||||
|
if [ "$2" == "stderr" ] || [ "$2" == "STDERR" ]; then redirect="stderr"; fi
|
||||||
|
|
||||||
|
stub_and_echo "$1" "$1 stub: \$@" "$redirect"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Stub given command, and echo given string.
|
# Public: Stub given command, and echo given string.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to stub.
|
# - $1: Name of command to stub.
|
||||||
# - $2: String to echo when stub is called.
|
# - $2: String to echo when stub is called.
|
||||||
# - $3: When set to "STDERR", echo to STDERR instead of STDOUT.
|
# - $3: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||||
|
# When set to "null", all output is redirected to /dev/null.
|
||||||
#
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
stub_and_echo() {
|
stub_and_echo() {
|
||||||
if [ "$3" == "STDERR" ]; then local redirect=" 1>&2"; fi
|
local redirect=""
|
||||||
|
if [ "$3" == "stderr" ] || [ "$3" == "STDERR" ]; then redirect=" 1>&2"; fi
|
||||||
|
if [ "$3" == "null" ]; then redirect=" &>/dev/null"; fi
|
||||||
|
|
||||||
stub_and_eval "$1" "echo \"$2\"$redirect"
|
stub_and_eval "$1" "echo \"$2\"$redirect"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Stub given command, and execute given string with eval.
|
# Public: Stub given command, and execute given string with eval.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to stub.
|
# - $1: Name of command to stub.
|
||||||
# - $2: String to eval when stub is called.
|
# - $2: String to eval when stub is called.
|
||||||
#
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
stub_and_eval() {
|
stub_and_eval() {
|
||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
|
|
||||||
|
# Setup empty list of active stubs.
|
||||||
|
if [ -z "$STUB_ACTIVE_STUBS" ]; then STUB_ACTIVE_STUBS=(); fi
|
||||||
|
|
||||||
# If stubbing a function, store non-stubbed copy of it required for restore.
|
# If stubbing a function, store non-stubbed copy of it required for restore.
|
||||||
if [ -n "$(command -v "$cmd")" ]; then
|
if [ -n "$(command -v "$cmd")" ]; then
|
||||||
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
if [ -z "$(command -v "non_stubbed_${cmd}")" ]; then
|
||||||
local source="$(type "$cmd" | tail -n +2)"
|
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||||
source="${source/$cmd/non_stubbed_${cmd}}"
|
local source="$(type "$cmd" | tail -n +2)"
|
||||||
eval "$source"
|
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||||
|
eval "$source"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use a function to keep track of if the command is stubbed, as variable
|
# Prepare stub index and call list for this stub.
|
||||||
# names doesn't support the "-" character, while function names do.
|
__stub_register "$cmd"
|
||||||
eval "$(echo -e "${cmd}_is_stubbed() {\n return 0\n}")"
|
|
||||||
|
# Keep track of what is currently stubbed to ensure restore only acts on
|
||||||
|
# actual stubs.
|
||||||
|
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $cmd "* ]]; then
|
||||||
|
STUB_ACTIVE_STUBS+=("$cmd")
|
||||||
|
fi
|
||||||
|
|
||||||
# Create the stub.
|
# Create the stub.
|
||||||
eval "$(echo -e "${cmd}() {\n $2\n}")"
|
eval "$( printf "%s" "${cmd}() { __stub_call \"${cmd}\" \$@; $2;}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Restore the original command/function that was stubbed.
|
# Public: Find out if stub has been called. Returns 0 if yes, 1 if no.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) is stub has been called, 1 (error) otherwise.
|
||||||
|
stub_called() {
|
||||||
|
if [ "$(stub_called_times "$1")" -lt 1 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called with specific arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# stub uname
|
||||||
|
# uname
|
||||||
|
# uname -r -a
|
||||||
|
# stub_called_with uname # Returns 0 (success).
|
||||||
|
# stub_called_with uname -r # Returns 1 (error).
|
||||||
|
# stub_called_with uname -r -a # Returns 0 (success).
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if specified stub has been called with given arguments,
|
||||||
|
# otherwise returns 1 (error).
|
||||||
|
stub_called_with() {
|
||||||
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt 1 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out how many times a stub has been called.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
#
|
||||||
|
# Echoes number of times stub has been called if $2 is not given, otherwise
|
||||||
|
# echoes nothing.
|
||||||
|
# Returns 0 (success) if $2 is not given, or if it is given and it matches the
|
||||||
|
# number of times the stub has been called. Otherwise 1 (error) is returned if
|
||||||
|
# it doesn't match..
|
||||||
|
stub_called_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
local index="$(__stub_index "$1")"
|
||||||
|
local count=0
|
||||||
|
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "count=\"\${#STUB_${index}_CALLS[@]}\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called exactly the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Exact number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_exactly_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" != "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called at least the given number of times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times, otherwise 1 (error) is returned.
|
||||||
|
stub_called_at_least_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" -lt "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
|
# times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
|
# times, otherwise 1 (error) is returned.
|
||||||
|
stub_called_at_most_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" -gt "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out how many times a stub has been called with specific
|
||||||
|
# arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes number of times stub has been called with given arguments.
|
||||||
|
# Return 0 (success).
|
||||||
|
stub_called_with_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" == "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local count=0
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
|
||||||
|
for call in "${calls[@]}"; do
|
||||||
|
if [ "$call" == "$args" ]; then ((count++)); fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called exactly the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Exact number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_exactly_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" != "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called at least the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_at_least_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
|
# times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_at_most_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -gt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Restore the original command/function that was stubbed.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of command to restore.
|
# - $1: Name of command to restore.
|
||||||
#
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
restore() {
|
restore() {
|
||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
|
|
||||||
# Don't do anything if the command isn't currently stubbed.
|
# Don't do anything if the command isn't currently stubbed.
|
||||||
if [[ "$(type "${cmd}_is_stubbed" 2>&1)" == *"not found"* ]]; then
|
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $1 "* ]]; then
|
||||||
return 0;
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove stub functions.
|
# Remove stub functions.
|
||||||
unset -f "${cmd}_is_stubbed"
|
|
||||||
unset -f "$cmd"
|
unset -f "$cmd"
|
||||||
|
|
||||||
|
# Remove stub from list of active stubs.
|
||||||
|
STUB_ACTIVE_STUBS=(${STUB_ACTIVE_STUBS[@]/$cmd/})
|
||||||
|
|
||||||
# If stub was for a function, restore the original function.
|
# If stub was for a function, restore the original function.
|
||||||
if type "non_stubbed_${cmd}" &>/dev/null; then
|
if type "non_stubbed_${cmd}" &>/dev/null; then
|
||||||
if [[ "$(type "non_stubbed_${cmd}" | head -1)" == *"is a function" ]]; then
|
local original_type="$(type "non_stubbed_${cmd}" | head -1)"
|
||||||
|
if [[ "$original_type" == *"is a function" ]]; then
|
||||||
local source="$(type "non_stubbed_$cmd" | tail -n +2)"
|
local source="$(type "non_stubbed_$cmd" | tail -n +2)"
|
||||||
source="${source/non_stubbed_${cmd}/$cmd}"
|
source="${source/non_stubbed_${cmd}/$cmd}"
|
||||||
eval "$source"
|
eval "$source"
|
||||||
@@ -103,3 +350,70 @@ restore() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Internal functions
|
||||||
|
#
|
||||||
|
|
||||||
|
# Private: Used to keep track of which stubs have been called and how many
|
||||||
|
# times.
|
||||||
|
__stub_call() {
|
||||||
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" == "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "STUB_${index}_CALLS+=(\"\$args\")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Get index value of stub. Required to access list of stub calls.
|
||||||
|
__stub_index() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
for item in ${STUB_INDEX[@]}; do
|
||||||
|
if [[ "$item" == "${cmd}="* ]]; then
|
||||||
|
local index="$item"
|
||||||
|
index="${index/${cmd}=/}"
|
||||||
|
echo "$index"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Prepare for the creation of a new stub. Adds stub to index and
|
||||||
|
# sets up an empty call list.
|
||||||
|
__stub_register() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
if [ -z "$STUB_NEXT_INDEX" ]; then STUB_NEXT_INDEX=0; fi
|
||||||
|
if [ -z "$STUB_INDEX" ]; then STUB_INDEX=(); fi
|
||||||
|
|
||||||
|
# Clean up after any previous stub for the same command.
|
||||||
|
__stub_clean "$cmd"
|
||||||
|
|
||||||
|
# Add stub to index.
|
||||||
|
STUB_INDEX+=("${cmd}=${STUB_NEXT_INDEX}")
|
||||||
|
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
|
||||||
|
|
||||||
|
# Increment stub count.
|
||||||
|
((STUB_NEXT_INDEX++))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Private: Cleans out and removes a stub's call list, and removes stub from
|
||||||
|
# index.
|
||||||
|
__stub_clean() {
|
||||||
|
local cmd="$1"
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
|
||||||
|
# Remove all relevant details from any previously existing stub for the same
|
||||||
|
# command.
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "unset STUB_${index}_CALLS"
|
||||||
|
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|||||||
34
test.sh
34
test.sh
@@ -1,34 +0,0 @@
|
|||||||
#! /usr/bin/env bash
|
|
||||||
|
|
||||||
resolve_link() {
|
|
||||||
$(type -p greadlink readlink | head -1) $1
|
|
||||||
}
|
|
||||||
|
|
||||||
abs_dirname() {
|
|
||||||
local cwd="$(pwd)"
|
|
||||||
local path="$1"
|
|
||||||
|
|
||||||
while [ -n "$path" ]; do
|
|
||||||
cd "${path%/*}"
|
|
||||||
local name="${path##*/}"
|
|
||||||
path="$(resolve_link "$name" || true)"
|
|
||||||
done
|
|
||||||
|
|
||||||
pwd
|
|
||||||
cd "$cwd"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
testdir="$(abs_dirname "$0")/test"
|
|
||||||
testfiles="$(find "$testdir" -name "*-test.sh")"
|
|
||||||
|
|
||||||
RET=0
|
|
||||||
for testfile in $testfiles; do
|
|
||||||
echo ""
|
|
||||||
echo "running: ${testfile/#$(dirname "$testdir")\//}"
|
|
||||||
cd "$(dirname "$testfile")"
|
|
||||||
"$testfile"
|
|
||||||
if [ "$?" != "0" ]; then RET=1; fi
|
|
||||||
done
|
|
||||||
echo ""
|
|
||||||
exit $RET
|
|
||||||
21
test/__stub_call.test.sh
Executable file
21
test/__stub_call.test.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_call() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Adds call to stub call list.
|
||||||
|
STUB_INDEX=("uname=0")
|
||||||
|
STUB_0_CALLS=()
|
||||||
|
__stub_call "uname"
|
||||||
|
__stub_call "uname" -r
|
||||||
|
__stub_call "uname" -r -a
|
||||||
|
assert 'echo ${STUB_0_CALLS[@]}' "<none> -r -r -a"
|
||||||
|
assert 'echo ${STUB_0_CALLS[0]}' "<none>"
|
||||||
|
assert 'echo ${STUB_0_CALLS[1]}' "-r"
|
||||||
|
assert 'echo ${STUB_0_CALLS[2]}' "-r -a"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_call()"
|
||||||
19
test/__stub_clean.test.sh
Executable file
19
test/__stub_clean.test.sh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_clean() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Removes unsets stub call list, removes stub from index
|
||||||
|
STUB_INDEX=("uname=0" "top=1")
|
||||||
|
STUB_0_CALLS=("<none>" "-r" "-r -a")
|
||||||
|
STUB_1_CALLS=("-h")
|
||||||
|
__stub_clean "uname"
|
||||||
|
assert 'echo ${STUB_INDEX[@]}' "top=1"
|
||||||
|
assert 'echo ${STUB_INDEX[0]}' "top=1"
|
||||||
|
assert 'echo ${STUB_0_CALLS[@]}' ""
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_clean()"
|
||||||
21
test/__stub_index.test.sh
Executable file
21
test/__stub_index.test.sh
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_index() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Echoes index of given stub.
|
||||||
|
STUB_INDEX=("uname=1" "top=3")
|
||||||
|
assert '__stub_index "uname"' "1"
|
||||||
|
assert '__stub_index "top"' "3"
|
||||||
|
unset STUB_INDEX
|
||||||
|
|
||||||
|
# Echoes nothing if stub is not in the index.
|
||||||
|
STUB_INDEX=("uname=1")
|
||||||
|
assert '__stub_index "top"' ""
|
||||||
|
unset STUB_INDEX
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_index()"
|
||||||
22
test/__stub_register.test.sh
Executable file
22
test/__stub_register.test.sh
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# __stub_register() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Sets up stub index, stub call list, and adds stub to index.
|
||||||
|
__stub_register "uname"
|
||||||
|
__stub_register "top"
|
||||||
|
assert 'echo ${STUB_INDEX[@]}' 'uname=0 top=1'
|
||||||
|
assert 'echo ${STUB_INDEX[0]}' 'uname=0'
|
||||||
|
assert 'echo ${STUB_INDEX[1]}' 'top=1'
|
||||||
|
assert 'echo $STUB_NEXT_INDEX' "2"
|
||||||
|
|
||||||
|
# Note: There seems to be no possible way to validate if a empty array
|
||||||
|
# variable has been set, as it appears to be empty/null/undefined whatever I
|
||||||
|
# try.
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "__stub_register()"
|
||||||
@@ -10,16 +10,16 @@ source "test-helper.sh"
|
|||||||
my-name-is() { echo "My name is $@."; }
|
my-name-is() { echo "My name is $@."; }
|
||||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||||
|
|
||||||
stub "my-name-is"
|
stub "my-name-is" stdout
|
||||||
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
||||||
|
|
||||||
restore "my-name-is"
|
restore "my-name-is" stdout
|
||||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||||
|
|
||||||
|
|
||||||
# Stubbing and restoring a executable file.
|
# Stubbing and restoring a executable file.
|
||||||
actual_uname="$(uname)"
|
actual_uname="$(uname)"
|
||||||
stub "uname"
|
stub "uname" stdout
|
||||||
assert "uname" "uname stub: "
|
assert "uname" "uname stub: "
|
||||||
assert "uname -a" "uname stub: -a"
|
assert "uname -a" "uname stub: -a"
|
||||||
|
|
||||||
@@ -43,5 +43,17 @@ restore "my-name-is"
|
|||||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||||
|
|
||||||
|
|
||||||
|
# Stubbing the same function multiple times and then restoring it.
|
||||||
|
my-name-is() { echo "My name is $@."; }
|
||||||
|
stub "my-name-is"
|
||||||
|
assert "my-name-is Edward Elric" ""
|
||||||
|
stub "my-name-is" stdout
|
||||||
|
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
||||||
|
|
||||||
|
restore "my-name-is"
|
||||||
|
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# End of tests.
|
# End of tests.
|
||||||
assert_end "restore()"
|
assert_end "restore()"
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#! /usr/bin/env bash
|
|
||||||
source "test-helper.sh"
|
|
||||||
|
|
||||||
#
|
|
||||||
# stub() tests.
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
# Stubbing a bash function.
|
|
||||||
my-name-is() { echo "My name is $@."; }
|
|
||||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
|
||||||
|
|
||||||
stub "my-name-is"
|
|
||||||
assert "my-name-is" "my-name-is stub: "
|
|
||||||
assert "my-name-is Edward" "my-name-is stub: Edward"
|
|
||||||
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
|
||||||
unset -f my-name-is
|
|
||||||
|
|
||||||
|
|
||||||
# Stubbing a executable file.
|
|
||||||
stub "uname"
|
|
||||||
assert "uname" "uname stub: "
|
|
||||||
assert "uname -h" "uname stub: -h"
|
|
||||||
unset -f uname
|
|
||||||
|
|
||||||
|
|
||||||
# Redirect stub output to STDERR.
|
|
||||||
my-name-is() { echo "My name is $@."; }
|
|
||||||
stub "my-name-is" STDERR
|
|
||||||
assert "my-name-is Edward" ""
|
|
||||||
assert "my-name-is Edward 2>&1" "my-name-is stub: Edward"
|
|
||||||
unset -f my-name-is
|
|
||||||
|
|
||||||
|
|
||||||
# Stubbing something that doesn't exist.
|
|
||||||
stub "cowabunga-dude"
|
|
||||||
assert "cowabunga-dude yeah dude" "cowabunga-dude stub: yeah dude"
|
|
||||||
unset -f cowabunga-dude
|
|
||||||
|
|
||||||
|
|
||||||
# End of tests.
|
|
||||||
assert_end "stub()"
|
|
||||||
72
test/stub.test.sh
Executable file
72
test/stub.test.sh
Executable file
@@ -0,0 +1,72 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
# Stubbing a bash function.
|
||||||
|
my-name-is() { echo "My name is $@."; }
|
||||||
|
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||||
|
stub "my-name-is"
|
||||||
|
assert "my-name-is" ""
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Stubbing a executable file.
|
||||||
|
stub "uname"
|
||||||
|
assert "uname" ""
|
||||||
|
unset -f uname
|
||||||
|
|
||||||
|
|
||||||
|
# Redirect stub of bash function output to STDOUT.
|
||||||
|
my-name-is() { echo "My name is $@."; }
|
||||||
|
stub "my-name-is" STDOUT
|
||||||
|
assert "my-name-is" "my-name-is stub: "
|
||||||
|
assert "my-name-is Edward" "my-name-is stub: Edward"
|
||||||
|
assert "my-name-is Edward Elric" "my-name-is stub: Edward Elric"
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Redirect stub of executable file output to STDOUT.
|
||||||
|
stub "uname" STDOUT
|
||||||
|
assert "uname" "uname stub: "
|
||||||
|
assert "uname -r" "uname stub: -r"
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Redirect stub of bash function output to STDERR.
|
||||||
|
my-name-is() { echo "My name is $@."; }
|
||||||
|
stub "my-name-is" STDERR
|
||||||
|
assert "my-name-is Edward" ""
|
||||||
|
assert "my-name-is Edward 2>&1" "my-name-is stub: Edward"
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Redirect stub of executable output to STDERR.
|
||||||
|
stub "uname" STDERR
|
||||||
|
assert "uname -r" ""
|
||||||
|
assert "uname 2>&1" "uname stub: "
|
||||||
|
assert "uname -r 2>&1" "uname stub: -r"
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Redirect stub of bash function output to /dev/null.
|
||||||
|
my-name-is() { echo "My name is $@."; }
|
||||||
|
stub "my-name-is" null
|
||||||
|
assert "my-name-is Edward" ""
|
||||||
|
assert "my-name-is Edward 2>&1" ""
|
||||||
|
unset -f my-name-is
|
||||||
|
|
||||||
|
|
||||||
|
# Stubbing something that doesn't exist.
|
||||||
|
assert_raises "cowabunga-dude" 127
|
||||||
|
stub "cowabunga-dude" stdout
|
||||||
|
assert_raises "cowabunga-dude" 0
|
||||||
|
assert "cowabunga-dude yeah dude" "cowabunga-dude stub: yeah dude"
|
||||||
|
unset -f cowabunga-dude
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub()"
|
||||||
44
test/stub_called.test.sh
Executable file
44
test/stub_called.test.sh
Executable file
@@ -0,0 +1,44 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Returns 1 when stub doesn't exist.
|
||||||
|
assert_raises 'stub_called "uname"' 1
|
||||||
|
|
||||||
|
# Returns 1 when stub hasn't been called.
|
||||||
|
stub "uname"
|
||||||
|
assert_raises 'stub_called "uname"' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Returns 0 when stub has been called.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
assert_raises 'stub_called "uname"' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Stub called state is reset by creating a new stub, not by restore.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
restore "uname"
|
||||||
|
assert_raises 'stub_called "uname"' 0
|
||||||
|
stub "uname"
|
||||||
|
assert_raises 'stub_called "uname"' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Recreating a stub only resets called state of recreated stub.
|
||||||
|
stub "uname"
|
||||||
|
stub "top"
|
||||||
|
uname
|
||||||
|
top
|
||||||
|
stub "uname"
|
||||||
|
assert_raises 'stub_called "uname"' 1
|
||||||
|
assert_raises 'stub_called "top"' 0
|
||||||
|
restore "uname"
|
||||||
|
restore "top"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called()"
|
||||||
32
test/stub_called_at_least_times.test.sh
Executable file
32
test/stub_called_at_least_times.test.sh
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_at_least_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname
|
||||||
|
|
||||||
|
# Returns 0 when stub called at least given number of times.
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 0' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 1' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 2' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has been called less than given number of times.
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 3' 1
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 4' 1
|
||||||
|
assert_raises 'stub_called_at_least_times "uname" 5' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_at_least_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_at_least_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_at_least_times()"
|
||||||
33
test/stub_called_at_most_times.test.sh
Executable file
33
test/stub_called_at_most_times.test.sh
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_at_most_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname
|
||||||
|
uname
|
||||||
|
|
||||||
|
# Returns 0 when stub called no more than given number of times.
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 5' 0
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 4' 0
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 3' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has been called more than given number of times.
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 2' 1
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 1' 1
|
||||||
|
assert_raises 'stub_called_at_most_times "uname" 0' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_at_most_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_at_most_times "top" 1' 0
|
||||||
|
|
||||||
|
# Teardown
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_at_most_times()"
|
||||||
32
test/stub_called_exactly_times.test.sh
Executable file
32
test/stub_called_exactly_times.test.sh
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_exactly_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
|
||||||
|
|
||||||
|
# Returns 0 when stub called exactly given number of times.
|
||||||
|
assert_raises 'stub_called_exactly_times "uname" 2' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has not been called the exact given number of times.
|
||||||
|
assert_raises 'stub_called_exactly_times "uname" 4' 1
|
||||||
|
assert_raises 'stub_called_exactly_times "uname" 3' 1
|
||||||
|
assert_raises 'stub_called_exactly_times "uname" 1' 1
|
||||||
|
assert_raises 'stub_called_exactly_times "uname" 0' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_exactly_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_exactly_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_times()"
|
||||||
34
test/stub_called_times.test.sh
Executable file
34
test/stub_called_times.test.sh
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Echoes 0 if the command isn't stubed.
|
||||||
|
assert 'stub_called_times "top"' "0"
|
||||||
|
|
||||||
|
# Echoes the number of times a stub was called.
|
||||||
|
stub "uname"
|
||||||
|
assert 'stub_called_times "uname"' "0"
|
||||||
|
uname
|
||||||
|
assert 'stub_called_times "uname"' "1"
|
||||||
|
uname
|
||||||
|
assert 'stub_called_times "uname"' "2"
|
||||||
|
uname
|
||||||
|
assert 'stub_called_times "uname"' "3"
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Echoes 0 after a called stub has been recreated.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
assert 'stub_called_times "uname"' "1"
|
||||||
|
restore "uname"
|
||||||
|
assert 'stub_called_times "uname"' "1"
|
||||||
|
stub "uname"
|
||||||
|
assert 'stub_called_times "uname"' "0"
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_times()"
|
||||||
54
test/stub_called_with.test.sh
Executable file
54
test/stub_called_with.test.sh
Executable file
@@ -0,0 +1,54 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Returns 1 when stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with "top"' 1
|
||||||
|
|
||||||
|
# Returns 0 when stub has been called with given arguments.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
assert_raises 'stub_called_with "uname"' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r -a' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Returns 1 when stub has not been called with given arguments.
|
||||||
|
stub "uname"
|
||||||
|
uname -r
|
||||||
|
assert_raises 'stub_called_with "uname"' 1
|
||||||
|
assert_raises 'stub_called_with "uname" -a' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Only matches against exact argument lists.
|
||||||
|
stub "uname"
|
||||||
|
uname -r -a
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 1
|
||||||
|
assert_raises 'stub_called_with "uname" -r -a' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Call history is only reset when restubbing a command, not when restoring.
|
||||||
|
stub "uname"
|
||||||
|
uname -r
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
restore "uname"
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 0
|
||||||
|
stub "uname"
|
||||||
|
assert_raises 'stub_called_with "uname" -r' 1
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
# Handling of string arguments containing spaces.
|
||||||
|
stub "uname"
|
||||||
|
uname -r "foo bar"
|
||||||
|
assert_raises 'stub_called_with "uname" -r "foo bar"' 0
|
||||||
|
assert_raises 'stub_called_with "uname" -r foo bar' 0
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with()"
|
||||||
34
test/stub_called_with_at_least_times.test.sh
Executable file
34
test/stub_called_with_at_least_times.test.sh
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_at_least_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# Retruns 0 when stub called with at least given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 0 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 1 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 2 -r' 0
|
||||||
|
|
||||||
|
# Retruns 1 when stub called with less than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 3 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 4 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_least_times "uname" 5 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_at_least_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_at_least_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_at_least_times()"
|
||||||
35
test/stub_called_with_at_most_times.test.sh
Executable file
35
test/stub_called_with_at_most_times.test.sh
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_at_most_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# Returns 0 when stub called no more than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 5 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 4 -r' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 3 -r' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has been called more than given number of times.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 2 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 1 -r' 1
|
||||||
|
assert_raises 'stub_called_with_at_most_times "uname" 0 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_at_most_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_at_most_times "top" 1' 0
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_at_most_times()"
|
||||||
33
test/stub_called_with_exactly_times.test.sh
Executable file
33
test/stub_called_with_exactly_times.test.sh
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_exactly_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Setup.
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
|
||||||
|
# Returns 0 when stub called exactly given number of times.
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 2 -r' 0
|
||||||
|
|
||||||
|
# Returns 1 when stub has not been called the exact given number of times.
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 4 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 3 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 1 -r' 1
|
||||||
|
assert_raises 'stub_called_with_exactly_times "uname" 0 -r' 1
|
||||||
|
|
||||||
|
# Behaves as if stub has not been called when the stub doesn't exist.
|
||||||
|
assert_raises 'stub_called_with_exactly_times "top" 0' 0
|
||||||
|
assert_raises 'stub_called_with_exactly_times "top" 1' 1
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_exactly_times()"
|
||||||
43
test/stub_called_with_times.test.sh
Executable file
43
test/stub_called_with_times.test.sh
Executable file
@@ -0,0 +1,43 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
source "test-helper.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# stub_called_with_times() tests.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Echoes 0 when stub doesn't exist.
|
||||||
|
assert 'stub_called_with_times "cowabunga-dude"' "0"
|
||||||
|
|
||||||
|
# Echoes how many times a stub has been called with given arguments
|
||||||
|
stub "uname"
|
||||||
|
uname
|
||||||
|
uname -r
|
||||||
|
uname -r
|
||||||
|
uname -r -a
|
||||||
|
uname -r -a
|
||||||
|
uname -r -a
|
||||||
|
assert 'stub_called_with_times "uname"' "1"
|
||||||
|
assert 'stub_called_with_times "uname" -r' "2"
|
||||||
|
assert 'stub_called_with_times "uname" -r -a' "3"
|
||||||
|
assert 'stub_called_with_times "uname" -a' "0"
|
||||||
|
|
||||||
|
# Keeps track of identical argument calls to different stubs.
|
||||||
|
stub "top"
|
||||||
|
top
|
||||||
|
top
|
||||||
|
top -r
|
||||||
|
top -r
|
||||||
|
top -r
|
||||||
|
top -r -a
|
||||||
|
assert 'stub_called_with_times "top"' "2"
|
||||||
|
assert 'stub_called_with_times "top" -r' "3"
|
||||||
|
assert 'stub_called_with_times "top" -r -a' "1"
|
||||||
|
assert 'stub_called_with_times "top" -a' "0"
|
||||||
|
|
||||||
|
# Teardown.
|
||||||
|
restore "uname"
|
||||||
|
restore "top"
|
||||||
|
|
||||||
|
|
||||||
|
# End of tests.
|
||||||
|
assert_end "stub_called_with_times()"
|
||||||
Reference in New Issue
Block a user