From 341f738e780214f09b3c4069536aaffef8d514c0 Mon Sep 17 00:00:00 2001 From: Dmytro Serdiuk Date: Fri, 15 Dec 2017 20:41:30 +0200 Subject: [PATCH] Use teardown within the tests instead of addons #101 --- CONTRIBUTING.md | 9 +++++---- src/test/addons-fake.bash | 4 +--- src/test/addons-git.bash | 8 +++++++- src/test/git-elegant-add.bats | 3 +++ src/test/git-elegant-check.bats | 4 ++++ src/test/git-elegant-clear-local.bats | 4 ++++ src/test/git-elegant-clone.bats | 4 ++++ src/test/git-elegant-configure.bats | 4 ++++ src/test/git-elegant-feature.bats | 4 ++++ src/test/git-elegant-init.bats | 4 ++++ src/test/git-elegant-pull.bats | 4 ++++ src/test/git-elegant-push-after-rebase.bats | 4 ++++ src/test/git-elegant-push.bats | 4 ++++ src/test/git-elegant-rebase.bats | 4 ++++ src/test/git-elegant.bats | 4 ++++ 15 files changed, 60 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7261a5b..089c7fb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,10 +21,11 @@ Add the following line to the test file if the extension is required: - `load addons-cd` to fake `cd` command (**optional**) - `load addons-read` to fake `read` command (**optional**) -Bats restrictions ------------------ -1. **Don't use `setup()` or `teardown()`** bats methods. -2. Use **`check` instead of bats `run`** to execute a command to test. +Writing tests +------------- +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. If `addons-fake` or `addons-git` is used, call `clean-fake` or `clean-git` within a `teardown()` method. Assertions ---------- diff --git a/src/test/addons-fake.bash b/src/test/addons-fake.bash index 4b1dfa9..3f7c965 100644 --- a/src/test/addons-fake.bash +++ b/src/test/addons-fake.bash @@ -53,9 +53,7 @@ fake-fail() { fake "$COMMAND" "$SUBCOMMAND" 100 " " "$@" } -teardown() { - # @todo #89 Use $BATS_TMPDIR instead of teardown methods for fakes deletion - #teardown for bats tests +clean-fake() { if [ -d "$MOCK_DIR" ]; then rm -r "$MOCK_DIR" fi diff --git a/src/test/addons-git.bash b/src/test/addons-git.bash index 94ded79..44d7699 100644 --- a/src/test/addons-git.bash +++ b/src/test/addons-git.bash @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -GIT_REPO_DIR="$BATS_TMPDIR/mock-repo" +GIT_REPO_DIR="/tmp/elegant-git-repo" FILE_TO_MODIFY=file _gilog(){ @@ -36,3 +36,9 @@ add-st-change(){ add-unst-change "$@" _ex git add $FILE_TO_MODIFY } + +clean-git() { + if [ -d "$GIT_REPO_DIR" ]; then + rm -rf "$GIT_REPO_DIR" + fi +} diff --git a/src/test/git-elegant-add.bats b/src/test/git-elegant-add.bats index d94151b..3eea4c3 100644 --- a/src/test/git-elegant-add.bats +++ b/src/test/git-elegant-add.bats @@ -4,6 +4,9 @@ load addons-common load addons-read load addons-fake +teardown() { + clean-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 e2bcba3..cb4f873 100644 --- a/src/test/git-elegant-check.bats +++ b/src/test/git-elegant-check.bats @@ -4,6 +4,10 @@ load addons-common load addons-git load addons-fake +teardown() { + clean-fake + clean-git +} 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 c77c92b..9200bb3 100644 --- a/src/test/git-elegant-clear-local.bats +++ b/src/test/git-elegant-clear-local.bats @@ -10,6 +10,10 @@ setup() { fake-pass git "branch -d first" } +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant clear-local'" { run git-elegant clear-local [ "$status" -eq 0 ] diff --git a/src/test/git-elegant-clone.bats b/src/test/git-elegant-clone.bats index 08c43c3..d6f0f2e 100644 --- a/src/test/git-elegant-clone.bats +++ b/src/test/git-elegant-clone.bats @@ -11,6 +11,10 @@ setup() { fake-pass git "elegant configure --local" } +teardown() { + clean-fake +} + @test "print message when run 'git-elegant clone'" { run git-elegant clone [[ "${lines[0]}" =~ "Clonable URL is not set" ]] diff --git a/src/test/git-elegant-configure.bats b/src/test/git-elegant-configure.bats index 8ab7288..19f834a 100644 --- a/src/test/git-elegant-configure.bats +++ b/src/test/git-elegant-configure.bats @@ -22,6 +22,10 @@ setup() { fake-pass git "config --local apply.whitespace fix" } +teardown() { + clean-fake +} + @test "'configure': exit code is 11 when run without arguments" { run git-elegant configure [ "$status" -eq 11 ] diff --git a/src/test/git-elegant-feature.bats b/src/test/git-elegant-feature.bats index b73eba2..3cf10d6 100644 --- a/src/test/git-elegant-feature.bats +++ b/src/test/git-elegant-feature.bats @@ -9,6 +9,10 @@ setup() { fake-pass git "stash save elegant-git" } +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant feature test-feature'" { run git-elegant feature test-feature [ "$status" -eq 0 ] diff --git a/src/test/git-elegant-init.bats b/src/test/git-elegant-init.bats index ca6eb7a..99e55ff 100644 --- a/src/test/git-elegant-init.bats +++ b/src/test/git-elegant-init.bats @@ -9,6 +9,10 @@ setup() { fake-pass git "elegant configure --local" } +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant init'" { run git-elegant init [ "$status" -eq 0 ] diff --git a/src/test/git-elegant-pull.bats b/src/test/git-elegant-pull.bats index 52fc74d..a5d6d36 100644 --- a/src/test/git-elegant-pull.bats +++ b/src/test/git-elegant-pull.bats @@ -3,6 +3,10 @@ load addons-common load addons-fake +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant pull' without parameters" { fake-pass git "fetch --tags" fake-pass git pull diff --git a/src/test/git-elegant-push-after-rebase.bats b/src/test/git-elegant-push-after-rebase.bats index feaf681..3728bf3 100644 --- a/src/test/git-elegant-push-after-rebase.bats +++ b/src/test/git-elegant-push-after-rebase.bats @@ -9,6 +9,10 @@ setup() { fake-pass git "elegant push" } +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant push-after-rebase'" { run git-elegant push-after-rebase [ "$status" -eq 0 ] diff --git a/src/test/git-elegant-push.bats b/src/test/git-elegant-push.bats index 6ad0529..7c73d01 100644 --- a/src/test/git-elegant-push.bats +++ b/src/test/git-elegant-push.bats @@ -4,6 +4,10 @@ load addons-common load addons-read load addons-fake +teardown() { + clean-fake +} + setup() { fake-pass git branch *master fake-pass git "push -u origin master:master" diff --git a/src/test/git-elegant-rebase.bats b/src/test/git-elegant-rebase.bats index 9e2046f..8c8c278 100644 --- a/src/test/git-elegant-rebase.bats +++ b/src/test/git-elegant-rebase.bats @@ -8,6 +8,10 @@ setup() { fake-pass git "rebase origin/master" } +teardown() { + clean-fake +} + @test "exit code is 0 when run 'git-elegant rebase'" { run git-elegant rebase [ "$status" -eq 0 ] diff --git a/src/test/git-elegant.bats b/src/test/git-elegant.bats index 9b4bdfb..8f8a7f6 100644 --- a/src/test/git-elegant.bats +++ b/src/test/git-elegant.bats @@ -3,6 +3,10 @@ load addons-common load addons-fake +teardown() { + clean-fake +} + @test "print available commands when run 'git-elegant commands'" { run git-elegant commands [ "${lines[0]}" = "feature" ]