Skip to content

Commit

Permalink
Disallow save-work for local master branch
Browse files Browse the repository at this point in the history
According to Elegant Git philosophy, changes have to be applied for
non-master branches only. It's relevant for commits too.

Add `gitrepo` command which allow executing commands on real git
repository for unit testing.
  • Loading branch information
extsoft committed Jul 29, 2019
1 parent fa99021 commit 070dfbf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions libexec/git-elegant-save-work
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
set -e

default(){
local BRANCH=$(__branches 'git branch | grep \*')
if [[ "$BRANCH" == "master" ]]; then
box "No commits to 'master' branch. Please read more on ${__site}"
box "Try 'git elegant start-work' prior to retrying this command."
exit 42
fi
boxtee git add --interactive
boxtee git diff --cached --check
boxtee git commit
Expand Down
8 changes: 8 additions & 0 deletions tests/addons-git.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ add-st-change(){
testtee git add $FILE_TO_MODIFY
}

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

clean-git() {
if [ -d "$GIT_REPO_DIR" ]; then
rm -rf "$GIT_REPO_DIR"
Expand Down
18 changes: 14 additions & 4 deletions tests/git-elegant-save-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@
load addons-common
load addons-fake
load addons-read
load addons-git

setup() {
init-repo
}

teardown() {
clean-fake
clean-git
}

setup() {
@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
fake-pass git "commit"
gitrepo git checkout -b test
check git-elegant save-work
[[ "${status}" -eq 0 ]]
}

@test "'save-work': command is available" {
@test "'save-work': exit code is 42 when current local branch is master" {
check git-elegant save-work
[ "$status" -eq 0 ]
[[ "${status}" -eq 42 ]]
[[ "${lines[@]}" =~ "== No commits to 'master' branch. Please read more on https://elegant-git.bees-hive.org ==" ]]
[[ "${lines[@]}" =~ "== Try 'git elegant start-work' prior to retrying this command. ==" ]]
}

0 comments on commit 070dfbf

Please sign in to comment.