feat: ignore_difs key to ignore diffs in tests
This commit is contained in:
+33
-13
@@ -22,10 +22,11 @@ var (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
TestDir string
|
||||
Sandbox map[string]string
|
||||
Mounts []string
|
||||
Paths []string
|
||||
TestDir string
|
||||
IgnoreDiffs []string
|
||||
Sandbox map[string]string
|
||||
Mounts []string
|
||||
Paths []string
|
||||
}
|
||||
|
||||
type tomlConfig struct {
|
||||
@@ -34,7 +35,8 @@ type tomlConfig struct {
|
||||
}
|
||||
|
||||
type tomlMireConfig struct {
|
||||
TestDir string `toml:"test_dir"`
|
||||
TestDir string `toml:"test_dir"`
|
||||
IgnoreDiffs []string `toml:"ignore_diffs"`
|
||||
}
|
||||
|
||||
type tomlSandboxConfig struct {
|
||||
@@ -70,6 +72,13 @@ func ReadConfig(path string) (Config, error) {
|
||||
if raw.Mire.TestDir == "" {
|
||||
return Config{}, fmt.Errorf("failed to read %s: empty mire.test_dir", path)
|
||||
}
|
||||
if !meta.IsDefined("mire", "ignore_diffs") {
|
||||
return Config{}, fmt.Errorf("failed to read %s: missing required mire.ignore_diffs", path)
|
||||
}
|
||||
ignoreDiffs, err := validateIgnoreDiffs(path, raw.Mire.IgnoreDiffs)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if !meta.IsDefined("sandbox") {
|
||||
return Config{}, fmt.Errorf("failed to read %s: missing [sandbox] config", path)
|
||||
}
|
||||
@@ -89,10 +98,11 @@ func ReadConfig(path string) (Config, error) {
|
||||
}
|
||||
|
||||
return Config{
|
||||
TestDir: raw.Mire.TestDir,
|
||||
Sandbox: sandbox,
|
||||
Mounts: mounts,
|
||||
Paths: paths,
|
||||
TestDir: raw.Mire.TestDir,
|
||||
IgnoreDiffs: ignoreDiffs,
|
||||
Sandbox: sandbox,
|
||||
Mounts: mounts,
|
||||
Paths: paths,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -170,6 +180,16 @@ func validateSandbox(path string, sandbox map[string]string, mounts, paths []str
|
||||
return validated, validatedMounts, validatedPaths, nil
|
||||
}
|
||||
|
||||
func validateIgnoreDiffs(path string, ignoreDiffs []string) ([]string, error) {
|
||||
for i, pattern := range ignoreDiffs {
|
||||
if _, err := regexp.Compile(pattern); err != nil {
|
||||
return nil, fmt.Errorf("failed to read %s: invalid mire.ignore_diffs[%d] regex %q: %v", path, i, pattern, err)
|
||||
}
|
||||
}
|
||||
|
||||
return cloneStrings(ignoreDiffs), nil
|
||||
}
|
||||
|
||||
func normalizeMounts(configPath string, mounts []string) ([]string, error) {
|
||||
normalized := make([]string, 0, len(mounts))
|
||||
for _, mount := range mounts {
|
||||
@@ -225,13 +245,13 @@ func cloneSandbox(sandbox map[string]string) map[string]string {
|
||||
return cloned
|
||||
}
|
||||
|
||||
func cloneMounts(mounts []string) []string {
|
||||
if len(mounts) == 0 {
|
||||
func cloneStrings(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
cloned := make([]string, len(mounts))
|
||||
copy(cloned, mounts)
|
||||
cloned := make([]string, len(values))
|
||||
copy(cloned, values)
|
||||
|
||||
return cloned
|
||||
}
|
||||
|
||||
@@ -10,19 +10,20 @@ import (
|
||||
|
||||
func TestReadConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
wantDir string
|
||||
wantSandbox map[string]string
|
||||
wantMounts []string
|
||||
wantPaths []string
|
||||
wantErr string
|
||||
wantMissing bool
|
||||
setup func(t *testing.T, dir string) (string, []string, []string)
|
||||
name string
|
||||
content string
|
||||
wantDir string
|
||||
wantIgnoreDiffs []string
|
||||
wantSandbox map[string]string
|
||||
wantMounts []string
|
||||
wantPaths []string
|
||||
wantErr string
|
||||
wantMissing bool
|
||||
setup func(t *testing.T, dir string) (string, []string, []string, []string)
|
||||
}{
|
||||
{
|
||||
name: "with test dir and sandbox",
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string) {
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string, []string) {
|
||||
hostData := filepath.Join(dir, "host-data")
|
||||
hostCache := filepath.Join(dir, "host-cache")
|
||||
for _, path := range []string{hostData, hostCache} {
|
||||
@@ -30,11 +31,16 @@ func TestReadConfig(t *testing.T) {
|
||||
t.Fatalf("MkdirAll(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = [\"" + hostData + ":/sandbox/data\", \"" + hostCache + ":/sandbox/cache\"]\npaths = []\nkey_word = \"value\"\n",
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = [\"^ts=.*$\", \"^id=.*$\"]\n\n[sandbox]\nhome = \"/home/test\"\nmounts = [\"" + hostData + ":/sandbox/data\", \"" + hostCache + ":/sandbox/cache\"]\npaths = []\nkey_word = \"value\"\n",
|
||||
[]string{hostData + ":/sandbox/data", hostCache + ":/sandbox/cache"},
|
||||
nil
|
||||
nil,
|
||||
[]string{"^ts=.*$", "^id=.*$"}
|
||||
},
|
||||
wantDir: "custom/suite",
|
||||
wantIgnoreDiffs: []string{
|
||||
"^ts=.*$",
|
||||
"^id=.*$",
|
||||
},
|
||||
wantSandbox: map[string]string{
|
||||
"home": "/home/test",
|
||||
"key_word": "value",
|
||||
@@ -55,6 +61,11 @@ func TestReadConfig(t *testing.T) {
|
||||
content: "[mire]\n",
|
||||
wantErr: "missing required mire.test_dir",
|
||||
},
|
||||
{
|
||||
name: "without ignore diffs",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n",
|
||||
wantErr: "missing required mire.ignore_diffs",
|
||||
},
|
||||
{
|
||||
name: "empty test dir",
|
||||
content: "[mire]\ntest_dir = \"\"\n",
|
||||
@@ -62,44 +73,59 @@ func TestReadConfig(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "without sandbox table",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n",
|
||||
wantErr: "missing [sandbox] config",
|
||||
},
|
||||
{
|
||||
name: "without required home",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nmounts = []\npaths = []\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nmounts = []\npaths = []\n",
|
||||
wantErr: "missing required sandbox.home",
|
||||
},
|
||||
{
|
||||
name: "without required mounts",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\npaths = []\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\npaths = []\n",
|
||||
wantErr: "missing required sandbox.mounts",
|
||||
},
|
||||
{
|
||||
name: "empty required home",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"\"\nmounts = []\npaths = []\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"\"\nmounts = []\npaths = []\n",
|
||||
wantErr: "empty sandbox.home",
|
||||
},
|
||||
{
|
||||
name: "relative home",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"home/test\"\nmounts = []\npaths = []\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"home/test\"\nmounts = []\npaths = []\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\"\nmounts = []\npaths = []\nKeyWord = \"value\"\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = []\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 = []\npaths = []\nkey_word = 1\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = []\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\"\npaths = []\n",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\nmounts = \"oops\"\npaths = []\n",
|
||||
wantErr: "failed to read",
|
||||
},
|
||||
{
|
||||
name: "ignore diffs wrong type",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = \"oops\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = []\n",
|
||||
wantErr: "failed to read",
|
||||
},
|
||||
{
|
||||
name: "ignore diff entry not string",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = [1]\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = []\n",
|
||||
wantErr: "failed to read",
|
||||
},
|
||||
{
|
||||
name: "ignore diff regex invalid",
|
||||
content: "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = [\"(\"]\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = []\n",
|
||||
wantErr: "invalid mire.ignore_diffs[0] regex",
|
||||
},
|
||||
{
|
||||
name: "invalid toml",
|
||||
content: "[mire]\ntest_dir = [\n",
|
||||
@@ -111,14 +137,15 @@ func TestReadConfig(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "normalizes relative mount host path",
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string) {
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string, []string) {
|
||||
hostBuild := filepath.Join(dir, "build")
|
||||
if err := os.MkdirAll(hostBuild, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll(%q) error = %v", hostBuild, err)
|
||||
}
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = [\"./build:/sandbox/build\"]\npaths = []\n",
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\nmounts = [\"./build:/sandbox/build\"]\npaths = []\n",
|
||||
[]string{hostBuild + ":/sandbox/build"},
|
||||
nil
|
||||
nil,
|
||||
[]string{}
|
||||
},
|
||||
wantDir: "custom/suite",
|
||||
wantSandbox: map[string]string{
|
||||
@@ -127,7 +154,7 @@ func TestReadConfig(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "normalizes relative paths host path",
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string) {
|
||||
setup: func(t *testing.T, dir string) (string, []string, []string, []string) {
|
||||
hostTool := filepath.Join(dir, "build", "mend")
|
||||
if err := os.MkdirAll(filepath.Dir(hostTool), 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll(%q) error = %v", filepath.Dir(hostTool), err)
|
||||
@@ -135,9 +162,10 @@ func TestReadConfig(t *testing.T) {
|
||||
if err := os.WriteFile(hostTool, []byte("tool"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile(%q) error = %v", hostTool, err)
|
||||
}
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = [\"./build/mend\"]\n",
|
||||
return "[mire]\ntest_dir = \"custom/suite\"\nignore_diffs = []\n\n[sandbox]\nhome = \"/home/test\"\nmounts = []\npaths = [\"./build/mend\"]\n",
|
||||
nil,
|
||||
[]string{hostTool}
|
||||
[]string{hostTool},
|
||||
[]string{}
|
||||
},
|
||||
wantDir: "custom/suite",
|
||||
wantSandbox: map[string]string{
|
||||
@@ -152,9 +180,10 @@ func TestReadConfig(t *testing.T) {
|
||||
path := filepath.Join(dir, "mire.toml")
|
||||
wantMounts := tt.wantMounts
|
||||
wantPaths := tt.wantPaths
|
||||
wantIgnoreDiffs := tt.wantIgnoreDiffs
|
||||
content := tt.content
|
||||
if tt.setup != nil {
|
||||
content, wantMounts, wantPaths = tt.setup(t, dir)
|
||||
content, wantMounts, wantPaths, wantIgnoreDiffs = tt.setup(t, dir)
|
||||
}
|
||||
if !tt.wantMissing {
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
@@ -184,6 +213,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.IgnoreDiffs) != len(wantIgnoreDiffs) {
|
||||
t.Fatalf("ReadConfig() IgnoreDiffs = %#v, want %#v", got.IgnoreDiffs, wantIgnoreDiffs)
|
||||
}
|
||||
for i, want := range wantIgnoreDiffs {
|
||||
if got.IgnoreDiffs[i] != want {
|
||||
t.Fatalf("ReadConfig() IgnoreDiffs[%d] = %q, want %q", i, got.IgnoreDiffs[i], want)
|
||||
}
|
||||
}
|
||||
if len(got.Sandbox) != len(tt.wantSandbox) {
|
||||
t.Fatalf("ReadConfig() Sandbox = %#v, want %#v", got.Sandbox, tt.wantSandbox)
|
||||
}
|
||||
@@ -226,6 +263,9 @@ func TestWriteDefaultConfig(t *testing.T) {
|
||||
if got.TestDir != "e2e" {
|
||||
t.Fatalf("ReadConfig() TestDir = %q, want %q", got.TestDir, "e2e")
|
||||
}
|
||||
if len(got.IgnoreDiffs) != 0 {
|
||||
t.Fatalf("ReadConfig() IgnoreDiffs = %#v, want empty", got.IgnoreDiffs)
|
||||
}
|
||||
if got.Sandbox["home"] != DefaultVisibleHome {
|
||||
t.Fatalf("ReadConfig() Sandbox[home] = %q, want %q", got.Sandbox["home"], DefaultVisibleHome)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
[mire]
|
||||
# which folder to strore tests in
|
||||
test_dir = "e2e"
|
||||
# regexes for differing lines to ignore during replay comparison
|
||||
ignore_diffs = []
|
||||
|
||||
[sandbox]
|
||||
# home is where mire would drop you by default on record
|
||||
|
||||
Reference in New Issue
Block a user