mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3663e1488b | |||
|
|
14b1b07d45 | ||
| cdc428f859 | |||
|
|
30db3416ea | ||
|
|
b75a952a20 | ||
| b6a1ae189c | |||
|
|
bd6f3c4666 | ||
| 89aed2999c | |||
| de104f7cd5 | |||
| 58e1b8dfa7 | |||
| e8388b911b | |||
| d9e3e14c76 | |||
|
|
f46506d00e | ||
|
|
59a69b883f | ||
| 1f6dc46f3f | |||
| 332cfb8d28 | |||
| f3e0f666cb | |||
| 47ce28bba6 | |||
| 6e8e9fcfcf | |||
|
|
0ea845751e | ||
| 3b02e60207 | |||
| 78f31d9bc9 | |||
| e5382ea2dc | |||
| 9a34d8fa90 | |||
| 8c18d4075c | |||
| 407fefcb34 | |||
| ef607e8eec | |||
| 49ec034829 | |||
| 5ac835a822 | |||
| 51f6c8776e |
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: INVARIANT=1 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/v1.0.2/assert.sh \
|
test/assert.sh:
|
||||||
)
|
echo "fetching assert.sh..." && \
|
||||||
|
curl -s -L -o test/assert.sh \
|
||||||
|
https://raw.github.com/lehmannro/assert.sh/v1.1/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
|
||||||
|
|||||||
106
README.md
106
README.md
@@ -1,4 +1,4 @@
|
|||||||
# stub.sh [](https://travis-ci.org/jimeh/stub.sh)
|
# stub.sh [](https://travis-ci.org/jimeh/stub.sh)
|
||||||
|
|
||||||
Helpers for bash script testing to stub/fake binaries and functions. Includes
|
Helpers for bash script testing to stub/fake binaries and functions. Includes
|
||||||
support for validating number of stub calls, and/or if stub has been called
|
support for validating number of stub calls, and/or if stub has been called
|
||||||
@@ -7,6 +7,34 @@ 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.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
The `stub.sh` script can be installed in several ways:
|
||||||
|
|
||||||
|
- Downloading the "raw" `stub.sh` file from Github
|
||||||
|
- Using `bpkg`
|
||||||
|
|
||||||
|
### Downloading the `stub.sh` file
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -LO 'https://github.com/jimeh/stub.sh/raw/master/stub.sh'
|
||||||
|
```
|
||||||
|
|
||||||
|
(Or manually open https://github.com/jimeh/stub.sh/raw/master/stub.sh in a webbrowser).
|
||||||
|
|
||||||
|
### Using bpkg
|
||||||
|
|
||||||
|
[BPKG](http://www.bpkg.io/) is a lightweight package manager for bash.
|
||||||
|
|
||||||
|
`stub.sh` is available as [bpkg](http://www.bpkg.io/) package:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
bpkg install jimeh/stub.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
This will install `stub.sh` at `./deps/stub.sh/stub.sh` in the directory that `bpkg` has been run in.
|
||||||
|
|
||||||
|
To install globally run: `bpkg install -g jimeh/stub.sh`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -25,7 +53,7 @@ uname #=> Darwin
|
|||||||
stub uname # silent stub
|
stub uname # silent stub
|
||||||
uname #=>
|
uname #=>
|
||||||
stub uname STDOUT # stub prints to STDOUT
|
stub uname STDOUT # stub prints to STDOUT
|
||||||
uname #=> uname stub:
|
uname #=> uname stub:
|
||||||
uname -r #=> uname stub: -r
|
uname -r #=> uname stub: -r
|
||||||
restore uname # remove stub
|
restore uname # remove stub
|
||||||
uname #=> Darwin
|
uname #=> Darwin
|
||||||
@@ -38,7 +66,7 @@ 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 # silent stub
|
stub my-name-is # silent stub
|
||||||
my-name-is Edward Elric #=>
|
my-name-is Edward Elric #=>
|
||||||
stub my-name-is STDOUT # stub prints to STDOUT
|
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 # remove stub
|
restore my-name-is # remove stub
|
||||||
@@ -63,52 +91,71 @@ Asserting stub has been called X times:
|
|||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
my-uname() { uname; }
|
my-uname() { uname; }
|
||||||
stub_and_echo uname "FooBar"
|
stub_and_echo uname "FooBar"
|
||||||
stub_called_times uname #=> 0
|
stub_called_times uname #=> 0
|
||||||
stub_called_times uname 2 # returns 1 (error)
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
my-uname #=> FooBar
|
my-uname #=> FooBar
|
||||||
stub_called_times uname #=> 1
|
stub_called_times uname #=> 1
|
||||||
stub_called_times uname 2 # returns 1 (error)
|
stub_called_exactly_times uname 2 # returns 1 (error)
|
||||||
my-uname #=> FooBar
|
my-uname #=> FooBar
|
||||||
stub_called_times uname #=> 2
|
stub_called_times uname #=> 2
|
||||||
stub_called_times uname 2 # returns 0 (success)
|
stub_called_exactly_times uname 2 # returns 0 (success)
|
||||||
restore uname
|
restore uname
|
||||||
```
|
```
|
||||||
|
|
||||||
Asserting stub has been called with specific attributes:
|
Asserting stub has been called with specific arguments:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
source "stub.sh"
|
source "stub.sh"
|
||||||
my-uname() { uname $@; }
|
my-uname() { uname $@; }
|
||||||
stub_and_echo uname "FooBar"
|
stub_and_echo uname "FooBar"
|
||||||
stub_called_with uname -r # returns 1 (error)
|
|
||||||
my-uname -r -a #=> FooBar
|
|
||||||
stub_called_with uname -r -a # returns 1 (error)
|
stub_called_with uname -r -a # returns 1 (error)
|
||||||
stub_called_with uname -r # 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
|
my-uname -r #=> FooBar
|
||||||
|
stub_called_with uname -r -a # returns 0 (success)
|
||||||
stub_called_with uname -r # returns 0 (success)
|
stub_called_with uname -r # returns 0 (success)
|
||||||
restore uname
|
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
|
||||||
|
|
||||||
### Stubbing and Restoring
|
### Stubbing and Restoring
|
||||||
|
|
||||||
- **`stub`**: Basic stubbing command. Will echo a default message to STDOUT.
|
- **`stub`**: Basic stubbing command.
|
||||||
Arguments:
|
|
||||||
- `$1`: Name of command to stub
|
- `$1`: Name of command to stub
|
||||||
- `$2`: When set to "STDERR", echo to STDERR instead of STDOUT (optional).
|
- `$2`: (optional) When set to "STDERR" or "STDOUT", will echo a default
|
||||||
|
message to specified output. If no output is specified, nothing is
|
||||||
|
echoed.
|
||||||
- **`stub_and_echo`**: Stub given command and echo a custom string to STDOUT.
|
- **`stub_and_echo`**: Stub given command and echo a custom string to STDOUT.
|
||||||
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 (optional).
|
- `$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 Validation
|
||||||
@@ -136,26 +183,31 @@ restore uname
|
|||||||
- **`stub_called_with`**: Check if given stub has been called with specified
|
- **`stub_called_with`**: Check if given stub has been called with specified
|
||||||
arguments.
|
arguments.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: 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
|
- **`stub_called_with_times`**: Find out how many times a stub has been
|
||||||
called with specified arguments.
|
called with specified arguments.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
- **`stub_called_with_exactly_times`**: Validate that stub has been called
|
- **`stub_called_with_exactly_times`**: Validate that stub has been called
|
||||||
with specified arguments exactly given number of times.
|
with specified arguments exactly given number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Exact number of times stub should have been called.
|
- `$2`: Exact number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: 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
|
- **`stub_called_with_at_least_times`**: Validate that stub has been called
|
||||||
with specified arguments at least X number of times.
|
with specified arguments at least X number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Minimum number of times stub should have been called.
|
- `$2`: Minimum number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: 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
|
- **`stub_called_with_at_most_times`**: Validate that stub has been called
|
||||||
with specified arguments no more than X number of times.
|
with specified arguments no more than X number of times.
|
||||||
- `$1`: Name of stub to check.
|
- `$1`: Name of stub to check.
|
||||||
- `$2`: Maximum number of times stub should have been called.
|
- `$2`: Maximum number of times stub should have been called.
|
||||||
- `$@`: All additional arguments are used to specify what stub was with.
|
- `$@`: Any/all additional arguments are used to specify what stub was
|
||||||
|
called with.
|
||||||
|
|
||||||
|
|
||||||
## Todo
|
## Todo
|
||||||
|
|||||||
9
package.json
Normal file
9
package.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "stub.sh",
|
||||||
|
"description": "Utilities to mock/fake/stub binaries and functions for testing with BASH.",
|
||||||
|
"author": "Jim Myhrberg <contact@jimeh.me>",
|
||||||
|
"license": "MIT",
|
||||||
|
"homepage": "https://github.com/jimeh/stub.sh",
|
||||||
|
"scripts": [],
|
||||||
|
"files": ["README.md", "stub.sh"]
|
||||||
|
}
|
||||||
61
stub.sh
61
stub.sh
@@ -1,5 +1,6 @@
|
|||||||
# !/usr/bin/env bash
|
# !/usr/bin/env bash
|
||||||
# stub.sh 1.0.0 - stubbing helpers for simplifying bash script tests.
|
#
|
||||||
|
# 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)
|
# (The MIT License)
|
||||||
@@ -26,19 +27,19 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
# Public: 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 "null", all output is redirected to /dev/null.
|
# When set to "STDERR", echo default message to STDERR.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
stub() {
|
stub() {
|
||||||
local redirect="null"
|
local redirect="null"
|
||||||
if [ "$2" == "stdout" ] || [ "$2" == "STDOUT" ]; then redirect=""; fi
|
if [ "$2" = "stdout" ] || [ "$2" = "STDOUT" ]; then redirect=""; fi
|
||||||
if [ "$2" == "stderr" ] || [ "$2" == "STDERR" ]; then redirect="stderr"; fi
|
if [ "$2" = "stderr" ] || [ "$2" = "STDERR" ]; then redirect="stderr"; fi
|
||||||
|
|
||||||
stub_and_echo "$1" "$1 stub: \$@" "$redirect"
|
stub_and_echo "$1" "$1 stub: \$@" "$redirect"
|
||||||
}
|
}
|
||||||
@@ -49,15 +50,15 @@ stub() {
|
|||||||
# 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.
|
# When set to "null", all output is redirected to /dev/null.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
stub_and_echo() {
|
stub_and_echo() {
|
||||||
local redirect=""
|
local redirect=""
|
||||||
if [ "$3" == "stderr" ] || [ "$3" == "STDERR" ]; then redirect=" 1>&2"; fi
|
if [ "$3" = "stderr" ] || [ "$3" = "STDERR" ]; then redirect=" 1>&2"; fi
|
||||||
if [ "$3" == "null" ]; then redirect=" &>/dev/null"; fi
|
if [ "$3" = "null" ]; then redirect=" &>/dev/null"; fi
|
||||||
|
|
||||||
stub_and_eval "$1" "echo \"$2\"$redirect"
|
stub_and_eval "$1" "echo \"$2\"$redirect"
|
||||||
}
|
}
|
||||||
@@ -98,7 +99,7 @@ stub_and_eval() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Create the stub.
|
# Create the stub.
|
||||||
eval "$(echo -e "${cmd}() {\n __stub_call \"${cmd}\" \$@\n $2\n}")"
|
eval "$( printf "%s" "${cmd}() { __stub_call \"${cmd}\" \$@; $2;}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -120,11 +121,11 @@ stub_called() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# stub "uname"
|
# stub uname
|
||||||
# uname
|
# uname
|
||||||
# uname -r -a
|
# uname -r -a
|
||||||
# stub_called_with uname # Returns 0 (success).
|
# stub_called_with uname # Returns 0 (success).
|
||||||
@@ -148,13 +149,6 @@ stub_called_with() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: When specified, will check if stub was called exactly the given
|
|
||||||
# number of times (optional).
|
|
||||||
#
|
|
||||||
# Examples:
|
|
||||||
# stub_called_times "uname" # Echoes "2" if stub has been called twice.
|
|
||||||
# stub_called_times "uname" 2 # Returns 0 (success).
|
|
||||||
# stub_called_times "uname" 3 # Returns 1 (error).
|
|
||||||
#
|
#
|
||||||
# Echoes number of times stub has been called if $2 is not given, otherwise
|
# Echoes number of times stub has been called if $2 is not given, otherwise
|
||||||
# echoes nothing.
|
# echoes nothing.
|
||||||
@@ -230,8 +224,8 @@ stub_called_at_most_times() {
|
|||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes number of times stub has been called with given arguments.
|
# Echoes number of times stub has been called with given arguments.
|
||||||
# Return 0 (success).
|
# Return 0 (success).
|
||||||
@@ -240,14 +234,14 @@ stub_called_with_times() {
|
|||||||
|
|
||||||
shift 1
|
shift 1
|
||||||
local args="$@"
|
local args="$@"
|
||||||
if [ "$args" == "" ]; then args="<none>"; fi
|
if [ "$args" = "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
local count=0
|
local count=0
|
||||||
local index="$(__stub_index "$cmd")"
|
local index="$(__stub_index "$cmd")"
|
||||||
if [ -n "$index" ]; then
|
if [ -n "$index" ]; then
|
||||||
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
|
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
|
||||||
for call in "${calls[@]}"; do
|
for call in "${calls[@]}"; do
|
||||||
if [ "$call" == "$args" ]; then ((count++)); fi
|
if [ "$call" = "$args" ]; then ((count++)); fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -261,8 +255,8 @@ stub_called_with_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Exact number of times stub has been called.
|
# - $2: Exact number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called at least the given number of
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
@@ -284,8 +278,8 @@ stub_called_with_exactly_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Minimum required number of times stub has been called.
|
# - $2: Minimum required number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called at least the given number of
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
@@ -307,8 +301,8 @@ stub_called_with_at_least_times() {
|
|||||||
# Arguments:
|
# Arguments:
|
||||||
# - $1: Name of stubbed command.
|
# - $1: Name of stubbed command.
|
||||||
# - $2: Maximum allowed number of times stub has been called.
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
# - $@: All additional arguments are used to specify what stub was called
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
# with.
|
# called with.
|
||||||
#
|
#
|
||||||
# Echoes nothing.
|
# Echoes nothing.
|
||||||
# Returns 0 (success) if stub has been called no more than the given number of
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
@@ -368,7 +362,7 @@ __stub_call() {
|
|||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
shift 1
|
shift 1
|
||||||
local args="$@"
|
local args="$@"
|
||||||
if [ "$args" == "" ]; then args="<none>"; fi
|
if [ "$args" = "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
local index="$(__stub_index "$cmd")"
|
local index="$(__stub_index "$cmd")"
|
||||||
if [ -n "$index" ]; then
|
if [ -n "$index" ]; then
|
||||||
@@ -382,7 +376,7 @@ __stub_index() {
|
|||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
|
|
||||||
for item in ${STUB_INDEX[@]}; do
|
for item in ${STUB_INDEX[@]}; do
|
||||||
if [[ "$item" == "${cmd}="* ]]; then
|
if [[ "$item" = "${cmd}="* ]]; then
|
||||||
local index="$item"
|
local index="$item"
|
||||||
index="${index/${cmd}=/}"
|
index="${index/${cmd}=/}"
|
||||||
echo "$index"
|
echo "$index"
|
||||||
@@ -407,7 +401,7 @@ __stub_register() {
|
|||||||
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
|
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
|
||||||
|
|
||||||
# Increment stub count.
|
# Increment stub count.
|
||||||
((STUB_NEXT_INDEX++))
|
((++STUB_NEXT_INDEX))
|
||||||
}
|
}
|
||||||
|
|
||||||
# Private: Cleans out and removes a stub's call list, and removes stub from
|
# Private: Cleans out and removes a stub's call list, and removes stub from
|
||||||
@@ -422,5 +416,4 @@ __stub_clean() {
|
|||||||
eval "unset STUB_${index}_CALLS"
|
eval "unset STUB_${index}_CALLS"
|
||||||
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
|
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
|
||||||
fi
|
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
|
|
||||||
@@ -6,7 +6,7 @@ source "test-helper.sh"
|
|||||||
#
|
#
|
||||||
|
|
||||||
# Sets up stub index, stub call list, and adds stub to index.
|
# Sets up stub index, stub call list, and adds stub to index.
|
||||||
__stub_register "uname"
|
__stub_register "uname" || assert_raises "false"
|
||||||
__stub_register "top"
|
__stub_register "top"
|
||||||
assert 'echo ${STUB_INDEX[@]}' 'uname=0 top=1'
|
assert 'echo ${STUB_INDEX[@]}' 'uname=0 top=1'
|
||||||
assert 'echo ${STUB_INDEX[0]}' 'uname=0'
|
assert 'echo ${STUB_INDEX[0]}' 'uname=0'
|
||||||
Reference in New Issue
Block a user