mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 07:26:50 +00:00
Test: Internal exchange coverage (#1525)
* Test: Internal exchange coverage * Tests: Rename exchange.TestInstance to Setup This package function is either going to be imported and used as just exchange.Setup, or more likely testexch.Setup. This removes the Stutter of having internal/testing/exchange.TestInstance which is implicit given the package path
This commit is contained in:
35
internal/testing/utils/path.go
Normal file
35
internal/testing/utils/path.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package path
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Exported public errors
|
||||
var (
|
||||
ErrRootNotFound = errors.New("could not find root of gocryptotrader")
|
||||
)
|
||||
|
||||
// RootPathFromCWD returns the system path to GoCryptoTrader from the current working directory
|
||||
// Expects to find LICENSE file
|
||||
func RootPathFromCWD() (string, error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return RootPath(wd)
|
||||
}
|
||||
|
||||
// RootPath returns the system path to GoCryptoTrader from a sub-directory path
|
||||
func RootPath(p string) (string, error) {
|
||||
parts := strings.Split(p, string(filepath.Separator))
|
||||
for i := len(parts); i > 0; i-- {
|
||||
dir := strings.Join(parts[:i], string(filepath.Separator))
|
||||
if _, err := os.Stat(filepath.Join(dir, "LICENSE")); err == nil {
|
||||
return dir, nil
|
||||
}
|
||||
}
|
||||
return "", ErrRootNotFound
|
||||
}
|
||||
38
internal/testing/utils/path_test.go
Normal file
38
internal/testing/utils/path_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package path
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestRootPathFromCWD exercises RootPathFromCWD and RootPath
|
||||
func TestRootPathFromCWD(t *testing.T) {
|
||||
r, err := RootPathFromCWD()
|
||||
require.NoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(r, "LICENSE"))
|
||||
require.NoError(t, err, "Must find a LICENSE file")
|
||||
|
||||
// Ensure there are no other license files in sub-directories
|
||||
err = filepath.WalkDir(r, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Skip the root of the project
|
||||
w, _ := filepath.Split(p)
|
||||
w = filepath.Clean(w)
|
||||
if w == r {
|
||||
return nil
|
||||
}
|
||||
if d.Type().IsRegular() && d.Name() == "LICENSE" {
|
||||
return fmt.Errorf("found an unexpected LICENSE file in a sub-directory: %s", p)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user