...
1
16
17 package diff
18
19 import (
20 "fmt"
21 "os"
22
23 "edge-infra.dev/third_party/gopherage/pkg/cov"
24 "edge-infra.dev/third_party/gopherage/pkg/util"
25 "github.com/spf13/cobra"
26 )
27
28 type flags struct {
29 OutputFile string
30 }
31
32
33 func MakeCommand() *cobra.Command {
34 flags := &flags{}
35 cmd := &cobra.Command{
36 Use: "diff [first] [second]",
37 Short: "Diffs two Go coverage files.",
38 Long: `Takes the difference between two Go coverage files, producing another Go coverage file
39 showing only what was covered between the two files being generated. This works best when using
40 files generated in "count" or "atomic" mode; "set" may drastically underreport.
41
42 It is assumed that both files came from the same execution, and so all values in the second file are
43 at least equal to those in the first file.`,
44 Run: func(cmd *cobra.Command, args []string) {
45 run(flags, cmd, args)
46 },
47 }
48 cmd.Flags().StringVarP(&flags.OutputFile, "output", "o", "-", "output file")
49 return cmd
50 }
51
52 func run(flags *flags, cmd *cobra.Command, args []string) {
53 if len(args) != 2 {
54 fmt.Fprintln(os.Stderr, "Expected two files.")
55 cmd.Usage()
56 os.Exit(2)
57 }
58
59 before, err := util.LoadProfile(args[0])
60 if err != nil {
61 fmt.Fprintf(os.Stderr, "Couldn't load %s: %v.", args[0], err)
62 os.Exit(1)
63 }
64
65 after, err := util.LoadProfile(args[1])
66 if err != nil {
67 fmt.Fprintf(os.Stderr, "Couldn't load %s: %v.", args[0], err)
68 os.Exit(1)
69 }
70
71 diff, err := cov.DiffProfiles(before, after)
72 if err != nil {
73 fmt.Fprintf(os.Stderr, "failed to diff profiles: %v", err)
74 os.Exit(1)
75 }
76
77 if err := util.DumpProfile(flags.OutputFile, diff); err != nil {
78 fmt.Fprintln(os.Stderr, err)
79 os.Exit(1)
80 }
81 }
82
View as plain text