Skip to content

Commit

Permalink
exchange_template: Fix test regression (thrasher-corp#1128)
Browse files Browse the repository at this point in the history
* exchange_template: Fix test regression

* Add coverage for saveConfig, fix test error

* Swap
  • Loading branch information
thrasher- authored Jan 31, 2023
1 parent 82c79a9 commit b384991
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
23 changes: 12 additions & 11 deletions cmd/exchange_template/exchange_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 24 additions & 8 deletions cmd/exchange_template/exchange_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/config"
)

Expand Down Expand Up @@ -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)
}
}

0 comments on commit b384991

Please sign in to comment.