Skip to content

Commit

Permalink
refactor ctx
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
  • Loading branch information
kartikaysaxena committed Feb 20, 2025
1 parent c28c7b5 commit 8c68cf0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
5 changes: 4 additions & 1 deletion magefiles/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module magefiles

go 1.23.1
go 1.23.4

toolchain go1.24.0

require (
github.com/agnivade/wasmbrowsertest v0.8.0
Expand All @@ -25,6 +27,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/authzed/ctxkey v0.0.0-20250127172433-d71cd97e3833 // indirect
github.com/bufbuild/protocompile v0.14.0 // indirect
github.com/bufbuild/protoplugin v0.0.0-20240323223605-e2735f6c31ee // indirect
github.com/bufbuild/protovalidate-go v0.6.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions magefiles/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/agnivade/wasmbrowsertest v0.8.0 h1:ZSp3cz/J0AiaSnmDMJwZUu3zEBiE+0rkYu
github.com/agnivade/wasmbrowsertest v0.8.0/go.mod h1:CEWQKD6CPRjw8nzQ0X1vQt696t8WQ0g1S7x35FQJFqE=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/authzed/ctxkey v0.0.0-20250127172433-d71cd97e3833 h1:rby2719UdmcOltB30ALyPY4Lozz9nkYSpjqnBiHXBQI=
github.com/authzed/ctxkey v0.0.0-20250127172433-d71cd97e3833/go.mod h1:wnimjr5RPPouIhZQ3ztDBLMUKKuUroj3U9Jy0PxeaEE=
github.com/bufbuild/buf v1.35.1 h1:aiCi/YFOg7eXKZeveWb2ZhnmLFwUMM/FnDCM0roFp+M=
github.com/bufbuild/buf v1.35.1/go.mod h1:SM7b5QW3FkQPNkkqIa/9UWzLOoe51la+GGZpEgH9b68=
github.com/bufbuild/protocompile v0.14.0 h1:z3DW4IvXE5G/uTOnSQn+qwQQxvhckkTWLS/0No/o7KU=
Expand Down
20 changes: 10 additions & 10 deletions magefiles/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"strings"

"github.com/authzed/ctxkey"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
Expand All @@ -20,20 +22,18 @@ var emptyEnv map[string]string
func (t Test) All() error {
ds := Testds{}
c := Testcons{}
ctx := context.Background()
cover := false
for _, arg := range os.Args {
if arg == "-cover=true" {
cover = true
break
}
}
ctx = context.WithValue(ctx, "cover", cover)
cover := parseCommandLineFlags()
ctxMyKey := ctxkey.New[bool]()
ctx := ctxMyKey.WithValue(context.Background(), cover)
mg.CtxDeps(ctx, t.Unit, t.Integration, t.Steelthread, t.Image, t.Analyzers,
ds.Crdb, ds.Postgres, ds.Spanner, ds.Mysql,
c.Crdb, c.Spanner, c.Postgres, c.Mysql)

return nil
if !cover {
return nil
}

return combineCoverage()
}

func (t Test) Combine() error {
Expand Down
44 changes: 27 additions & 17 deletions magefiles/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/authzed/ctxkey"
"github.com/google/uuid"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand All @@ -24,39 +25,38 @@ func goTest(ctx context.Context, path string, args ...string) error {

// run go test in a directory
func goDirTest(ctx context.Context, dir string, path string, args ...string) error {
testArgs := append([]string{
"test",
"-failfast",
"-count=1",
}, args...)
if cover, _ := ctx.Value("cover").(bool); cover {
if err := os.MkdirAll("coverage", 0o755); err != nil {
return fmt.Errorf("failed to create coverage directory: %w", err)
}
testArgs = append(testArgs, []string{
"-covermode=atomic",
fmt.Sprintf("-coverprofile=coverage-%s.txt", uuid.New().String()),
}...)
testArgs, err := testWithArgs(ctx, args...)
if err != nil {
return err
}
return RunSh(goCmdForTests(), WithV(), WithDir(dir), WithArgs(testArgs...))(path)
}

func goDirTestWithEnv(ctx context.Context, dir string, path string, env map[string]string, args ...string) error {
testArgs, err := testWithArgs(ctx, args...)
if err != nil {
return err
}
return RunSh(goCmdForTests(), WithV(), WithDir(dir), WithEnv(env), WithArgs(testArgs...))(path)
}

func testWithArgs(ctx context.Context, args ...string) ([]string, error) {
testArgs := append([]string{
"test",
"-failfast",
"-count=1",
}, args...)
if cover, _ := ctx.Value("cover").(bool); cover {
if err := os.MkdirAll("coverage", 0o755); err != nil {
return fmt.Errorf("failed to create coverage directory: %w", err)
ctxMyKey := ctxkey.New[bool]()
if cover := ctxMyKey.MustValue(ctx); cover {
if err := os.MkdirAll("coverage", 0o700); err != nil {
return nil, fmt.Errorf("failed to create coverage directory: %w", err)
}
testArgs = append(testArgs, []string{
"-covermode=atomic",
fmt.Sprintf("-coverprofile=coverage-%s.txt", uuid.New().String()),
}...)
}
return RunSh(goCmdForTests(), WithV(), WithDir(dir), WithEnv(env), WithArgs(testArgs...))(path)
return testArgs, nil
}

// check if docker is installed and running
Expand Down Expand Up @@ -265,3 +265,13 @@ func combineCoverage() error {

return nil
}

func parseCommandLineFlags() (cover bool) {
for _, arg := range os.Args {
if arg == "-cover=true" {
cover = true
break
}
}
return cover
}

0 comments on commit 8c68cf0

Please sign in to comment.