Skip to content

Commit

Permalink
Add option to run Go tests automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
konradreiche committed May 15, 2024
1 parent f1624a7 commit a1f1769
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ go install github.com/konradreiche/coverdiff@latest
```
Usage:
coverdiff [file]
coverdiff test
Flags:
-h, --help print help text
Examples:
coverdiff test
go test -cover -coverprofile=coverage.out
cat coverage.out | coverdiff
Expand Down
47 changes: 43 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
package main

import (
"flag"
"fmt"
"io"
"os"
"os/exec"

"golang.org/x/tools/cover"
)

func command(stdin io.Reader, stdout io.Writer) error {
var (
fileName string
err error
)
switch flag.CommandLine.Arg(0) {
case "test":
fileName, err = runGoTests()
if err != nil {
return err
}
defer deleteTempFile(fileName)
case "-":
// user explicitly specified to use stdin
default:
fileName = flag.CommandLine.Arg(0)
}

// parse coverage profiles from stdin or file path if provided
profiles, err := parseCoverProfiles(stdin)
profiles, err := parseCoverProfiles(fileName, stdin)
if err != nil {
return err
}
Expand All @@ -23,9 +43,28 @@ func command(stdin io.Reader, stdout io.Writer) error {
return printDiff(stdout, profiles, moduleInfo)
}

func parseCoverProfiles(stdin io.Reader) ([]*cover.Profile, error) {
if len(os.Args) > 1 && os.Args[1] != "-" {
profiles, err := cover.ParseProfiles(os.Args[1])
func runGoTests() (string, error) {
f, err := os.CreateTemp(os.TempDir(), "coverdiff-*")
if err != nil {
return "", err
}
cmd := exec.Command("go", "test", "./...", "-cover", "-coverprofile="+f.Name())
if err := cmd.Run(); err != nil {
return "", err
}
return f.Name(), nil
}

func deleteTempFile(fileName string) {
if err := os.Remove(fileName); err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("coverdiff: %w", err))
os.Exit(1)
}
}

func parseCoverProfiles(fileName string, stdin io.Reader) ([]*cover.Profile, error) {
if fileName != "" {
profiles, err := cover.ParseProfiles(fileName)
return profiles, err
}
profiles, err := cover.ParseProfilesFromReader(stdin)
Expand Down
39 changes: 39 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"flag"
"os"
"path/filepath"
"testing"
Expand All @@ -16,6 +17,24 @@ func TestCommand(t *testing.T) {
os.Args = []string{
"coverdiff",
}
flag.Parse()

var stdout bytes.Buffer
stdin := bytes.NewBufferString(readFile(t, "testdata/coverage.out"))
if err := command(stdin, &stdout); err != nil {
t.Fatal(err)
}

got := stdout.String()
want := readFile(t, "testdata/coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})

t.Run("from-stdin-dash", func(t *testing.T) {
os.Args[1] = "-"
flag.Parse()

var stdout bytes.Buffer
stdin := bytes.NewBufferString(readFile(t, "testdata/coverage.out"))
Expand All @@ -32,6 +51,7 @@ func TestCommand(t *testing.T) {

t.Run("from-file", func(t *testing.T) {
os.Args[1] = filepath.Join(projectPath, "testdata/coverage.out")
flag.Parse()

var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
Expand All @@ -55,6 +75,7 @@ func TestCommand(t *testing.T) {
}
})
os.Args[1] = filepath.Join(projectPath, "testdata/coverage.out")
flag.Parse()

var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
Expand All @@ -80,6 +101,24 @@ func TestCommand(t *testing.T) {
t.Errorf("got %s, want: %s", got, want)
}
})

t.Run("run-go-tests", func(t *testing.T) {
os.Args[1] = "test"
flag.Parse()

changeDir(t, "testdata")

var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
t.Fatal(err)
}

got := stdout.String()
want := readFile(t, "coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
}

func readFile(tb testing.TB, name string) string {
Expand Down
11 changes: 7 additions & 4 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ const usage = `📑 coverdiff - print Go test coverage as diff
Usage:
coverdiff [file]
coverdiff test
Flags:
-h, --help print help text
coverdiff is a tool designed to process the cover profile output of Go and
display the coverage as a diff, similar to Go's -html option but optimized for
terminal convenience. You can provide the cover profile as a file or pass it
through standard input.
coverdiff is a tool that processes Go cover profiles and displays coverage
differences directly on the terminal, similar to Go's -html option. You can
provide the cover profile as a file, pass it through standard input, or let
coverdiff run the Go tests.
Examples:
coverdiff test
go test -cover -coverprofile=coverage.out
cat coverage.out | coverdiff
Expand Down

0 comments on commit a1f1769

Please sign in to comment.