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
+25 -6
View File
@@ -1,6 +1,7 @@
package config
import (
"bytes"
"errors"
"fmt"
"os"
@@ -13,10 +14,6 @@ type Config struct {
}
type tomlConfig struct {
Miro tomlMiroConfig `toml:"miro"`
}
type tomlMiroConfig struct {
TestDir string `toml:"test_dir"`
}
@@ -24,15 +21,37 @@ type tomlMiroConfig struct {
func ReadConfig(path string) (Config, error) {
var raw tomlConfig
_, err := toml.DecodeFile(path, &raw)
meta, err := toml.DecodeFile(path, &raw)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Config{}, 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{
TestDir: raw.Miro.TestDir,
TestDir: raw.TestDir,
}, 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)
}
+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")
}
}