Skip to content

Commit

Permalink
Don't use obtain-work as a checkout mechanism in start-work
Browse files Browse the repository at this point in the history
Since release 0.5.0, `obtain-work` works only with remote
braches. But we try to use it as a checkout mechanism for a local
branch in `start-work` which breaks its execution.

In order to resolve this issue, the following is made:
1. The checkout procedure of `start-work` is replaced with alternative
`git` commands.
2. Now the `addons-fake.bash` supports redirection of incomming
request to original command if there is no mocked command. This allows
having partially-mocked executions for testing (previously, either usage
of real git or faked command was allowed).

Also, one additional rule is added to the "Writing tests" section. It
was added as faking a `git-elegant` commands lead to such issues. In
the future, any newly-created or updated test(s) should follow it.
  • Loading branch information
extsoft committed Sep 13, 2019
1 parent 449668e commit a735820
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Also, there are several optional addons which can be useful in some circumstance
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 `clean-fake` or `clean-git` within a `teardown()` method.
5. Do not fake `git-elegant` commands within the tests.

#### Assertions
- `[[ "${lines[0]}" = "+ the space " ]]` for an output line (index starts from 0)
Expand Down
1 change: 0 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ Approximate commands flow is
==> git elegant start-work task-123
git stash save elegant-git
git checkout master
git fetch --tags
git pull
git checkout -b task-123
git stash apply stash^{/elegant-git}
Expand Down
4 changes: 2 additions & 2 deletions libexec/git-elegant-start-work
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Approximate commands flow is
==> git elegant start-work task-123
git stash save elegant-git
git checkout master
git fetch --tags
git pull
git checkout -b task-123
git stash apply stash^{/elegant-git}
Expand All @@ -36,7 +35,8 @@ MESSAGE
default() {
_error-if-empty "$1" "Please give a name for the new branch."
status=$(boxtee git stash save elegant-git)
git elegant obtain-work ${MASTER}
git checkout ${MASTER}
git pull
boxtee git checkout -b "$1"

if [[ "$status" =~ "Saved working directory" ]]; then
Expand Down
33 changes: 23 additions & 10 deletions tests/addons-fake.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,34 @@ fake() {
FIXTURE_HOME="$PROGRAM_PATH/$(echo "$2" | sed 's/[^0-9a-zA-Z]*//g')"
MOCK="$MOCK_DIR/$BASENAME"

[ -d "$FIXTURE_HOME" ] && rm -r "$FIXTURE_HOME"
[[ -d "$FIXTURE_HOME" ]] && rm -r "$FIXTURE_HOME"
echo ""
echo "==>> Creating mock: ${FIXTURE_HOME}"
_ex_fake mkdir -p "$FIXTURE_HOME"
echo -e "$3" | tee -i "$FIXTURE_HOME/exit_code"
echo -e "$4" | tee -i "$FIXTURE_HOME/stdout"
echo -e "$5" | tee -i "$FIXTURE_HOME/stderr"

[ -e "$MOCK" ] && rm -r "$MOCK"
echo "#!/usr/bin/env bash
PROGRAM_PATH=\"$MOCK_DIR/$BASENAME-app\"
FIXTURE_HOME=\"\$PROGRAM_PATH/\$(echo \"\$@\" | sed 's/[^0-9a-zA-Z]*//g')\"
cat \"\$FIXTURE_HOME/stdout\"
cat \"\$FIXTURE_HOME/stderr\" >&2
exit \$(cat \"\$FIXTURE_HOME/exit_code\")
" | tee -i "$MOCK"
_ex_fake chmod +x "$MOCK"
if [[ ! -e "${MOCK}" ]]; then
echo ""
echo "==>> Creating executable: ${MOCK}"
ORIGIN_BINARY=$(which ${BASENAME})
cat <<MOCK | tee -i ${MOCK} && _ex_fake chmod +x "$MOCK"
#!/usr/bin/env bash
# This is an alternative executable for "${BASENAME}".
# It's purpose is to use a mock, if available, otherwise,
# run original executable.
PROGRAM_PATH="${MOCK_DIR}/${BASENAME}-app"
FIXTURE_HOME="\${PROGRAM_PATH}/\$(echo "\$@" | sed 's/[^0-9a-zA-Z]*//g')"
if [[ -e "\${FIXTURE_HOME}" ]]; then
cat "\${FIXTURE_HOME}/stdout"
cat "\${FIXTURE_HOME}/stderr" >&2
exit \$(cat "\${FIXTURE_HOME}/exit_code")
else
${ORIGIN_BINARY} \$@
fi
MOCK
fi
}

fake-pass() {
Expand Down
45 changes: 23 additions & 22 deletions tests/git-elegant-start-work.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,51 @@

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

setup() {
fake-pass git "elegant obtain-work master"
fake-pass git "checkout -b test-feature"
fake-pass git "stash save elegant-git"
init-repo
fake-pass git "pull"
}

teardown() {
clean-fake
clean-git
}

@test "'start-work': branch with given name is created successfully" {
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
}

@test "'start-work': exit code is 45 when branch name isn't set" {
check git-elegant start-work
[ "$status" -eq 45 ]
check git-elegant start-work
[ "$status" -eq 45 ]
}

@test "'start-work': print error message when branch name isn't set" {
check git-elegant start-work
[[ "${lines[0]}" =~ "Please give a name for the new branch." ]]
check git-elegant start-work
[[ "${lines[0]}" =~ "Please give a name for the new branch." ]]
}

@test "'start-work': use stash for available changes" {
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-pass git "stash drop stash@{0}"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-pass git "stash drop stash@{0}"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
}

@test "'start-work': ignore stash if there are no changes" {
fake-pass git "stash save elegant-git" "No local changes to save"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
fake-pass git "stash save elegant-git" "No local changes to save"
check git-elegant start-work test-feature
[ "$status" -eq 0 ]
}

@test "'start-work': exit code is 100 when stash wasn't applied" {
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-fail git "stash drop stash@{0}"
check git-elegant start-work test-feature
[ "$status" -eq 100 ]
fake-pass git "stash save elegant-git" "Saved working directory"
fake-pass git "stash apply stash^{/elegant-git}"
fake-fail git "stash drop stash@{0}"
check git-elegant start-work test-feature
[ "$status" -eq 100 ]
}

0 comments on commit a735820

Please sign in to comment.