From 32d6f8a398cd89b98aa8a0a2f838bb40b512d7eb Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Fri, 24 Nov 2017 21:22:01 +0000 Subject: [PATCH] Test in Travis --- .gitignore | 25 +------------- .travis.yml | 16 +++++++++ Gopkg.lock | 15 +++++++++ Gopkg.toml | 26 +++++++++++++++ README.md | 4 ++- bin/coverage | 58 ++++++++++++++++++++++++++++++++ bin/lint | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/setup-travis | 18 ++++++++++ bin/start | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/test | 22 ++++++++++++ region.go | 2 +- 11 files changed, 334 insertions(+), 26 deletions(-) create mode 100644 .travis.yml create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100755 bin/coverage create mode 100755 bin/lint create mode 100755 bin/setup-travis create mode 100644 bin/start create mode 100755 bin/test diff --git a/.gitignore b/.gitignore index daf913b..61ead86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof +/vendor diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7932999 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: go +go: + - 1.9.x +go_import_path: github.com/teamwork/vat +notifications: + email: false +cache: + directories: + - $HOME/gopath/pkg +install: + - ./bin/setup-travis +script: + - cd $HOME/gopath/src/github.com/teamwork/vat + - ./bin/test + - ./bin/coverage + - ./bin/lint diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..14a0462 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/mattes/vat" + packages = ["."] + revision = "922d56449314355779ce449d646a491e6f7192cf" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "484db29fdbbd5f0b9f2d755ed264886d105ea9d80a622b008dc37f2074d556d1" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..46a1859 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,26 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + branch = "master" + name = "github.com/mattes/vat" diff --git a/README.md b/README.md index 5eb60b2..474aaa0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ +[![Build Status](https://travis-ci.org/Teamwork/vat.svg?branch=master)](https://travis-ci.org/Teamwork/vat) + # vat VAT matching and validation in Go ## Usage -See http://godoc.org/github.com/Teamwork/vat for usage and examples \ No newline at end of file +See http://godoc.org/github.com/Teamwork/vat for usage and examples diff --git a/bin/coverage b/bin/coverage new file mode 100755 index 0000000..a62171d --- /dev/null +++ b/bin/coverage @@ -0,0 +1,58 @@ +#!/bin/bash + +usage="Run coverage analyses and send it to Codecov." + +# Setup +####### +set -euC +root="$(cd "$(dirname "$0")/.." && pwd)" +. "$root/bin/start" + +# Run action +############ + +# Only test packages with actual test files. +test_pkgs=$(find -name "*_test.go" | + grep -v /vendor/ | + xargs dirname | + sort -u | + sed -e "s#^\.#$pkgname#") + +# Pass all packages to -coverpkg to list coverage for all packages, even those +# without tests. +allpkg=$(go list -tags="$test_tags" ./... | + grep -v /vendor/ | + tr '\n' , | + sed -e 's/,$//') + +# Cache +go test -i -covermode=count -tags="$test_tags" $(go list ./... | grep -v /vendor/) + +$pre_test_func + +echo 'mode: count' >| coverage.txt +for pkg in $test_pkgs; do + go test \ + -tags="$test_tags" \ + -covermode=count \ + -coverprofile=profile.out \ + -coverpkg=$allpkg \ + $pkg 2>&1 | grep -v 'warning: no packages being tested depend on ' + + if [ -f profile.out ]; then + tail -n+2 profile.out >> coverage.txt + rm profile.out + fi +done + +# The token is optional for public repos. +[ -n "${codecov_token:-}" ] && codecov_token="-t $codecov_token" + +if [ -n ${TRAVIS} ]; then + bash <(curl -s https://codecov.io/bash) $codecov_token +else + bash <(curl -s https://codecov.io/bash) \ + $codecov_token \ + -B $(git rev-parse --abbrev-ref HEAD) \ + -C $(git rev-parse HEAD) +fi diff --git a/bin/lint b/bin/lint new file mode 100755 index 0000000..b53a7f4 --- /dev/null +++ b/bin/lint @@ -0,0 +1,87 @@ +#!/bin/sh + +usage="Run Go linter tools" + +# Setup +####### +set -euC +root="$(cd "$(dirname "$0")/.." && pwd)" +. "$root/bin/start" + +# Run action +############ + +tmpfile="$(mktemp)" + +ignore_grep() { + local IFS="$(printf '\n\b')" + for d in $lint_ignore; do printf "|$d"; done +} + +ignore_metalinter() { + local IFS="$(printf '\n\b')" + for d in $lint_ignore; do printf " -s $d"; done +} + +paths=$(go list ./... | grep -Ev "/(vendor$(ignore_grep))" | sed -e "s#^$pkgname#.#") + +cleanup() { + rm -f "$tmpfile" +} +trap cleanup EXIT + +# Skip the linter if we're on master or beta +if [ -n "$TRAVIS" ]; then + branch=${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH:-}} + branch=${branch:-$(git rev-parse --abbrev-ref HEAD)} + branch=${branch:-unknown} + case "$branch" in + master|beta|eu|unknown) + echo "We're on $branch; skipping lint tests" + exit 0 + ;; + esac +fi + +# Ensure generated files are up to date; only run on Travis to prevent +# clobbering people's working directories. +if [ -n "$TRAVIS" ]; then + go generate $(go list ./... | grep -v /vendor/) + changes="$(git diff)" + if [ -n "$changes" ]; then + echo + echo "*****************************************************" + echo "*** ***" + echo "*** Changes in generated files: ***" + echo "*** ***" + echo "*****************************************************" + echo + echo "$changes" + fi +fi + +if [ -n "$TRAVIS" ]; then + # Set up git for Travis here + git fetch -q origin master:refs/remotes/origin/master + + # Install for Travis. + go get -u github.com/alecthomas/gometalinter + gometalinter --install +fi + +gometalinter --deadline=120s --vendor --sort=path --tests --disable-all \ + $(ignore_metalinter) \ + --enable=vet \ + --enable=golint \ + --enable=varcheck \ + --enable=structcheck \ + --enable=errcheck \ + --enable=megacheck \ + --enable=ineffassign \ + --enable=interfacer \ + --enable=unconvert \ + --enable=goconst \ + --enable=gas \ + --enable=goimports \ + --enable=lll --line-length=120 \ + ./... diff --git a/bin/setup-travis b/bin/setup-travis new file mode 100755 index 0000000..14bf119 --- /dev/null +++ b/bin/setup-travis @@ -0,0 +1,18 @@ +#!/bin/sh + +usage="Setup the environment for Travis" + +# Setup +####### +set -euC +root="$(cd "$(dirname "$0")/.." && pwd)" +. "$root/bin/start" + +# Run action +############ + +# Setup dep if it's used and vendor isn't in git. +if [ -f Gopkg.toml -a ! -d vendor ]; then + go get -u github.com/golang/dep/cmd/dep + dep ensure +fi diff --git a/bin/start b/bin/start new file mode 100644 index 0000000..caf2e27 --- /dev/null +++ b/bin/start @@ -0,0 +1,87 @@ +# vi:ft=sh + +set -euC + +cd "$root" + +# Load settings +[ -f "$root/bin/settings" ] && . "$root/bin/settings" + +### +# Stuff to set in settings file. + +# Codecov token for coverage reports; only needed for private repos. +codecov_token=${codecov_token:-} + +# Tags to add to "go test" when running tests. +test_tags=${test_tags:-} + +# Verbose output? +verbose=${verbose:-0} + +# Directory names to ignore in the ./bin/lint script; multiple directories are +# separated by newlines. +lint_ignore="${lint_ignore:-}" + +# Callback to run before "go test" in ./bin/test and ./bin/coverage. +pre_test_func=${pre_test_func:-:} + + +### +# For the following the defaults should almost always be okay. If not, then +# consider fixing the project maybe? + +# Binary name. +name=${name:-$(basename "$root")} + +# Go package name +pkgname=${pkgname:-"github.com/teamwork/$name"} + +# Git branch +branch=${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}} + +# Docker image tag; usually the commit +docker_tag=${docker_tag:-${TRAVIS_COMMIT:-dev-env}} + +# Make sure this is set locally so we can do -z/-n. +TRAVIS=${TRAVIS:-} + +# So scripts can override this. +usage_func=${usage_func:-_usage} +options_func=${options_func:-_parse_options} + +# Usage info. +_usage() { + echo "Usage: ${0##*/} [-hv] [-t tag]" + echo + echo "$usage" + echo + echo "Options:" + echo " -h Show this help" + echo " -v Enable verbose output" + echo " -t Set tag for Docker and main.version" + echo " -b Go build tag; can be given more than once" + echo +} + +# Parse options +_parse_options() { + while getopts "hvt:b:" option; do + case "$option" in + h) $usage_func; exit 0 ;; + v) verbose=1; set -x ;; + t) tag=$OPTARG ;; + b) test_tags="$test_tags $OPTARG" ;; + *) + echo "error: unknown option '$option'" + $usage_func + exit 1 + ;; + esac + done + + [ -n "$test_tags" ] && test_tags="${test_tags# }" + return 0 +} + +$options_func "$@" diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..ba1e08e --- /dev/null +++ b/bin/test @@ -0,0 +1,22 @@ +#!/bin/sh + +usage="Run all tests" + +# Setup +####### +set -euC +root="$(cd "$(dirname "$0")/.." && pwd)" +. "$root/bin/start" + +# Run action +############ + +# If we don't set this some stacks may not be complete when encountering race +# conditions. Uses a bit more memory, but we usually have enough of that. +export GORACE="history_size=4" + +$pre_test_func + +v_flag="" +[ $verbose -ge 1 ] && v_flag="-v" +go test $v_flag -race -tags="${test_tags# }" $(go list ./... | grep -v /vendor/) diff --git a/region.go b/region.go index dc4a2ec..04d33a7 100644 --- a/region.go +++ b/region.go @@ -112,6 +112,6 @@ var validRegions []string func init() { for k := range regionPatterns { - validRegions = append(validRegions, string(k)) + validRegions = append(validRegions, k) } }