diff --git a/assets/config-invalid.toml b/assets/config-invalid.toml new file mode 100644 index 0000000..8c1b662 --- /dev/null +++ b/assets/config-invalid.toml @@ -0,0 +1 @@ +[[chains]] diff --git a/cmd/missed-blocks-checker.go b/cmd/missed-blocks-checker.go index 0420c65..c1b8b41 100644 --- a/cmd/missed-blocks-checker.go +++ b/cmd/missed-blocks-checker.go @@ -1,46 +1,33 @@ package main import ( + "github.com/spf13/cobra" "main/pkg" configPkg "main/pkg/config" "main/pkg/fs" "main/pkg/logger" - "os" - - "github.com/spf13/cobra" ) var ( version = "unknown" ) -type OsFS struct { -} - -func (fs *OsFS) ReadFile(name string) ([]byte, error) { - return os.ReadFile(name) -} - -func (fs *OsFS) Create(path string) (fs.File, error) { - return os.Create(path) -} - func ExecuteMain(configPath string) { - filesystem := &OsFS{} + filesystem := &fs.OsFS{} app := pkg.NewApp(configPath, filesystem, version) app.Start() } func ExecuteValidateConfig(configPath string) { - filesystem := &OsFS{} + filesystem := &fs.OsFS{} config, err := configPkg.GetConfig(configPath, filesystem) if err != nil { - logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config!") + logger.GetDefaultLogger().Panic().Err(err).Msg("Could not load config!") } if err := config.Validate(); err != nil { - logger.GetDefaultLogger().Fatal().Err(err).Msg("Config is invalid!") + logger.GetDefaultLogger().Panic().Err(err).Msg("Config is invalid!") } logger.GetDefaultLogger().Info().Msg("Provided config is valid.") @@ -68,18 +55,14 @@ func main() { } rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path") - if err := rootCmd.MarkPersistentFlagRequired("config"); err != nil { - logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set flag as required") - } + _ = rootCmd.MarkPersistentFlagRequired("config") validateConfigCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path") - if err := validateConfigCmd.MarkPersistentFlagRequired("config"); err != nil { - logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not set flag as required") - } + _ = validateConfigCmd.MarkPersistentFlagRequired("config") rootCmd.AddCommand(validateConfigCmd) if err := rootCmd.Execute(); err != nil { - logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not start application") + logger.GetDefaultLogger().Panic().Err(err).Msg("Could not start application") } } diff --git a/cmd/missed-blocks-checker_test.go b/cmd/missed-blocks-checker_test.go new file mode 100644 index 0000000..62fb7bb --- /dev/null +++ b/cmd/missed-blocks-checker_test.go @@ -0,0 +1,81 @@ +package main + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +//nolint:paralleltest // disabled +func TestValidateConfigNoConfigProvided(t *testing.T) { + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + os.Args = []string{"cmd", "validate-config"} + main() + assert.True(t, true) +} + +//nolint:paralleltest // disabled +func TestValidateConfigFailedToLoad(t *testing.T) { + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-not-found.toml"} + main() + assert.True(t, true) +} + +//nolint:paralleltest // disabled +func TestValidateConfigInvalid(t *testing.T) { + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-invalid.toml"} + main() + assert.True(t, true) +} + +//nolint:paralleltest // disabled +func TestValidateConfigValid(t *testing.T) { + os.Args = []string{"cmd", "validate-config", "--config", "../assets/valid.toml"} + main() + assert.True(t, true) +} + +//nolint:paralleltest // disabled +func TestStartNoConfigProvided(t *testing.T) { + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + os.Args = []string{"cmd"} + main() + assert.True(t, true) +} + +//nolint:paralleltest // disabled +func TestStartConfigProvided(t *testing.T) { + defer func() { + if r := recover(); r == nil { + require.Fail(t, "Expected to have a panic here!") + } + }() + + os.Args = []string{"cmd", "--config", "../assets/config-invalid.toml"} + main() + assert.True(t, true) +} diff --git a/pkg/app.go b/pkg/app.go index 9863a3c..bdc42bb 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -23,11 +23,11 @@ type App struct { func NewApp(configPath string, filesystem fs.FS, version string) *App { config, err := configPkg.GetConfig(configPath, filesystem) if err != nil { - loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config") + loggerPkg.GetDefaultLogger().Panic().Err(err).Msg("Could not load config") } if err = config.Validate(); err != nil { - loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Provided config is invalid!") + loggerPkg.GetDefaultLogger().Panic().Err(err).Msg("Provided config is invalid!") } for _, chainConfig := range config.ChainConfigs { diff --git a/pkg/fs/os_fs.go b/pkg/fs/os_fs.go new file mode 100644 index 0000000..59e46ca --- /dev/null +++ b/pkg/fs/os_fs.go @@ -0,0 +1,14 @@ +package fs + +import "os" + +type OsFS struct { +} + +func (fs *OsFS) ReadFile(name string) ([]byte, error) { + return os.ReadFile(name) +} + +func (fs *OsFS) Create(path string) (File, error) { + return os.Create(path) +} diff --git a/pkg/fs/os_fs_test.go b/pkg/fs/os_fs_test.go new file mode 100644 index 0000000..dfc478f --- /dev/null +++ b/pkg/fs/os_fs_test.go @@ -0,0 +1,15 @@ +package fs + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOsFsCreate(t *testing.T) { + t.Parallel() + + fs := &OsFS{} + _, err := fs.Create("/tmp/file.txt") + require.NoError(t, err) +}