1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
//go:build mage
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
var Default = Build
// Build compiles the yoga binary.
func Build() error {
return run("go", "build", "./cmd/yoga")
}
// Test runs the unit test suite.
func Test() error {
return run("go", "test", "./...")
}
// Run runs the unit test suite.
func Run() error {
return run("go", "run", "./cmd/yoga")
}
// Install installs the yoga binary into GOPATH/bin or GOBIN.
func Install() error {
return run("go", "install", "./cmd/yoga")
}
// Coverage runs the unit tests with coverage and enforces the minimum target.
func Coverage() error {
profile := filepath.Join(os.TempDir(), "yoga-coverage.out")
if err := run("go", "test", "-coverprofile="+profile, "./..."); err != nil {
return err
}
defer os.Remove(profile)
out, err := exec.Command("go", "tool", "cover", "-func="+profile).CombinedOutput()
if err != nil {
fmt.Print(string(out))
return err
}
fmt.Print(string(out))
total, err := parseTotalCoverage(string(out))
if err != nil {
return err
}
if total < 85.0 {
return fmt.Errorf("coverage %.1f%% below required 85%%", total)
}
return nil
}
func parseTotalCoverage(report string) (float64, error) {
lines := strings.Split(strings.TrimSpace(report), "\n")
if len(lines) == 0 {
return 0, errors.New("empty coverage report")
}
last := lines[len(lines)-1]
fields := strings.Fields(last)
if len(fields) < 3 {
return 0, fmt.Errorf("unexpected coverage line: %s", last)
}
value := strings.TrimSuffix(fields[len(fields)-1], "%")
percent, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
}
return percent, nil
}
func run(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
|