mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-05 05:00:15 -05:00
Add -prop flag to add properties to a generated report
The value of the -prop flag should be specified as key=value. The -prop flag only supports specifying one property at a time. To add multiple properties, add a `-prop key=value` argument for each property.
This commit is contained in:
parent
10affc0da1
commit
c50f4331dc
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jstemmer/go-junit-report/v2/pkg/gtr"
|
||||
@ -28,12 +29,14 @@ var (
|
||||
input = flag.String("in", "", "read go test log from file")
|
||||
output = flag.String("out", "", "write XML report to file")
|
||||
iocopy = flag.Bool("iocopy", false, "copy input to stdout; can only be used in conjunction with -out")
|
||||
properties = make(keyValueFlag)
|
||||
|
||||
// debug flags
|
||||
printEvents = flag.Bool("debug.print-events", false, "print events generated by the go test parser")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Var(&properties, "prop", "add property to generated report; properties should be specified as \"key=value\"")
|
||||
flag.Parse()
|
||||
if *iocopy && *output == "" {
|
||||
exitf("you must specify an output file with -out when using -iocopy")
|
||||
@ -76,6 +79,11 @@ func main() {
|
||||
}
|
||||
}
|
||||
report := gtr.FromEvents(events, *packageName)
|
||||
for i := range report.Packages {
|
||||
for k, v := range properties {
|
||||
report.Packages[i].AddProperty(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
hostname, _ := os.Hostname() // ignore error
|
||||
testsuites := junit.CreateFromReport(report, hostname, time.Now())
|
||||
@ -114,3 +122,26 @@ func exitf(msg string, args ...interface{}) {
|
||||
}
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
type keyValueFlag map[string]string
|
||||
|
||||
func (f *keyValueFlag) String() string {
|
||||
if f != nil {
|
||||
var pairs []string
|
||||
for k, v := range *f {
|
||||
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
return strings.Join(pairs, ",")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f *keyValueFlag) Set(value string) error {
|
||||
idx := strings.IndexByte(value, '=')
|
||||
if idx == -1 {
|
||||
return fmt.Errorf("%v is not specified as \"key=value\"", value)
|
||||
}
|
||||
k, v := value[:idx], value[idx+1:]
|
||||
(*f)[k] = v
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user