-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'test-common-package' into refactor-common-package-rename
- Loading branch information
Showing
53 changed files
with
2,794 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/spf13/afero" | ||
) | ||
|
||
var AppFs = afero.NewOsFs() | ||
|
||
const ( | ||
FlagConfig = "config" | ||
defaultCfgFileName = "zetatool_config.json" | ||
ZetaURL = "127.0.0.1:1317" | ||
BtcExplorerURL = "https://blockstream.info/api/" | ||
EthRPCURL = "https://ethereum-rpc.publicnode.com" | ||
ConnectorAddress = "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a" | ||
CustodyAddress = "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10" | ||
) | ||
|
||
// Config is a struct the defines the configuration fields used by zetatool | ||
type Config struct { | ||
ZetaURL string | ||
BtcExplorerURL string | ||
EthRPCURL string | ||
EtherscanAPIkey string | ||
ConnectorAddress string | ||
CustodyAddress string | ||
} | ||
|
||
func DefaultConfig() *Config { | ||
return &Config{ | ||
ZetaURL: ZetaURL, | ||
BtcExplorerURL: BtcExplorerURL, | ||
EthRPCURL: EthRPCURL, | ||
ConnectorAddress: ConnectorAddress, | ||
CustodyAddress: CustodyAddress, | ||
} | ||
} | ||
|
||
func (c *Config) Save() error { | ||
file, err := json.MarshalIndent(c, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
err = afero.WriteFile(AppFs, defaultCfgFileName, file, 0600) | ||
return err | ||
} | ||
|
||
func (c *Config) Read(filename string) error { | ||
data, err := afero.ReadFile(AppFs, filename) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(data, c) | ||
return err | ||
} | ||
|
||
func GetConfig(filename string) (*Config, error) { | ||
//Check if cfgFile is empty, if so return default Config and save to file | ||
if filename == "" { | ||
cfg := DefaultConfig() | ||
err := cfg.Save() | ||
return cfg, err | ||
} | ||
|
||
//if file is specified, open file and return struct | ||
cfg := &Config{} | ||
err := cfg.Read(filename) | ||
return cfg, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDefaultConfig(t *testing.T) { | ||
cfg := DefaultConfig() | ||
require.Equal(t, cfg.EthRPCURL, EthRPCURL) | ||
require.Equal(t, cfg.ZetaURL, ZetaURL) | ||
require.Equal(t, cfg.BtcExplorerURL, BtcExplorerURL) | ||
require.Equal(t, cfg.ConnectorAddress, ConnectorAddress) | ||
require.Equal(t, cfg.CustodyAddress, CustodyAddress) | ||
} | ||
|
||
func TestGetConfig(t *testing.T) { | ||
AppFs = afero.NewMemMapFs() | ||
defaultCfg := DefaultConfig() | ||
|
||
t.Run("No config file specified", func(t *testing.T) { | ||
cfg, err := GetConfig("") | ||
require.NoError(t, err) | ||
require.Equal(t, cfg, defaultCfg) | ||
|
||
exists, err := afero.Exists(AppFs, defaultCfgFileName) | ||
require.NoError(t, err) | ||
require.True(t, exists) | ||
}) | ||
|
||
t.Run("config file specified", func(t *testing.T) { | ||
cfg, err := GetConfig(defaultCfgFileName) | ||
require.NoError(t, err) | ||
require.Equal(t, cfg, defaultCfg) | ||
}) | ||
} | ||
|
||
func TestConfig_Read(t *testing.T) { | ||
AppFs = afero.NewMemMapFs() | ||
cfg, err := GetConfig("") | ||
require.NoError(t, err) | ||
|
||
t.Run("read existing file", func(t *testing.T) { | ||
c := &Config{} | ||
err := c.Read(defaultCfgFileName) | ||
require.NoError(t, err) | ||
require.Equal(t, c, cfg) | ||
}) | ||
|
||
t.Run("read non-existent file", func(t *testing.T) { | ||
err := AppFs.Remove(defaultCfgFileName) | ||
require.NoError(t, err) | ||
c := &Config{} | ||
err = c.Read(defaultCfgFileName) | ||
require.ErrorContains(t, err, "file does not exist") | ||
require.NotEqual(t, c, cfg) | ||
}) | ||
} | ||
|
||
func TestConfig_Save(t *testing.T) { | ||
AppFs = afero.NewMemMapFs() | ||
cfg := DefaultConfig() | ||
cfg.EtherscanAPIkey = "DIFFERENTAPIKEY" | ||
|
||
t.Run("save modified cfg", func(t *testing.T) { | ||
err := cfg.Save() | ||
require.NoError(t, err) | ||
|
||
newCfg, err := GetConfig(defaultCfgFileName) | ||
require.NoError(t, err) | ||
require.Equal(t, cfg, newCfg) | ||
}) | ||
|
||
// Should test invalid json encoding but currently not able to without interface | ||
} |
Oops, something went wrong.