refactor: move testfixtures to internal package and update imports
- Relocated testfixtures from the root to the internal package for better encapsulation. - Updated import paths in codec test files across env, ini, json, yaml directories to reflect the new location. - Removed the now obsolete testfixtures.go file.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// Package testfixtures resolves paths to files under the module's fixtures/ directory.
|
||||
package testfixtures
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func moduleRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("runtime.Caller")
|
||||
}
|
||||
dir := filepath.Dir(file)
|
||||
for {
|
||||
data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
|
||||
if err == nil && strings.Contains(string(data), "module github.com/eslider/go-config") {
|
||||
return dir
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
t.Fatal("module root not found")
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
// Path joins repo fixtures/ with parts.
|
||||
func Path(t *testing.T, parts ...string) string {
|
||||
t.Helper()
|
||||
return filepath.Join(append([]string{moduleRoot(t), "fixtures"}, parts...)...)
|
||||
}
|
||||
|
||||
// Load reads a fixture file relative to fixtures/.
|
||||
func Load(t *testing.T, parts ...string) []byte {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(Path(t, parts...))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user