Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Add dependencies command to mrreport
Browse files Browse the repository at this point in the history
[#165364355]

Co-authored-by: Ernie Billing <[email protected]>
  • Loading branch information
Pete Wall [email protected] and Ernie Billing committed Jul 16, 2019
1 parent 42de6ad commit 51bd958
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/mrreport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ import (
"os"

"github.com/cf-platform-eng/mrreport"
"github.com/cf-platform-eng/mrreport/dependencies"
"github.com/cf-platform-eng/mrreport/generate"
"github.com/cf-platform-eng/mrreport/version"

"github.com/jessevdk/go-flags"

"github.com/cf-platform-eng/mrreport/version"
)

var config mrreport.Config
var parser = flags.NewParser(&config, flags.Default)

func main() {
_, err := parser.AddCommand(
"dependencies",
"show logged dependencies",
"filter the logs to show only the dependencies",
&dependencies.DependenciesCommand{})
if err != nil {
fmt.Println("Could not add dependencies command")
os.Exit(1)
}

_, err = parser.AddCommand(
"generate",
"generate the report",
"generate a report from your test-case logging",
Expand Down
44 changes: 44 additions & 0 deletions dependencies/dependencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dependencies

import (
"fmt"
"github.com/pkg/errors"
"io"
"io/ioutil"
"os"
"regexp"
)

type DependenciesCommand struct {}

func (cmd *DependenciesCommand) Filter(in io.Reader, out io.WriteCloser) error {
logData, err := ioutil.ReadAll(in)
if err != nil {
return errors.Wrap(err, "error reading from stdin")
}

pattern, err := regexp.Compile("dependency: .*")
if err != nil { // !branch-not-tested we know of no way for this to occur.
return errors.Wrap(err, "error building the regex")
}

matches := pattern.FindAll(logData, -1)

for _, match := range matches {
_, err = fmt.Fprintf(out, "%s\n", string(match))
if err != nil {
return errors.Wrap(err, "failed to write to the output file")
}
}

err = out.Close()
if err != nil {
return errors.Wrap(err, "was unable to close the output file")
}

return nil
}

func (cmd *DependenciesCommand) Execute(args []string) error {
return cmd.Filter(os.Stdin, os.Stdout)
}
13 changes: 13 additions & 0 deletions dependencies/dependencies_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dependencies_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestDependencies(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Dependencies Suite")
}
133 changes: 133 additions & 0 deletions dependencies/dependencies_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package dependencies_test

import (
"bytes"
"errors"
. "github.com/MakeNowJust/heredoc/dot"
"github.com/cf-platform-eng/mrreport/dependencies"
"github.com/cf-platform-eng/mrreport/dependencies/dependenciesfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)

//go:generate counterfeiter io.Reader
//go:generate counterfeiter io.WriteCloser

var _ = Describe("Dependencies", func() {
var (
command *dependencies.DependenciesCommand
)

BeforeEach(func() {
command = &dependencies.DependenciesCommand{}
})

Context("Log has dependencies", func() {
var (
reader *bytes.Buffer
)

BeforeEach(func() {
reader = bytes.NewBufferString(D(`
this is the log
Here is a dependency
dependency: 'uaac' version '4.1.0' MRL:{"type":"dependency","version":"4.1.0","name":"uaac","time":"2019-07-16T19:46:20.490884996Z"}
dependency: 'something-else' version '1.2.3' MRL:{"type":"dependency","version":"1.2.3","name":"something-else","time":"2019-07-16T19:46:20.490884996Z"}
That was the dependency
`))
})

It("outputs only the logged dependencies", func() {
output := NewBuffer()
err := command.Filter(reader, output)
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Say(`dependency: 'uaac' version '4.1.0' MRL:{"type":"dependency","version":"4.1.0","name":"uaac","time":"2019-07-16T19:46:20.490884996Z"}\n`))
Expect(output).To(Say(`dependency: 'something-else' version '1.2.3' MRL:{"type":"dependency","version":"1.2.3","name":"something-else","time":"2019-07-16T19:46:20.490884996Z"}\n`))
})
})

Context("Log has no dependencies", func() {
var (
reader *bytes.Buffer
)

BeforeEach(func() {
reader = bytes.NewBufferString(D(`
this is the log
it has multiple lines
but none of them are dependencies
`))
})

It("outputs nothing", func() {
output := NewBuffer()
err := command.Filter(reader, output)
Expect(err).ToNot(HaveOccurred())
Expect(output.Contents()).To(BeEmpty())
})
})

Context("failed to read log", func() {
var (
reader *dependenciesfakes.FakeReader
)

BeforeEach(func() {
readError := errors.New("read error")
reader = &dependenciesfakes.FakeReader{}
reader.ReadReturns(0, readError)
})

It("returns an error", func() {
output := NewBuffer()
err := command.Filter(reader, output)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error reading from stdin"))
})
})

Context("cannot write to output", func() {
var (
output *dependenciesfakes.FakeWriteCloser
reader *bytes.Buffer
)

BeforeEach(func() {
output = &dependenciesfakes.FakeWriteCloser{}
output.WriteReturns(0, errors.New("couldn't write"))
reader = bytes.NewBufferString(D(`
this is the log
Here is a dependency
dependency: 'uaac' version '4.1.0' MRL:{"type":"dependency","version":"4.1.0","name":"uaac","time":"2019-07-16T19:46:20.490884996Z"}
dependency: 'something-else' version '1.2.3' MRL:{"type":"dependency","version":"1.2.3","name":"something-else","time":"2019-07-16T19:46:20.490884996Z"}
That was the dependency
`))
})

It("returns an error", func() {
err := command.Filter(reader, output)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("couldn't write"))
Expect(err.Error()).To(ContainSubstring("failed to write to the output file"))
})
})

Context("cannot close output", func() {
var (
output *dependenciesfakes.FakeWriteCloser
)
BeforeEach(func() {
output = &dependenciesfakes.FakeWriteCloser{}
output.CloseReturns(errors.New("couldn't close"))
})

It("returns an error", func() {
err := command.Filter(bytes.NewBufferString("log"), output)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("couldn't close"))
Expect(err.Error()).To(ContainSubstring("was unable to close the output file"))
})
})
})
119 changes: 119 additions & 0 deletions dependencies/dependenciesfakes/fake_reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 51bd958

Please sign in to comment.