Skip to content

Commit

Permalink
Add initial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
extsoft committed Aug 21, 2017
1 parent 4b3c473 commit fa67d38
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rultor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ merge:
release:
script: |-
echo "Release ${tag}"
bats $PWD
bats src/test
3 changes: 2 additions & 1 deletion run-tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash -e

bats $PWD
bats src/test
rm -rf tmp/*
pdd --source=$(pwd) --verbose --file=/dev/null
2 changes: 1 addition & 1 deletion src/main/git-elegant
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ clear-local() {


FUNC=$1
[ -z "$FUNC" ] && commands && exit -1
[ -z "$FUNC" ] && commands && exit 10
shift
eval '$FUNC $@'

51 changes: 51 additions & 0 deletions src/test/commons.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -e

THIS="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BIN_DIR="$THIS/../../src/main"
TMP_DIR="$THIS/../../tmp"
export PATH=$TMP_DIR:$BIN_DIR:$PATH


fake() {
# sample: fake <command> <subcommand> <exit> <stdout> <stderr>
BASENAME=$(basename $1)
PROGRAM_PATH="$TMP_DIR/$BASENAME-app"
FIXTURE_HOME="$PROGRAM_PATH/$(echo "$2" | sed 's/[^0-9a-zA-Z]*//g')"
MOCK="$TMP_DIR/$BASENAME"

[ -d "$FIXTURE_HOME" ] && rm -r "$FIXTURE_HOME"
mkdir -p "$FIXTURE_HOME"
echo -e "$3" > "$FIXTURE_HOME/exit_code"
echo -e "$4" > "$FIXTURE_HOME/stdout"
echo -e "$5" > "$FIXTURE_HOME/stderr"

[ -e "$MOCK" ] && rm -r "$MOCK"
echo "#!/usr/bin/env bash
PROGRAM_PATH=\"$TMP_DIR/$BASENAME-app\"
FIXTURE_HOME=\"\$PROGRAM_PATH/\$(echo \"\$@\" | sed 's/[^0-9a-zA-Z]*//g')\"
cat \"\$FIXTURE_HOME/stdout\"
cat \"\$FIXTURE_HOME/stderr\" >&2
read -r exit_code < \"\$FIXTURE_HOME/exit_code\"
exit \$exit_code
" > "$MOCK"
chmod +x "$MOCK"
}

fake-pass() {
# sample: fake-pass <command> <subcommand> <stdout>
COMMAND="$1"; shift
SUBCOMMAND="$1"; shift
fake "$COMMAND" "$SUBCOMMAND" 0 "$@"
}

fake-fail() {
# sample: fake-fail <command> <subcommand> <stderr>
COMMAND="$1"; shift
SUBCOMMAND="$2"; shift
fake "$COMMAND" "$SUBCOMMAND" 100 " " "$@"
}

export -f fake
export -f fake-pass
export -f fake-fail
9 changes: 9 additions & 0 deletions src/test/fake-read.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This is a mock for system function 'read'
#!/usr/bin/env bash
set -e

read() {
sleep 0
}

export -f read
44 changes: 44 additions & 0 deletions src/test/git-elegant.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bats -ex

load commons
load fake-read

@test "print available commands when run 'git-elegant'" {
run git-elegant
[[ "${lines[0]}" =~ "feature" ]]
[[ "${lines[1]}" =~ "pull" ]]
[[ "${lines[2]}" =~ "push" ]]
[[ "${lines[3]}" =~ "push-after-rebase" ]]
[[ "${lines[4]}" =~ "rebase" ]]
[[ "${lines[5]}" =~ "init" ]]
[[ "${lines[6]}" =~ "clone" ]]
[[ "${lines[7]}" =~ "add" ]]
[[ "${lines[8]}" =~ "clear-local" ]]
}


@test "exit code is 10 when run 'git-elegant'" {
run git-elegant
echo $status
[ "$status" -eq 10 ]
}


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


setup() {
fake-pass git init
fake-pass git "config --global user.name" aaa
fake-pass git "config --global user.email" [email protected]
fake-pass git "config --local user.name aaa"
fake-pass git "config --local user.email [email protected]"
}

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

0 comments on commit fa67d38

Please sign in to comment.