Skip to content

Commit

Permalink
Simplify work with a repository for tests
Browse files Browse the repository at this point in the history
`addons-git.bash` is renamed to `addons-repo.bash`. Also, all functions
inside are using unified naming convention - `repo[...]`. This should
simplify reading of tests and their writting.

Also, where possible, the updated functions were applied for current
BATS tests.
  • Loading branch information
extsoft committed Sep 30, 2019
1 parent 51b4409 commit d012ec3
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ file. This addon configures right access to executables (`libexec` directory) an
functions.

Also, there are several optional addons which can be useful in some circumstances:
- add `load addons-git` to interact with real git repository
- add `load addons-repo` to interact with real git repository
- add `load addons-fake` to fake a Linux command
- add `load addons-cd` to fake `cd` command
- add `load addons-read` to fake `read` command
Expand All @@ -126,7 +126,7 @@ Also, there are several optional addons which can be useful in some circumstance
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. Use **`testtee`** to execute any real command within a test which should not be tested.
4. If `addons-fake` or `addons-git` is used, call `fake-clean` or `clean-git` within a `teardown()` method.
4. If `addons-fake` or `addons-repo` is used, call `fake-clean` or `repo-clean` within a `teardown()` method.
5. Do not fake `git-elegant` commands within the tests.

#### Assertions
Expand Down
44 changes: 0 additions & 44 deletions tests/addons-git.bash

This file was deleted.

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

GIT_REPO_DIR="/tmp/elegant-git-repo"
FILE_TO_MODIFY=file

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

repo-non-staged-change(){
# modifies committed file
# usage: repo-non-staged-change <text>...
testtee "echo -e \"${@}\" >> ${FILE_TO_MODIFY}"
}

repo-staged-change(){
# modifies committed file and stages changes
# usage: repo-staged-change <text>...
repo-non-staged-change "${@}"
testtee git add ${FILE_TO_MODIFY}
}

repo() {
# execute given arguments on real git repo
# usage: repo <command and arguments>
testtee cd ${GIT_REPO_DIR}
testtee "${@}"
}

repo-clean() {
if [[ -d "${GIT_REPO_DIR}" ]]; then
rm -rf "${GIT_REPO_DIR}"
fi
}
10 changes: 5 additions & 5 deletions tests/git-elegant-accept-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

setup() {
init-repo
repo-new
fake-pass "git elegant obtain-work test-feature __eg"
fake-pass "git rebase origin/master"
fake-pass "git fetch --all"
Expand All @@ -17,7 +17,7 @@ setup() {
}

teardown() {
clean-git
repo-clean
fake-clean
}

Expand All @@ -32,8 +32,8 @@ teardown() {
}

@test "'accept-work': save WIP prior accepting and restore after it" {
gitrepo "git checkout -b other"
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
repo "git checkout -b other"
repo-non-staged-change "A new line..."
check git-elegant accept-work test-feature
[[ "${status}" -eq 0 ]]
[[ "${lines[@]}" =~ "git stash push" ]]
Expand Down
14 changes: 7 additions & 7 deletions tests/git-elegant-acquire-repository.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
load addons-common
load addons-read
load addons-fake
load addons-git
load addons-repo

setup() {
init-repo
repo-new
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'acquire-repository': all configurations work as expected" {
Expand Down Expand Up @@ -83,17 +83,17 @@ teardown() {
}

@test "'acquire-repository': local aliases are removed as expected" {
gitrepo git config --local "alias.aaa" "\"elegant aaa\""
gitrepo git config --local "alias.bbb" "\"elegant bbb\""
repo git config --local "alias.aaa" "\"elegant aaa\""
repo git config --local "alias.bbb" "\"elegant bbb\""
check git-elegant acquire-repository
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "2 git aliases were removed that contained 'elegant git' reference." ]]
}

@test "'acquire-repository': global aliases aren't removed" {
gitrepo git config --global "alias.glb" "\"elegant glb\""
repo git config --global "alias.glb" "\"elegant glb\""
check git-elegant acquire-repository
gitrepo git config --global --unset "alias.glb"
repo git config --global --unset "alias.glb"
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "Non-local alias! Remove it if needed using 'git config --global --unset alias.glb'" ]]
[[ "${lines[@]}" =~ "0 git aliases were removed that contained 'elegant git' reference." ]]
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-amend-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
load addons-common
load addons-fake
load addons-read
load addons-git
load addons-repo

