Skip to content

Commit

Permalink
chore: add cmd test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 28, 2024
1 parent 0ef0bb8 commit 4b960fa
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
14 changes: 5 additions & 9 deletions cmd/cosmos-validators-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func ExecuteValidateConfig(configPath string) {
filesystem := &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!")
}

warnings := config.DisplayWarnings()
Expand Down Expand Up @@ -73,18 +73,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")
}
}
88 changes: 88 additions & 0 deletions cmd/cosmos-validators-exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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 TestValidateConfigWithWarnings(t *testing.T) {
os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-with-warnings.toml"}
main()
assert.True(t, true)
}

//nolint:paralleltest // disabled
func TestValidateConfigValid(t *testing.T) {
os.Args = []string{"cmd", "validate-config", "--config", "../assets/config-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)
}

0 comments on commit 4b960fa

Please sign in to comment.