This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependencies command to mrreport
[#165364355] Co-authored-by: Ernie Billing <[email protected]>
- Loading branch information
1 parent
42de6ad
commit 51bd958
Showing
6 changed files
with
504 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.