setup() {
init-repo
repo-new
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'amend-work': command works as expected for non-master branch" {
fake-pass "git add --interactive"
fake-pass "git diff --cached --check"
fake-pass "git commit --amend"
gitrepo git checkout -b test
repo git checkout -b test
check git-elegant amend-work
[[ "${status}" -eq 0 ]]
}
Expand Down
12 changes: 6 additions & 6 deletions tests/git-elegant-clear-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
load addons-common
load addons-read
load addons-fake
load addons-git
load addons-repo

setup() {
init-repo
gitrepo git branch --force first
repo-new
repo git branch --force first
fake-pass "git branch -lvv" "first [gone]"
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'clear-local': command is available" {
Expand All @@ -22,8 +22,8 @@ teardown() {
}

@test "'clear-local': save WIP prior cleaning and restore after it" {
gitrepo "git checkout -b other"
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
repo "git checkout -b other"
repo-non-staged-change "A new line..."
check git-elegant clear-local
[[ "${status}" -eq 0 ]]
[[ "${lines[@]}" =~ "git stash push" ]]
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-deliver-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
load addons-common
load addons-read
load addons-fake
load addons-git
load addons-repo

setup() {
init-repo
repo-new
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'deliver-work': by default, a name of remote branch is equal to local branch" {
Expand Down Expand Up @@ -46,7 +46,7 @@ teardown() {

@test "'deliver-work': use stash pipe if there are uncommitted changes" {
fake-pass "git pull"
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
repo-non-staged-change "A new line..."
check git-elegant start-work test-feature
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "git stash push" ]]
Expand Down
8 changes: 4 additions & 4 deletions tests/git-elegant-save-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
load addons-common
load addons-fake
load addons-read
load addons-git
load addons-repo

setup() {
init-repo
repo-new
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'save-work': command works as expected for non-master branch" {
fake-pass "git add --interactive"
fake-pass "git diff --cached --check"
fake-pass "git commit"
gitrepo git checkout -b test
repo git checkout -b test
check git-elegant save-work
[[ "${status}" -eq 0 ]]
}
Expand Down
10 changes: 5 additions & 5 deletions tests/git-elegant-start-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

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

setup() {
init-repo
repo-new
fake-pass "git pull"
}

teardown() {
fake-clean
clean-git
repo-clean
}

@test "'start-work': branch with given name is created successfully" {
Expand All @@ -30,7 +30,7 @@ teardown() {
}

@test "'start-work': use stash pipe if there are uncommitted changes" {
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
repo-non-staged-change "A new line..."
check git-elegant start-work test-feature
[[ "$status" -eq 0 ]]
[[ "${lines[@]}" =~ "stash push" ]]
Expand All @@ -44,7 +44,7 @@ teardown() {
}

@test "'start-work': exit code is 100 when stash pipe wasn't applied" {
gitrepo "echo stash >> ${FILE_TO_MODIFY}"
repo-non-staged-change "A new line..."
fake-fail "git stash pop stash@{0}"
check git-elegant start-work test-feature
[[ "$status" -eq 100 ]]
Expand Down
2 changes: 1 addition & 1 deletion workflows
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repository() {
info-text "Start container..."
docker run -idt --rm --name repository --workdir /tmp/elegant-git-repo -v $PWD:/eg ${WORKER_IMAGE} bash
info-text "Init repository..."
docker exec -it repository bash -c "source /eg/tests/addons-git.bash;source /eg/tests/addons-common.bash; init-repo"
docker exec -it repository bash -c "source /eg/tests/addons-repo.bash;source /eg/tests/addons-common.bash; repo-new"
info-text "Install Elegant Git..."
docker exec -it repository bash -c "cd /eg; ./install.bash /usr/local src"
info-text "Ready! Enjoy experiments..."
Expand Down

0 comments on commit d012ec3

Please sign in to comment.