8 Commits

Author SHA1 Message Date
3663e1488b Merge pull request #6 from TsutomuNakamura/change_increment_in_stub_register
Changed increment in __stub_register() not to return non 0
2020-02-11 09:01:28 +00:00
Tsutomu Nakamura
14b1b07d45 Changed increment in __stub_register() not to return non 0 2018-03-06 00:41:29 +09:00
cdc428f859 Merge pull request #5 from potherca-contrib/feature/support-bpkg
Adds support for installing `stub.sh` using BPKG
2018-01-21 19:31:51 +00:00
Ben Peachey
30db3416ea Adds mention of BPKG install to README file. 2018-01-21 16:59:08 +00:00
Ben Peachey
b75a952a20 Adds BPKG package file. 2018-01-21 16:55:11 +00:00
b6a1ae189c Merge pull request #3 from ericfreese/master
Use single equal sign in single bracket tests for zsh compatibility.
2016-03-05 10:40:59 +00:00
Eric Freese
bd6f3c4666 Use single equal sign in single bracket tests for zsh compatibility. 2016-02-13 12:45:23 -07:00
89aed2999c Update Travis-CI build status image in readme 2014-09-08 20:47:07 +01:00
5 changed files with 51 additions and 14 deletions

View File

@@ -8,7 +8,7 @@ update: update_test-runner.sh update_assert.sh
test/assert.sh: test/assert.sh:
echo "fetching assert.sh..." && \ echo "fetching assert.sh..." && \
curl -s -L -o test/assert.sh \ curl -s -L -o test/assert.sh \
https://raw.github.com/lehmannro/assert.sh/v1.0.2/assert.sh https://raw.github.com/lehmannro/assert.sh/v1.1/assert.sh
remove_assert.sh: remove_assert.sh:
( \ ( \

View File

@@ -1,4 +1,4 @@
# stub.sh [![Build Status](https://travis-ci.org/jimeh/stub.sh.png)](https://travis-ci.org/jimeh/stub.sh) # stub.sh [![Build Status](https://api.travis-ci.org/jimeh/stub.sh.svg)](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

9
package.json Normal file
View 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"]
}

18
stub.sh
View File

@@ -38,8 +38,8 @@
# 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"
} }
@@ -57,8 +57,8 @@ stub() {
# 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"
} }
@@ -234,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
@@ -362,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
@@ -376,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"
@@ -401,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

View File

@@ -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'