Skip to content

Commit

Permalink
Open data test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Brophy committed Dec 2, 2017
1 parent 29d893f commit c9e90b3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
58 changes: 58 additions & 0 deletions blaster/blaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,67 @@ import (

"strings"

"reflect"

"io/ioutil"

"github.com/pkg/errors"
)

func TestOpenData(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

b := New(ctx, cancel)

must(t, b.openData(ctx, "", false))

must(t, b.openData(ctx, "a,b\n1,2", false))
if len(b.Headers) != 0 {
t.Fatal("Incorrect headers, got:", b.Headers)
}

b = New(ctx, cancel)
must(t, b.openData(ctx, "a,b\n1,2", true))
if !reflect.DeepEqual(b.Headers, []string{"a", "b"}) {
t.Fatal("Incorrect headers, got:", b.Headers)
}
r, err := b.dataReader.Read()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(r, []string{"1", "2"}) {
t.Fatal("Incorrect data, got:", r)
}

f, _ := ioutil.TempFile("", "")
f.WriteString("a,b,c\n1,2,3")
f.Close()

b = New(ctx, cancel)
must(t, b.openData(ctx, f.Name(), true))
if !reflect.DeepEqual(b.Headers, []string{"a", "b", "c"}) {
t.Fatal("Incorrect headers, got:", b.Headers)
}
r, err = b.dataReader.Read()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(r, []string{"1", "2", "3"}) {
t.Fatal("Incorrect data, got:", r)
}
}

func TestReadHeaders(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
b := New(ctx, cancel)
data := NewLoggingReadWriteCloser("a,b,c\n1,2,3\n4,5,6")
b.SetData(data)
must(t, b.ReadHeaders())
if !reflect.DeepEqual(b.Headers, []string{"a", "b", "c"}) {
t.Fatal("Incorrect headers, got:", b.Headers)
}
}

func TestExit(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
b := New(ctx, cancel)
Expand Down
9 changes: 9 additions & 0 deletions blaster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Config struct {
// LoadConfig parses command line flags and loads a config file from disk. A Config is returned which may be used with the Initialise method to complete configuration.
func (b *Blaster) LoadConfig() (Config, error) {

// notest

c := Config{}

dryRunFlag, configFlag := b.setupFlags()
Expand All @@ -94,6 +96,9 @@ func (b *Blaster) LoadConfig() (Config, error) {
}

func (b *Blaster) setupFlags() (dryRunFlag bool, configFlag string) {

// notest

dryRunFlagRaw := pflag.Bool("dry", false, "`` If true, just prints the current config and exits.")
configFlagRaw := pflag.String("config", "", "`` The config file to load.")

Expand Down Expand Up @@ -126,6 +131,8 @@ func (b *Blaster) setupFlags() (dryRunFlag bool, configFlag string) {

func (b *Blaster) setupViper(configFlag string) error {

// notest

if configFlag != "" {
b.viper.SetConfigFile(configFlag)
} else {
Expand Down Expand Up @@ -307,12 +314,14 @@ func (b *Blaster) Initialise(ctx context.Context, c Config) error {
}

if c.Data != "" {
// notest
if err := b.openData(ctx, c.Data, len(c.Headers) == 0); err != nil {
return err
}
}

if c.Log != "" {
// notest
if err := b.initialiseLog(c.Log); err != nil {
return err
}
Expand Down

0 comments on commit c9e90b3

Please sign in to comment.