2016-06-29 07:36:40 +00:00
|
|
|
// +build ignore
|
2015-08-19 18:12:41 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-20 17:09:35 +00:00
|
|
|
"bytes"
|
2016-01-17 17:33:03 +00:00
|
|
|
"flag"
|
2015-08-19 18:12:41 +00:00
|
|
|
"fmt"
|
2016-01-26 22:42:45 +00:00
|
|
|
"io"
|
2015-12-19 16:21:45 +00:00
|
|
|
"io/ioutil"
|
2016-01-26 22:42:45 +00:00
|
|
|
"net/http"
|
2015-08-19 18:12:41 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-12-19 17:22:57 +00:00
|
|
|
"regexp"
|
2015-08-19 18:12:41 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
var runCrossCompile = flag.Bool("cross-compile", true, "run cross compilation tests")
|
2016-01-26 22:42:45 +00:00
|
|
|
var minioServer = flag.String("minio", "", "path to the minio server binary")
|
2016-04-17 16:22:26 +00:00
|
|
|
var debug = flag.Bool("debug", false, "output debug messages")
|
2016-01-17 17:33:03 +00:00
|
|
|
|
2016-04-17 15:49:36 +00:00
|
|
|
var minioServerEnv = map[string]string{
|
|
|
|
"MINIO_ACCESS_KEY": "KEBIYDZ87HCIH5D17YCN",
|
|
|
|
"MINIO_SECRET_KEY": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe",
|
|
|
|
}
|
|
|
|
|
|
|
|
var minioEnv = map[string]string{
|
|
|
|
"RESTIC_TEST_S3_SERVER": "http://127.0.0.1:9000",
|
|
|
|
"AWS_ACCESS_KEY_ID": "KEBIYDZ87HCIH5D17YCN",
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe",
|
|
|
|
}
|
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// CIEnvironment is implemented by environments where tests can be run.
|
2015-08-19 18:12:41 +00:00
|
|
|
type CIEnvironment interface {
|
2016-04-17 16:22:26 +00:00
|
|
|
Prepare() error
|
|
|
|
RunTests() error
|
|
|
|
Teardown() error
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// TravisEnvironment is the environment in which Travis tests run.
|
2015-08-19 18:12:41 +00:00
|
|
|
type TravisEnvironment struct {
|
2016-05-08 21:25:30 +00:00
|
|
|
goxOSArch []string
|
|
|
|
minio string
|
2016-04-17 15:49:36 +00:00
|
|
|
|
|
|
|
minioSrv *Background
|
|
|
|
minioTempdir string
|
|
|
|
|
|
|
|
env map[string]string
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *TravisEnvironment) getMinio() error {
|
2016-01-26 22:42:45 +00:00
|
|
|
if *minioServer != "" {
|
|
|
|
msg("using minio server at %q\n", *minioServer)
|
|
|
|
env.minio = *minioServer
|
2016-04-17 16:22:26 +00:00
|
|
|
return nil
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tempfile, err := ioutil.TempFile("", "minio-server-")
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("create tempfile for minio download failed: %v\n", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:52:39 +00:00
|
|
|
url := fmt.Sprintf("https://dl.minio.io/server/minio/release/%s-%s/minio",
|
|
|
|
runtime.GOOS, runtime.GOARCH)
|
|
|
|
msg("downloading %v\n", url)
|
|
|
|
res, err := http.Get(url)
|
2016-01-26 22:42:45 +00:00
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error downloading minio server: %v\n", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(tempfile, res.Body)
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error saving minio server to file: %v\n", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error closing HTTP download: %v\n", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tempfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
msg("closing tempfile failed: %v\n", err)
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error closing minio server file: %v\n", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(tempfile.Name(), 0755)
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("chmod(minio-server) failed: %v", err)
|
2016-01-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg("downloaded minio server to %v\n", tempfile.Name())
|
|
|
|
env.minio = tempfile.Name()
|
2016-04-17 16:22:26 +00:00
|
|
|
return nil
|
2015-12-19 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *TravisEnvironment) runMinio() error {
|
|
|
|
if env.minio == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-17 15:49:36 +00:00
|
|
|
// start minio server
|
2016-04-17 16:22:26 +00:00
|
|
|
msg("starting minio server at %s", env.minio)
|
2016-04-17 15:49:36 +00:00
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
dir, err := ioutil.TempDir("", "minio-root")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("running minio server failed: %v", err)
|
|
|
|
}
|
2016-04-17 15:49:36 +00:00
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
env.minioSrv, err = StartBackgroundCommand(minioServerEnv, env.minio,
|
|
|
|
"server",
|
|
|
|
"--address", "127.0.0.1:9000",
|
|
|
|
dir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error running minio server: %v", err)
|
|
|
|
}
|
2016-04-17 15:49:36 +00:00
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
// go func() {
|
|
|
|
// time.Sleep(300 * time.Millisecond)
|
|
|
|
// env.minioSrv.Cmd.Process.Kill()
|
|
|
|
// }()
|
2016-04-17 15:49:36 +00:00
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
for k, v := range minioEnv {
|
|
|
|
env.env[k] = v
|
2016-04-17 15:49:36 +00:00
|
|
|
}
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
env.minioTempdir = dir
|
|
|
|
return nil
|
2016-04-17 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Prepare installs dependencies and starts services in order to run the tests.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *TravisEnvironment) Prepare() error {
|
2016-04-17 15:49:36 +00:00
|
|
|
env.env = make(map[string]string)
|
|
|
|
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("preparing environment for Travis CI\n")
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2016-04-17 16:39:19 +00:00
|
|
|
for _, pkg := range []string{
|
|
|
|
"golang.org/x/tools/cmd/cover",
|
|
|
|
"github.com/mattn/goveralls",
|
|
|
|
"github.com/pierrre/gotestcover",
|
|
|
|
} {
|
|
|
|
err := run("go", "get", pkg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := env.getMinio(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := env.runMinio(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// install the libraries necessary for fuse
|
2016-08-19 17:04:02 +00:00
|
|
|
for _, cmd := range [][]string{
|
|
|
|
{"brew", "update"},
|
|
|
|
{"brew", "tap", "caskroom/cask"},
|
|
|
|
{"brew", "cask", "install", "osxfuse"},
|
|
|
|
} {
|
|
|
|
if err := run(cmd[0], cmd[1:]...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-17 16:39:19 +00:00
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 19:02:30 +00:00
|
|
|
if *runCrossCompile && !(runtime.Version() < "go1.7") {
|
2016-01-17 17:33:03 +00:00
|
|
|
// only test cross compilation on linux with Travis
|
2016-04-17 16:39:19 +00:00
|
|
|
if err := run("go", "get", "github.com/mitchellh/gox"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-17 17:33:03 +00:00
|
|
|
if runtime.GOOS == "linux" {
|
2016-05-08 21:25:30 +00:00
|
|
|
env.goxOSArch = []string{
|
|
|
|
"linux/386", "linux/amd64",
|
|
|
|
"windows/386", "windows/amd64",
|
|
|
|
"darwin/386", "darwin/amd64",
|
|
|
|
"freebsd/386", "freebsd/amd64",
|
|
|
|
"opendbsd/386", "opendbsd/amd64",
|
|
|
|
}
|
2016-01-17 17:33:03 +00:00
|
|
|
if !strings.HasPrefix(runtime.Version(), "go1.3") {
|
2016-05-08 21:25:30 +00:00
|
|
|
env.goxOSArch = append(env.goxOSArch,
|
2016-05-08 21:42:13 +00:00
|
|
|
"linux/arm", "freebsd/arm")
|
2016-01-17 17:33:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-08 21:25:30 +00:00
|
|
|
env.goxOSArch = []string{runtime.GOOS + "/" + runtime.GOARCH}
|
2016-01-17 17:33:03 +00:00
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2016-05-08 21:25:30 +00:00
|
|
|
msg("gox: OS/ARCH %v\n", env.goxOSArch)
|
2015-08-20 17:42:40 +00:00
|
|
|
|
2016-08-16 18:51:46 +00:00
|
|
|
if runtime.Version() < "go1.5" {
|
2016-04-17 16:39:19 +00:00
|
|
|
err := run("gox", "-build-toolchain",
|
2016-05-08 21:25:30 +00:00
|
|
|
"-osarch", strings.Join(env.goxOSArch, " "))
|
2016-04-17 16:39:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-17 17:33:03 +00:00
|
|
|
}
|
2015-08-20 17:42:40 +00:00
|
|
|
}
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
return nil
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Teardown stops backend services and cleans the environment again.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *TravisEnvironment) Teardown() error {
|
2016-04-17 15:49:36 +00:00
|
|
|
msg("run travis teardown\n")
|
|
|
|
if env.minioSrv != nil {
|
|
|
|
msg("stopping minio server\n")
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
if env.minioSrv.Cmd.ProcessState == nil {
|
|
|
|
err := env.minioSrv.Cmd.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error killing minio server process: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result := <-env.minioSrv.Result
|
|
|
|
if result.Error != nil {
|
|
|
|
msg("minio server returned error: %v\n", result.Error)
|
|
|
|
msg("stdout: %s\n", result.Stdout)
|
|
|
|
msg("stderr: %s\n", result.Stderr)
|
|
|
|
}
|
2016-04-17 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := os.RemoveAll(env.minioTempdir)
|
|
|
|
if err != nil {
|
|
|
|
msg("error removing minio tempdir %v: %v\n", env.minioTempdir, err)
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
return nil
|
2016-04-17 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 17:22:57 +00:00
|
|
|
func goVersionAtLeast151() bool {
|
|
|
|
v := runtime.Version()
|
|
|
|
|
|
|
|
if match, _ := regexp.MatchString(`^go1\.[0-4]`, v); match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == "go1.5" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Background is a program running in the background.
|
2016-04-17 15:49:36 +00:00
|
|
|
type Background struct {
|
|
|
|
Cmd *exec.Cmd
|
|
|
|
Result chan Result
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Result is the result of a program that ran in the background.
|
2016-04-17 15:49:36 +00:00
|
|
|
type Result struct {
|
|
|
|
Stdout, Stderr string
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// StartBackgroundCommand runs a program in the background.
|
2016-04-17 15:49:36 +00:00
|
|
|
func StartBackgroundCommand(env map[string]string, cmd string, args ...string) (*Background, error) {
|
|
|
|
msg("running background command %v %v\n", cmd, args)
|
|
|
|
b := Background{
|
|
|
|
Result: make(chan Result, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
|
|
stderr := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
c := exec.Command(cmd, args...)
|
|
|
|
c.Stdout = stdout
|
|
|
|
c.Stderr = stderr
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
if *debug {
|
|
|
|
c.Stdout = io.MultiWriter(c.Stdout, os.Stdout)
|
|
|
|
c.Stderr = io.MultiWriter(c.Stderr, os.Stderr)
|
|
|
|
}
|
2016-04-17 15:49:36 +00:00
|
|
|
c.Env = updateEnv(os.Environ(), env)
|
|
|
|
|
|
|
|
b.Cmd = c
|
|
|
|
|
|
|
|
err := c.Start()
|
|
|
|
if err != nil {
|
|
|
|
msg("error starting background job %v: %v\n", cmd, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := b.Cmd.Wait()
|
|
|
|
msg("background job %v returned: %v\n", cmd, err)
|
2016-04-17 16:22:26 +00:00
|
|
|
msg("stdout: %s\n", stdout.Bytes())
|
|
|
|
msg("stderr: %s\n", stderr.Bytes())
|
2016-04-17 15:49:36 +00:00
|
|
|
b.Result <- Result{
|
|
|
|
Stdout: string(stdout.Bytes()),
|
|
|
|
Stderr: string(stderr.Bytes()),
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &b, nil
|
2015-12-20 19:42:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// RunTests starts the tests for Travis.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *TravisEnvironment) RunTests() error {
|
2015-08-19 18:12:41 +00:00
|
|
|
// run fuse tests on darwin
|
|
|
|
if runtime.GOOS != "darwin" {
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("skip fuse integration tests on %v\n", runtime.GOOS)
|
2015-08-19 18:12:41 +00:00
|
|
|
os.Setenv("RESTIC_TEST_FUSE", "0")
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:39:51 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("Getwd() returned error: %v", err)
|
2016-02-14 16:39:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 15:49:36 +00:00
|
|
|
env.env["GOPATH"] = cwd + ":" + filepath.Join(cwd, "vendor")
|
2016-02-14 16:39:51 +00:00
|
|
|
|
2016-08-16 19:02:30 +00:00
|
|
|
if *runCrossCompile && !(runtime.Version() < "go1.7") {
|
2016-01-17 17:33:03 +00:00
|
|
|
// compile for all target architectures with tags
|
|
|
|
for _, tags := range []string{"release", "debug"} {
|
2016-08-21 12:21:19 +00:00
|
|
|
err := runWithEnv(env.env, "gox", "-verbose",
|
2016-05-08 21:25:30 +00:00
|
|
|
"-osarch", strings.Join(env.goxOSArch, " "),
|
2016-01-17 17:33:03 +00:00
|
|
|
"-tags", tags,
|
|
|
|
"-output", "/tmp/{{.Dir}}_{{.OS}}_{{.Arch}}",
|
2016-02-22 20:03:07 +00:00
|
|
|
"cmds/restic")
|
2016-08-21 12:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-17 17:33:03 +00:00
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run the build script
|
2016-04-17 16:39:19 +00:00
|
|
|
if err := run("go", "run", "build.go"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2015-12-19 16:21:45 +00:00
|
|
|
// run the tests and gather coverage information
|
2016-04-17 16:39:19 +00:00
|
|
|
err = runWithEnv(env.env, "gotestcover", "-coverprofile", "all.cov", "cmds/...", "restic/...")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-19 16:21:45 +00:00
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
return runGofmt()
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// AppveyorEnvironment is the environment on Windows.
|
2015-08-19 18:45:54 +00:00
|
|
|
type AppveyorEnvironment struct{}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Prepare installs dependencies and starts services in order to run the tests.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *AppveyorEnvironment) Prepare() error {
|
2015-08-19 18:45:54 +00:00
|
|
|
msg("preparing environment for Appveyor CI\n")
|
2016-04-17 16:22:26 +00:00
|
|
|
return nil
|
2015-08-19 18:45:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// RunTests start the tests.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *AppveyorEnvironment) RunTests() error {
|
2016-04-17 16:39:19 +00:00
|
|
|
return run("go", "run", "build.go", "-v", "-T")
|
2015-08-19 18:45:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:33:45 +00:00
|
|
|
// Teardown is a noop.
|
2016-04-17 16:22:26 +00:00
|
|
|
func (env *AppveyorEnvironment) Teardown() error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-17 15:49:36 +00:00
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
// findGoFiles returns a list of go source code file names below dir.
|
|
|
|
func findGoFiles(dir string) (list []string, err error) {
|
|
|
|
err = filepath.Walk(dir, func(name string, fi os.FileInfo, err error) error {
|
2016-02-14 17:44:08 +00:00
|
|
|
if filepath.Base(name) == "vendor" {
|
2015-08-19 18:12:41 +00:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(name) == ".go" {
|
|
|
|
relpath, err := filepath.Rel(dir, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, relpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:29:06 +00:00
|
|
|
func msg(format string, args ...interface{}) {
|
|
|
|
fmt.Printf("CI: "+format, args...)
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:21:45 +00:00
|
|
|
func updateEnv(env []string, override map[string]string) []string {
|
|
|
|
var newEnv []string
|
|
|
|
for _, s := range env {
|
|
|
|
d := strings.SplitN(s, "=", 2)
|
|
|
|
key := d[0]
|
|
|
|
|
|
|
|
if _, ok := override[key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newEnv = append(newEnv, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range override {
|
|
|
|
newEnv = append(newEnv, k+"="+v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newEnv
|
|
|
|
}
|
|
|
|
|
2016-04-17 16:22:26 +00:00
|
|
|
func runGofmt() error {
|
2015-08-19 18:12:41 +00:00
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("Getwd(): %v\n", err)
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
files, err := findGoFiles(dir)
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error finding Go files: %v\n", err)
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("runGofmt() with %d files\n", len(files))
|
2015-08-19 18:12:41 +00:00
|
|
|
args := append([]string{"-l"}, files...)
|
|
|
|
cmd := exec.Command("gofmt", args...)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
buf, err := cmd.Output()
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error running gofmt: %v\noutput: %s\n", err, buf)
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) > 0 {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("not formatted with `gofmt`:\n%s\n", buf)
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
2016-04-17 16:22:26 +00:00
|
|
|
|
|
|
|
return nil
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 16:39:19 +00:00
|
|
|
func run(command string, args ...string) error {
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("run %v %v\n", command, strings.Join(args, " "))
|
2016-04-17 16:39:19 +00:00
|
|
|
return runWithEnv(nil, command, args...)
|
2015-12-19 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runWithEnv calls a command with the current environment, except the entries
|
|
|
|
// of the env map are set additionally.
|
2016-04-17 16:22:26 +00:00
|
|
|
func runWithEnv(env map[string]string, command string, args ...string) error {
|
2015-12-19 16:21:45 +00:00
|
|
|
msg("runWithEnv %v %v\n", command, strings.Join(args, " "))
|
2015-08-19 18:12:41 +00:00
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2015-12-19 16:21:45 +00:00
|
|
|
if env != nil {
|
|
|
|
cmd.Env = updateEnv(os.Environ(), env)
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
2016-04-17 16:22:26 +00:00
|
|
|
return fmt.Errorf("error running %v %v: %v",
|
2015-08-19 18:12:41 +00:00
|
|
|
command, strings.Join(args, " "), err)
|
|
|
|
}
|
2016-04-17 16:22:26 +00:00
|
|
|
return nil
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isTravis() bool {
|
|
|
|
return os.Getenv("TRAVIS_BUILD_DIR") != ""
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:45:54 +00:00
|
|
|
func isAppveyor() bool {
|
|
|
|
return runtime.GOOS == "windows"
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func main() {
|
|
|
|
var env CIEnvironment
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isTravis():
|
|
|
|
env = &TravisEnvironment{}
|
2015-08-19 18:45:54 +00:00
|
|
|
case isAppveyor():
|
|
|
|
env = &AppveyorEnvironment{}
|
2015-08-19 18:12:41 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "unknown CI environment")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-04-17 16:47:00 +00:00
|
|
|
foundError := false
|
2016-04-17 16:22:26 +00:00
|
|
|
for _, f := range []func() error{env.Prepare, env.RunTests, env.Teardown} {
|
|
|
|
err := f()
|
|
|
|
if err != nil {
|
2016-04-17 16:47:00 +00:00
|
|
|
foundError = true
|
2016-04-17 16:22:26 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
2016-04-17 16:47:00 +00:00
|
|
|
|
|
|
|
if foundError {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|