Skip to content

Commit

Permalink
Simplify usage of fake commands
Browse files Browse the repository at this point in the history
All `fake` functions have a new usage format now:
- `fake <full command> <exit> <stdout> <stderr>`
- `fake-pass <full command> <stdout>`
- `fake-fail <full command> <stderr>`

Also, clean up function is now available as `fake-clean`.

#155
  • Loading branch information
extsoft committed Sep 30, 2019
1 parent 5643820 commit 4b8e27b
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Also, there are several optional addons which can be useful in some circumstance
1. **Use `setup()` or `teardown()`** bats methods only in the tests.
2. Use **`check` instead of bats `run`** to execute a command to be tested.
3. Use **`testtee`** to execute any real command within a test which should not be tested.
4. If `addons-fake` or `addons-git` is used, call `clean-fake` or `clean-git` within a `teardown()` method.
4. If `addons-fake` or `addons-git` is used, call `fake-clean` or `clean-git` within a `teardown()` method.
5. Do not fake `git-elegant` commands within the tests.

#### Assertions
Expand Down
74 changes: 36 additions & 38 deletions tests/addons-fake.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bash
set -e

MOCK_DIR="/tmp/elegant-git-mock"
export PATH=$MOCK_DIR:$PATH

FAKES_DIRECTORY="/tmp/elegant-git-fakes"
export PATH=${FAKES_DIRECTORY}:${PATH}

