mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 15:10:03 +00:00
exchange_template: Fix test regression (#1128)
* exchange_template: Fix test regression * Add coverage for saveConfig, fix test error * Swap
This commit is contained in:
@@ -232,24 +232,25 @@ func makeExchange(exchangeDirectory string, configTestFile *config.Config, exch
|
||||
}
|
||||
|
||||
func saveConfig(exchangeDirectory string, configTestFile *config.Config, newExchConfig *config.Exchange) error {
|
||||
cmd := exec.Command("go", "fmt")
|
||||
cmd.Dir = exchangeDirectory
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to go fmt. output: %s err: %s", out, err)
|
||||
}
|
||||
|
||||
configTestFile.Exchanges = append(configTestFile.Exchanges, *newExchConfig)
|
||||
err = configTestFile.SaveConfigToFile(exchangeConfigPath)
|
||||
if err != nil {
|
||||
if err := runCommand(exchangeDirectory, "fmt"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd = exec.Command("go", "test")
|
||||
cmd.Dir = exchangeDirectory
|
||||
out, err = cmd.Output()
|
||||
configTestFile.Exchanges = append(configTestFile.Exchanges, *newExchConfig)
|
||||
if err := configTestFile.SaveConfigToFile(exchangeConfigPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return runCommand(exchangeDirectory, "test")
|
||||
}
|
||||
|
||||
func runCommand(dir, param string) error {
|
||||
cmd := exec.Command("go", param)
|
||||
cmd.Dir = dir
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to go test. output: %s err: %s", out, err)
|
||||
return fmt.Errorf("unable to go %s stdout: %s stderr: %s",
|
||||
param, out, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/common/file"
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
)
|
||||
|
||||
@@ -44,23 +45,38 @@ func TestCheckExchangeName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewExchange(t *testing.T) {
|
||||
testExchangeName := "testexch"
|
||||
func TestNewExchangeAndSaveConfig(t *testing.T) {
|
||||
const testExchangeName = "testexch"
|
||||
testExchangeDir := filepath.Join(targetPath, testExchangeName)
|
||||
cfg := config.GetConfig()
|
||||
|
||||
_, err := makeExchange(
|
||||
t.Cleanup(func() {
|
||||
if err := os.RemoveAll(testExchangeDir); err != nil {
|
||||
t.Errorf("RemoveAll failed: %s, manual deletion of test directory required", err)
|
||||
}
|
||||
})
|
||||
|
||||
exchCfg, err := makeExchange(
|
||||
testExchangeDir,
|
||||
config.GetConfig(),
|
||||
cfg,
|
||||
&exchange{
|
||||
Name: testExchangeName,
|
||||
REST: true,
|
||||
WS: true,
|
||||
})
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(testExchangeDir); err != nil {
|
||||
t.Errorf("unable to remove dir: %s, manual removal required", err)
|
||||
cfgData, err := os.ReadFile(exchangeConfigPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = saveConfig(testExchangeDir, cfg, exchCfg); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = os.WriteFile(exchangeConfigPath, cfgData, file.DefaultPermissionOctal); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user