Skip to content

Commit

Permalink
feat: add cmd test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 21, 2024
1 parent ffe37a4 commit d1b1942
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 37 deletions.
40 changes: 7 additions & 33 deletions cmd/cosmos-transactions-bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
configPkg "main/pkg/config"
"main/pkg/fs"
loggerPkg "main/pkg/logger"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -14,39 +13,18 @@ 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{}
config, err := configPkg.GetConfig(configPath, filesystem)
if err != nil {
loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
}
warnings := config.DisplayWarnings()

for _, warning := range warnings {
warning.Log(loggerPkg.GetDefaultLogger())
}

app := pkg.NewApp(config, version, filesystem)
filesystem := &fs.OsFS{}
app := pkg.NewApp(filesystem, configPath, version)
app.Start()
}

func ExecuteValidateConfig(configPath string) {
filesystem := &OsFS{}
filesystem := &fs.OsFS{}

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 warnings := config.DisplayWarnings(); len(warnings) > 0 {
Expand Down Expand Up @@ -80,18 +58,14 @@ func main() {
}

rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path")
if err := rootCmd.MarkPersistentFlagRequired("config"); err != nil {
loggerPkg.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 {
loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not set flag as required")
}
_ = validateConfigCmd.MarkPersistentFlagRequired("config")

rootCmd.AddCommand(validateConfigCmd)

if err := rootCmd.Execute(); err != nil {
loggerPkg.GetDefaultLogger().Fatal().Err(err).Msg("Could not start application")
loggerPkg.GetDefaultLogger().Panic().Err(err).Msg("Could not start application")
}
}
88 changes: 88 additions & 0 deletions cmd/cosmos-transactions-bot_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/invalid-timezone.toml"}
main()
assert.True(t, true)
}

//nolint:paralleltest // disabled
func TestValidateConfigWithWarnings(t *testing.T) {
os.Args = []string{"cmd", "validate-config", "--config", "../assets/valid-unused-chain.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/invalid-timezone.toml"}
main()
assert.True(t, true)
}
18 changes: 14 additions & 4 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"syscall"

"main/pkg/alias_manager"
"main/pkg/config"
configPkg "main/pkg/config"
"main/pkg/data_fetcher"
filtererPkg "main/pkg/filterer"
loggerPkg "main/pkg/logger"
Expand All @@ -21,7 +21,7 @@ import (

type App struct {
Logger zerolog.Logger
Config *config.AppConfig
Config *configPkg.AppConfig
Chains []*configTypes.Chain
NodesManager *nodesManagerPkg.NodesManager
Reporters reportersPkg.Reporters
Expand All @@ -32,9 +32,19 @@ type App struct {
Version string
}

func NewApp(config *config.AppConfig, version string, fs fsPkg.FS) *App {
func NewApp(filesystem fsPkg.FS, configPath string, version string) *App {
config, err := configPkg.GetConfig(configPath, filesystem)
if err != nil {
loggerPkg.GetDefaultLogger().Panic().Err(err).Msg("Could not load config")
}
warnings := config.DisplayWarnings()

Check warning on line 40 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L40

Added line #L40 was not covered by tests

for _, warning := range warnings {
warning.Log(loggerPkg.GetDefaultLogger())

Check warning on line 43 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}

logger := loggerPkg.GetLogger(config.LogConfig)
aliasManager := alias_manager.NewAliasManager(logger, config, fs)
aliasManager := alias_manager.NewAliasManager(logger, config, filesystem)

Check warning on line 47 in pkg/app.go

View check run for this annotation

Codecov / codecov/patch

pkg/app.go#L47

Added line #L47 was not covered by tests
aliasManager.Load()

metricsManager := metricsPkg.NewManager(logger, config.Metrics)
Expand Down
14 changes: 14 additions & 0 deletions pkg/fs/os_fs.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions pkg/fs/os_fs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d1b1942

Please sign in to comment.