-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
447 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/bketelsen/logr" | ||
) | ||
|
||
// DoFormat applies a format to a given input. | ||
func DoFormat(cfg Config, log logr.Logger) (result error) { | ||
reader, err := cfg.Input() | ||
if err != nil { | ||
return fmt.Errorf("failed to open input: %v", err) | ||
} | ||
|
||
if closer, ok := reader.(io.Closer); ok { | ||
defer closer.Close() | ||
} | ||
|
||
formats, err := cfg.Formats() | ||
if err != nil { | ||
return fmt.Errorf("failed to setup output formatting: %v", err) | ||
} | ||
|
||
factory := NewFactory(formats, false, log) | ||
defer func() { | ||
if err := factory.Close(); err != nil && result == nil { | ||
result = err | ||
} | ||
}() | ||
|
||
writer, err := factory.Create(0) | ||
if err != nil { | ||
return fmt.Errorf("failed to setup output writer: %v", err) | ||
} | ||
|
||
if _, err := io.Copy(writer, reader); nil != err { | ||
result = fmt.Errorf("failed to process data: %v", err) | ||
} | ||
|
||
return | ||
} |
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,90 @@ | ||
package format_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/corvus-ch/horcrux/format" | ||
"github.com/corvus-ch/horcrux/format/raw" | ||
"github.com/corvus-ch/horcrux/input" | ||
"github.com/corvus-ch/logr/buffered" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
dir := createDir(t) | ||
defer os.RemoveAll(dir) | ||
|
||
f := raw.New(input.NewStreamInput(outputStem(dir, raw.Name))) | ||
cfg := NewConfig(t.Name(), f) | ||
log := buffered.New(1) | ||
|
||
err := format.DoFormat(cfg, log) | ||
|
||
assert.Nil(t, err) | ||
mock.AssertExpectationsForObjects(t, cfg) | ||
|
||
files, _ := filepath.Glob(fmt.Sprintf("%s.*", outputStem(dir, raw.Name))) | ||
|
||
assert.Empty(t, log.Buf()) | ||
assert.Len(t, files, 1) | ||
} | ||
|
||
var errorTests = []struct { | ||
name string | ||
setup func(t *testing.T, cfg *Config, dir string) | ||
output string | ||
}{ | ||
{"input", func(t *testing.T, cfg *Config, _ string) { | ||
cfg.On("Input").Maybe().Return(nil, errors.New("input error")) | ||
}, "failed to open input: input error"}, | ||
|
||
{"formats", func(t *testing.T, cfg *Config, _ string) { | ||
cfg.On("Input").Return(bytes.NewBufferString(t.Name()), nil) | ||
cfg.On("Formats").Return(nil, errors.New("format error")) | ||
}, "failed to setup output formatting: format error"}, | ||
|
||
{"copy", func(t *testing.T, cfg *Config, dir string) { | ||
f, _ := os.Open(t.Name()) | ||
cfg.On("Input").Return(f, nil) | ||
cfg.On("InputInfo").Maybe().Return(input.NewStreamInput(outputStem(dir, raw.Name))) | ||
cfg.On("Formats").Maybe().Return([]format.Format{raw.New(input.NewStreamInput(outputStem(dir, raw.Name)))}, nil) | ||
}, "failed to process data: invalid argument"}, | ||
} | ||
|
||
func TestErrors(t *testing.T) { | ||
for _, test := range errorTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
dir := createDir(t) | ||
defer os.RemoveAll(dir) | ||
cfg := &Config{} | ||
log := buffered.New(1) | ||
test.setup(t, cfg, dir) | ||
|
||
err := format.DoFormat(cfg, log) | ||
|
||
assert.Error(t, err, test.output) | ||
assert.Empty(t, log.Buf()) | ||
mock.AssertExpectationsForObjects(t, cfg) | ||
}) | ||
} | ||
} | ||
|
||
func createDir(t *testing.T) string { | ||
dir, err := ioutil.TempDir("", "horcrux_create") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
return dir | ||
} | ||
|
||
func outputStem(dir, format string) string { | ||
return fmt.Sprintf("%s/%s", dir, format) | ||
} |
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,14 @@ | ||
package format | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/corvus-ch/horcrux/input" | ||
) | ||
|
||
// Config define the configuration input required for creating an output format. | ||
type Config interface { | ||
Input() (io.Reader, error) | ||
InputInfo() input.Input | ||
Formats() ([]Format, error) | ||
} |
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,49 @@ | ||
package format_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
|
||
"github.com/corvus-ch/horcrux/format" | ||
"github.com/corvus-ch/horcrux/input" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type Config struct { | ||
mock.Mock | ||
} | ||
|
||
func NewConfig(name string, f format.Format) *Config { | ||
cfg := &Config{} | ||
cfg.On("Input").Maybe().Return(bytes.NewBufferString(name), nil) | ||
cfg.On("InputInfo").Maybe().Return(input.NewStreamInput("")) | ||
cfg.On("Formats").Maybe().Return([]format.Format{f}, nil) | ||
|
||
return cfg | ||
} | ||
|
||
func (c *Config) Input() (io.Reader, error) { | ||
args := c.Called() | ||
r := args.Get(0) | ||
if r == nil { | ||
return nil, args.Error(1) | ||
} | ||
|
||
return r.(io.Reader), args.Error(1) | ||
} | ||
|
||
func (c *Config) InputInfo() input.Input { | ||
args := c.Called() | ||
|
||
return args.Get(0).(input.Input) | ||
} | ||
|
||
func (c *Config) Formats() ([]format.Format, error) { | ||
args := c.Called() | ||
f := args.Get(0) | ||
if f == nil { | ||
return nil, args.Error(1) | ||
} | ||
|
||
return f.([]format.Format), args.Error(1) | ||
} |
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
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
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 @@ | ||
ERROR usage: horcrux format [<flags>] [<input>] | ||
ERROR | ||
ERROR formats input the same way as create would do without doing the split | ||
ERROR | ||
ERROR Flags: | ||
ERROR --help Show context-sensitive help (also try --help-long and | ||
ERROR --help-man). | ||
ERROR -f, --format=text ... the output formats | ||
ERROR -o, --output=OUTPUT name stem for the output files | ||
ERROR | ||
ERROR Args: | ||
ERROR [<input>] the input file | ||
ERROR |
Oops, something went wrong.