feat: add mounts as an option in mire config + refactor default config generation

This commit is contained in:
2026-03-22 13:47:12 +00:00
parent f3b80ddf05
commit 517dc908bc
14 changed files with 189 additions and 125 deletions
+71 -31
View File
@@ -1,13 +1,12 @@
package config
import (
"bytes"
"embed"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"github.com/BurntSushi/toml"
)
@@ -24,17 +23,26 @@ var (
type Config struct {
TestDir string
Sandbox map[string]string
Mounts []string
}
type tomlConfig struct {
Mire tomlMireConfig `toml:"mire"`
Sandbox map[string]string `toml:"sandbox"`
Mire tomlMireConfig `toml:"mire"`
Sandbox toml.Primitive `toml:"sandbox"`
}
type tomlMireConfig struct {
TestDir string `toml:"test_dir"`
}
type tomlSandboxConfig struct {
Home string `toml:"home"`
Mounts []string `toml:"mounts"`
}
//go:embed mire.toml
var defaultConfigFS embed.FS
func DefaultSandboxConfig() map[string]string {
return cloneSandbox(requiredSandboxDefaults)
}
@@ -62,8 +70,14 @@ func ReadConfig(path string) (Config, error) {
if !meta.IsDefined("sandbox") {
return Config{}, fmt.Errorf("failed to read %s: missing [sandbox] config", path)
}
if !meta.IsDefined("sandbox", "home") {
return Config{}, fmt.Errorf("failed to read %s: missing required sandbox.home", path)
}
if !meta.IsDefined("sandbox", "mounts") {
return Config{}, fmt.Errorf("failed to read %s: missing required sandbox.mounts", path)
}
sandbox, err := validateSandbox(path, raw.Sandbox)
sandbox, mounts, err := decodeSandbox(meta, path, raw.Sandbox)
if err != nil {
return Config{}, err
}
@@ -71,58 +85,73 @@ func ReadConfig(path string) (Config, error) {
return Config{
TestDir: raw.Mire.TestDir,
Sandbox: sandbox,
Mounts: mounts,
}, nil
}
// WriteConfig writes mire.toml.
func WriteConfig(path string, cfg Config) error {
if cfg.TestDir == "" {
return errors.New("empty mire.test_dir")
}
sandbox, err := validateSandbox(path, cfg.Sandbox)
// WriteDefaultConfig writes the embedded default mire.toml.
func WriteDefaultConfig(path string) error {
body, err := defaultConfigFS.ReadFile("mire.toml")
if err != nil {
return err
return fmt.Errorf("read embedded default mire.toml: %v", err)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "[mire]\n test_dir = %q\n\n[sandbox]\n", cfg.TestDir)
keys := make([]string, 0, len(sandbox))
for key := range sandbox {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Fprintf(&buf, " %s = %q\n", key, sandbox[key])
}
return os.WriteFile(path, buf.Bytes(), 0o644)
return os.WriteFile(path, body, 0o644)
}
func validateSandbox(path string, sandbox map[string]string) (map[string]string, error) {
func decodeSandbox(meta toml.MetaData, path string, raw toml.Primitive) (map[string]string, []string, error) {
var typed tomlSandboxConfig
if err := meta.PrimitiveDecode(raw, &typed); err != nil {
return nil, nil, fmt.Errorf("failed to read %s: %v", path, err)
}
var sandboxTable map[string]any
if err := meta.PrimitiveDecode(raw, &sandboxTable); err != nil {
return nil, nil, fmt.Errorf("failed to read %s: %v", path, err)
}
sandbox := map[string]string{
"home": typed.Home,
}
for key, value := range sandboxTable {
if key == "home" || key == "mounts" {
continue
}
str, ok := value.(string)
if !ok {
return nil, nil, fmt.Errorf("failed to read %s: sandbox.%s must be a string", path, key)
}
sandbox[key] = str
}
return validateSandbox(path, sandbox, typed.Mounts)
}
func validateSandbox(path string, sandbox map[string]string, mounts []string) (map[string]string, []string, error) {
validated := cloneSandbox(sandbox)
for key := range validated {
if !lowerSnakeCasePattern.MatchString(key) {
return nil, fmt.Errorf("failed to read %s: invalid sandbox key %q: must be lower_snake_case", path, key)
return nil, nil, fmt.Errorf("failed to read %s: invalid sandbox key %q: must be lower_snake_case", path, key)
}
}
for key := range requiredSandboxDefaults {
value, ok := validated[key]
if !ok {
return nil, fmt.Errorf("failed to read %s: missing required sandbox.%s", path, key)
return nil, nil, fmt.Errorf("failed to read %s: missing required sandbox.%s", path, key)
}
if value == "" {
return nil, fmt.Errorf("failed to read %s: empty sandbox.%s", path, key)
return nil, nil, fmt.Errorf("failed to read %s: empty sandbox.%s", path, key)
}
}
if !filepath.IsAbs(validated["home"]) {
return nil, fmt.Errorf("failed to read %s: sandbox.home must be an absolute path", path)
return nil, nil, fmt.Errorf("failed to read %s: sandbox.home must be an absolute path", path)
}
return validated, nil
return validated, cloneMounts(mounts), nil
}
func cloneSandbox(sandbox map[string]string) map[string]string {
@@ -137,3 +166,14 @@ func cloneSandbox(sandbox map[string]string) map[string]string {
return cloned
}
func cloneMounts(mounts []string) []string {
if len(mounts) == 0 {
return []string{}
}
cloned := make([]string, len(mounts))
copy(cloned, mounts)
return cloned
}
+43 -47
View File
@@ -14,17 +14,19 @@ func TestReadConfig(t *testing.T) {
content string
wantDir string
wantSandbox map[string]string
wantMounts []string
wantErr string
wantMissing bool
}{
{
name: "with test dir and sandbox",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nkey_word = \"value\"\n",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = [\"/host/data:/sandbox/data\", \"/host/cache:/sandbox/cache\"]\nkey_word = \"value\"\n",
wantDir: "custom/suite",
wantSandbox: map[string]string{
"home": "/home/test",
"key_word": "value",
},
wantMounts: []string{"/host/data:/sandbox/data", "/host/cache:/sandbox/cache"},
},
{
name: "legacy top level key",
@@ -53,24 +55,39 @@ func TestReadConfig(t *testing.T) {
},
{
name: "without required home",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\n",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nmounts = []\n",
wantErr: "missing required sandbox.home",
},
{
name: "without required mounts",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\n",
wantErr: "missing required sandbox.mounts",
},
{
name: "empty required home",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"\"\n",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"\"\nmounts = []\n",
wantErr: "empty sandbox.home",
},
{
name: "relative home",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"home/test\"\n",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"home/test\"\nmounts = []\n",
wantErr: "sandbox.home must be an absolute path",
},
{
name: "invalid sandbox key",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nKeyWord = \"value\"\n",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\nKeyWord = \"value\"\n",
wantErr: "invalid sandbox key",
},
{
name: "non string sandbox value",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\nkey_word = 1\n",
wantErr: "sandbox.key_word must be a string",
},
{
name: "mounts wrong type",
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = \"oops\"\n",
wantErr: "failed to read",
},
{
name: "invalid toml",
content: "[mire]\ntest_dir = [\n",
@@ -121,57 +138,36 @@ func TestReadConfig(t *testing.T) {
t.Fatalf("ReadConfig() Sandbox[%q] = %q, want %q", key, got.Sandbox[key], want)
}
}
if len(got.Mounts) != len(tt.wantMounts) {
t.Fatalf("ReadConfig() Mounts = %#v, want %#v", got.Mounts, tt.wantMounts)
}
for i, want := range tt.wantMounts {
if got.Mounts[i] != want {
t.Fatalf("ReadConfig() Mounts[%d] = %q, want %q", i, got.Mounts[i], want)
}
}
})
}
}
func TestWriteConfig(t *testing.T) {
func TestWriteDefaultConfig(t *testing.T) {
path := filepath.Join(t.TempDir(), "mire.toml")
if err := WriteConfig(path, Config{
TestDir: "e2e",
Sandbox: map[string]string{
"home": "/home/test",
"alpha_key": "a",
"zulu_key": "z",
},
}); err != nil {
t.Fatalf("WriteConfig() error = %v", err)
if err := WriteDefaultConfig(path); err != nil {
t.Fatalf("WriteDefaultConfig() error = %v", err)
}
got, err := os.ReadFile(path)
got, err := ReadConfig(path)
if err != nil {
t.Fatalf("ReadFile() error = %v", err)
t.Fatalf("ReadConfig() error = %v", err)
}
want := "[mire]\n test_dir = \"e2e\"\n\n[sandbox]\n alpha_key = \"a\"\n home = \"/home/test\"\n zulu_key = \"z\"\n"
if string(got) != want {
t.Fatalf("config = %q, want %q", string(got), want)
}
}
func TestWriteConfigEmptyTestDirFails(t *testing.T) {
path := filepath.Join(t.TempDir(), "mire.toml")
err := WriteConfig(path, Config{})
if err == nil {
t.Fatal("WriteConfig() error = nil, want error")
}
if err.Error() != "empty mire.test_dir" {
t.Fatalf("WriteConfig() error = %q, want %q", err.Error(), "empty mire.test_dir")
}
}
func TestWriteConfigMissingRequiredSandboxFails(t *testing.T) {
path := filepath.Join(t.TempDir(), "mire.toml")
err := WriteConfig(path, Config{
TestDir: "e2e",
Sandbox: map[string]string{},
})
if err == nil {
t.Fatal("WriteConfig() error = nil, want error")
}
if !strings.Contains(err.Error(), "missing required sandbox.home") {
t.Fatalf("WriteConfig() error = %q, want home error", err.Error())
if got.TestDir != "e2e" {
t.Fatalf("ReadConfig() TestDir = %q, want %q", got.TestDir, "e2e")
}
if got.Sandbox["home"] != DefaultVisibleHome {
t.Fatalf("ReadConfig() Sandbox[home] = %q, want %q", got.Sandbox["home"], DefaultVisibleHome)
}
if len(got.Mounts) != 0 {
t.Fatalf("ReadConfig() Mounts = %#v, want empty", got.Mounts)
}
}
+9
View File
@@ -0,0 +1,9 @@
[mire]
# which folder to strore tests in
test_dir = "e2e"
[sandbox]
# home is where mire would drop you by default on record
home = "/home/test"
# read only paths from host, entry looks like "path on host:path on sanbox"
mounts = []