fix: make miro testdir resolution explicit based on config
This commit is contained in:
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
/verti/
|
/verti/
|
||||||
/AGENTS.md
|
/AGENTS.md
|
||||||
/build/
|
/build/
|
||||||
/e2e/
|
/e2e/
|
||||||
|
/miro.toml
|
||||||
+61
-16
@@ -32,19 +32,25 @@ func TestRunShowsHelpWhenNoArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunInit(t *testing.T) {
|
func TestRunInit(t *testing.T) {
|
||||||
addFakeRecordDependencies(t, "script", "bwrap", "bash")
|
root := t.TempDir()
|
||||||
|
|
||||||
stdout, stderr := captureOutput(t, func() {
|
withWorkingDir(t, root, func() {
|
||||||
if got := Run([]string{"init"}); got != 0 {
|
stdout, stderr := captureOutput(t, func() {
|
||||||
t.Fatalf("Run() code = %d, want %d", got, 0)
|
if got := Run([]string{"init"}); got != 0 {
|
||||||
|
t.Fatalf("Run() code = %d, want %d", got, 0)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if stdout != prefixed("Done initialising...\n") {
|
||||||
|
t.Fatalf("stdout = %q, want %q", stdout, prefixed("Done initialising...\n"))
|
||||||
|
}
|
||||||
|
if stderr != "" {
|
||||||
|
t.Fatalf("stderr = %q, want empty", stderr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if stdout != prefixed("Done initialising...\n") {
|
if got := mustReadFile(t, filepath.Join(root, "miro.toml")); got != "test_dir = \"e2e\"\n" {
|
||||||
t.Fatalf("stdout = %q, want %q", stdout, prefixed("Done initialising...\n"))
|
t.Fatalf("config = %q, want %q", got, "test_dir = \"e2e\"\n")
|
||||||
}
|
|
||||||
if stderr != "" {
|
|
||||||
t.Fatalf("stderr = %q, want empty", stderr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +62,7 @@ func TestRunRecord(t *testing.T) {
|
|||||||
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
||||||
t.Fatalf("MkdirAll() error = %v", err)
|
t.Fatalf("MkdirAll() error = %v", err)
|
||||||
}
|
}
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
|
|
||||||
withWorkingDir(t, root, func() {
|
withWorkingDir(t, root, func() {
|
||||||
withStdin(t, "y\n", func() {})
|
withStdin(t, "y\n", func() {})
|
||||||
@@ -89,6 +96,7 @@ func TestRunRecordWithExplicitTestDirPath(t *testing.T) {
|
|||||||
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
||||||
t.Fatalf("MkdirAll() error = %v", err)
|
t.Fatalf("MkdirAll() error = %v", err)
|
||||||
}
|
}
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
|
|
||||||
withWorkingDir(t, root, func() {
|
withWorkingDir(t, root, func() {
|
||||||
withStdin(t, "y\n", func() {})
|
withStdin(t, "y\n", func() {})
|
||||||
@@ -120,6 +128,7 @@ func TestRunRecordRejectsAbsolutePathOutsideTestDir(t *testing.T) {
|
|||||||
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
||||||
t.Fatalf("MkdirAll() error = %v", err)
|
t.Fatalf("MkdirAll() error = %v", err)
|
||||||
}
|
}
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
|
|
||||||
outside := filepath.Join(root, "outside", "spec")
|
outside := filepath.Join(root, "outside", "spec")
|
||||||
withWorkingDir(t, root, func() {
|
withWorkingDir(t, root, func() {
|
||||||
@@ -141,12 +150,8 @@ func TestRunRecordRejectsAbsolutePathOutsideTestDir(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunFailsWhenDependenciesMissing(t *testing.T) {
|
func TestRunInitFailsWhenDependenciesMissing(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
wantDir := filepath.Join(root, "e2e")
|
|
||||||
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
|
||||||
t.Fatalf("MkdirAll() error = %v", err)
|
|
||||||
}
|
|
||||||
t.Setenv("PATH", "")
|
t.Setenv("PATH", "")
|
||||||
|
|
||||||
withWorkingDir(t, root, func() {
|
withWorkingDir(t, root, func() {
|
||||||
@@ -162,8 +167,8 @@ func TestRunFailsWhenDependenciesMissing(t *testing.T) {
|
|||||||
if !strings.Contains(stderr, `required command "script" not found in PATH`) {
|
if !strings.Contains(stderr, `required command "script" not found in PATH`) {
|
||||||
t.Fatalf("stderr = %q, want missing dependency error", stderr)
|
t.Fatalf("stderr = %q, want missing dependency error", stderr)
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(filepath.Join(wantDir, "suite", "spec", "in")); !os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(root, "miro.toml")); !os.IsNotExist(err) {
|
||||||
t.Fatalf("Stat() error = %v, want not exists", err)
|
t.Fatalf("Stat(%q) error = %v, want not exists", filepath.Join(root, "miro.toml"), err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -174,6 +179,7 @@ func TestRunRecordFailsWhenDependencyMissing(t *testing.T) {
|
|||||||
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
if err := os.MkdirAll(wantDir, 0o755); err != nil {
|
||||||
t.Fatalf("MkdirAll() error = %v", err)
|
t.Fatalf("MkdirAll() error = %v", err)
|
||||||
}
|
}
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
@@ -209,6 +215,27 @@ func TestRunRecordFailsWhenDependencyMissing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunRecordMissingConfigFails(t *testing.T) {
|
||||||
|
addFakeRecordDependencies(t, "script", "bwrap", "bash")
|
||||||
|
|
||||||
|
root := t.TempDir()
|
||||||
|
|
||||||
|
withWorkingDir(t, root, func() {
|
||||||
|
stdout, stderr := captureOutput(t, func() {
|
||||||
|
if got := Run([]string{"record", "suite/spec"}); got != 1 {
|
||||||
|
t.Fatalf("Run() code = %d, want %d", got, 1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if stdout != "" {
|
||||||
|
t.Fatalf("stdout = %q, want empty", stdout)
|
||||||
|
}
|
||||||
|
if !strings.Contains(stderr, "failed to resolve test directory") || !strings.Contains(stderr, "miro.toml") {
|
||||||
|
t.Fatalf("stderr = %q, want missing config error", stderr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunRecordMissingPath(t *testing.T) {
|
func TestRunRecordMissingPath(t *testing.T) {
|
||||||
addFakeRecordDependencies(t, "script", "bwrap", "bash")
|
addFakeRecordDependencies(t, "script", "bwrap", "bash")
|
||||||
|
|
||||||
@@ -313,6 +340,24 @@ func prefixed(msg string) string {
|
|||||||
return output.Format(msg)
|
return output.Format(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeFile(t *testing.T, path, content string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustReadFile(t *testing.T, path string) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ReadFile(%q) error = %v", path, err)
|
||||||
|
}
|
||||||
|
return string(data)
|
||||||
|
}
|
||||||
|
|
||||||
func withWorkingDir(t *testing.T, dir string, fn func()) {
|
func withWorkingDir(t *testing.T, dir string, fn func()) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,10 +14,6 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type tomlConfig struct {
|
type tomlConfig struct {
|
||||||
Miro tomlMiroConfig `toml:"miro"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type tomlMiroConfig struct {
|
|
||||||
TestDir string `toml:"test_dir"`
|
TestDir string `toml:"test_dir"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,15 +21,37 @@ type tomlMiroConfig struct {
|
|||||||
func ReadConfig(path string) (Config, error) {
|
func ReadConfig(path string) (Config, error) {
|
||||||
var raw tomlConfig
|
var raw tomlConfig
|
||||||
|
|
||||||
_, err := toml.DecodeFile(path, &raw)
|
meta, err := toml.DecodeFile(path, &raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return Config{}, err
|
return Config{}, err
|
||||||
}
|
}
|
||||||
return Config{}, fmt.Errorf("failed to read %s: %v", path, err)
|
return Config{}, fmt.Errorf("failed to read %s: %v", path, err)
|
||||||
}
|
}
|
||||||
|
if !meta.IsDefined("test_dir") {
|
||||||
|
return Config{}, fmt.Errorf("failed to read %s: missing required test_dir", path)
|
||||||
|
}
|
||||||
|
if raw.TestDir == "" {
|
||||||
|
return Config{}, fmt.Errorf("failed to read %s: empty test_dir", path)
|
||||||
|
}
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
TestDir: raw.Miro.TestDir,
|
TestDir: raw.TestDir,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteConfig writes miro.toml.
|
||||||
|
func WriteConfig(path string, cfg Config) error {
|
||||||
|
if cfg.TestDir == "" {
|
||||||
|
return errors.New("empty test_dir")
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := toml.NewEncoder(&buf).Encode(tomlConfig{
|
||||||
|
TestDir: cfg.TestDir,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("failed to encode %s: %v", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.WriteFile(path, buf.Bytes(), 0o644)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,36 +13,63 @@ func TestReadConfig(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
content string
|
content string
|
||||||
wantDir string
|
wantDir string
|
||||||
wantReadErr bool
|
wantErr string
|
||||||
|
wantMissing bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "with test dir",
|
name: "with test dir",
|
||||||
content: "[miro]\ntest_dir = \"custom/suite\"\n",
|
content: "test_dir = \"custom/suite\"\n",
|
||||||
wantDir: "custom/suite",
|
wantDir: "custom/suite",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "without test dir",
|
name: "legacy miro table",
|
||||||
content: "[miro]\n",
|
content: "[miro]\ntest_dir = \"custom/suite\"\n",
|
||||||
|
wantErr: "missing required test_dir",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid toml",
|
name: "without test dir",
|
||||||
content: "[miro]\ntest_dir = [\n",
|
content: "",
|
||||||
wantReadErr: true,
|
wantErr: "missing required test_dir",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty test dir",
|
||||||
|
content: "test_dir = \"\"\n",
|
||||||
|
wantErr: "empty test_dir",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid toml",
|
||||||
|
content: "test_dir = [\n",
|
||||||
|
wantErr: "failed to read",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing file",
|
||||||
|
wantMissing: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
path := filepath.Join(t.TempDir(), "miro.toml")
|
path := filepath.Join(t.TempDir(), "miro.toml")
|
||||||
if err := os.WriteFile(path, []byte(tt.content), 0o644); err != nil {
|
if !tt.wantMissing {
|
||||||
t.Fatalf("WriteFile() error = %v", err)
|
if err := os.WriteFile(path, []byte(tt.content), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile() error = %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
got, err := ReadConfig(path)
|
got, err := ReadConfig(path)
|
||||||
if tt.wantReadErr {
|
if tt.wantMissing {
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Fatalf("ReadConfig() error = %v, want os.ErrNotExist", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tt.wantErr != "" {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("ReadConfig() error = nil, want error")
|
t.Fatal("ReadConfig() error = nil, want error")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||||
|
t.Fatalf("ReadConfig() error = %q, want substring %q", err.Error(), tt.wantErr)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -52,3 +81,31 @@ func TestReadConfig(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteConfig(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "miro.toml")
|
||||||
|
|
||||||
|
if err := WriteConfig(path, Config{TestDir: "e2e"}); err != nil {
|
||||||
|
t.Fatalf("WriteConfig() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ReadFile() error = %v", err)
|
||||||
|
}
|
||||||
|
if string(got) != "test_dir = \"e2e\"\n" {
|
||||||
|
t.Fatalf("config = %q, want %q", string(got), "test_dir = \"e2e\"\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteConfigEmptyTestDirFails(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "miro.toml")
|
||||||
|
|
||||||
|
err := WriteConfig(path, Config{})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("WriteConfig() error = nil, want error")
|
||||||
|
}
|
||||||
|
if err.Error() != "empty test_dir" {
|
||||||
|
t.Fatalf("WriteConfig() error = %q, want %q", err.Error(), "empty test_dir")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+33
-1
@@ -1,6 +1,38 @@
|
|||||||
package miro
|
package miro
|
||||||
|
|
||||||
// Init is the current no-op initializer placeholder.
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
miroconfig "miro/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultTestDir = "e2e"
|
||||||
|
|
||||||
|
// Init creates the default config when missing and leaves valid config untouched.
|
||||||
func Init() error {
|
func Init() error {
|
||||||
|
root, err := currentProjectRoot()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
configPath := filepath.Join(root, "miro.toml")
|
||||||
|
if _, err := os.Stat(configPath); err == nil {
|
||||||
|
if _, err := miroconfig.ReadConfig(configPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
} else if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return fmt.Errorf("failed to check %s: %v", configPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := miroconfig.WriteConfig(configPath, miroconfig.Config{
|
||||||
|
TestDir: defaultTestDir,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("failed to write %s: %v", configPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ func TestRecordCreatesRelativePath(t *testing.T) {
|
|||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
testDir := filepath.Join(root, "e2e")
|
testDir := filepath.Join(root, "e2e")
|
||||||
mustMkdirAll(t, testDir)
|
mustMkdirAll(t, testDir)
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
addFakeRecordDependencies(t, "script")
|
addFakeRecordDependencies(t, "script")
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
got := withWorkingDir(t, root, func() string {
|
||||||
@@ -45,10 +46,11 @@ func TestRecordCreatesRelativePath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRecordStripsExplicitTestDirPrefix(t *testing.T) {
|
func TestRecordAcceptsExplicitTestDirPrefix(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
testDir := filepath.Join(root, "e2e")
|
testDir := filepath.Join(root, "e2e")
|
||||||
mustMkdirAll(t, testDir)
|
mustMkdirAll(t, testDir)
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
addFakeRecordDependencies(t, "script")
|
addFakeRecordDependencies(t, "script")
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
got := withWorkingDir(t, root, func() string {
|
||||||
@@ -79,6 +81,7 @@ func TestRecordStripsExplicitTestDirPrefix(t *testing.T) {
|
|||||||
func TestRecordRejectsAbsolutePathOutsideTestDir(t *testing.T) {
|
func TestRecordRejectsAbsolutePathOutsideTestDir(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
mustMkdirAll(t, filepath.Join(root, "e2e"))
|
mustMkdirAll(t, filepath.Join(root, "e2e"))
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
outside := filepath.Join(root, "outside", "a", "b", "c")
|
outside := filepath.Join(root, "outside", "a", "b", "c")
|
||||||
|
|
||||||
err := withWorkingDir(t, root, func() error {
|
err := withWorkingDir(t, root, func() error {
|
||||||
|
|||||||
+33
-56
@@ -11,52 +11,50 @@ import (
|
|||||||
miroconfig "miro/internal/config"
|
miroconfig "miro/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fallbackTestDirs = []string{
|
// ResolveTestDir resolves the project test directory from config.
|
||||||
"e2e",
|
func ResolveTestDir() (string, error) {
|
||||||
filepath.Join("test", "e2e"),
|
root, err := currentProjectRoot()
|
||||||
filepath.Join("tests", "e2e"),
|
if err != nil {
|
||||||
"miro",
|
return "", err
|
||||||
filepath.Join("test", "miro"),
|
}
|
||||||
filepath.Join("tests", "miro"),
|
|
||||||
|
return resolveTestDirFromRoot(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveTestDir resolves the project test directory from config or conventions.
|
func resolveTestDirFromRoot(root string) (string, error) {
|
||||||
func ResolveTestDir() (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()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to get working directory: %v", err)
|
return "", fmt.Errorf("failed to get working directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
root := projectRoot(cwd)
|
return projectRoot(cwd), nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the git root if in a git repo else current pwd
|
// returns the git root if in a git repo else current pwd
|
||||||
func projectRoot(cwd string) string {
|
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 {
|
if err != nil {
|
||||||
return cwd
|
return cwd
|
||||||
}
|
}
|
||||||
@@ -67,24 +65,3 @@ func projectRoot(cwd string) string {
|
|||||||
}
|
}
|
||||||
return filepath.Clean(root)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
+117
-96
@@ -8,12 +8,77 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResolveTestDirConfigOverride(t *testing.T) {
|
func TestInitCreatesConfigAtProjectRoot(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
|
||||||
|
withWorkingDir(t, root, func() struct{} {
|
||||||
|
if err := Init(); err != nil {
|
||||||
|
t.Fatalf("Init() error = %v", err)
|
||||||
|
}
|
||||||
|
return struct{}{}
|
||||||
|
})
|
||||||
|
|
||||||
|
got := readFile(t, filepath.Join(root, "miro.toml"))
|
||||||
|
if got != "test_dir = \"e2e\"\n" {
|
||||||
|
t.Fatalf("config = %q, want %q", got, "test_dir = \"e2e\"\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitUsesGitRoot(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
mustGitInit(t, root)
|
||||||
|
subdir := filepath.Join(root, "nested", "dir")
|
||||||
|
mustMkdirAll(t, subdir)
|
||||||
|
|
||||||
|
withWorkingDir(t, subdir, func() struct{} {
|
||||||
|
if err := Init(); err != nil {
|
||||||
|
t.Fatalf("Init() error = %v", err)
|
||||||
|
}
|
||||||
|
return struct{}{}
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "miro.toml")); err != nil {
|
||||||
|
t.Fatalf("Stat(%q) error = %v", filepath.Join(root, "miro.toml"), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitLeavesExistingValidConfigUntouched(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"custom/suite\"\n")
|
||||||
|
|
||||||
|
withWorkingDir(t, root, func() struct{} {
|
||||||
|
if err := Init(); err != nil {
|
||||||
|
t.Fatalf("Init() error = %v", err)
|
||||||
|
}
|
||||||
|
return struct{}{}
|
||||||
|
})
|
||||||
|
|
||||||
|
got := readFile(t, filepath.Join(root, "miro.toml"))
|
||||||
|
if got != "test_dir = \"custom/suite\"\n" {
|
||||||
|
t.Fatalf("config = %q, want %q", got, "test_dir = \"custom/suite\"\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitFailsWhenExistingConfigInvalid(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"e2e\"\n")
|
||||||
|
|
||||||
|
err := withWorkingDir(t, root, func() error {
|
||||||
|
return Init()
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("Init() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "missing required test_dir") {
|
||||||
|
t.Fatalf("Init() error = %q, want missing test_dir error", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTestDirFromConfig(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
configured := filepath.Join(root, "custom", "suite")
|
configured := filepath.Join(root, "custom", "suite")
|
||||||
mustMkdirAll(t, configured)
|
mustMkdirAll(t, configured)
|
||||||
mustMkdirAll(t, filepath.Join(root, "e2e"))
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"custom/suite\"\n")
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"custom/suite\"\n")
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
got := withWorkingDir(t, root, func() string {
|
||||||
path, err := ResolveTestDir()
|
path, err := ResolveTestDir()
|
||||||
@@ -28,47 +93,59 @@ func TestResolveTestDirConfigOverride(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirFallsBackWhenConfigOmitsTestDir(t *testing.T) {
|
func TestResolveTestDirMissingConfigFails(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
want := filepath.Join(root, "e2e")
|
|
||||||
mustMkdirAll(t, want)
|
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\n")
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
err := withWorkingDir(t, root, func() error {
|
||||||
path, err := ResolveTestDir()
|
_, err := ResolveTestDir()
|
||||||
if err != nil {
|
return err
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if got != want {
|
if err == nil {
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
t.Fatal("ResolveTestDir() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("ResolveTestDir() error = %v, want os.ErrNotExist", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirFallsBackWhenConfiguredPathEmpty(t *testing.T) {
|
func TestResolveTestDirMissingTestDirFails(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
want := filepath.Join(root, "e2e")
|
writeFile(t, filepath.Join(root, "miro.toml"), "")
|
||||||
mustMkdirAll(t, want)
|
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"\"\n")
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
err := withWorkingDir(t, root, func() error {
|
||||||
path, err := ResolveTestDir()
|
_, err := ResolveTestDir()
|
||||||
if err != nil {
|
return err
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if got != want {
|
if err == nil {
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
t.Fatal("ResolveTestDir() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "missing required test_dir") {
|
||||||
|
t.Fatalf("ResolveTestDir() error = %q, want missing required test_dir", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTestDirEmptyTestDirFails(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"\"\n")
|
||||||
|
|
||||||
|
err := withWorkingDir(t, root, func() error {
|
||||||
|
_, err := ResolveTestDir()
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("ResolveTestDir() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "empty test_dir") {
|
||||||
|
t.Fatalf("ResolveTestDir() error = %q, want empty test_dir", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirMalformedConfigFails(t *testing.T) {
|
func TestResolveTestDirMalformedConfigFails(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = [\n")
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = [\n")
|
||||||
|
|
||||||
err := withWorkingDir(t, root, func() error {
|
err := withWorkingDir(t, root, func() error {
|
||||||
_, err := ResolveTestDir()
|
_, err := ResolveTestDir()
|
||||||
@@ -79,14 +156,14 @@ func TestResolveTestDirMalformedConfigFails(t *testing.T) {
|
|||||||
t.Fatal("ResolveTestDir() error = nil, want error")
|
t.Fatal("ResolveTestDir() error = nil, want error")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "failed to read") {
|
if !strings.Contains(err.Error(), "failed to read") {
|
||||||
t.Fatalf("ResolveTestDir() error = %q, want read failure", err)
|
t.Fatalf("ResolveTestDir() error = %q, want read failure", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirConfiguredMissingDirectoryReturnsConfiguredPath(t *testing.T) {
|
func TestResolveTestDirConfiguredMissingDirectoryReturnsConfiguredPath(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
want := filepath.Join(root, "missing")
|
want := filepath.Join(root, "missing")
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"missing\"\n")
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"missing\"\n")
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
got := withWorkingDir(t, root, func() string {
|
||||||
path, err := ResolveTestDir()
|
path, err := ResolveTestDir()
|
||||||
@@ -101,60 +178,21 @@ func TestResolveTestDirConfiguredMissingDirectoryReturnsConfiguredPath(t *testin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirConfiguredFileReturnsConfiguredPath(t *testing.T) {
|
func TestResolveTestDirConfiguredFileFails(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
want := filepath.Join(root, "case.txt")
|
|
||||||
writeFile(t, filepath.Join(root, "case.txt"), "hello\n")
|
writeFile(t, filepath.Join(root, "case.txt"), "hello\n")
|
||||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"case.txt\"\n")
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"case.txt\"\n")
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
err := withWorkingDir(t, root, func() error {
|
||||||
path, err := ResolveTestDir()
|
_, err := ResolveTestDir()
|
||||||
if err != nil {
|
return err
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if got != want {
|
if err == nil {
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
t.Fatal("ResolveTestDir() error = nil, want error")
|
||||||
}
|
}
|
||||||
}
|
if !strings.Contains(err.Error(), "configured test_dir is not a directory") {
|
||||||
|
t.Fatalf("ResolveTestDir() error = %q, want file path error", err.Error())
|
||||||
func TestResolveTestDirFallbackOrder(t *testing.T) {
|
|
||||||
root := t.TempDir()
|
|
||||||
lower := filepath.Join(root, "tests", "miro")
|
|
||||||
higher := filepath.Join(root, "tests", "e2e")
|
|
||||||
mustMkdirAll(t, lower)
|
|
||||||
mustMkdirAll(t, higher)
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
|
||||||
path, err := ResolveTestDir()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
|
||||||
|
|
||||||
if got != higher {
|
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, higher)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveTestDirFallbackToMiroDirectories(t *testing.T) {
|
|
||||||
root := t.TempDir()
|
|
||||||
want := filepath.Join(root, "test", "miro")
|
|
||||||
mustMkdirAll(t, want)
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
|
||||||
path, err := ResolveTestDir()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
|
||||||
|
|
||||||
if got != want {
|
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +200,7 @@ func TestResolveTestDirUsesGitRoot(t *testing.T) {
|
|||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
mustGitInit(t, root)
|
mustGitInit(t, root)
|
||||||
want := filepath.Join(root, "e2e")
|
want := filepath.Join(root, "e2e")
|
||||||
mustMkdirAll(t, want)
|
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||||
subdir := filepath.Join(root, "nested", "dir")
|
subdir := filepath.Join(root, "nested", "dir")
|
||||||
mustMkdirAll(t, subdir)
|
mustMkdirAll(t, subdir)
|
||||||
|
|
||||||
@@ -179,23 +217,6 @@ func TestResolveTestDirUsesGitRoot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveTestDirReturnsFirstFallbackWhenNothingDetected(t *testing.T) {
|
|
||||||
root := t.TempDir()
|
|
||||||
want := filepath.Join(root, "e2e")
|
|
||||||
|
|
||||||
got := withWorkingDir(t, root, func() string {
|
|
||||||
path, err := ResolveTestDir()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
|
||||||
}
|
|
||||||
return path
|
|
||||||
})
|
|
||||||
|
|
||||||
if got != want {
|
|
||||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func withWorkingDir[T any](t *testing.T, dir string, fn func() T) T {
|
func withWorkingDir[T any](t *testing.T, dir string, fn func() T) T {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user