From 96b1817d62a0877061dbc675fe9a2f70dd501d2b Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Mon, 30 Mar 2020 11:44:17 +0200 Subject: [PATCH] add multi files config test and some more --- internal/config/config_test.go | 37 +++++++++- internal/config/testdata/more_data.yaml | 1 + internal/crypto/aes_test.go | 1 + internal/crypto/code_mock.go | 90 +++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 internal/config/testdata/more_data.yaml create mode 100644 internal/crypto/code_mock.go diff --git a/internal/config/config_test.go b/internal/config/config_test.go index ba517b6061..98221851dc 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -17,6 +17,11 @@ type validatable struct { Test bool } +type multiple struct { + Test bool + MoreData string +} + func (v *validatable) Validate() error { if v.Test { return nil @@ -33,22 +38,25 @@ func TestRead(t *testing.T) { name string args args wantErr bool + want interface{} }{ { "not supoorted config file error", args{ configFiles: []string{"notsupported.unknown"}, - obj: nil, + obj: &test{}, }, true, + &test{}, }, { "non existing config file error", args{ configFiles: []string{"nonexisting.yaml"}, - obj: nil, + obj: &test{}, }, true, + &test{}, }, { "non parsable config file error", @@ -57,6 +65,7 @@ func TestRead(t *testing.T) { obj: &test{}, }, true, + &test{}, }, { "invalid parsable config file error", @@ -65,6 +74,16 @@ func TestRead(t *testing.T) { obj: &validatable{}, }, true, + &validatable{}, + }, + { + "multiple files, one non parsable error ", + args{ + configFiles: []string{"./testdata/non_parsable.json", "./testdata/more_data.yaml"}, + obj: &multiple{}, + }, + true, + &multiple{}, }, { "parsable config file ok", @@ -73,6 +92,16 @@ func TestRead(t *testing.T) { obj: &test{}, }, false, + &test{Test: true}, + }, + { + "multiple parsable config files ok", + args{ + configFiles: []string{"./testdata/valid.json", "./testdata/more_data.yaml"}, + obj: &multiple{}, + }, + false, + &multiple{Test: true, MoreData: "data"}, }, { "valid parsable config file ok", @@ -81,6 +110,7 @@ func TestRead(t *testing.T) { obj: &validatable{}, }, false, + &validatable{Test: true}, }, } for _, tt := range tests { @@ -88,6 +118,9 @@ func TestRead(t *testing.T) { if err := Read(tt.args.obj, tt.args.configFiles...); (err != nil) != tt.wantErr { t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) } + if !reflect.DeepEqual(tt.args.obj, tt.want) { + t.Errorf("Read() got = %v, want = %v", tt.args.obj, tt.want) + } }) } } diff --git a/internal/config/testdata/more_data.yaml b/internal/config/testdata/more_data.yaml new file mode 100644 index 0000000000..d8fc5877da --- /dev/null +++ b/internal/config/testdata/more_data.yaml @@ -0,0 +1 @@ +MoreData: data diff --git a/internal/crypto/aes_test.go b/internal/crypto/aes_test.go index c7e31de3d5..87af88d0be 100644 --- a/internal/crypto/aes_test.go +++ b/internal/crypto/aes_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) +//TODO: refactor test style func TestDecrypt_OK(t *testing.T) { encryptedpw, err := EncryptAESString("ThisIsMySecretPw", "passphrasewhichneedstobe32bytes!") assert.NoError(t, err) diff --git a/internal/crypto/code_mock.go b/internal/crypto/code_mock.go new file mode 100644 index 0000000000..916a7c225d --- /dev/null +++ b/internal/crypto/code_mock.go @@ -0,0 +1,90 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: code.go + +// Package crypto is a generated GoMock package. +package crypto + +import ( + gomock "github.com/golang/mock/gomock" + reflect "reflect" + time "time" +) + +// MockGenerator is a mock of Generator interface +type MockGenerator struct { + ctrl *gomock.Controller + recorder *MockGeneratorMockRecorder +} + +// MockGeneratorMockRecorder is the mock recorder for MockGenerator +type MockGeneratorMockRecorder struct { + mock *MockGenerator +} + +// NewMockGenerator creates a new mock instance +func NewMockGenerator(ctrl *gomock.Controller) *MockGenerator { + mock := &MockGenerator{ctrl: ctrl} + mock.recorder = &MockGeneratorMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockGenerator) EXPECT() *MockGeneratorMockRecorder { + return m.recorder +} + +// Length mocks base method +func (m *MockGenerator) Length() uint { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Length") + ret0, _ := ret[0].(uint) + return ret0 +} + +// Length indicates an expected call of Length +func (mr *MockGeneratorMockRecorder) Length() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Length", reflect.TypeOf((*MockGenerator)(nil).Length)) +} + +// Expiry mocks base method +func (m *MockGenerator) Expiry() time.Duration { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Expiry") + ret0, _ := ret[0].(time.Duration) + return ret0 +} + +// Expiry indicates an expected call of Expiry +func (mr *MockGeneratorMockRecorder) Expiry() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Expiry", reflect.TypeOf((*MockGenerator)(nil).Expiry)) +} + +// Alg mocks base method +func (m *MockGenerator) Alg() Crypto { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Alg") + ret0, _ := ret[0].(Crypto) + return ret0 +} + +// Alg indicates an expected call of Alg +func (mr *MockGeneratorMockRecorder) Alg() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Alg", reflect.TypeOf((*MockGenerator)(nil).Alg)) +} + +// Runes mocks base method +func (m *MockGenerator) Runes() []rune { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Runes") + ret0, _ := ret[0].([]rune) + return ret0 +} + +// Runes indicates an expected call of Runes +func (mr *MockGeneratorMockRecorder) Runes() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Runes", reflect.TypeOf((*MockGenerator)(nil).Runes)) +}