diff --git a/src/app/command/bootstrap.go b/src/app/command/bootstrap.go index 8194bb2..62ca580 100644 --- a/src/app/command/bootstrap.go +++ b/src/app/command/bootstrap.go @@ -69,6 +69,7 @@ type Bootstrap struct { ProfilesCFG proxy.ProfilesConfig SchemesCFG proxy.SchemesConfig SamplerCFG proxy.SamplerConfig + AdvancedCFG proxy.AdvancedConfig Vfs storage.VirtualFS } @@ -101,6 +102,7 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command { Profiles: &MsProfilesConfigReader{}, Schemes: &MsSchemesConfigReader{}, Sampler: &MsSamplerConfigReader{}, + Advanced: &MsAdvancedConfigReader{}, }, }, } @@ -237,4 +239,5 @@ func (b *Bootstrap) viper() { b.ProfilesCFG, _ = b.OptionsInfo.Config.Readers.Profiles.Read(b.OptionsInfo.Config.Viper) b.SchemesCFG, _ = b.OptionsInfo.Config.Readers.Schemes.Read(b.OptionsInfo.Config.Viper) b.SamplerCFG, _ = b.OptionsInfo.Config.Readers.Sampler.Read(b.OptionsInfo.Config.Viper) + b.AdvancedCFG, _ = b.OptionsInfo.Config.Readers.Advanced.Read(b.OptionsInfo.Viper) } diff --git a/src/app/command/bootstrap_test.go b/src/app/command/bootstrap_test.go index 45737d0..b0d5f6e 100644 --- a/src/app/command/bootstrap_test.go +++ b/src/app/command/bootstrap_test.go @@ -5,10 +5,13 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/snivilised/cobrass/src/assistant/configuration" + cmocks "github.com/snivilised/cobrass/src/assistant/mocks" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" + "github.com/snivilised/pixa/src/app/mocks" "github.com/snivilised/pixa/src/internal/helpers" - "github.com/snivilised/pixa/src/internal/matchers" + "go.uber.org/mock/gomock" "golang.org/x/text/language" ) @@ -38,27 +41,50 @@ func (j *DetectorStub) Scan() language.Tag { var _ = Describe("Bootstrap", Ordered, func() { var ( - repo string - l10nPath string - configPath string - nfs storage.VirtualFS + repo string + l10nPath string + configPath string + config configuration.ViperConfig + vfs storage.VirtualFS + ctrl *gomock.Controller + mockProfilesReader *mocks.MockProfilesConfigReader + mockSchemesReader *mocks.MockSchemesConfigReader + mockSamplerReader *mocks.MockSamplerConfigReader + mockAdvancedReader *mocks.MockAdvancedConfigReader + mockViperConfig *cmocks.MockViperConfig ) BeforeAll(func() { - nfs = storage.UseNativeFS() repo = helpers.Repo(filepath.Join("..", "..", "..")) + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") + }) + + BeforeEach(func() { + vfs, _, config = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) + ctrl = gomock.NewController(GinkgoT()) + mockViperConfig = cmocks.NewMockViperConfig(ctrl) + mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) + mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) + mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) + helpers.DoMockReadInConfig(mockViperConfig) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) + }) - configPath = filepath.Join(repo, "test", "data", "configuration") - Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs)) + AfterEach(func() { + ctrl.Finish() }) Context("given: root defined with magick sub-command", func() { It("๐Ÿงช should: setup command without error", func() { bootstrap := command.Bootstrap{ - Vfs: nfs, + Vfs: vfs, } rootCmd := bootstrap.Root(func(co *command.ConfigureOptionsInfo) { co.Detector = &DetectorStub{} @@ -67,7 +93,15 @@ var _ = Describe("Bootstrap", Ordered, func() { } co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath + co.Viper = &configuration.GlobalViperConfig{} + co.Config.Readers = command.ConfigReaders{ + Profiles: mockProfilesReader, + Schemes: mockSchemesReader, + Sampler: mockSamplerReader, + Advanced: mockAdvancedReader, + } }) + Expect(rootCmd).NotTo(BeNil()) }) }) diff --git a/src/app/command/config-readers.go b/src/app/command/config-readers.go index 9d3b240..3e29cae 100644 --- a/src/app/command/config-readers.go +++ b/src/app/command/config-readers.go @@ -63,8 +63,21 @@ func (r *MsSamplerConfigReader) Read(viper configuration.ViperConfig) (proxy.Sam return &samplerCFG, err } +type MsAdvancedConfigReader struct{} + +func (r *MsAdvancedConfigReader) Read(viper configuration.ViperConfig) (proxy.AdvancedConfig, error) { + var ( + advancedCFG proxy.MsAdvancedConfig + ) + + err := viper.UnmarshalKey("advanced", &advancedCFG) + + return &advancedCFG, err +} + type ConfigReaders struct { Profiles proxy.ProfilesConfigReader Schemes proxy.SchemesConfigReader Sampler proxy.SamplerConfigReader + Advanced proxy.AdvancedConfigReader } diff --git a/src/app/command/magick-cmd_test.go b/src/app/command/magick-cmd_test.go index d295144..acef8b5 100644 --- a/src/app/command/magick-cmd_test.go +++ b/src/app/command/magick-cmd_test.go @@ -5,45 +5,86 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "go.uber.org/mock/gomock" + "github.com/snivilised/cobrass/src/assistant/configuration" + cmocks "github.com/snivilised/cobrass/src/assistant/mocks" xi18n "github.com/snivilised/extendio/i18n" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" + "github.com/snivilised/pixa/src/app/mocks" "github.com/snivilised/pixa/src/internal/helpers" "github.com/snivilised/pixa/src/internal/matchers" ) var _ = Describe("MagickCmd", Ordered, func() { var ( - repo string - l10nPath string - nfs storage.VirtualFS + repo string + l10nPath string + configPath string + // root string + config configuration.ViperConfig + vfs storage.VirtualFS + ctrl *gomock.Controller + mockProfilesReader *mocks.MockProfilesConfigReader + mockSchemesReader *mocks.MockSchemesConfigReader + mockSamplerReader *mocks.MockSamplerConfigReader + mockAdvancedReader *mocks.MockAdvancedConfigReader + mockViperConfig *cmocks.MockViperConfig ) BeforeAll(func() { - nfs = storage.UseNativeFS() + vfs = storage.UseNativeFS() repo = helpers.Repo(filepath.Join("..", "..", "..")) l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) + Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(vfs)) + }) + + BeforeAll(func() { + repo = helpers.Repo(filepath.Join("..", "..", "..")) + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") }) BeforeEach(func() { xi18n.ResetTx() + vfs, _, config = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) - if err := helpers.UseI18n(l10nPath); err != nil { - Fail(err.Error()) - } + ctrl = gomock.NewController(GinkgoT()) + mockViperConfig = cmocks.NewMockViperConfig(ctrl) + mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) + mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) + mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) + helpers.DoMockReadInConfig(mockViperConfig) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) }) When("specified flags are valid", func() { It("๐Ÿงช should: execute without error", func() { bootstrap := command.Bootstrap{ - Vfs: nfs, + Vfs: vfs, } tester := helpers.CommandTester{ Args: []string{"mag"}, Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) { co.Detector = &DetectorStub{} + co.Program = &helpers.ExecutorStub{ + Name: helpers.ProgName, + } + co.Config.Name = helpers.PixaConfigTestFilename + co.Config.ConfigPath = configPath + co.Viper = &configuration.GlobalViperConfig{} + co.Config.Readers = command.ConfigReaders{ + Profiles: mockProfilesReader, + Schemes: mockSchemesReader, + Sampler: mockSamplerReader, + Advanced: mockAdvancedReader, + } }), } _, err := tester.Execute() diff --git a/src/app/command/shrink-cmd.go b/src/app/command/shrink-cmd.go index aba56d1..fd0ba55 100644 --- a/src/app/command/shrink-cmd.go +++ b/src/app/command/shrink-cmd.go @@ -139,6 +139,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob b.ProfilesCFG, b.SchemesCFG, b.SamplerCFG, + b.AdvancedCFG, b.Vfs, ) } else { diff --git a/src/app/command/shrink-cmd_test.go b/src/app/command/shrink-cmd_test.go index 21c9d77..b49deb6 100644 --- a/src/app/command/shrink-cmd_test.go +++ b/src/app/command/shrink-cmd_test.go @@ -63,10 +63,13 @@ func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE, root mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) ) helpers.DoMockReadInConfig(mockViperConfig) - helpers.DoMockConfigs(config, mockProfilesReader, mockSchemesReader, mockSamplerReader) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) tester := helpers.CommandTester{ Args: append(args, entry.args...), @@ -83,6 +86,7 @@ func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE, root Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, + Advanced: mockAdvancedReader, } }), } diff --git a/src/app/mocks/mocks-config.go b/src/app/mocks/mocks-config.go index 9a92c57..3aaa88b 100644 --- a/src/app/mocks/mocks-config.go +++ b/src/app/mocks/mocks-config.go @@ -1,15 +1,16 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: sampler-config.go +// Source: config.go // // Generated by this command: // -// mockgen -destination ../mocks/mocks-config.go -package mocks -source sampler-config.go +// mockgen -destination ../mocks/mocks-config.go -package mocks -source config.go // // Package mocks is a generated GoMock package. package mocks import ( reflect "reflect" + time "time" configuration "github.com/snivilised/cobrass/src/assistant/configuration" clif "github.com/snivilised/cobrass/src/clif" @@ -271,3 +272,163 @@ func (mr *MockSamplerConfigReaderMockRecorder) Read(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockSamplerConfigReader)(nil).Read), arg0) } + +// MockAdvancedConfig is a mock of AdvancedConfig interface. +type MockAdvancedConfig struct { + ctrl *gomock.Controller + recorder *MockAdvancedConfigMockRecorder +} + +// MockAdvancedConfigMockRecorder is the mock recorder for MockAdvancedConfig. +type MockAdvancedConfigMockRecorder struct { + mock *MockAdvancedConfig +} + +// NewMockAdvancedConfig creates a new mock instance. +func NewMockAdvancedConfig(ctrl *gomock.Controller) *MockAdvancedConfig { + mock := &MockAdvancedConfig{ctrl: ctrl} + mock.recorder = &MockAdvancedConfigMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAdvancedConfig) EXPECT() *MockAdvancedConfigMockRecorder { + return m.recorder +} + +// AbortOnError mocks base method. +func (m *MockAdvancedConfig) AbortOnError() bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AbortOnError") + ret0, _ := ret[0].(bool) + return ret0 +} + +// AbortOnError indicates an expected call of AbortOnError. +func (mr *MockAdvancedConfigMockRecorder) AbortOnError() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AbortOnError", reflect.TypeOf((*MockAdvancedConfig)(nil).AbortOnError)) +} + +// AdhocLabel mocks base method. +func (m *MockAdvancedConfig) AdhocLabel() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AdhocLabel") + ret0, _ := ret[0].(string) + return ret0 +} + +// AdhocLabel indicates an expected call of AdhocLabel. +func (mr *MockAdvancedConfigMockRecorder) AdhocLabel() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AdhocLabel", reflect.TypeOf((*MockAdvancedConfig)(nil).AdhocLabel)) +} + +// JournalLabel mocks base method. +func (m *MockAdvancedConfig) JournalLabel() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "JournalLabel") + ret0, _ := ret[0].(string) + return ret0 +} + +// JournalLabel indicates an expected call of JournalLabel. +func (mr *MockAdvancedConfigMockRecorder) JournalLabel() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JournalLabel", reflect.TypeOf((*MockAdvancedConfig)(nil).JournalLabel)) +} + +// LegacyLabel mocks base method. +func (m *MockAdvancedConfig) LegacyLabel() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LegacyLabel") + ret0, _ := ret[0].(string) + return ret0 +} + +// LegacyLabel indicates an expected call of LegacyLabel. +func (mr *MockAdvancedConfigMockRecorder) LegacyLabel() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LegacyLabel", reflect.TypeOf((*MockAdvancedConfig)(nil).LegacyLabel)) +} + +// NoRetries mocks base method. +func (m *MockAdvancedConfig) NoRetries() uint { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NoRetries") + ret0, _ := ret[0].(uint) + return ret0 +} + +// NoRetries indicates an expected call of NoRetries. +func (mr *MockAdvancedConfigMockRecorder) NoRetries() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NoRetries", reflect.TypeOf((*MockAdvancedConfig)(nil).NoRetries)) +} + +// ProgramTimeout mocks base method. +func (m *MockAdvancedConfig) ProgramTimeout() (time.Duration, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProgramTimeout") + ret0, _ := ret[0].(time.Duration) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProgramTimeout indicates an expected call of ProgramTimeout. +func (mr *MockAdvancedConfigMockRecorder) ProgramTimeout() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProgramTimeout", reflect.TypeOf((*MockAdvancedConfig)(nil).ProgramTimeout)) +} + +// TrashLabel mocks base method. +func (m *MockAdvancedConfig) TrashLabel() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TrashLabel") + ret0, _ := ret[0].(string) + return ret0 +} + +// TrashLabel indicates an expected call of TrashLabel. +func (mr *MockAdvancedConfigMockRecorder) TrashLabel() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TrashLabel", reflect.TypeOf((*MockAdvancedConfig)(nil).TrashLabel)) +} + +// MockAdvancedConfigReader is a mock of AdvancedConfigReader interface. +type MockAdvancedConfigReader struct { + ctrl *gomock.Controller + recorder *MockAdvancedConfigReaderMockRecorder +} + +// MockAdvancedConfigReaderMockRecorder is the mock recorder for MockAdvancedConfigReader. +type MockAdvancedConfigReaderMockRecorder struct { + mock *MockAdvancedConfigReader +} + +// NewMockAdvancedConfigReader creates a new mock instance. +func NewMockAdvancedConfigReader(ctrl *gomock.Controller) *MockAdvancedConfigReader { + mock := &MockAdvancedConfigReader{ctrl: ctrl} + mock.recorder = &MockAdvancedConfigReaderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAdvancedConfigReader) EXPECT() *MockAdvancedConfigReaderMockRecorder { + return m.recorder +} + +// Read mocks base method. +func (m *MockAdvancedConfigReader) Read(arg0 configuration.ViperConfig) (proxy.AdvancedConfig, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Read", arg0) + ret0, _ := ret[0].(proxy.AdvancedConfig) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Read indicates an expected call of Read. +func (mr *MockAdvancedConfigReaderMockRecorder) Read(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockAdvancedConfigReader)(nil).Read), arg0) +} diff --git a/src/app/proxy/config-defaults.go b/src/app/proxy/config-defaults.go index ee2953c..58f50b1 100644 --- a/src/app/proxy/config-defaults.go +++ b/src/app/proxy/config-defaults.go @@ -5,14 +5,16 @@ import ( ) const ( - defaultNoFiles = 3 - defaultNoFolders = 3 + defaultNoFiles = 3 + defaultNoFolders = 3 + defaultNoProgramRetries = 2 ) var ( DefaultProfilesConfig *MsProfilesConfig DefaultSamplerConfig *MsSamplerConfig DefaultSchemesConfig *MsSchemesConfig + DefaultAdvancedConfig *MsAdvancedConfig ) func init() { @@ -57,4 +59,16 @@ func init() { Files: defaultNoFiles, Folders: defaultNoFolders, } + + DefaultAdvancedConfig = &MsAdvancedConfig{ + Abort: false, + Timeout: "10s", + NoProgramRetries: defaultNoProgramRetries, + Labels: MsLabelsConfig{ + Adhoc: "ADHOC", + Journal: ".journal.txt", + Legacy: ".LEGACY", + Trash: "TRASH", + }, + } } diff --git a/src/app/proxy/config.go b/src/app/proxy/config.go index ce5766c..a523076 100644 --- a/src/app/proxy/config.go +++ b/src/app/proxy/config.go @@ -2,12 +2,13 @@ package proxy import ( "fmt" + "time" "github.com/snivilised/cobrass/src/assistant/configuration" "github.com/snivilised/cobrass/src/clif" ) -//go:generate mockgen -destination ../mocks/mocks-config.go -package mocks -source sampler-config.go +//go:generate mockgen -destination ../mocks/mocks-config.go -package mocks -source config.go type ( MsSchemeConfig struct { @@ -41,6 +42,20 @@ type ( SamplerConfigReader interface { Read(configuration.ViperConfig) (SamplerConfig, error) } + + AdvancedConfig interface { + AbortOnError() bool + ProgramTimeout() (duration time.Duration, err error) + NoRetries() uint + AdhocLabel() string + JournalLabel() string + LegacyLabel() string + TrashLabel() string + } + + AdvancedConfigReader interface { + Read(configuration.ViperConfig) (AdvancedConfig, error) + } ) type MsProfilesConfig struct { @@ -96,3 +111,45 @@ func (cfg *MsSamplerConfig) NoFiles() uint { func (cfg *MsSamplerConfig) NoFolders() uint { return cfg.Folders } + +type MsLabelsConfig struct { + Adhoc string `mapstructure:"adhoc"` + Journal string `mapstructure:"journal-suffix"` + Legacy string `mapstructure:"legacy"` + Trash string `mapstructure:"trash"` +} + +type MsAdvancedConfig struct { + Abort bool `mapstructure:"abort-on-error"` + Timeout string `mapstructure:"program-timeout"` + NoProgramRetries uint `mapstructure:"no-program-retries"` + Labels MsLabelsConfig `mapstructure:"labels"` +} + +func (cfg *MsAdvancedConfig) AbortOnError() bool { + return cfg.Abort +} + +func (cfg *MsAdvancedConfig) ProgramTimeout() (duration time.Duration, err error) { + return time.ParseDuration(cfg.Timeout) +} + +func (cfg *MsAdvancedConfig) NoRetries() uint { + return cfg.NoProgramRetries +} + +func (cfg *MsAdvancedConfig) AdhocLabel() string { + return cfg.Labels.Adhoc +} + +func (cfg *MsAdvancedConfig) JournalLabel() string { + return cfg.Labels.Journal +} + +func (cfg *MsAdvancedConfig) LegacyLabel() string { + return cfg.Labels.Legacy +} + +func (cfg *MsAdvancedConfig) TrashLabel() string { + return cfg.Labels.Trash +} diff --git a/src/app/proxy/config_test.go b/src/app/proxy/config_test.go index 09c4c34..45e1a8c 100644 --- a/src/app/proxy/config_test.go +++ b/src/app/proxy/config_test.go @@ -6,23 +6,37 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/spf13/viper" + "go.uber.org/mock/gomock" "github.com/snivilised/cobrass/src/assistant/configuration" - ci18n "github.com/snivilised/cobrass/src/assistant/i18n" - xi18n "github.com/snivilised/extendio/i18n" + cmocks "github.com/snivilised/cobrass/src/assistant/mocks" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" - "github.com/snivilised/pixa/src/i18n" + "github.com/snivilised/pixa/src/app/mocks" "github.com/snivilised/pixa/src/internal/helpers" - "github.com/snivilised/pixa/src/internal/matchers" ) -func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *configTE) { +func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *configTE, + config configuration.ViperConfig, +) { bootstrap := command.Bootstrap{ Vfs: vfs, } + var ( + ctrl = gomock.NewController(GinkgoT()) + mockViperConfig = cmocks.NewMockViperConfig(ctrl) + mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) + mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) + mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) + ) + + helpers.DoMockReadInConfig(mockViperConfig) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) + options := []string{ entry.comm, entry.file, "--dry-run", @@ -41,6 +55,13 @@ func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *configTE) { } co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath + co.Viper = &configuration.GlobalViperConfig{} + co.Config.Readers = command.ConfigReaders{ + Profiles: mockProfilesReader, + Schemes: mockSchemesReader, + Sampler: mockSamplerReader, + Advanced: mockAdvancedReader, + } }), } @@ -60,54 +81,44 @@ type configTE struct { var _ = Describe("Config", Ordered, func() { var ( - repo string - l10nPath string - configPath string - config configuration.ViperConfig - nfs storage.VirtualFS + repo string + l10nPath string + configPath string + config configuration.ViperConfig + vfs storage.VirtualFS + ctrl *gomock.Controller + mockProfilesReader *mocks.MockProfilesConfigReader + mockSchemesReader *mocks.MockSchemesConfigReader + mockSamplerReader *mocks.MockSamplerConfigReader + mockAdvancedReader *mocks.MockAdvancedConfigReader + mockViperConfig *cmocks.MockViperConfig ) BeforeAll(func() { - nfs = storage.UseNativeFS() repo = helpers.Repo(filepath.Join("..", "..", "..")) - - l10nPath = helpers.Path(repo, filepath.Join("test", "data", "l10n")) - Expect(matchers.AsDirectory(l10nPath)).To(matchers.ExistInFS(nfs)) - - configPath = filepath.Join(repo, "test", "data", "configuration") - Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs)) + l10nPath = helpers.Path(repo, "test/data/l10n") + configPath = helpers.Path(repo, "test/data/configuration") }) BeforeEach(func() { - viper.Reset() - config = &configuration.GlobalViperConfig{} - - config.SetConfigType(helpers.PixaConfigType) - config.SetConfigName(helpers.PixaConfigTestFilename) - - config.AddConfigPath(configPath) - if err := config.ReadInConfig(); err != nil { - Fail(fmt.Sprintf("๐Ÿ”ฅ can't read config (err: '%v')", err)) - } - - err := xi18n.Use(func(uo *xi18n.UseOptions) { - uo.From = xi18n.LoadFrom{ - Path: l10nPath, - Sources: xi18n.TranslationFiles{ - i18n.PixaSourceID: xi18n.TranslationSource{ - Name: "dummy-cobrass", - }, - - ci18n.CobrassSourceID: xi18n.TranslationSource{ - Name: "dummy-cobrass", - }, - }, - } - }) + vfs, _, config = helpers.SetupTest( + "nasa-scientist-index.xml", configPath, l10nPath, helpers.Silent, + ) + + ctrl = gomock.NewController(GinkgoT()) + mockViperConfig = cmocks.NewMockViperConfig(ctrl) + mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) + mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) + mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) + helpers.DoMockReadInConfig(mockViperConfig) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) + }) - if err != nil { - Fail(err.Error()) - } + AfterEach(func() { + ctrl.Finish() }) DescribeTable("profile", @@ -117,7 +128,7 @@ var _ = Describe("Config", Ordered, func() { _ = actual Expect(1).To(Equal(1)) - expectValidShrinkCmdInvocation(nfs, entry) + expectValidShrinkCmdInvocation(vfs, entry, config) } else { actual := entry.actual(entry) entry.assert(entry, actual) diff --git a/src/app/proxy/controller-sampler_test.go b/src/app/proxy/controller-sampler_test.go index e57ed52..db5343e 100644 --- a/src/app/proxy/controller-sampler_test.go +++ b/src/app/proxy/controller-sampler_test.go @@ -27,6 +27,7 @@ var ( _ proxy.SamplerConfig = &proxy.MsSamplerConfig{} _ proxy.ProfilesConfigReader = &command.MsProfilesConfigReader{} _ proxy.SamplerConfigReader = &command.MsSamplerConfigReader{} + _ proxy.AdvancedConfigReader = &command.MsAdvancedConfigReader{} ) type controllerTE struct { @@ -60,6 +61,7 @@ var _ = Describe("SamplerController", Ordered, func() { mockProfilesReader *mocks.MockProfilesConfigReader mockSchemesReader *mocks.MockSchemesConfigReader mockSamplerReader *mocks.MockSamplerConfigReader + mockAdvancedReader *mocks.MockAdvancedConfigReader mockViperConfig *cmocks.MockViperConfig ) @@ -79,6 +81,7 @@ var _ = Describe("SamplerController", Ordered, func() { mockProfilesReader = mocks.NewMockProfilesConfigReader(ctrl) mockSchemesReader = mocks.NewMockSchemesConfigReader(ctrl) mockSamplerReader = mocks.NewMockSamplerConfigReader(ctrl) + mockAdvancedReader = mocks.NewMockAdvancedConfigReader(ctrl) helpers.DoMockReadInConfig(mockViperConfig) }) @@ -88,7 +91,9 @@ var _ = Describe("SamplerController", Ordered, func() { DescribeTable("sampler", func(entry *samplerTE) { - helpers.DoMockConfigs(config, mockProfilesReader, mockSchemesReader, mockSamplerReader) + helpers.DoMockConfigs(config, + mockProfilesReader, mockSchemesReader, mockSamplerReader, mockAdvancedReader, + ) directory := helpers.Path(root, entry.relative) options := []string{ @@ -124,6 +129,7 @@ var _ = Describe("SamplerController", Ordered, func() { Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, + Advanced: mockAdvancedReader, } }), } diff --git a/src/app/proxy/enter-shrink.go b/src/app/proxy/enter-shrink.go index f5c3deb..654185e 100644 --- a/src/app/proxy/enter-shrink.go +++ b/src/app/proxy/enter-shrink.go @@ -174,6 +174,13 @@ func (e *ShrinkEntry) createFinder() *PathFinder { finder.Trash = e.Inputs.ParamSet.Native.TrashPath } + finder.statics = &staticInfo{ + adhoc: e.AdvancedCFG.AdhocLabel(), + journal: e.AdvancedCFG.JournalLabel(), + legacy: e.AdvancedCFG.LegacyLabel(), + trash: e.AdvancedCFG.TrashLabel(), + } + return finder } @@ -202,6 +209,7 @@ func (e *ShrinkEntry) ConfigureOptions(o *nav.TraverseOptions) { profiles: e.ProfilesCFG, schemes: e.SchemesCFG, sampler: e.SamplerCFG, + advanced: e.AdvancedCFG, Inputs: e.Inputs, finder: finder, fileManager: e.FileManager, @@ -289,6 +297,7 @@ func EnterShrink( profilesCFG ProfilesConfig, schemesCFG SchemesConfig, samplerCFG SamplerConfig, + advancedCFG AdvancedConfig, vfs storage.VirtualFS, ) error { fmt.Printf("---> ๐Ÿ”Š๐Ÿ”Š Directory: '%v'\n", inputs.Root.ParamSet.Native.Directory) @@ -301,6 +310,7 @@ func EnterShrink( ProfilesCFG: profilesCFG, SchemesCFG: schemesCFG, SamplerCFG: samplerCFG, + AdvancedCFG: advancedCFG, Vfs: vfs, }, Inputs: inputs, diff --git a/src/app/proxy/entry-base.go b/src/app/proxy/entry-base.go index a8ff4b4..9eb41a6 100644 --- a/src/app/proxy/entry-base.go +++ b/src/app/proxy/entry-base.go @@ -44,6 +44,7 @@ type EntryBase struct { ProfilesCFG ProfilesConfig SchemesCFG SchemesConfig SamplerCFG SamplerConfig + AdvancedCFG AdvancedConfig Vfs storage.VirtualFS FileManager *FileManager } diff --git a/src/app/proxy/path-finder.go b/src/app/proxy/path-finder.go index a97593a..ba73ad3 100644 --- a/src/app/proxy/path-finder.go +++ b/src/app/proxy/path-finder.go @@ -188,6 +188,13 @@ so we have 3 parameters: * --output --trash [ous=eject; des=eject] */ +type staticInfo struct { + adhoc string + journal string + legacy string + trash string +} + // PathFinder provides the common paths required, but its the controller that know // the specific paths based around this common framework @@ -219,11 +226,8 @@ type PathFinder struct { arity int transparentInput bool -} -type staticInfo struct { - trashLabel string - legacyLabel string + statics *staticInfo } func (f *PathFinder) JournalFile(item *nav.TraverseItem) string { diff --git a/src/app/proxy/proxy-defs.go b/src/app/proxy/proxy-defs.go index d432839..dbd47cd 100644 --- a/src/app/proxy/proxy-defs.go +++ b/src/app/proxy/proxy-defs.go @@ -10,6 +10,7 @@ type SharedControllerInfo struct { profiles ProfilesConfig schemes SchemesConfig sampler SamplerConfig + advanced AdvancedConfig Inputs *ShrinkCommandInputs finder *PathFinder fileManager *FileManager diff --git a/src/internal/helpers/mock-config-data.go b/src/internal/helpers/mock-config-data.go index 6a6a242..404e435 100644 --- a/src/internal/helpers/mock-config-data.go +++ b/src/internal/helpers/mock-config-data.go @@ -5,6 +5,12 @@ import ( "github.com/snivilised/pixa/src/app/proxy" ) +const ( + noSampleFiles = 2 + noSampleFolders = 1 + noRetries = 2 +) + var ( BackyardWorldsPlanet9Scan01First2 []string BackyardWorldsPlanet9Scan01First4 []string @@ -15,6 +21,7 @@ var ( ProfilesConfigData proxy.ProfilesConfigMap SchemesConfigData *proxy.MsSchemesConfig SamplerConfigData *proxy.MsSamplerConfig + AdvancedConfigData *proxy.MsAdvancedConfig ) func init() { @@ -84,7 +91,19 @@ func init() { } SamplerConfigData = &proxy.MsSamplerConfig{ - Files: 2, //nolint:gomnd // not magic - Folders: 1, + Files: noSampleFiles, + Folders: noSampleFolders, + } + + AdvancedConfigData = &proxy.MsAdvancedConfig{ + Abort: false, + Timeout: "10s", + NoProgramRetries: noRetries, + Labels: proxy.MsLabelsConfig{ + Adhoc: "ADHOC", + Journal: ".journal.txt", + Legacy: ".LEGACY", + Trash: "TRASH", + }, } } diff --git a/src/internal/helpers/test-utils.go b/src/internal/helpers/test-utils.go index 2017a1e..2733e19 100644 --- a/src/internal/helpers/test-utils.go +++ b/src/internal/helpers/test-utils.go @@ -171,10 +171,12 @@ func DoMockConfigs( profilesReader *mocks.MockProfilesConfigReader, schemesReader *mocks.MockSchemesConfigReader, samplerReader *mocks.MockSamplerConfigReader, + mockAdvancedReader *mocks.MockAdvancedConfigReader, ) { DoMockProfilesConfigsWith(ProfilesConfigData, config, profilesReader) DoMockSchemesConfigWith(SchemesConfigData, config, schemesReader) DoMockSamplerConfigWith(SamplerConfigData, config, samplerReader) + DoMockAdvancedConfigWith(AdvancedConfigData, config, mockAdvancedReader) } func DoMockReadInConfig(config *cmocks.MockViperConfig) { @@ -229,6 +231,20 @@ func DoMockSamplerConfigWith( ).AnyTimes() } +func DoMockAdvancedConfigWith( + data *proxy.MsAdvancedConfig, + config configuration.ViperConfig, + reader *mocks.MockAdvancedConfigReader, +) { + reader.EXPECT().Read(config).DoAndReturn( + func(viper configuration.ViperConfig) (proxy.AdvancedConfig, error) { + stub := data + + return stub, nil + }, + ).AnyTimes() +} + func ResetFS(index string, silent bool) (vfs storage.VirtualFS, root string) { vfs = storage.UseMemFS() root = Scientist(vfs, index, silent) diff --git a/test/data/configuration/pixa-test.yml b/test/data/configuration/pixa-test.yml index 110eb9e..6a6fb62 100644 --- a/test/data/configuration/pixa-test.yml +++ b/test/data/configuration/pixa-test.yml @@ -26,9 +26,10 @@ sampler: folders: 1 advanced: abort-on-error: false - external-program-execution-timeout: "20s" - external-program-execution-retry: 0 - legacy-file-dot-suffix: LEGACY - journal-file-dot-suffix: JOURNAL - trash-label: TRASH - shrink-label: SHRINK + program-timeout: "20s" + no-program-retries: 0 + labels: + adhoc: ADHOC + legacy: .LEGACY + journal-suffix: .journal.txt + trash: TRASH