Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions bash/bats/bats_tap12_to_tap13.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/awk -f
#
# usage: awk -f bats_tap12_to_tap13.awk /path/to/bash/script > output.tap13
#

# BEGIN used for top level declarations for vars that need a reset per top-level match.
BEGIN {
tap13 = ""
sys_out = ""
cmd_output = ""
}

function append_sysout(sys_out, cmd_output) {
if (sys_out) {
if (cmd_output) {
sys_out = sys_out " output: |\n" cmd_output
}
sys_out = " ---\n" sys_out " ...\n"
tap13 = tap13 sys_out
}
return tap13
}

! /^#/ {
tap13 = append_sysout(sys_out, cmd_output)
sys_out = ""
cmd_output = ""
tap13 = tap13 $0 "\n"
}

/^#/ {
in_sys_out = 1
}

in_sys_out {
if (! /^#/) {
in_cmd_out = 0
in_sys_out = 0
}
else {
file_name = ""
line_no = ""
status = ""
line = ""
failed_assertion = ""
if (/.*in test file/) {
file_name = $0
sub(/^.*in test file */, "", file_name)
sub(/,.*$/, "", file_name)

line_no = $0
sub(/^.*line */, "", line_no)
sub(/[^0-9]+$/, "", line_no)
sys_out = sys_out " file: " file_name "\n"
sys_out = sys_out " line: " line_no "\n"
}
else if (/^# *status: [0-9]+/) {
status = $0
gsub(/[^0-9]/, "", status)
sys_out = sys_out " status: " status "\n"

}
else {
line = $0
sub(/^# */, " ", line)
cmd_output = cmd_output line "\n"
}
}
}

END {
tap13 = append_sysout(sys_out, cmd_output)
sys_out = ""
cmd_output = ""
print tap13
}
189 changes: 189 additions & 0 deletions bash/bats/habitual/git/check_for_changes.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#!/usr/bin/env bats
# vim: et sr sw=4 ts=4 smartindent syntax=sh:
#

load git

@test "check_for_changes takes dir as arg" {
# ... setup - starting dir is non-git dir
dir="$TMPDIR/not-a-git-dir"
mkdir -p $dir
use_test_repo_copy
cd $dir # move out of test repo, so check_for_changes moves to user-specified dir

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
echo $output | grep -q "checking for uncommitted changes in $TEST_REPO"
}

@test "check_for_changes defaults to current dir if no arg" {
# ... setup
use_test_repo_copy

# ... run
run check_for_changes
print_on_err

# ... verify
echo $output | grep -q "checking for uncommitted changes in $TEST_REPO"
}

@test "check_for_changes fails if dir does not exist" {
# ... setup
dir="$TMPDIR/does-not-exist"

# ... run
run check_for_changes "$dir"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "couldn't cd to $dir"
}

@test "check_for_changes fails if dir not a git dir" {
# ... setup
dir="$TMPDIR/not-a-git-dir"
mkdir -p $dir

# ... run
run check_for_changes "$dir"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q 'not a git dir'
}

@test "check_for_changes passes if no uncommitted changes" {
# ... setup
use_test_repo_copy

# ... run
run check_for_changes
print_on_err

# ... verify
[[ $status -eq 0 ]]
echo $output | grep -q "checking for uncommitted changes in $TEST_REPO"
echo $output | grep -q '... none found'
}

@test "check_for_changes fails if git-tracked file modified (no add)" {
# ... setup
use_test_repo_copy
echo "foo" >> README.md

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes fails if git-tracked file modified (git added)" {
# ... setup
use_test_repo_copy
echo "foo" >> README.md
git add README.md

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes fails if git-tracked file deleted (no add)" {
# ... setup
use_test_repo_copy
rm README.md

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes fails if git-tracked file deleted (git add)" {
# ... setup
use_test_repo_copy
rm README.md
git add --all

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes fails if git-tracked file perms changed (no add)" {
# ... setup
use_test_repo_copy
chmod a+x README.md

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes fails if git-tracked file perms changed (git add)" {
# ... setup
use_test_repo_copy
chmod a+x README.md
git add --all

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}

@test "check_for_changes succeeds if new file appears (no add)" {
# ... setup
use_test_repo_copy
echo "foo" > new_file

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 0 ]]
echo $output | grep -q "none found"
}

