From 1fe68df68ed9589e3e9d28ebd7184787cb131f87 Mon Sep 17 00:00:00 2001 From: Bruno FELIX Date: Sat, 15 Sep 2018 00:27:38 +0200 Subject: [PATCH] initial commit --- .drone.yml | 14 +++++++++ DOCS.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 +- main.go | 12 ++++++++ main_test.go | 16 ++++++++++ 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 .drone.yml create mode 100644 DOCS.md create mode 100644 main.go create mode 100644 main_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..2de0e7e --- /dev/null +++ b/.drone.yml @@ -0,0 +1,14 @@ +workspace: + base: /go + path: src/github.com/drone-demos/drone-with-go + +pipeline: + test: + image: golang:latest + commands: + - go vet + - go test -v -cover + build: + image: golang:latest + commands: + - go build diff --git a/DOCS.md b/DOCS.md new file mode 100644 index 0000000..7858f68 --- /dev/null +++ b/DOCS.md @@ -0,0 +1,85 @@ +# drone-with-go [![Build Status](http://beta.drone.io/api/badges/drone-demos/drone-with-go/status.svg)](http://beta.drone.io/drone-demos/drone-with-go) [![Build Status](https://aircover.co/badges/drone-demos/drone-with-go/coverage.svg)](https://aircover.co/drone-demos/drone-with-go) + +An example of how to test Go code with Drone. + +# Basic Testing +To run basic CI tests use the following in your `.drone.yml` file. + +```yaml +build: + image: golang:1.5.3 + commands: + - go test ./... +``` + +In this config `image: golang:1.5.3` references the official Golang Docker image hosted at https://hub.docker.com/r/_/golang/ and Go tests are execute with the `go test ./...` command. + +# Advanced Testing + +## Environment Variables +Use environment variables to configure Go testing. +Set environment variables with the `build` section's `environment`. + +```yaml +build: + image: golang:1.5.3 + environment: + - GO15VENDOREXPERIMENT=1 + - GOOS=linux + - GOARCH=amd64 + - CGO_ENABLED=0 + commands: + - go test ./... +``` + +## Coverage +Drone tests work best with the [Coverage plugin](http://readme.drone.io/plugins/coverage/) and the [aircover.co](https://aircover.co/docs/overview/) service. +We only want to send a coverage report when all tests pass, so the Coverage plugin uses `publish`. +Also, we should specify a particular branch so that coverage reports are consistent. + +```yaml +build: + image: golang:1.5.3 + environment: + - GO15VENDOREXPERIMENT=1 + - GOOS=linux + - GOARCH=amd64 + - CGO_ENABLED=0 + commands: + - go test -cover -coverprofile coverage.out + +publish: + coverage: + when: + branch: master +``` + +## Plugins +Notification plugins use `notify` for integrations like HipChat. +`publish` is used for publishing GitHub Releases, Coverage reports, and more. +`deploy` is used for deployments to systems like AWS and Rancher. +You can find a list of plugins at [readme.drone.io/plugins](http://readme.drone.io/plugins/). + +```yaml +build: + image: golang:1.5.3 + environment: + - GO15VENDOREXPERIMENT=1 + - GOOS=linux + - GOARCH=amd64 + - CGO_ENABLED=0 + commands: + - go test -cover -coverprofile coverage.out + +publish: + coverage: + when: + branch: master + +notify: + hipchat: + from: Your_Project + notify: true + room_id_or_name: Your_Room + auth_token: $$HIPCHAT_DRONE_TOKEN +``` diff --git a/README.md b/README.md index 0681ac5..5e1df5b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# drone-with-go-test +# drone-with-go [![Build Status](http://beta.drone.io/api/badges/drone-demos/drone-with-go/status.svg)](http://beta.drone.io/drone-demos/drone-with-go) +An example of how to test Go code with Drone. diff --git a/main.go b/main.go new file mode 100644 index 0000000..4d35264 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + fmt.Println(HelloWorld()) +} + +// HelloWorld is a function that returns a string containing "hello world". +func HelloWorld() string { + return "hello world" +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..887a4df --- /dev/null +++ b/main_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "os" + "testing" +) + +func TestMain(m *testing.M) { + os.Exit(m.Run()) +} + +func TestHelloWorld(t *testing.T) { + if HelloWorld() != "hello world" { + t.Errorf("got %s expected %s", HelloWorld(), "hello world") + } +}