Skip to content

Commit

Permalink
Move 'fake's to separate addons file
Browse files Browse the repository at this point in the history
  • Loading branch information
extsoft committed Dec 15, 2017
1 parent 7d0e6dc commit a0dfda8
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 55 deletions.
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Addons
Add the following line to the test file if the extension is required:
- `load addons-common` to have the working test (**mandatory**)
- `load addons-git` to interact with real git repository (**optional**)
- `load addons-cd` to fake `cd` unix command (**optional**)
- `load addons-read` to fake `read` unix command (**optional**)
- `load addons-fake` to fake a Linux command (**optional**)
- `load addons-cd` to fake `cd` command (**optional**)
- `load addons-read` to fake `read` command (**optional**)

Bats restrictions
-----------------
Expand Down
54 changes: 1 addition & 53 deletions src/test/addons-common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ set -e

THIS="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BIN_DIR="$THIS/../../src/main"
MOCK_DIR="$THIS/../../mock"
export PATH=$MOCK_DIR:$BIN_DIR:$PATH
export PATH=$BIN_DIR:$PATH

check(){
run "$@"
Expand All @@ -14,54 +13,3 @@ check(){
echo "> stdout+stderr: '$line'"
done
}

# @todo #89 Move 'fake's to separate addons file.
fake() {
# @todo #89 Implement logging of commands execution like in addons-git.bash

# 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"

[ -d "$FIXTURE_HOME" ] && rm -r "$FIXTURE_HOME"
mkdir -p "$FIXTURE_HOME"
echo -e "$3" > "$FIXTURE_HOME/exit_code"
echo -e "$4" > "$FIXTURE_HOME/stdout"
echo -e "$5" > "$FIXTURE_HOME/stderr"

[ -e "$MOCK" ] && rm -r "$MOCK"
echo "#!/usr/bin/env bash
PROGRAM_PATH=\"$MOCK_DIR/$BASENAME-app\"
FIXTURE_HOME=\"\$PROGRAM_PATH/\$(echo \"\$@\" | sed 's/[^0-9a-zA-Z]*//g')\"
cat \"\$FIXTURE_HOME/stdout\"
cat \"\$FIXTURE_HOME/stderr\" >&2
read -r exit_code < \"\$FIXTURE_HOME/exit_code\"
exit \$exit_code
" > "$MOCK"
chmod +x "$MOCK"
}

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

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

teardown() {
# @todo #89 Use $BATS_TMPDIR instead of teardown methods for fakes deletion
#teardown for bats tests
if [ -d "$MOCK_DIR" ]; then
rm -r "$MOCK_DIR"
fi
}

55 changes: 55 additions & 0 deletions src/test/addons-fake.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -e

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


fake() {
# @todo #89 Implement logging of commands execution like in addons-git.bash

# 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"

[ -d "$FIXTURE_HOME" ] && rm -r "$FIXTURE_HOME"
mkdir -p "$FIXTURE_HOME"
echo -e "$3" > "$FIXTURE_HOME/exit_code"
echo -e "$4" > "$FIXTURE_HOME/stdout"
echo -e "$5" > "$FIXTURE_HOME/stderr"

[ -e "$MOCK" ] && rm -r "$MOCK"
echo "#!/usr/bin/env bash
PROGRAM_PATH=\"$MOCK_DIR/$BASENAME-app\"
FIXTURE_HOME=\"\$PROGRAM_PATH/\$(echo \"\$@\" | sed 's/[^0-9a-zA-Z]*//g')\"
cat \"\$FIXTURE_HOME/stdout\"
cat \"\$FIXTURE_HOME/stderr\" >&2
read -r exit_code < \"\$FIXTURE_HOME/exit_code\"
exit \$exit_code
" > "$MOCK"
chmod +x "$MOCK"
}

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

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

teardown() {
# @todo #89 Use $BATS_TMPDIR instead of teardown methods for fakes deletion
#teardown for bats tests
if [ -d "$MOCK_DIR" ]; then
rm -r "$MOCK_DIR"
fi
}
2 changes: 2 additions & 0 deletions src/test/git-elegant-add.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

load addons-common
load addons-read
load addons-fake


setup() {
fake-pass git "ls-files -m" src/test/git-elegant
Expand Down
2 changes: 2 additions & 0 deletions src/test/git-elegant-check.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

load addons-common
load addons-git
load addons-fake


preconditions() {
fake-pass git "diff --check"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-clear-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load addons-common
load addons-read
load addons-fake

setup() {
fake-pass git "branch -lvv" "first [gone]"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-clone.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load addons-common
load addons-read
load addons-cd
load addons-fake

setup() {
fake-pass git clone
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-configure.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load addons-common
load addons-read
load addons-fake

setup() {
fake-pass git "elegant commands"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-feature.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats -ex

load addons-common
load addons-fake

setup() {
fake-pass git "elegant pull master"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load addons-common
load addons-read
load addons-fake

setup() {
fake-pass git init
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-pull.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats

load addons-common
load addons-fake

@test "exit code is 0 when run 'git-elegant pull' without parameters" {
fake-pass git "fetch --tags"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-push-after-rebase.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load addons-common
load addons-read
load addons-fake

setup() {
fake-pass git "elegant rebase"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-push.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load addons-common
load addons-read
load addons-fake

setup() {
fake-pass git branch *master
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant-rebase.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats

load addons-common
load addons-fake

setup() {
fake-pass git "fetch --tags"
Expand Down
1 change: 1 addition & 0 deletions src/test/git-elegant.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bats

load addons-common
load addons-fake

@test "print available commands when run 'git-elegant commands'" {
run git-elegant commands
Expand Down

0 comments on commit a0dfda8

Please sign in to comment.