package testing_test

import (
	"io/fs"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)

func repoRoot(t *testing.T) string {
	t.Helper()
	_, file, _, ok := runtime.Caller(0)
	if !ok {
		t.Fatal("could not resolve testing module location")
	}
	return filepath.Clean(filepath.Join(filepath.Dir(file), ".."))
}

func walkSourceFiles(t *testing.T, root string, fn func(path string, info fs.DirEntry) error) {
	t.Helper()
	walkErr := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			switch d.Name() {
			case ".git", ".venv", ".venv-ci", "build", "tmp", "artifacts", ".pytest_cache", ".ruff_cache":
				return filepath.SkipDir
			}
		}
		return fn(path, d)
	})
	if walkErr != nil {
		t.Fatal(walkErr)
	}
}

func readFile(t *testing.T, path string) []byte {
	t.Helper()
	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	return data
}
