diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 17c908e..7261a5b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ----------------- diff --git a/src/test/addons-common.bash b/src/test/addons-common.bash index 547ccfd..7e84063 100755 --- a/src/test/addons-common.bash +++ b/src/test/addons-common.bash @@ -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 "$@" @@ -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 - 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="$1"; shift - SUBCOMMAND="$1"; shift - fake "$COMMAND" "$SUBCOMMAND" 0 "$@" -} - -fake-fail() { - # sample: fake-fail - 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 -} - diff --git a/src/test/addons-fake.bash b/src/test/addons-fake.bash new file mode 100644 index 0000000..4708aee --- /dev/null +++ b/src/test/addons-fake.bash @@ -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 + 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="$1"; shift + SUBCOMMAND="$1"; shift + fake "$COMMAND" "$SUBCOMMAND" 0 "$@" +} + +fake-fail() { + # sample: fake-fail + 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 +} diff --git a/src/test/git-elegant-add.bats b/src/test/git-elegant-add.bats index 51da0df..d94151b 100644 --- a/src/test/git-elegant-add.bats +++ b/src/test/git-elegant-add.bats @@ -2,6 +2,8 @@ load addons-common load addons-read +load addons-fake + setup() { fake-pass git "ls-files -m" src/test/git-elegant diff --git a/src/test/git-elegant-check.bats b/src/test/git-elegant-check.bats index 15ad5e2..e2bcba3 100644 --- a/src/test/git-elegant-check.bats +++ b/src/test/git-elegant-check.bats @@ -2,6 +2,8 @@ load addons-common load addons-git +load addons-fake + preconditions() { fake-pass git "diff --check" diff --git a/src/test/git-elegant-clear-local.bats b/src/test/git-elegant-clear-local.bats index 8bbb94e..c77c92b 100644 --- a/src/test/git-elegant-clear-local.bats +++ b/src/test/git-elegant-clear-local.bats @@ -2,6 +2,7 @@ load addons-common load addons-read +load addons-fake setup() { fake-pass git "branch -lvv" "first [gone]" diff --git a/src/test/git-elegant-clone.bats b/src/test/git-elegant-clone.bats index 6644556..08c43c3 100644 --- a/src/test/git-elegant-clone.bats +++ b/src/test/git-elegant-clone.bats @@ -3,6 +3,7 @@ load addons-common load addons-read load addons-cd +load addons-fake setup() { fake-pass git clone diff --git a/src/test/git-elegant-configure.bats b/src/test/git-elegant-configure.bats index 5f8b23d..8ab7288 100644 --- a/src/test/git-elegant-configure.bats +++ b/src/test/git-elegant-configure.bats @@ -2,6 +2,7 @@ load addons-common load addons-read +load addons-fake setup() { fake-pass git "elegant commands" diff --git a/src/test/git-elegant-feature.bats b/src/test/git-elegant-feature.bats index eeda640..b73eba2 100644 --- a/src/test/git-elegant-feature.bats +++ b/src/test/git-elegant-feature.bats @@ -1,6 +1,7 @@ #!/usr/bin/env bats -ex load addons-common +load addons-fake setup() { fake-pass git "elegant pull master" diff --git a/src/test/git-elegant-init.bats b/src/test/git-elegant-init.bats index 1427389..ca6eb7a 100644 --- a/src/test/git-elegant-init.bats +++ b/src/test/git-elegant-init.bats @@ -2,6 +2,7 @@ load addons-common load addons-read +load addons-fake setup() { fake-pass git init diff --git a/src/test/git-elegant-pull.bats b/src/test/git-elegant-pull.bats index ad49e38..52fc74d 100644 --- a/src/test/git-elegant-pull.bats +++ b/src/test/git-elegant-pull.bats @@ -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" diff --git a/src/test/git-elegant-push-after-rebase.bats b/src/test/git-elegant-push-after-rebase.bats index dfe522a..feaf681 100644 --- a/src/test/git-elegant-push-after-rebase.bats +++ b/src/test/git-elegant-push-after-rebase.bats @@ -2,6 +2,7 @@ load addons-common load addons-read +load addons-fake setup() { fake-pass git "elegant rebase" diff --git a/src/test/git-elegant-push.bats b/src/test/git-elegant-push.bats index 92ceb6c..6ad0529 100644 --- a/src/test/git-elegant-push.bats +++ b/src/test/git-elegant-push.bats @@ -2,6 +2,7 @@ load addons-common load addons-read +load addons-fake setup() { fake-pass git branch *master diff --git a/src/test/git-elegant-rebase.bats b/src/test/git-elegant-rebase.bats index 5bbffed..9e2046f 100644 --- a/src/test/git-elegant-rebase.bats +++ b/src/test/git-elegant-rebase.bats @@ -1,6 +1,7 @@ #!/usr/bin/env bats load addons-common +load addons-fake setup() { fake-pass git "fetch --tags" diff --git a/src/test/git-elegant.bats b/src/test/git-elegant.bats index 2eb81e8..9b4bdfb 100644 --- a/src/test/git-elegant.bats +++ b/src/test/git-elegant.bats @@ -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