refactor: more current config keys under [miro] table
This commit is contained in:
@@ -14,6 +14,10 @@ type Config struct {
|
||||
}
|
||||
|
||||
type tomlConfig struct {
|
||||
Miro tomlMiroConfig `toml:"miro"`
|
||||
}
|
||||
|
||||
type tomlMiroConfig struct {
|
||||
TestDir string `toml:"test_dir"`
|
||||
}
|
||||
|
||||
@@ -28,27 +32,32 @@ func ReadConfig(path string) (Config, error) {
|
||||
}
|
||||
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 !meta.IsDefined("miro") {
|
||||
return Config{}, fmt.Errorf("failed to read %s: missing [miro] config", path)
|
||||
}
|
||||
if raw.TestDir == "" {
|
||||
return Config{}, fmt.Errorf("failed to read %s: empty test_dir", path)
|
||||
if !meta.IsDefined("miro", "test_dir") {
|
||||
return Config{}, fmt.Errorf("failed to read %s: missing required miro.test_dir", path)
|
||||
}
|
||||
if raw.Miro.TestDir == "" {
|
||||
return Config{}, fmt.Errorf("failed to read %s: empty miro.test_dir", path)
|
||||
}
|
||||
|
||||
return Config{
|
||||
TestDir: raw.TestDir,
|
||||
TestDir: raw.Miro.TestDir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// WriteConfig writes miro.toml.
|
||||
func WriteConfig(path string, cfg Config) error {
|
||||
if cfg.TestDir == "" {
|
||||
return errors.New("empty test_dir")
|
||||
return errors.New("empty miro.test_dir")
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := toml.NewEncoder(&buf).Encode(tomlConfig{
|
||||
TestDir: cfg.TestDir,
|
||||
Miro: tomlMiroConfig{
|
||||
TestDir: cfg.TestDir,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("failed to encode %s: %v", path, err)
|
||||
}
|
||||
|
||||
@@ -18,27 +18,32 @@ func TestReadConfig(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "with test dir",
|
||||
content: "test_dir = \"custom/suite\"\n",
|
||||
content: "[miro]\ntest_dir = \"custom/suite\"\n",
|
||||
wantDir: "custom/suite",
|
||||
},
|
||||
{
|
||||
name: "legacy miro table",
|
||||
content: "[miro]\ntest_dir = \"custom/suite\"\n",
|
||||
wantErr: "missing required test_dir",
|
||||
name: "legacy top level key",
|
||||
content: "test_dir = \"custom/suite\"\n",
|
||||
wantErr: "missing [miro] config",
|
||||
},
|
||||
{
|
||||
name: "without miro table",
|
||||
content: "",
|
||||
wantErr: "missing [miro] config",
|
||||
},
|
||||
{
|
||||
name: "without test dir",
|
||||
content: "",
|
||||
wantErr: "missing required test_dir",
|
||||
content: "[miro]\n",
|
||||
wantErr: "missing required miro.test_dir",
|
||||
},
|
||||
{
|
||||
name: "empty test dir",
|
||||
content: "test_dir = \"\"\n",
|
||||
wantErr: "empty test_dir",
|
||||
content: "[miro]\ntest_dir = \"\"\n",
|
||||
wantErr: "empty miro.test_dir",
|
||||
},
|
||||
{
|
||||
name: "invalid toml",
|
||||
content: "test_dir = [\n",
|
||||
content: "[miro]\ntest_dir = [\n",
|
||||
wantErr: "failed to read",
|
||||
},
|
||||
{
|
||||
@@ -93,8 +98,8 @@ func TestWriteConfig(t *testing.T) {
|
||||
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")
|
||||
if string(got) != "[miro]\n test_dir = \"e2e\"\n" {
|
||||
t.Fatalf("config = %q, want %q", string(got), "[miro]\n test_dir = \"e2e\"\n")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +110,7 @@ func TestWriteConfigEmptyTestDirFails(t *testing.T) {
|
||||
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")
|
||||
if err.Error() != "empty miro.test_dir" {
|
||||
t.Fatalf("WriteConfig() error = %q, want %q", err.Error(), "empty miro.test_dir")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user