mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-19 15:10:05 +00:00
* Add exchange manager to engine
* Several improvements for engine and friends
1) New file.Exists func
2) gRPC TLS cert expiration date check and regeneration
3) New donation var for use across the codebase
4) Use Go log package until the logger is initialised
* Add cert tests and create dir tree if it doesn't exist for file.Write
* Link up donation address to documentation tool plus minor adjustments
* Fix remaining donation addrs
* Move non-needed reload exchange funcs
* Revert accidental config_example.json changes 🕯️
* Use go logger for logging until the logger has initiliased, otherwise no output will be seen
* Link up portfolio delay val and other fixes
* Run go mod tidy after dependabot PR
* Address nitterinos
128 lines
3.0 KiB
Go
128 lines
3.0 KiB
Go
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestWrite(t *testing.T) {
|
|
tester := func(in string) error {
|
|
err := Write(in, []byte("GoCryptoTrader"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Remove(in)
|
|
}
|
|
|
|
type testTable struct {
|
|
InFile string
|
|
ErrExpected bool
|
|
Cleanup bool
|
|
}
|
|
|
|
var tests []testTable
|
|
tempDir := filepath.Join(os.TempDir(), "gct-temp")
|
|
testFile := filepath.Join(tempDir, "gcttest.txt")
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
tests = []testTable{
|
|
{InFile: "*", ErrExpected: true},
|
|
{InFile: testFile, ErrExpected: false},
|
|
}
|
|
default:
|
|
tests = []testTable{
|
|
{InFile: "", ErrExpected: true},
|
|
{InFile: testFile, ErrExpected: false},
|
|
}
|
|
}
|
|
|
|
for x := range tests {
|
|
err := tester(tests[x].InFile)
|
|
if err != nil && !tests[x].ErrExpected {
|
|
t.Errorf("Test %d failed, unexpected err %s\n", x, err)
|
|
}
|
|
}
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
|
t.Errorf("unable to remove temp test dir %s, manual deletion required", tempDir)
|
|
}
|
|
}
|
|
|
|
func TestMove(t *testing.T) {
|
|
tester := func(in, out string, write bool) error {
|
|
if write {
|
|
if err := ioutil.WriteFile(in, []byte("GoCryptoTrader"), 0770); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := Move(in, out); err != nil {
|
|
return err
|
|
}
|
|
|
|
contents, err := ioutil.ReadFile(out)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !strings.Contains(string(contents), "GoCryptoTrader") {
|
|
return fmt.Errorf("unable to find previously written data")
|
|
}
|
|
|
|
return os.Remove(out)
|
|
}
|
|
|
|
type testTable struct {
|
|
InFile string
|
|
OutFile string
|
|
Write bool
|
|
ErrExpected bool
|
|
}
|
|
|
|
var tests []testTable
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
tests = []testTable{
|
|
{InFile: "*", OutFile: "gct.txt", Write: true, ErrExpected: true},
|
|
{InFile: "*", OutFile: "gct.txt", Write: false, ErrExpected: true},
|
|
{InFile: "in.txt", OutFile: "*", Write: true, ErrExpected: true},
|
|
{InFile: "in.txt", OutFile: "gct.txt", Write: true, ErrExpected: false},
|
|
}
|
|
default:
|
|
tests = []testTable{
|
|
{InFile: "", OutFile: "gct.txt", Write: true, ErrExpected: true},
|
|
{InFile: "", OutFile: "gct.txt", Write: false, ErrExpected: true},
|
|
{InFile: "in.txt", OutFile: "", Write: true, ErrExpected: true},
|
|
{InFile: "in.txt", OutFile: "gct.txt", Write: true, ErrExpected: false},
|
|
}
|
|
}
|
|
|
|
for x := range tests {
|
|
err := tester(tests[x].InFile, tests[x].OutFile, tests[x].Write)
|
|
if err != nil && !tests[x].ErrExpected {
|
|
t.Errorf("Test %d failed, unexpected err %s\n", x, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExists(t *testing.T) {
|
|
if e := Exists("non-existent"); e {
|
|
t.Error("non-existent file should not exist")
|
|
}
|
|
tmpFile := filepath.Join(os.TempDir(), "gct-test.txt")
|
|
if err := ioutil.WriteFile(tmpFile, []byte("hello world"), os.ModeAppend); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := Exists(tmpFile); !e {
|
|
t.Error("file should exist")
|
|
}
|
|
if err := os.Remove(tmpFile); err != nil {
|
|
t.Errorf("unable to remove %s, manual deletion is required", tmpFile)
|
|
}
|
|
}
|