Skip to content

Commit

Permalink
Use teardown within the tests instead of addons
Browse files Browse the repository at this point in the history
  • Loading branch information
extsoft committed Dec 15, 2017
1 parent 5d346be commit 341f738
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 8 deletions.
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down
4 changes: 1 addition & 3 deletions src/test/addons-fake.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/test/addons-git.bash
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions src/test/git-elegant-add.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-check.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ load addons-common
load addons-git
load addons-fake

teardown() {
clean-fake
clean-git
}

preconditions() {
fake-pass git "diff --check"
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-clear-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-clone.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-configure.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-feature.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-pull.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-push-after-rebase.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-push.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant-rebase.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/git-elegant.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down

3 comments on commit 341f738

@0pdd
Copy link

@0pdd 0pdd commented on 341f738 Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 89-c39c3654 disappeared from src/test/commons.bash, that's why I closed #99. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 341f738 Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 89-5ae02b5b disappeared from src/test/commons.bash, that's why I closed #100. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 341f738 Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 89-c19177fa disappeared from src/test/commons.bash, that's why I closed #101. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.