-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
magefile: added combined test coverage support for mage test:all
#2235
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Kartikay <[email protected]>
magefiles/util.go
Outdated
testArgs := append([]string{ | ||
"test", | ||
"-covermode=atomic", | ||
"-coverprofile=coverage.txt", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this overwrite the coverage.txt file when each individual test suite runs? Can you try running mage test:all
and ensure it combines the coverage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Kartikay <[email protected]>
46942c5
to
64f1377
Compare
magefiles/util.go
Outdated
@@ -210,3 +223,45 @@ func run(dir string, env map[string]string, stdout, stderr io.Writer, cmd string | |||
err = c.Run() | |||
return sh.CmdRan(err), sh.ExitStatus(err), err | |||
} | |||
|
|||
func sanitizePath(path string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend hashing the path here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Signed-off-by: Kartikay <[email protected]>
ba7d1da
to
59fd80d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the PR enables coverage by default on all
, whereas before it was opt in. Do we have concerns that would add overhead on the inner loop? Or is the overhead negligible?
4fb47db
to
7498752
Compare
Signed-off-by: Kartikay <[email protected]>
7498752
to
3f62b00
Compare
I used |
magefiles/util.go
Outdated
@@ -210,3 +221,40 @@ func run(dir string, env map[string]string, stdout, stderr io.Writer, cmd string | |||
err = c.Run() | |||
return sh.CmdRan(err), sh.ExitStatus(err), err | |||
} | |||
|
|||
func hashPath(path string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to hash the path? I'd keep it simpler by generating a UUID as suffix
magefiles/test.go
Outdated
func (t Test) AllCover() error { | ||
ds := Testds{} | ||
c := Testcons{} | ||
mg.Deps(t.UnitCover, t.IntegrationCover, t.SteelthreadCover, t.ImageCover, t.AnalyzersCover, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like a lot of duplication that would lead to drift w.r.t to All()
method. Ideally All
and AllCover
invoke `all(coverage bool)
magefiles/util.go
Outdated
return err | ||
} | ||
|
||
for _, file := range files { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why deleting the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was for deleting individual coverage files after merging them. reverting
magefiles/test.go
Outdated
"-tags", "ci,docker,datastoreconsistency", | ||
"-timeout", "10m", | ||
"-run", fmt.Sprintf("TestConsistencyPerDatastore/%s", datastore)) | ||
if coverage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all this if coverage
blocks could be refactored to avoid all the duplication - e.g. a function that takes the args
and the coverage
bool, and returns the args with the appended parameters.
magefiles/test.go
Outdated
|
||
func (Testds) spanner(coverage bool) error { | ||
args := []string{"-tags", "ci,docker", "-timeout", "10m"} | ||
if coverage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of duplication: this coverage
if statement should be handled inside datastoreTest
magefiles/test.go
Outdated
func (Test) image(coverage bool) error { | ||
mg.Deps(Build{}.Testimage) | ||
args := []string{"-tags", "docker,image"} | ||
if coverage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be moved into goDirTest
, otherwise it leads to a lot of duplication
magefiles/test.go
Outdated
@@ -43,27 +53,75 @@ func (Test) unit(coverage bool) error { | |||
fmt.Println("running unit tests") | |||
args := []string{"-tags", "ci,skipintegrationtests", "-race", "-timeout", "10m", "-count=1"} | |||
if coverage { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be moved into goTest
, otherwise it leads to a lot of duplication
magefiles/test.go
Outdated
} | ||
|
||
// SteelthreadCover Runs the steelthread tests and generates a coverage report | ||
func (t Test) SteelthreadCover() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's annoying we end up with proliferation of commands just to have with/without coverage versions of them. Calling mage
leads to a very long list that starts to become not very user friendly.
I know mage supports arguments in commands, but not if it supports commands with arguments and default values, could you investigate?
Signed-off-by: Kartikay <[email protected]>
Utilised uuid instead, also passed the bool through a context to |
magefiles/util.go
Outdated
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()), | ||
}...) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please refactor this logic into a single method that is used in both goDirTestWithEnv
and goDirTest
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done with testWithArgs
magefiles/util.go
Outdated
"-failfast", | ||
"-count=1", | ||
}, args...) | ||
if cover, _ := ctx.Value("cover").(bool); cover { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would you mind replacing this with https://github.com/authzed/ctxkey 🙏🏻
magefiles/util.go
Outdated
"-count=1", | ||
}, args...) | ||
if cover, _ := ctx.Value("cover").(bool); cover { | ||
if err := os.MkdirAll("coverage", 0o755); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to give permissions to group
and public
? I think the owner
having access is enough
magefiles/test.go
Outdated
ctx := context.Background() | ||
cover := false | ||
for _, arg := range os.Args { | ||
if arg == "-cover=true" { | ||
cover = true | ||
break | ||
} | ||
} | ||
ctx = context.WithValue(ctx, "cover", cover) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the context to propagate arguments is a good idea to workaround mage parameterization limitations 👍🏻
Can you refactor this into a function in case we need to add more flags for other commands? I also suggest using https://github.com/authzed/ctxkey for type-safe context values
magefiles/test.go
Outdated
return nil | ||
} | ||
|
||
func (t Test) Combine() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is combining everything something that has to be invoked explicitly? Could we call this from All
if the cover flag was enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: Kartikay <[email protected]>
8c68cf0
to
dcdc4e4
Compare
Signed-off-by: Kartikay <[email protected]>
Signed-off-by: Kartikay <[email protected]>
Signed-off-by: Kartikay <[email protected]>
Signed-off-by: Kartikay <[email protected]>
Signed-off-by: Kartikay <[email protected]>
Signed-off-by: Kartikay <[email protected]>
dcdc4e4
to
8d38fc5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment; once that's through it looks good to me.
magefiles/test.go
Outdated
ctxMyKey := ctxkey.New[bool]() | ||
ctx := ctxMyKey.WithValue(context.Background(), cover) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work to declare ctxMyKey
twice? I'd expect that that would give you two different identities of keys, which wouldn't allow you to pull values out of context. The idea is that you'd give the key a name that means something and declare it once at the top of the file. There's an example in the ctxkey examples dir.
Would you please declare this key once at the top of the module with a meaningful name and then reuse it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was doing that to avoid a global var, seems like it should be the way to go for this.
magefiles/test.go
Outdated
@@ -19,51 +22,56 @@ var emptyEnv map[string]string | |||
func (t Test) All() error { | |||
ds := Testds{} | |||
c := Testcons{} | |||
mg.Deps(t.Unit, t.Integration, t.Steelthread, t.Image, t.Analyzers, | |||
cover := parseCommandLineFlags() | |||
ctxMyKey := ctxkey.New[bool]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be defined as a global var, a key to a specific value.
Also you could add context.Context
to All
signature, mage supports it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
magefiles/util.go
Outdated
"-failfast", | ||
"-count=1", | ||
}, args...) | ||
ctxMyKey := ctxkey.New[bool]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should reference the global var
…a/spicedb into mage-test-coverage
fce741b
to
b995558
Compare
magefiles/test.go
Outdated
ds := Testds{} | ||
c := Testcons{} | ||
cover := parseCommandLineFlags() | ||
ctxMyKey := ctxkey.New[bool]() | ||
ctx := ctxMyKey.WithValue(context.Background(), cover) | ||
ctx = ctxMyKey.WithValue(context.Background(), cover) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the ctx
argument provided by the method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah overlooked that, thanks for pointing out!
magefiles/util.go
Outdated
cover, err := ctxMyKey.Value(ctx) | ||
if cover && err { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not idiomatic, because the second argument is not of error
type, and that confuses the reader thinking it is an error. Also the if expression reads weird because it says if coverage is enabled and an error happened...
.
Methods that return value
and a ok
boolean are typically used like this:
if cover, ok := ctxMyKey.Value(ctx); cover && ok {
}
Signed-off-by: Kartikay <[email protected]>
b995558
to
e00732f
Compare
Fixes #1348
mage test:all
now generates combined coverprofile aggregated intocoverage.txt