@@ -7,15 +7,11 @@ package main
77
88import (
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
2016func 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
3429func 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
5040func 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