fix: make miro testdir resolution explicit based on config

This commit is contained in:
2026-03-18 12:48:09 +00:00
parent 3949ee98bc
commit 666219edbf
8 changed files with 342 additions and 187 deletions
+67 -10
View File
@@ -1,8 +1,10 @@
package config
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
@@ -11,36 +13,63 @@ func TestReadConfig(t *testing.T) {
name string
content string
wantDir string
wantReadErr bool
wantErr string
wantMissing bool
}{
{
name: "with test dir",
content: "[miro]\ntest_dir = \"custom/suite\"\n",
content: "test_dir = \"custom/suite\"\n",
wantDir: "custom/suite",
},
{
name: "without test dir",
content: "[miro]\n",
name: "legacy miro table",
content: "[miro]\ntest_dir = \"custom/suite\"\n",
wantErr: "missing required test_dir",
},
{
name: "invalid toml",
content: "[miro]\ntest_dir = [\n",
wantReadErr: true,
name: "without test dir",
content: "",
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 {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "miro.toml")
if err := os.WriteFile(path, []byte(tt.content), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
if !tt.wantMissing {
if err := os.WriteFile(path, []byte(tt.content), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
}
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 {
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
}
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")
}
}