...
1
16
17 package junit
18
19 import (
20 "fmt"
21 "io"
22 "os"
23
24 "edge-infra.dev/third_party/gopherage/pkg/cov/junit"
25 "edge-infra.dev/third_party/gopherage/pkg/util"
26 "github.com/spf13/cobra"
27 )
28
29 type flags struct {
30 outputFile string
31 threshold float32
32 }
33
34
35 func MakeCommand() *cobra.Command {
36 flags := &flags{}
37 cmd := &cobra.Command{
38 Use: "junit [profile]",
39 Short: "Summarize coverage profile and produce the result in junit xml format.",
40 Long: `Summarize coverage profile and produce the result in junit xml format.
41 Summary done at per-file and per-package level. Any coverage below coverage-threshold will be marked
42 with a <failure> tag in the xml produced.`,
43 Run: func(cmd *cobra.Command, args []string) {
44 run(flags, cmd, args)
45 },
46 }
47 cmd.Flags().StringVarP(&flags.outputFile, "output", "o", "-", "output file")
48 cmd.Flags().Float32VarP(&flags.threshold, "threshold", "t", .8, "code coverage threshold")
49 return cmd
50 }
51
52 func run(flags *flags, cmd *cobra.Command, args []string) {
53 if len(args) != 1 {
54 fmt.Fprintln(os.Stderr, "Expected exactly one argument: coverage file path")
55 cmd.Usage()
56 os.Exit(2)
57 }
58
59 if flags.threshold < 0 || flags.threshold > 1 {
60 fmt.Fprintln(os.Stderr, "coverage threshold must be a float number between 0 to 1, inclusively")
61 os.Exit(1)
62 }
63
64 profilePath := args[0]
65
66 profiles, err := util.LoadProfile(profilePath)
67 if err != nil {
68 fmt.Fprintf(os.Stderr, "Failed to parse profile file: %v.", err)
69 os.Exit(1)
70 }
71
72 text, err := junit.ProfileToTestsuiteXML(profiles, flags.threshold)
73
74 if err != nil {
75 fmt.Fprintf(os.Stderr, "Failed to produce xml from profiles: %v.", err)
76 os.Exit(1)
77 }
78
79 var file io.WriteCloser
80 if flags.outputFile == "-" {
81 file = os.Stdout
82 } else {
83 file, err = os.Create(flags.outputFile)
84 if err != nil {
85 fmt.Fprintf(os.Stderr, "Failed to create file: %v.", err)
86 os.Exit(1)
87 }
88 defer file.Close()
89 }
90
91 if _, err = file.Write(text); err != nil {
92 fmt.Fprintf(os.Stderr, "Failed to write xml: %v.", err)
93 os.Exit(1)
94 }
95 }
96
View as plain text