Skip to content

Commit

Permalink
Refactor callback creation internals and fix callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro authored and crwr45 committed Jun 21, 2023
1 parent 05314be commit 628f759
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
34 changes: 24 additions & 10 deletions pkg/callbacks/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,41 @@ func getLine(c Callback, output OutputType, tag string) ([]byte, error) {
}
}

// SetupCallback if filename is empty or "-" it will output to stdout otherwise it will
// Returns the filehandle for callback
// if filename is empty or "-" it will output to stdout otherwise it will
// write to a file of the given name
func SetupCallback(filename string, format OutputFormat) (FileCallBack, error) {
func GetFileHandle(filename string) (io.WriteCloser, error) {
var (
fileHandle io.WriteCloser
err error
)

if filename == "-" || filename == "" {
fileHandle = os.Stdout
} else {
fileHandle, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, logFilePermissions)
fileHandle, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, logFilePermissions)
if err != nil {
return FileCallBack{}, fmt.Errorf("failed to open file: %w", err)
return fileHandle, fmt.Errorf("failed to open file: %w", err)
}
}
return FileCallBack{FileHandle: fileHandle, format: format}, nil
return fileHandle, nil
}

func NewFileCallback(fileHandle io.WriteCloser, format OutputFormat) FileCallBack {
return FileCallBack{fileHandle: fileHandle, format: format}
}

// SetupCallback returns a FileCallback
// if filename is empty or "-" it will output to stdout otherwise it will
// write to a file of the given name
func SetupCallback(filename string, format OutputFormat) (FileCallBack, error) {
fileHandle, err := GetFileHandle(filename)
if err != nil {
return FileCallBack{}, err
}
return NewFileCallback(fileHandle, format), nil
}

type FileCallBack struct {
FileHandle io.WriteCloser
fileHandle io.WriteCloser
format OutputFormat
}

Expand All @@ -89,7 +103,7 @@ func (c FileCallBack) Call(output OutputType, tag string) error {
return err
}
line = append(line, []byte("\n")...)
_, err = c.FileHandle.Write(line)
_, err = c.fileHandle.Write(line)
if err != nil {
return fmt.Errorf("failed to write to file in callback: %w", err)
}
Expand All @@ -101,7 +115,7 @@ func (c FileCallBack) getFormat() OutputFormat {
}

func (c FileCallBack) CleanUp() error {
err := c.FileHandle.Close()
err := c.fileHandle.Close()
if err != nil {
return fmt.Errorf("failed to close file handle in callback: %w", err)
}
Expand Down
46 changes: 36 additions & 10 deletions pkg/callbacks/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,51 @@ func (t *testFile) Close() error {
return errors.New("File is already closed") // TODO look up actual errors
}

type testOutputType struct {
Msg string `json:"msg"`
// responder func() (*callbacks.AnalyserFormatType, error)
}

func (t *testOutputType) GetAnalyserFormat() (*callbacks.AnalyserFormatType, error) {
fomatted := callbacks.AnalyserFormatType{
ID: "testOutput",
Data: []string{"Hello"},
}
return &fomatted, nil
}

var _ = Describe("Callbacks", func() {
var mockedFile *testFile
var callback *callbacks.FileCallBack

BeforeEach(func() {
mockedFile = NewTestFile()
callback = &callbacks.FileCallBack{FileHandle: mockedFile}
})

// When("A FileCallback is called", func() {
// It("should write to the file", func() {
// err := callback.Call("Test", "Nothing", "This is a test line")
// Expect(err).NotTo(HaveOccurred())
// Expect(mockedFile.ReadString('\n')).To(ContainSubstring("This is a test line"))
// })
// })

When("Raw FileCallback is called", func() {
It("should write to the file", func() {
callback := callbacks.NewFileCallback(mockedFile, callbacks.Raw)
out := testOutputType{
Msg: "This is a test line",
}
err := callback.Call(&out, "testOut")
Expect(err).NotTo(HaveOccurred())
Expect(mockedFile.ReadString('\n')).To(ContainSubstring("This is a test line"))
})
})
When("JSON FileCallback is called", func() {
It("should write to the file", func() {
callback := callbacks.NewFileCallback(mockedFile, callbacks.AnalyserJSON)
out := testOutputType{
Msg: "This is a test line",
}
err := callback.Call(&out, "testOut")
Expect(err).NotTo(HaveOccurred())
Expect(mockedFile.ReadString('\n')).To(Equal("{\"id\":\"testOutput\",\"data\":[\"Hello\"]}\n"))
})
})
When("A FileCallback is cleaned up", func() {
It("should close the file", func() {
callback := callbacks.NewFileCallback(mockedFile, callbacks.Raw)
err := callback.CleanUp()
Expect(err).NotTo(HaveOccurred())
Expect(mockedFile.open).To(BeFalse())
Expand Down

0 comments on commit 628f759

Please sign in to comment.