diff --git a/cmd/exchange_template/exchange_template.go b/cmd/exchange_template/exchange_template.go index 2c1750d2953..b429e0ab8ba 100644 --- a/cmd/exchange_template/exchange_template.go +++ b/cmd/exchange_template/exchange_template.go @@ -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) + if err := runCommand(exchangeDirectory, "fmt"); err != nil { + return err } configTestFile.Exchanges = append(configTestFile.Exchanges, *newExchConfig) - err = configTestFile.SaveConfigToFile(exchangeConfigPath) - if err != nil { + if err := configTestFile.SaveConfigToFile(exchangeConfigPath); err != nil { return err } - cmd = exec.Command("go", "test") - cmd.Dir = exchangeDirectory - out, err = cmd.Output() + 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 } diff --git a/cmd/exchange_template/exchange_template_test.go b/cmd/exchange_template/exchange_template_test.go index 40b8b66dc25..61b8fd785e2 100644 --- a/cmd/exchange_template/exchange_template_test.go +++ b/cmd/exchange_template/exchange_template_test.go @@ -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) } }