Repository: onsi/ginkgo (language: go)

Issue:
Coverage does not work for Go tests
We've been trying to unify our tooling to use the Gingko cli to run all of our test suites as we are migrating towards it for as many of our tests as we can.

As part of this, we are expecting to be able to include cover profiles from the tests, even if there are some Go tests still to be run.

The issue is, if you include `--cover --coverprofile=<something>`, we get to the following line to start the go test: https://github.com/onsi/ginkgo/blob/143d2083b07f1303276d45bea2d11d9985449a62/ginkgo/internal/run.go#L34

Which then calls `GenerateGoTestRunArgs`:
https://github.com/onsi/ginkgo/blob/143d2083b07f1303276d45bea2d11d9985449a62/ginkgo/internal/run.go#L66

Which for the Go test, will now return `--test.coverprofile=<something>`. Issue is, when running the Go test, Ginkgo runs it from the directory of the suite, so the coverprofile ends up in the test suite directory with the value of the Ginkgo `coverprofile` as its name.

Next up, is the clean up phase, `FinalizeProfilesAndReportsForSuites` which observes the suite and says, yep, this should have a coverprofile.

So it calculates the location of the file to pick the coverage up from:
https://github.com/onsi/ginkgo/blob/143d2083b07f1303276d45bea2d11d9985449a62/ginkgo/internal/profiles_and_reports.go#L38

Which is a combination of the suite package name and the output directory - which does not align with where the file actually is.

So I see two solutions:
* Fix the finalize to detect a Go suite and pick the file up from the Go suite
* Fix the Go test args to put the coverprofile in the location expected by the finalize

Have I followed this right? Any thoughts on a preferred direction?

An example of the error output:
```
ginkgo run failed
  could not finalize profiles:
  Unable to read coverage file /logs/artifacts/pkg_cloud_azure_actuators_machine_test-unit-coverage.out:
  open /logs/artifacts/pkg_cloud_azure_actuators_machine_test-unit-coverage.out: no such file or directory
```


Discussion / hints:
hey! sorry this isn't Just Working™  - i've managed to reproduce it locally and will take a look and circle back today/tomorrow.
I'm happy to work on a patch if you're open to that, would just like a little guidance on which of the suggested paths to take
that would be great!  my instinct is to fix the go test args that are being generated and not to change the finalize.  i think the main things to check are that things work for the various combinations of -output-dir (or no output-dir) and (-keep-separate-coverprofile)

thanks!

Fix patch:
diff --git a/ginkgo/internal/run.go b/ginkgo/internal/run.go
index cad238671..41052ea19 100644
--- a/ginkgo/internal/run.go
+++ b/ginkgo/internal/run.go
@@ -6,6 +6,7 @@ import (
 	"io"
 	"os"
 	"os/exec"
+	"path/filepath"
 	"regexp"
 	"strings"
 	"syscall"
@@ -63,6 +64,12 @@ func checkForNoTestsWarning(buf *bytes.Buffer) bool {
 }
 
 func runGoTest(suite TestSuite, cliConfig types.CLIConfig, goFlagsConfig types.GoFlagsConfig) TestSuite {
+	// As we run the go test from the suite directory, make sure the cover profile is absolute
+	// and placed into the expected output directory when one is configured.
+	if goFlagsConfig.Cover && !filepath.IsAbs(goFlagsConfig.CoverProfile) {
+		goFlagsConfig.CoverProfile = AbsPathForGeneratedAsset(goFlagsConfig.CoverProfile, suite, cliConfig, 0)
+	}
+
 	args, err := types.GenerateGoTestRunArgs(goFlagsConfig)
 	command.AbortIfError("Failed to generate test run arguments", err)
 	cmd, buf := buildAndStartCommand(suite, args, true)


Test patch: