Skip to content

Commit

Permalink
chore: validate, tests, build and more
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed May 26, 2017
1 parent d6214cf commit d4508b9
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 7 deletions.
28 changes: 28 additions & 0 deletions .script/.validate
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

if [ -z "${VALIDATE_UPSTREAM:-}" ]; then
# this is kind of an expensive check, so let's not do this twice if we
# are running more than one validate bundlescript

VALIDATE_REPO='https://github.com/ldez/gcg.git'
VALIDATE_BRANCH='master'

VALIDATE_HEAD="$(git rev-parse --verify HEAD)"

git fetch -q "$VALIDATE_REPO" "refs/heads/$VALIDATE_BRANCH"
VALIDATE_UPSTREAM="$(git rev-parse --verify FETCH_HEAD)"

VALIDATE_COMMIT_LOG="$VALIDATE_UPSTREAM..$VALIDATE_HEAD"
VALIDATE_COMMIT_DIFF="$VALIDATE_UPSTREAM...$VALIDATE_HEAD"

validate_diff() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git diff "$VALIDATE_COMMIT_DIFF" "$@"
fi
}
validate_log() {
if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then
git log "$VALIDATE_COMMIT_LOG" "$@"
fi
}
fi
29 changes: 29 additions & 0 deletions .script/make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -e

# List of bundles to create when no argument is passed
DEFAULT_BUNDLES=(
validate-gofmt
validate-govet
validate-golint

test-unit
)

bundle() {
local bundle="$1"; shift
echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
source ".script/$bundle" "$@"
}

if [ $# -lt 1 ]; then
bundles=(${DEFAULT_BUNDLES[@]})
else
bundles=($@)
fi
for bundle in ${bundles[@]}; do
export DEST=.
ABS_DEST="$(cd "$DEST" && pwd -P)"
bundle "$bundle"
echo
done
56 changes: 56 additions & 0 deletions .script/test-unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -e

RED=$'\033[31m'
GREEN=$'\033[32m'
TEXTRESET=$'\033[0m' # reset the foreground colour

# This helper function walks the current directory looking for directories
# holding certain files ($1 parameter), and prints their paths on standard
# output, one per line.
find_dirs() {
find . -not \( \
\( \
-path './.glide/*' \
-o -path './vendor/*' \
-o -path './.git/*' \
\) \
-prune \
\) -name "$1" -print0 | xargs -0n1 dirname | sort -u
}

TESTFLAGS="-cover -coverprofile=cover.out ${TESTFLAGS}"

if [ -n "$VERBOSE" ]; then
TESTFLAGS="${TESTFLAGS} -v"
fi

if [ -z "$TESTDIRS" ]; then
TESTDIRS=$(find_dirs '*_test.go')
fi

TESTS_FAILED=()

for dir in $TESTDIRS; do
echo '+ go test' $TESTFLAGS "${dir}"
go test ${TESTFLAGS} ${dir}
if [ $? != 0 ]; then
TESTS_FAILED+=("$dir")
echo
echo "${RED}Tests failed: $dir${TEXTRESET}"
sleep 1 # give it a second, so observers watching can take note
fi
done
echo

# if some tests fail, we want the bundlescript to fail, but we want to
# try running ALL the tests first, hence TESTS_FAILED
if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
echo
false
else
echo "${GREEN}Test success${TEXTRESET}"
echo
true
fi
13 changes: 13 additions & 0 deletions .script/validate-glide
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

source "$(dirname "$BASH_SOURCE")/.validate"

if grep -q "$(glide-hash)" glide.lock; then
echo 'Congratulations! glide.lock is unchanged.'
else
{
echo "Error: glide.lock has been manually changed. Don't do this. Use script/glide.sh up instead."
echo
} >&2
false
fi
30 changes: 30 additions & 0 deletions .script/validate-gofmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

source "$(dirname "$BASH_SOURCE")/.validate"

IFS=$'\n'
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^\(integration/\)\?vendor/' || true) )
unset IFS

badFiles=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed is formatted
if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then
badFiles+=( "$f" )
fi
done

if [ ${#badFiles[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files are properly formatted.'
else
{
echo "These files are not properly gofmt'd:"
for f in "${badFiles[@]}"; do
echo " - $f"
done
echo
echo 'Please reformat the above files using "gofmt -s -w" and commit the result.'
echo
} >&2
false
fi
31 changes: 31 additions & 0 deletions .script/validate-golint
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

source "$(dirname "$BASH_SOURCE")/.validate"

IFS=$'\n'
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^\(integration/\)\?vendor/\|autogen' || true) )
unset IFS

errors=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed passes go vet
failedLint=$(golint "$f")
if [ "$failedLint" ]; then
errors+=( "$failedLint" )
fi
done

if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files have been linted.'
else
{
echo "Errors from golint:"
for err in "${errors[@]}"; do
echo "$err"
done
echo
echo 'Please fix the above errors. You can test via "golint" and commit the result.'
echo
} >&2
false
fi
31 changes: 31 additions & 0 deletions .script/validate-govet
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

source "$(dirname "$BASH_SOURCE")/.validate"

IFS=$'\n'
files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^\(integration/\)\?vendor/' || true) )
unset IFS

errors=()
for f in "${files[@]}"; do
# we use "git show" here to validate that what's committed passes go vet
failedVet=$(go tool vet -printf=false "$f")
if [ "$failedVet" ]; then
errors+=( "$failedVet" )
fi
done

if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files have been vetted.'
else
{
echo "Errors from govet:"
for err in "${errors[@]}"; do
echo "$err"
done
echo
echo 'Please fix the above errors. You can test via "go vet" and commit the result.'
echo
} >&2
false
fi
33 changes: 33 additions & 0 deletions .script/validate-misspell
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

source "$(dirname "$BASH_SOURCE")/.validate"

IFS=$'\n'
src=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^\(integration/\)\?vendor/\|autogen' || true) )
docs=( $(validate_diff --diff-filter=ACMR --name-only -- 'docs/*.md') )
unset IFS
files=("${src[@]}" "${docs[@]}")

errors=()
for f in "${files[@]}"; do
# we use source text here so we also check spelling of variable names
failedSpell=$(misspell -source=text "$f")
if [ "$failedSpell" ]; then
errors+=( "$failedSpell" )
fi
done

if [ ${#errors[@]} -eq 0 ]; then
echo 'Congratulations! All Go source files and docs have been checked for common misspellings.'
else
{
echo "Errors from misspell:"
for err in "${errors[@]}"; do
echo "$err"
done
echo
echo 'Please fix the above errors. You can test via "misspell" and commit the result.'
echo
} >&2
false
fi
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ addons:

install: make dependencies

script: make test-unit

deploy:
- provider: releases
api_key: ${GITHUB_TOKEN}
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
.PHONY: all

default: test-unit binary
default: test-unit validate build

dependencies:
glide install

binary:
build:
go build

validate:
./.script/make.sh validate-glide validate-gofmt validate-govet validate-golint validate-misspell

test-unit:
go test -v -cover -coverprofile=cover.out "github.com/ldez/gcg" &&\
go test -v -cover -coverprofile=cover.out "github.com/ldez/gcg/core" &&\
go test -v -cover -coverprofile=cover.out "github.com/ldez/gcg/label"
./.script/make.sh test-unit

0 comments on commit d4508b9

Please sign in to comment.