Skip to content

Commit 1925dbc

Browse files
committed
build and coverage settings
1 parent ef64747 commit 1925dbc

File tree

5 files changed

+43
-65
lines changed

5 files changed

+43
-65
lines changed

.github/workflows/go.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
# This workflow will build a golang project
2-
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
2+
# For more information see:
3+
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
4+
# https://github.com/marketplace/actions/coveralls-github-action
35

46
name: Go
57

68
on:
79
push:
8-
branches: [ "main" ]
10+
branches: [ main ]
911
pull_request:
10-
branches: [ "main" ]
12+
branches: [ main ]
1113

1214
jobs:
1315
build:
1416
runs-on: ubuntu-latest
1517
steps:
16-
- uses: actions/checkout@v4
18+
- uses: actions/checkout@v4
1719

18-
- name: Set up Go
19-
uses: actions/setup-go@v4
20-
with:
21-
go-version: '1.24'
20+
- name: Set up Go 1.24.x
21+
uses: actions/setup-go@v4
22+
with:
23+
go-version: '1.24'
2224

23-
- name: Test
24-
run: go test ./...
25+
- name: Set up Mage
26+
run: go install tool
2527

26-
- name: Vet
27-
run: go vet ./...
28+
- name: Test
29+
run: mage build coverage
30+
31+
- name: Coveralls
32+
uses: coverallsapp/github-action@v2
33+
with:
34+
file: coverage.out
35+
format: golang
36+
debug: false

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
_obj
88
_test
99

10-
test.out
10+
*.out
1111

1212
*.cgo1.go
1313
*.cgo2.c

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![GoDoc](https://img.shields.io/badge/api-Godoc-blue.svg)](http://pkg.go.dev/github.com/rickb777/acceptable)
44
[![Go Report Card](https://goreportcard.com/badge/github.com/rickb777/acceptable)](https://goreportcard.com/report/github.com/rickb777/acceptable)
55
[![Build](https://github.com/rickb777/acceptable/actions/workflows/go.yml/badge.svg)](https://github.com/rickb777/acceptable/actions)
6+
[![Coverage](https://coveralls.io/repos/github/rickb777/acceptable/badge.svg?branch=main)](https://coveralls.io/github/rickb777/acceptable?branch=main)
67
[![Issues](https://img.shields.io/github/issues/rickb777/acceptable.svg)](https://github.com/rickb777/acceptable/issues)
78

89
This is a library that handles `Accept` headers, which form the basis of content negotiation in HTTP server applications written in Go. It provides an implementation of the proactive server-driven content negotiation algorithm specified in [RFC-7231 section 5.3](https://tools.ietf.org/html/rfc7231#section-5.3).

build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash -ex
22
cd "$(dirname "$0")"
33
go install tool
4-
mage
4+
mage build coverage
5+
cat report.out

magefiles/build.go

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ package main
77

88
import (
99
"github.com/magefile/mage/sh"
10-
"io/fs"
1110
"log"
1211
"os"
13-
"path/filepath"
14-
"slices"
15-
"strings"
1612
)
1713

18-
var Default = Coverage
14+
var Default = Build
1915

2016
func Build() error {
2117
if err := sh.RunV("go", "test", "./..."); err != nil {
@@ -30,63 +26,34 @@ func Build() error {
3026
return nil
3127
}
3228

33-
// runs all the unit tests and reports the test coverage
3429
func Coverage() error {
35-
if err := Build(); err != nil {
30+
if err := sh.RunV("go", "test", "-cover", "./...", "-coverprofile", "coverage.out", "-coverpkg", "./..."); err != nil {
3631
return err
3732
}
38-
for _, dir := range listOfFoldersContainingTests() {
39-
if err := sh.RunV("go", "test", "-covermode=count", "-coverprofile="+dir+"test.out", packageName(dir)); err != nil {
40-
return err
41-
}
42-
if err := sh.RunV("go", "tool", "cover", "-func="+dir+"test.out"); err != nil {
43-
return err
44-
}
33+
if err := sh.RunV("go", "tool", "cover", "-func", "coverage.out", "-o", "report.out"); err != nil {
34+
return err
4535
}
4636
return nil
4737
}
4838

49-
// tests the module on both amd64 and i386 architectures
39+
// tests the module on both amd64 and i386 architectures for Linux and Windows
5040
func CrossCompile() error {
41+
win := "build"
42+
linux := "test"
43+
if os.Getenv("GOOS") == "windows" {
44+
win = "test"
45+
linux = "build"
46+
}
47+
log.Printf("Testing on Windows\n")
48+
if err := sh.RunWithV(map[string]string{"GOOS": "windows"}, "go", win, "./..."); err != nil {
49+
return err
50+
}
5151
for _, arch := range []string{"amd64", "386"} {
52-
log.Printf("Testing on %s\n", arch)
53-
env := map[string]string{"GOARCH": arch}
54-
if _, err := sh.Exec(env, os.Stdout, os.Stderr, "go", "test", "./..."); err != nil {
52+
log.Printf("Testing on Linux/%s\n", arch)
53+
env := map[string]string{"GOOS": "linux", "GOARCH": arch}
54+
if _, err := sh.Exec(env, os.Stdout, os.Stderr, "go", linux, "./..."); err != nil {
5555
return err
5656
}
57-
log.Printf("%s is good.\n\n", arch)
5857
}
5958
return nil
6059
}
61-
62-
func listOfFoldersContainingTests() []string {
63-
root, _ := os.Getwd()
64-
fileSystem := os.DirFS(root)
65-
set := map[string]struct{}{}
66-
67-
fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
68-
if err != nil {
69-
log.Fatal(err)
70-
}
71-
if strings.HasSuffix(path, "_test.go") {
72-
dir, _ := filepath.Split(path)
73-
set[dir] = struct{}{}
74-
}
75-
return nil
76-
})
77-
78-
list := make([]string, 0, len(set))
79-
for dir := range set {
80-
list = append(list, dir)
81-
}
82-
slices.Sort(list)
83-
return list
84-
}
85-
86-
func packageName(dir string) string {
87-
dir, _ = strings.CutSuffix(dir, "/")
88-
if dir == "" {
89-
return dir
90-
}
91-
return "./" + dir
92-
}

0 commit comments

Comments
 (0)