_log_fake(){
echo "$(basename ${BASH_SOURCE[0]}): $@"
Expand All @@ -15,58 +14,57 @@ _ex_fake() {
}

fake() {
# sample: fake <command> <subcommand> <exit> <stdout> <stderr>
BASENAME=$(basename $1)
PROGRAM_PATH="$MOCK_DIR/$BASENAME-app"
FIXTURE_HOME="$PROGRAM_PATH/$(echo "$2" | sed 's/[^0-9a-zA-Z]*//g')"
MOCK="$MOCK_DIR/$BASENAME"
# sample: fake <command> <exit> <stdout> <stderr>
local executable=$(basename ${1%% *})
local fake_directory="${FAKES_DIRECTORY}/${executable}-app"
local command_directory="${fake_directory}/$(echo "${1}" | sed 's/[^0-9a-zA-Z]*//g')"
local fake="${FAKES_DIRECTORY}/${executable}"

if [[ ! -e "${MOCK}" ]]; then
if [[ ! -e "${fake}" ]]; then
mkdir -p ${FAKES_DIRECTORY}
echo ""
echo "==>> Creating fake executable: ${MOCK}"
ORIGIN_BINARY=$(which ${BASENAME})
cat <<MOCK | tee -i ${MOCK} && _ex_fake chmod +x "$MOCK"
echo "==>> Creating fake executable: ${fake}"
local origin_binary=$(which ${executable})
cat <<FAKE | tee -i ${fake} && _ex_fake chmod +x "${fake}"
#!/usr/bin/env bash
# This is an alternative executable for "${BASENAME}".
# This is an alternative executable for "${executable}".
# It's purpose is to use a mock, if available, otherwise,
# run original executable.
PROGRAM_PATH="${MOCK_DIR}/${BASENAME}-app"
FIXTURE_HOME="\${PROGRAM_PATH}/\$(echo "\$@" | sed 's/[^0-9a-zA-Z]*//g')"
if [[ -e "\${FIXTURE_HOME}" ]]; then
cat "\${FIXTURE_HOME}/stdout"
cat "\${FIXTURE_HOME}/stderr" >&2
exit \$(cat "\${FIXTURE_HOME}/exit_code")
root_directory=${fake_directory}
command_directory="\${root_directory}/\$(echo "${executable} \${@}" | sed 's/[^0-9a-zA-Z]*//g')"
if [[ -e "\${command_directory}" ]]; then
cat "\${command_directory}/stdout"
cat "\${command_directory}/stderr" >&2
exit \$(cat "\${command_directory}/exit_code")
else
${ORIGIN_BINARY} "\$@"
${origin_binary} "\$@"
fi
MOCK
FAKE
fi

[[ -d "$FIXTURE_HOME" ]] && rm -r "$FIXTURE_HOME"
[[ -d "${command_directory}" ]] && rm -r "${command_directory}"
echo ""
echo "==>> Creating fake command: ${FIXTURE_HOME}"
_ex_fake mkdir -p "$FIXTURE_HOME"
echo -e "$3" | tee -i "$FIXTURE_HOME/exit_code"
echo -e "$4" | tee -i "$FIXTURE_HOME/stdout"
echo -e "$5" | tee -i "$FIXTURE_HOME/stderr"
echo "==>> Creating fake command: ${command_directory}"
_ex_fake mkdir -p "${command_directory}"
echo -e "${2}" | tee -i "${command_directory}/exit_code"
echo -e "${3}" | tee -i "${command_directory}/stdout"
echo -e "${4}" | tee -i "${command_directory}/stderr"
}

fake-pass() {
# sample: fake-pass <command> <subcommand> <stdout>
COMMAND="$1"; shift
SUBCOMMAND="$1"; shift
fake "$COMMAND" "$SUBCOMMAND" 0 "$@"
# sample: fake-pass <command> <stdout>
local command="${1}"; shift
fake "${command}" 0 "$@"
}

fake-fail() {
# sample: fake-fail <command> <subcommand> <stderr>
COMMAND="$1"; shift
SUBCOMMAND="$1"; shift
fake "$COMMAND" "$SUBCOMMAND" 100 " " "$@"
# sample: fake-fail <command> <stderr>
local command="${1}"; shift
fake "${command}" 100 " " "$@"
}

clean-fake() {
if [ -d "$MOCK_DIR" ]; then
rm -r "$MOCK_DIR"
fake-clean() {
if [[ -d "${FAKES_DIRECTORY}" ]]; then
rm -r "${FAKES_DIRECTORY}"
fi
}
18 changes: 9 additions & 9 deletions tests/git-elegant-accept-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ load addons-git

setup() {
init-repo
fake-pass git "elegant obtain-work test-feature __eg"
fake-pass git "rebase origin/master"
fake-pass git "fetch --all"
fake-pass git "merge --ff-only __eg"
fake-pass git "push origin master:master"
fake-pass git "branch --delete --force __eg"
fake-pass git "for-each-ref --format='%(upstream:short)' refs/heads/_eg}" "origin/test-feature"
fake-pass git "push origin --delete test-feature"
fake-pass "git elegant obtain-work test-feature __eg"
fake-pass "git rebase origin/master"
fake-pass "git fetch --all"
fake-pass "git merge --ff-only __eg"
fake-pass "git push origin master:master"
fake-pass "git branch --delete --force __eg"
fake-pass "git for-each-ref --format='%(upstream:short)' refs/heads/_eg}" "origin/test-feature"
fake-pass "git push origin --delete test-feature"
}

teardown() {
clean-git
clean-fake
fake-clean
}

@test "'accept-work': a work is accepted successfully for given remote branch" {
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-acquire-repository.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ setup() {
}

teardown() {
clean-fake
fake-clean
clean-git
}

Expand All @@ -30,7 +30,7 @@ teardown() {
}

@test "'acquire-repository': mandatory configuration works as expected on Windows" {
fake-pass uname -s Windows
fake-pass "uname -s" Windows
check git-elegant acquire-repository
[[ "${lines[@]}" =~ "==>> git config --local core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --local apply.whitespace fix" ]]
Expand All @@ -45,7 +45,7 @@ teardown() {
}

@test "'acquire-repository': mandatory configuration works as expected on Linux" {
fake-pass uname -s Linux
fake-pass "uname -s" Linux
check git-elegant acquire-repository
[[ "${lines[@]}" =~ "==>> git config --local core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --local apply.whitespace fix" ]]
Expand All @@ -60,7 +60,7 @@ teardown() {
}

@test "'acquire-repository': mandatory configuration works as expected on Darwin" {
fake-pass uname -s Darwin
fake-pass "uname -s" Darwin
check git-elegant acquire-repository
[[ "${lines[@]}" =~ "==>> git config --local core.commentChar |" ]]
[[ "${lines[@]}" =~ "==>> git config --local apply.whitespace fix" ]]
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-amend-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ setup() {
}

teardown() {
clean-fake
fake-clean
clean-git
}

@test "'amend-work': command works as expected for non-master branch" {
fake-pass git "add --interactive"
fake-pass git "diff --cached --check"
fake-pass git "commit --amend"
fake-pass "git add --interactive"
fake-pass "git diff --cached --check"
fake-pass "git commit --amend"
gitrepo git checkout -b test
check git-elegant amend-work
[[ "${status}" -eq 0 ]]
Expand Down
4 changes: 2 additions & 2 deletions tests/git-elegant-clear-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ load addons-git
setup() {
init-repo
gitrepo git branch --force first
fake-pass git "branch -lvv" "first [gone]"
fake-pass "git branch -lvv" "first [gone]"
}

teardown() {
clean-fake
fake-clean
clean-git
}

Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-clone-repository.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ load addons-cd
load addons-fake

setup() {
fake-pass git clone-repository
fake-pass git "clone https://github.com/extsoft/elegant-git.git"
fake-pass git "elegant acquire-repository"
fake-pass "git clone-repository"
fake-pass "git clone https://github.com/extsoft/elegant-git.git"
fake-pass "git elegant acquire-repository"
}

teardown() {
clean-fake
fake-clean
}

@test "'clone-repository': raise an error if cloneable URL isn't set" {
Expand Down
2 changes: 1 addition & 1 deletion tests/git-elegant-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load addons-common
load addons-fake

teardown() {
clean-fake
fake-clean
}

@test "'commands': print all available commands" {
Expand Down
28 changes: 14 additions & 14 deletions tests/git-elegant-deliver-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ setup() {
}

teardown() {
clean-fake
fake-clean
clean-git
}

@test "'deliver-work': by default, a name of remote branch is equal to local branch" {
fake-pass git "rev-parse --abbrev-ref HEAD" feature1
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin feature1:feature1"
fake-pass "git rev-parse --abbrev-ref HEAD" feature1
fake-pass "git fetch"
fake-pass "git rebase origin/master"
fake-pass "git push --set-upstream --force origin feature1:feature1"
check git-elegant deliver-work
[ "$status" -eq 0 ]
[[ "${lines[@]}" =~ "git push --set-upstream --force origin feature1:feature1" ]]
}

@test "'deliver-work': if branch name passed, a name of remote branch is different to local branch" {
fake-pass git "rev-parse --abbrev-ref HEAD" feature1
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin feature1:feature2"
fake-pass "git rev-parse --abbrev-ref HEAD" feature1
fake-pass "git fetch"
fake-pass "git rebase origin/master"
fake-pass "git push --set-upstream --force origin feature1:feature2"
check git-elegant deliver-work feature2
[ "$status" -eq 0 ]
[[ "${lines[@]}" =~ "git push --set-upstream --force origin feature1:feature2" ]]
}

@test "'deliver-work': exit code is 42 when current local branch is master" {
fake-pass git "rev-parse --abbrev-ref HEAD" master
fake-pass git "fetch"
fake-pass git "rebase origin/master"
fake-pass git "push --set-upstream --force origin master:master"
fake-pass "git rev-parse --abbrev-ref HEAD" master
fake-pass "git fetch"
fake-pass "git rebase origin/master"
fake-pass "git push --set-upstream --force origin master:master"
check git-elegant deliver-work
[ "$status" -eq 42 ]
[ "${lines[1]}" = "== No pushes to 'master' branch. Please read more on https://elegant-git.bees-hive.org ==" ]
}

@test "'deliver-work': use stash pipe if there are uncommitted changes" {
fake-pass git pull
fake-pass "git pull"
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
check git-elegant start-work test-feature
[[ "$status" -eq 0 ]]
Expand Down
6 changes: 3 additions & 3 deletions tests/git-elegant-init-repository.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ load addons-read
load addons-fake

setup() {
fake-pass git init
fake-pass git "elegant acquire-repository"
fake-pass "git init"
fake-pass "git elegant acquire-repository"
}

teardown() {
clean-fake
fake-clean
}

@test "'init-repository': command is available" {
Expand Down
22 changes: 11 additions & 11 deletions tests/git-elegant-obtain-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load addons-common
load addons-fake

teardown() {
clean-fake
fake-clean
}

@test "'obtain-work': raise 45 error if branch name pattern in not set" {
Expand All @@ -14,35 +14,35 @@ teardown() {
}

@test "'obtain-work': use found remote branch when given pattern matches only one remote branch" {
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master"
fake-pass git "checkout -B rremote origin/rremote"
fake-pass "git fetch --all"
fake-pass "git for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master"
fake-pass "git checkout -B rremote origin/rremote"

check git-elegant obtain-work rr
[[ "$status" -eq 0 ]]
}

@test "'obtain-work': use given local branch name when it is provided" {
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master"
fake-pass git "checkout -B myname origin/rremote"
fake-pass "git fetch --all"
fake-pass "git for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master"
fake-pass "git checkout -B myname origin/rremote"

check git-elegant obtain-work rr myname
[[ "$status" -eq 0 ]]
}

@test "'obtain-work': raise 43 error when given pattern matches several remote branches" {
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master\nother-upstream/barr"
fake-pass "git fetch --all"
fake-pass "git for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master\nother-upstream/barr"

check git-elegant obtain-work rr
[[ "$status" -eq 43 ]]
[[ "${lines[@]}" =~ "Please re-run the command with concrete branch name from the list above!" ]]
}

@test "'obtain-work': raise 43 error when given pattern matches zero remote branches" {
fake-pass git "fetch --all"
fake-pass git "for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master\norigin/barr"
fake-pass "git fetch --all"
fake-pass "git for-each-ref --format='%(refname:strip=2)' refs/remotes/**" "origin/rremote\norigin/master\norigin/barr"

check git-elegant obtain-work aa
[[ "$status" -eq 43 ]]
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-save-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ setup() {
}

teardown() {
clean-fake
fake-clean
clean-git
}

@test "'save-work': command works as expected for non-master branch" {
fake-pass git "add --interactive"
fake-pass git "diff --cached --check"
fake-pass git "commit"
fake-pass "git add --interactive"
fake-pass "git diff --cached --check"
fake-pass "git commit"
gitrepo git checkout -b test
check git-elegant save-work
[[ "${status}" -eq 0 ]]
Expand Down
Loading

0 comments on commit 4b8e27b

Please sign in to comment.