mirror of
https://github.com/jimeh/stub.sh.git
synced 2026-02-19 13:46:40 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| efb9140329 | |||
| 0c4a5d11ee | |||
| ef231e420a |
25
stub.sh
25
stub.sh
@@ -1,5 +1,5 @@
|
||||
# !/usr/bin/env bash
|
||||
# stub.sh 0.1.0 - stubbing helpers for simplifying bash script tests.
|
||||
# stub.sh 0.2.0 - stubbing helpers for simplifying bash script tests.
|
||||
# Copyright (c) 2014 Jim Myhrberg.
|
||||
#
|
||||
# https://github.com/jimeh/stub.sh
|
||||
@@ -29,10 +29,15 @@
|
||||
# Arguments:
|
||||
# - $1: Name of command to stub.
|
||||
# - $2: When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||
# When set to "null", all output is redirected to /dev/null.
|
||||
#
|
||||
#
|
||||
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"
|
||||
}
|
||||
|
||||
|
||||
@@ -42,9 +47,13 @@ stub() {
|
||||
# - $1: Name of command to stub.
|
||||
# - $2: String to echo when stub is called.
|
||||
# - $3: When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||
# When set to "null", all output is redirected to /dev/null.
|
||||
#
|
||||
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"
|
||||
}
|
||||
|
||||
@@ -60,10 +69,12 @@ stub_and_eval() {
|
||||
|
||||
# If stubbing a function, store non-stubbed copy of it required for restore.
|
||||
if [ -n "$(command -v "$cmd")" ]; then
|
||||
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||
local source="$(type "$cmd" | tail -n +2)"
|
||||
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||
eval "$source"
|
||||
if [ -z "$(command -v "non_stubbed_${cmd}")" ]; then
|
||||
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||
local source="$(type "$cmd" | tail -n +2)"
|
||||
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||
eval "$source"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
@@ -10,16 +10,16 @@ source "test-helper.sh"
|
||||
my-name-is() { echo "My name is $@."; }
|
||||
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"
|
||||
|
||||
restore "my-name-is"
|
||||
restore "my-name-is" stdout
|
||||
assert "my-name-is Edward Elric" "My name is Edward Elric."
|
||||
|
||||
|
||||
# Stubbing and restoring a executable file.
|
||||
actual_uname="$(uname)"
|
||||
stub "uname"
|
||||
stub "uname" stdout
|
||||
assert "uname" "uname stub: "
|
||||
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."
|
||||
|
||||
|
||||
# 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.
|
||||
assert_end "restore()"
|
||||
|
||||
@@ -9,22 +9,34 @@ source "test-helper.sh"
|
||||
# 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
|
||||
|
||||
|
||||
# Stubbing a executable file.
|
||||
stub "uname"
|
||||
# Redirect stub of executable file output to STDOUT.
|
||||
stub "uname" STDOUT
|
||||
assert "uname" "uname stub: "
|
||||
assert "uname -h" "uname stub: -h"
|
||||
unset -f uname
|
||||
assert "uname -r" "uname stub: -r"
|
||||
unset -f my-name-is
|
||||
|
||||
|
||||
# Redirect stub output to STDERR.
|
||||
# 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" ""
|
||||
@@ -32,8 +44,26 @@ 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.
|
||||
stub "cowabunga-dude"
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user