Go/Travis CI: Difference between revisions
From charlesreid1
(→Flags) |
|||
| Line 14: | Line 14: | ||
Before starting, you'll need to set up a Travis account and integrate it with Github. See https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/ | Before starting, you'll need to set up a Travis account and integrate it with Github. See https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/ | ||
==A simple Go test== | |||
Let's start with a simple Go test that we can use to illustrate how to set up Travis CI. | |||
We begin with <code>main.go</code>, which defines a <code>Calculate()</code> method we can write a test for. | |||
<pre> | |||
// main.go | |||
package main | |||
import ( | |||
"fmt" | |||
) | |||
func calculate(x int) int { | |||
return x + 2 | |||
} | |||
func main() { | |||
fmt.Println("This is a test") | |||
} | |||
</pre> | |||
Next we write a unit test for our Go function. This is a simple barebones test, not following best practices. | |||
<pre> | |||
// main_test.go | |||
package main | |||
import "testing" | |||
func test_calculation(t *testing.T) { | |||
if calculate(2) != 4 { | |||
t.Fatal("Test of calculate method failed") | |||
} | |||
} | |||
</pre> | |||
==Writing a code-linting shell script== | |||
To have Travis CI automatically format our Go code, we can write a simple shell script to run the <code>gofmt</code> utility: | |||
<pre> | |||
#!/bin/bash | |||
# | |||
# .travis.gofmt.sh | |||
if [ -n "$(gofmt -l .)" ]; then | |||
echo "Go code is not formatted:" | |||
gofmt -d . | |||
exit 1 | |||
fi | |||
</pre> | |||
==dot travis yml== | ==dot travis yml== | ||
===barebones skeleton .travis.yml for Go=== | |||
<pre> | <pre> | ||
| Line 30: | Line 90: | ||
- "1.11" | - "1.11" | ||
- tip | - tip | ||
</pre> | |||
===add go test and shell script to .travis.yml=== | |||
<pre> | |||
language: go | |||
sudo: false | |||
go: | |||
- "1.8" | |||
- "1.9" | |||
- "1.10" | |||
- "1.11" | |||
- tip | |||
script: | |||
- ./.travis.gofmt.sh | |||
- go test -v -race $(go list ./... | grep -v vendor) | |||
</pre> | </pre> | ||
Revision as of 02:09, 13 December 2018
Basics
What this page covers
This page covers the setup of a simple Go pipeline on Travis CI:
- formats new code to meet standards
- test new additions to the code base (possibly using multiple versions of Go)
- prevent breaking changes from being merged to master
- ask for code reviews
before you begin
Before starting, you'll need to set up a Travis account and integrate it with Github. See https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/
A simple Go test
Let's start with a simple Go test that we can use to illustrate how to set up Travis CI.
We begin with main.go, which defines a Calculate() method we can write a test for.
// main.go
package main
import (
"fmt"
)
func calculate(x int) int {
return x + 2
}
func main() {
fmt.Println("This is a test")
}
Next we write a unit test for our Go function. This is a simple barebones test, not following best practices.
// main_test.go
package main
import "testing"
func test_calculation(t *testing.T) {
if calculate(2) != 4 {
t.Fatal("Test of calculate method failed")
}
}
Writing a code-linting shell script
To have Travis CI automatically format our Go code, we can write a simple shell script to run the gofmt utility:
#!/bin/bash # # .travis.gofmt.sh if [ -n "$(gofmt -l .)" ]; then echo "Go code is not formatted:" gofmt -d . exit 1 fi
dot travis yml
barebones skeleton .travis.yml for Go
--- # .travis.yml language: go sudo: false go: - "1.8" - "1.9" - "1.10" - "1.11" - tip
add go test and shell script to .travis.yml
language: go sudo: false go: - "1.8" - "1.9" - "1.10" - "1.11" - tip script: - ./.travis.gofmt.sh - go test -v -race $(go list ./... | grep -v vendor)
Resources
Travis CI integration pipeline: https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/
Flags
| Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|