forked from intel/pmem-csi
/
coverage.go
50 lines (43 loc) · 1.43 KB
/
coverage.go
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
/*
Copyright 2019 Intel Corporation
SPDX-License-Identifier: Apache-2.0
*/
// Package coverage adds one command line flag similar to
// -test.coverprofile. The difference is that the file is created
// during startup with ioutil.Tmpfile (i.e. * gets replaced with
// a unique string) and then passed to a restarted binary
// as -test.coverprofile.
package coverage
import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"k8s.io/klog"
)
var coverage = flag.String("coverprofile", "", "write a coverage profile to a unique file (* in name replaced with random string, otherwise appended)")
// Run re-execs the program with -test.coverprofile if -coverprofile
// was used, otherwise it executes the program's main function.
func Run(main func() int) {
if *coverage != "" {
abspath, err := filepath.Abs(*coverage)
if err != nil {
klog.Fatalf("cover profile %q: %s", *coverage, err)
}
f, err := ioutil.TempFile(filepath.Dir(abspath), filepath.Base(abspath))
if err != nil {
klog.Fatalf("temporary cover profile %q: %s", abspath, err)
}
// Overwrite -coverprofile with empty string during next program run to
// avoid endless recursion. Instead use -test.coverprofile with the new
// name.
args := os.Args
args = append(args, "-coverprofile=")
args = append(args, "-test.coverprofile="+f.Name())
if err := syscall.Exec(args[0], args, os.Environ()); err != nil {
klog.Fatalf("re-exec %v: %s", args, err)
}
}
main()
}