Skip to content

Commit

Permalink
Add ability to write integration tests
Browse files Browse the repository at this point in the history
CONTRIBUTING.md describes contribution rules for the project.
addons-git.bash allows to work with real git repository and create unit
integration tests.

#89
  • Loading branch information
extsoft authored and AntonKarabaza committed Nov 10, 2017
1 parent 60acb89 commit cbe6bc6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 13 deletions.
47 changes: 47 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Usage
=====
Installation
------------
Use `./install.bash dev` to get an installation from the current sources.

Debug mode
----------
Use `export GED=1` to switch on the debug of `git elegant` or `unset GED` to switch off.

Unit tests
==========
[bats](https://github.com/sstephenson/bats) is used for unit testing.

Addons
------
Add the following line to the test file if the extension is required:
- `load commons` to have the working test (**mandatory**)
- `load addons-git` to interact with real git repository (**optional**)
- `load fake-cd` to fake `cd` unix command (**optional**)
- `load fake-read` to fake `read` unix command (**optional**)

@todo #89 Use some name template for the addons.

Bats restrictions
-----------------
1. **Don't use `setup()` or `teardown()`** bats methods.
2. Use **`check` instead of bats `run`** to execute a command to test.

Assertions
----------
- `[ "${lines[0]}" = "+ the space " ]` for a output line (index starts from 0)
- `[ "$status" -eq 2 ]` for a command status
- `[ "${#lines[@]}" -eq 0 ]` for an empty command output

Test name template
------------------
Use the following test name template - `'<command args>': <describe what will be tested>` like `'check -s': trailing spaces in the staged changes`.

@todo #89 Apply test name template for all unit tests. Please update assertions if required.

Run
---
Use one of the following commands to run the unit tests:
- `bats src/test`
- `./run-tests`
- `docker run -t --rm -v $PWD:/src -w /src extsoft/rultor-runtime:r24b04p0g2 ./run-tests`
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,4 @@ clear-local

Development
===========
Installation
---------------
1. clone the repository
2. run `./install.bash dev`

Debug
-----
`export GED=1` to turn on the debug of `git elegant` execution.

`unset GED` to turn off the debug of `git elegant` execution.
Please follow [CONTRIBUTING.md](CONTRIBUTING.md) when working with the code.
38 changes: 38 additions & 0 deletions src/test/addons-git.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -e

GIT_REPO_DIR="$BATS_TMPDIR/mock-repo"
FILE_TO_MODIFY=file

_gilog(){
echo "$(basename ${BASH_SOURCE[0]}): $@"
}

_ex() {
_gilog "$@"
eval "$@"
}

init-repo() {
if [ -n "$GIT_REPO_DIR" ]; then
_ex mkdir -p $GIT_REPO_DIR
_ex cd $GIT_REPO_DIR
_ex git init
_ex git config --local user.email "[email protected]"
_ex git config --local user.name "Elegant Git"
_ex touch $FILE_TO_MODIFY
_ex git add .
_ex git commit -m "Add $FILE_TO_MODIFY"
else
exit 1
fi
}

add-unst-change(){
_ex "echo -e \"$@\" >> $FILE_TO_MODIFY"
}

add-st-change(){
add-unst-change "$@"
_ex git add $FILE_TO_MODIFY
}
11 changes: 11 additions & 0 deletions src/test/commons.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ BIN_DIR="$THIS/../../src/main"
MOCK_DIR="$THIS/../../mock"
export PATH=$MOCK_DIR:$BIN_DIR:$PATH

check(){
run "$@"
echo "> Exit code: \$status=$status"
for line in ${lines[@]}; do
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"
Expand Down Expand Up @@ -47,6 +57,7 @@ fake-fail() {
}

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"
Expand Down
23 changes: 20 additions & 3 deletions src/test/git-elegant-check.bats
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
#!/usr/bin/env bats

load commons
load addons-git

setup() {
preconditions() {
fake-pass git "diff --check"
fake-pass git "diff --cached --check"
fake-pass git "elegant check --all"
}

@test "exit code is 0 when run 'git-elegant check -a'" {
preconditions
run git-elegant check -a
[ "$status" -eq 0 ]
}

@test "exit code is 0 when run 'git-elegant check -s'" {
run git-elegant check -s
@test "'check -s': no trailing spaces in the staged changes" {
init-repo && add-st-change "no space"
check git-elegant check -s
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

@test "'check -s': trailing spaces in the staged changes" {
init-repo && add-st-change "the space "
check git-elegant check -s
[ "$status" -eq 2 ]
[ "${lines[1]}" = "+the space " ]

}

@test "exit code is 0 when run 'git-elegant check -u'" {
preconditions
run git-elegant check -u
[ "$status" -eq 0 ]
}

@test "exit code is 0 when run 'git-elegant check --all'" {
preconditions
run git-elegant check --all
[ "$status" -eq 0 ]
}

@test "exit code is 0 when run 'git-elegant check --staged'" {
preconditions
run git-elegant check --staged
[ "$status" -eq 0 ]
}

@test "exit code is 0 when run 'git-elegant check --unstaged'" {
preconditions
run git-elegant check --unstaged
[ "$status" -eq 0 ]
}

@test "exit code is 0 when run 'git-elegant check'" {
preconditions
run git-elegant check
[ "$status" -eq 0 ]
}

0 comments on commit cbe6bc6

Please sign in to comment.