@test "check_for_changes fails if new file appears (git added)" {
# ... setup
use_test_repo_copy
echo "foo" > new_file
git add new_file

# ... run
run check_for_changes "$TEST_REPO"
print_on_err

# ... verify
[[ $status -eq 1 ]]
echo $output | grep -q "local changes in $TEST_REPO"
}
74 changes: 74 additions & 0 deletions bash/bats/habitual/git/git.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
setup() {
. habitual/std.functions || return 1
. habitual/git.functions || return 1

export TMPDIR=$BATS_TMPDIR/$BATS_TEST_NAME
mkdir -p $TMPDIR || true

export GIT_REPO_URL="https://github.com/opsgang/libs"

export TMPL_REPO="/var/tmp/opsgang/libs/repo"
if [[ ! -d $TMPL_REPO ]] || [[ ! -r $TMPL_REPO ]]; then
echo >&2 "ERROR: $TMPL_REPO is not readable directory"
return 1
fi

export TEST_REPO="$TMPDIR/repo"

export _GIT_USER="boo"
export _GIT_EMAIL="boo@boo.com"

export NEW_BRANCH="new-branch"
export NEW_TAG="new-tag"

# ... set so that git does not use any global .gitconfig
export HOME=$TMPDIR

cat <<EOF >$HOME/.gitconfig
[user]
name = $_GIT_USER
email = $_GIT_EMAIL
EOF


}

export_shas() {
HEAD_SHA=$(git log --format='%H' | head -n 1)
SECOND_SHA=$(git log --format='%H' | sed -n '2p')
export HEAD_SHA SECOND_SHA
}

teardown() {
rm -rf $BATS_TMPDIR/$BATS_TEST_NAME || true
}

print_on_err() {
local mode="$1"
if [[ "$mode" == "git_vars" ]]; then
echo "GIT_BRANCH:[${GIT_BRANCH:-not set}]"
echo "GIT_TAG:[${GIT_TAG:-not set}]"
echo "GIT_SHA:[${GIT_SHA:-not set}]"
echo "GIT_USER:[${GIT_USER:-not set}]"
echo "GIT_EMAIL:[${GIT_EMAIL:-not set}]"
echo "GIT_ID:[${GIT_ID:-not set}]"
echo "GIT_INFO:[${GIT_INFO:-not set}]"
echo "EXP PATTERN:[$rx]"
echo "status: ${rc:-unknown}"
elif [[ "$mode" =~ ^git_(branch|tag|sha|user|email|id)$ ]]; then
local var="${mode^^}" ; local val="${!var}"
echo "${var}:[${val:-not set}]"
echo "EXP PATTERN:[$rx]"
echo "status: ${rc:-unknown}"
else
echo "START OUTPUT--|$output|--END OUTPUT"
echo "status: $status"
fi
}

use_test_repo_copy() {
cp -r $TMPL_REPO $TMPDIR
cd $TEST_REPO
git reset --hard &>/dev/null || true
}

56 changes: 56 additions & 0 deletions bash/bats/habitual/git/git_branch.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bats
# vim: et sr sw=4 ts=4 smartindent syntax=sh:
#

load git

@test "git_branch fails if run against a non-git dir" {

# ... setup - working dir is non-git dir
mkdir -p $TMPDIR/foo ; cd $TMPDIR/foo

# ... run
run git_branch
print_on_err

# ... verify
[[ $status -ne 0 ]]
echo $output | grep -q 'not in a git repo'
}

@test "git_branch shows branch name even if head commit tagged" {

# ... setup - create a tag on new branch
use_test_repo_copy
git checkout -b $NEW_BRANCH &>/dev/null
git tag -a "$NEW_TAG" -m 'bah'

# ... run
run git_branch
print_on_err

# ... verify
[[ $status -eq 0 ]]
[[ $output == $NEW_BRANCH ]]
}

@test "git_branch shows nothing if tag checked out" {

# ... setup - tag a new branch, move ahead a commit
# then check out created tag to be sure we are getting
# value of tag not HEAD.
use_test_repo_copy
git checkout -b $NEW_BRANCH &>/dev/null
git tag -a "$NEW_TAG" -m 'bah'
echo 'new commit' >>README.md
git commit -am "arbitrary change for test $BATS_TEST_NAME"
git checkout $NEW_TAG

# ... run
run git_branch
print_on_err

# ... verify
[[ $status -eq 0 ]]
[[ $output == "" ]]
}
Loading