Skip to content

Commit

Permalink
Add support for integration tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Feb 24, 2023
1 parent 611c4e3 commit ba81819
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.20

- name: build
run: make build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.20
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/fakedata
/fakedata-with-cover
.vscode
/dist
*.perf
*.db
.coverdata
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
SOURCE_FILES?=$$(go list ./...)
SOURCE_FILES?=$$(go list ./pkg/...)
TEST_PATTERN?=.
TEST_OPTIONS?=

test: ## Run tests
unit: ## Run tests
@go test $(TEST_OPTIONS) -cover $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s

integration: build-with-cover ## Run integration tests
@go test $(TEST_OPTIONS) $$(go list ./integration/...) -timeout=30s
@go tool covdata percent -i=.coverdata

test: unit integration

bench: ## Run benchmarks
@go test $(TEST_OPTIONS) -cover $(SOURCE_FILES) -bench $(TEST_PATTERN) -timeout=30s

Expand All @@ -14,13 +20,18 @@ lint: ## Run linters
build: ## Build a dev version of fakedata
@go build

import: ## Import or update data from dariusk/corpora
@go run cmd/import/main.go

build-debug-image:
@GOOS=linux GOARCH=amd64 go build
docker build -t fakedata .

build-with-cover: ## Build a cover version of fakedata
@rm .coverdata -fr
@mkdir .coverdata
@go build -cover -o ./fakedata-with-cover

import: ## Import or update data from dariusk/corpora
@go run cmd/import/main.go

# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down
10 changes: 3 additions & 7 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"os/exec"
"regexp"
"testing"

Expand Down Expand Up @@ -99,8 +98,7 @@ func TestCLI(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(binaryPath, tt.args...)
output, err := cmd.CombinedOutput()
output, err := runBinary(tt.args...)
if (err != nil) != tt.wantErr {
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err)
}
Expand Down Expand Up @@ -131,8 +129,7 @@ func TestGeneratorDescription(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(binaryPath, tt.args...)
output, err := cmd.CombinedOutput()
output, err := runBinary(tt.args...)
if err != nil {
t.Fatalf("test run returned an error: %v\n%s", err, output)
}
Expand Down Expand Up @@ -165,8 +162,7 @@ func TestFileGenerator(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command(binaryPath, tt.args...)
output, err := cmd.CombinedOutput()
output, err := runBinary(tt.args...)
if (err != nil) != tt.wantErr {
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err)
}
Expand Down
15 changes: 8 additions & 7 deletions integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"testing"
)

// In these tests, there's a lot going on. Have a look at this article for a
// longer explanation:
// In these tests, there's a lot going on. See this blog post for more details:
// https://lucapette.me/writing/writing-integration-tests-for-a-go-cli-application

var update = flag.Bool("update", false, "update golden files")

const binaryName = "fakedata"
const binaryName = "fakedata-with-cover"

var binaryPath string

Expand All @@ -34,9 +33,11 @@ func TestMain(m *testing.M) {

binaryPath = abs

if err := exec.Command("make").Run(); err != nil {
fmt.Printf("could not make binary for %s: %v", binaryName, err)
os.Exit(1)
}
os.Exit(m.Run())
}

func runBinary(args ...string) ([]byte, error) {
cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")
return cmd.CombinedOutput()
}
4 changes: 2 additions & 2 deletions integration/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ var templateTests = []struct {
func TestTemplatesWithCLIArgs(t *testing.T) {
for _, tt := range templateTests {
t.Run(tt.tmpl, func(t *testing.T) {
cmd := exec.Command(binaryPath, "--template", fmt.Sprintf("testutil/fixtures/%s", tt.tmpl))
output, err := cmd.CombinedOutput()
output, err := runBinary("--template", fmt.Sprintf("testutil/fixtures/%s", tt.tmpl))
if (err != nil) != tt.wantErr {
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err)
}
Expand All @@ -51,6 +50,7 @@ func TestTemplatesWithPipe(t *testing.T) {
fixture := testutil.NewFixture(t, tt.tmpl)
cmd := exec.Command(binaryPath)
cmd.Stdin = fixture.AsFile()
cmd.Env = append(cmd.Env, "GOCOVERDIR=.coverdata")
output, err := cmd.CombinedOutput()
if (err != nil) != tt.wantErr {
t.Fatalf("%s\nexpected (err != nil) to be %v, but got %v. err: %v", output, tt.wantErr, err != nil, err)
Expand Down

0 comments on commit ba81819

Please sign in to comment.