exchange_template: Fix test regression (#1128)

* exchange_template: Fix test regression

* Add coverage for saveConfig, fix test error

* Swap
This commit is contained in:
Adrian Gallagher
2023-02-01 09:31:19 +11:00
committed by GitHub
parent 82c79a9287
commit b384991183
2 changed files with 39 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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)
}
}