feat: arbitrary MIRO_ envs to shell script

This commit is contained in:
2026-03-19 18:31:55 +00:00
parent 96b0ce57d9
commit c3823ee9b0
11 changed files with 337 additions and 75 deletions
+80 -7
View File
@@ -5,22 +5,40 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"github.com/BurntSushi/toml"
)
const DefaultVisibleHome = "/home/test"
var (
lowerSnakeCasePattern = regexp.MustCompile(`^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$`)
requiredSandboxDefaults = map[string]string{
"visible_home": DefaultVisibleHome,
}
)
type Config struct {
TestDir string
Sandbox map[string]string
}
type tomlConfig struct {
Miro tomlMiroConfig `toml:"miro"`
Miro tomlMiroConfig `toml:"miro"`
Sandbox map[string]string `toml:"sandbox"`
}
type tomlMiroConfig struct {
TestDir string `toml:"test_dir"`
}
func DefaultSandboxConfig() map[string]string {
return cloneSandbox(requiredSandboxDefaults)
}
// ReadConfig reads miro.toml.
func ReadConfig(path string) (Config, error) {
var raw tomlConfig
@@ -41,9 +59,18 @@ func ReadConfig(path string) (Config, error) {
if raw.Miro.TestDir == "" {
return Config{}, fmt.Errorf("failed to read %s: empty miro.test_dir", path)
}
if !meta.IsDefined("sandbox") {
return Config{}, fmt.Errorf("failed to read %s: missing [sandbox] config", path)
}
sandbox, err := validateSandbox(path, raw.Sandbox)
if err != nil {
return Config{}, err
}
return Config{
TestDir: raw.Miro.TestDir,
Sandbox: sandbox,
}, nil
}
@@ -52,15 +79,61 @@ func WriteConfig(path string, cfg Config) error {
if cfg.TestDir == "" {
return errors.New("empty miro.test_dir")
}
sandbox, err := validateSandbox(path, cfg.Sandbox)
if err != nil {
return err
}
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(tomlConfig{
Miro: tomlMiroConfig{
TestDir: cfg.TestDir,
},
}); err != nil {
return fmt.Errorf("failed to encode %s: %v", path, err)
fmt.Fprintf(&buf, "[miro]\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)
}
func validateSandbox(path string, sandbox map[string]string) (map[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)
}
}
for key := range requiredSandboxDefaults {
value, ok := validated[key]
if !ok {
return 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)
}
}
if !filepath.IsAbs(validated["visible_home"]) {
return nil, fmt.Errorf("failed to read %s: sandbox.visible_home must be an absolute path", path)
}
return validated, nil
}
func cloneSandbox(sandbox map[string]string) map[string]string {
if len(sandbox) == 0 {
return map[string]string{}
}
cloned := make(map[string]string, len(sandbox))
for key, value := range sandbox {
cloned[key] = value
}
return cloned
}
+66 -5
View File
@@ -13,13 +13,18 @@ func TestReadConfig(t *testing.T) {
name string
content string
wantDir string
wantSandbox map[string]string
wantErr string
wantMissing bool
}{
{
name: "with test dir",
content: "[miro]\ntest_dir = \"custom/suite\"\n",
name: "with test dir and sandbox",
content: "[miro]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nvisible_home = \"/home/test\"\nkey_word = \"value\"\n",
wantDir: "custom/suite",
wantSandbox: map[string]string{
"visible_home": "/home/test",
"key_word": "value",
},
},
{
name: "legacy top level key",
@@ -41,6 +46,31 @@ func TestReadConfig(t *testing.T) {
content: "[miro]\ntest_dir = \"\"\n",
wantErr: "empty miro.test_dir",
},
{
name: "without sandbox table",
content: "[miro]\ntest_dir = \"custom/suite\"\n",
wantErr: "missing [sandbox] config",
},
{
name: "without required visible home",
content: "[miro]\ntest_dir = \"custom/suite\"\n\n[sandbox]\n",
wantErr: "missing required sandbox.visible_home",
},
{
name: "empty required visible home",
content: "[miro]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nvisible_home = \"\"\n",
wantErr: "empty sandbox.visible_home",
},
{
name: "relative visible home",
content: "[miro]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nvisible_home = \"home/test\"\n",
wantErr: "sandbox.visible_home must be an absolute path",
},
{
name: "invalid sandbox key",
content: "[miro]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nvisible_home = \"/home/test\"\nKeyWord = \"value\"\n",
wantErr: "invalid sandbox key",
},
{
name: "invalid toml",
content: "[miro]\ntest_dir = [\n",
@@ -83,6 +113,14 @@ func TestReadConfig(t *testing.T) {
if got.TestDir != tt.wantDir {
t.Fatalf("ReadConfig() TestDir = %q, want %q", got.TestDir, tt.wantDir)
}
if len(got.Sandbox) != len(tt.wantSandbox) {
t.Fatalf("ReadConfig() Sandbox = %#v, want %#v", got.Sandbox, tt.wantSandbox)
}
for key, want := range tt.wantSandbox {
if got.Sandbox[key] != want {
t.Fatalf("ReadConfig() Sandbox[%q] = %q, want %q", key, got.Sandbox[key], want)
}
}
})
}
}
@@ -90,7 +128,14 @@ 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 {
if err := WriteConfig(path, Config{
TestDir: "e2e",
Sandbox: map[string]string{
"visible_home": "/home/test",
"alpha_key": "a",
"zulu_key": "z",
},
}); err != nil {
t.Fatalf("WriteConfig() error = %v", err)
}
@@ -98,8 +143,9 @@ func TestWriteConfig(t *testing.T) {
if err != nil {
t.Fatalf("ReadFile() error = %v", err)
}
if string(got) != "[miro]\n test_dir = \"e2e\"\n" {
t.Fatalf("config = %q, want %q", string(got), "[miro]\n test_dir = \"e2e\"\n")
want := "[miro]\n test_dir = \"e2e\"\n\n[sandbox]\n alpha_key = \"a\"\n visible_home = \"/home/test\"\n zulu_key = \"z\"\n"
if string(got) != want {
t.Fatalf("config = %q, want %q", string(got), want)
}
}
@@ -114,3 +160,18 @@ func TestWriteConfigEmptyTestDirFails(t *testing.T) {
t.Fatalf("WriteConfig() error = %q, want %q", err.Error(), "empty miro.test_dir")
}
}
func TestWriteConfigMissingRequiredSandboxFails(t *testing.T) {
path := filepath.Join(t.TempDir(), "miro.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.visible_home") {
t.Fatalf("WriteConfig() error = %q, want visible_home error", err.Error())
}
}