fix: make miro testdir resolution explicit based on config
This commit is contained in:
+33
-56
@@ -11,52 +11,50 @@ import (
|
||||
miroconfig "miro/internal/config"
|
||||
)
|
||||
|
||||
var fallbackTestDirs = []string{
|
||||
"e2e",
|
||||
filepath.Join("test", "e2e"),
|
||||
filepath.Join("tests", "e2e"),
|
||||
"miro",
|
||||
filepath.Join("test", "miro"),
|
||||
filepath.Join("tests", "miro"),
|
||||
// ResolveTestDir resolves the project test directory from config.
|
||||
func ResolveTestDir() (string, error) {
|
||||
root, err := currentProjectRoot()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return resolveTestDirFromRoot(root)
|
||||
}
|
||||
|
||||
// ResolveTestDir resolves the project test directory from config or conventions.
|
||||
func ResolveTestDir() (string, error) {
|
||||
func resolveTestDirFromRoot(root string) (string, error) {
|
||||
configPath := filepath.Join(root, "miro.toml")
|
||||
cfg, err := miroconfig.ReadConfig(configPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
testDir := filepath.Join(root, cfg.TestDir)
|
||||
info, err := os.Stat(testDir)
|
||||
if err == nil {
|
||||
if !info.IsDir() {
|
||||
return "", fmt.Errorf("configured test_dir is not a directory: %s", testDir)
|
||||
}
|
||||
return testDir, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return "", fmt.Errorf("failed to check test directory %s: %v", testDir, err)
|
||||
}
|
||||
|
||||
return testDir, nil
|
||||
}
|
||||
|
||||
func currentProjectRoot() (string, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get working directory: %v", err)
|
||||
}
|
||||
|
||||
root := projectRoot(cwd)
|
||||
return resolveTestDirFromRoot(root)
|
||||
}
|
||||
|
||||
func resolveTestDirFromRoot(root string) (string, error) {
|
||||
candidates, err := testDirCandidates(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, candidate := range candidates {
|
||||
info, err := os.Stat(candidate)
|
||||
if err == nil && info.IsDir() {
|
||||
return candidate, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return "", fmt.Errorf("failed to check test directory %s: %v", candidate, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(candidates) == 0 {
|
||||
return "", errors.New("no test directory candidates available")
|
||||
}
|
||||
|
||||
return candidates[0], nil
|
||||
return projectRoot(cwd), nil
|
||||
}
|
||||
|
||||
// returns the git root if in a git repo else current pwd
|
||||
func projectRoot(cwd string) string {
|
||||
out, err := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
|
||||
out, err := exec.Command("git", "-C", cwd, "rev-parse", "--show-toplevel").CombinedOutput()
|
||||
if err != nil {
|
||||
return cwd
|
||||
}
|
||||
@@ -67,24 +65,3 @@ func projectRoot(cwd string) string {
|
||||
}
|
||||
return filepath.Clean(root)
|
||||
}
|
||||
|
||||
// list of e2e test directory candidates. uses from config if it exists
|
||||
// else uses fallback paths
|
||||
func testDirCandidates(root string) ([]string, error) {
|
||||
configPath := filepath.Join(root, "miro.toml")
|
||||
|
||||
cfg, err := miroconfig.ReadConfig(configPath)
|
||||
if err == nil && cfg.TestDir != "" {
|
||||
return []string{filepath.Join(root, cfg.TestDir)}, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
candidates := make([]string, 0, len(fallbackTestDirs))
|
||||
for _, candidate := range fallbackTestDirs {
|
||||
candidates = append(candidates, filepath.Join(root, candidate))
|
||||
}
|
||||
|
||||
return candidates, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user