test: refactor tests for a leaner test setup
This commit is contained in:
+39
-109
@@ -2,36 +2,37 @@ package miro
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"miro/internal/testutil"
|
||||
)
|
||||
|
||||
func TestInitCreatesConfigAtProjectRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
|
||||
withWorkingDir(t, root, func() struct{} {
|
||||
testutil.WithWorkingDir(t, root, func() struct{} {
|
||||
if err := Init(); err != nil {
|
||||
t.Fatalf("Init() error = %v", err)
|
||||
}
|
||||
return struct{}{}
|
||||
})
|
||||
|
||||
got := readFile(t, filepath.Join(root, "miro.toml"))
|
||||
if got != defaultWrittenConfig("e2e") {
|
||||
t.Fatalf("config = %q, want %q", got, defaultWrittenConfig("e2e"))
|
||||
got := testutil.ReadFile(t, filepath.Join(root, "miro.toml"))
|
||||
if got != testutil.DefaultWrittenConfig("e2e") {
|
||||
t.Fatalf("config = %q, want %q", got, testutil.DefaultWrittenConfig("e2e"))
|
||||
}
|
||||
assertRecordShell(t, filepath.Join(root, "e2e", recordShellName))
|
||||
}
|
||||
|
||||
func TestInitUsesGitRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
mustGitInit(t, root)
|
||||
testutil.MustGitInit(t, root)
|
||||
subdir := filepath.Join(root, "nested", "dir")
|
||||
mustMkdirAll(t, subdir)
|
||||
testutil.MustMkdirAll(t, subdir)
|
||||
|
||||
withWorkingDir(t, subdir, func() struct{} {
|
||||
testutil.WithWorkingDir(t, subdir, func() struct{} {
|
||||
if err := Init(); err != nil {
|
||||
t.Fatalf("Init() error = %v", err)
|
||||
}
|
||||
@@ -46,30 +47,30 @@ func TestInitUsesGitRoot(t *testing.T) {
|
||||
|
||||
func TestInitLeavesExistingValidConfigUntouchedAndRefreshesShell(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("custom/suite"))
|
||||
writeFile(t, filepath.Join(root, "custom", "suite", recordShellName), "outdated\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("custom/suite"))
|
||||
testutil.WriteFile(t, filepath.Join(root, "custom", "suite", recordShellName), "outdated\n")
|
||||
|
||||
withWorkingDir(t, root, func() struct{} {
|
||||
testutil.WithWorkingDir(t, root, func() struct{} {
|
||||
if err := Init(); err != nil {
|
||||
t.Fatalf("Init() error = %v", err)
|
||||
}
|
||||
return struct{}{}
|
||||
})
|
||||
|
||||
got := readFile(t, filepath.Join(root, "miro.toml"))
|
||||
if got != validConfigContent("custom/suite") {
|
||||
t.Fatalf("config = %q, want %q", got, validConfigContent("custom/suite"))
|
||||
got := testutil.ReadFile(t, filepath.Join(root, "miro.toml"))
|
||||
if got != testutil.ValidConfigContent("custom/suite") {
|
||||
t.Fatalf("config = %q, want %q", got, testutil.ValidConfigContent("custom/suite"))
|
||||
}
|
||||
if got := readFile(t, filepath.Join(root, "custom", "suite", recordShellName)); got != buildRecordShellScript() {
|
||||
if got := testutil.ReadFile(t, filepath.Join(root, "custom", "suite", recordShellName)); got != buildRecordShellScript() {
|
||||
t.Fatalf("shell = %q, want refreshed recorder shell", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitCreatesMissingConfiguredTestDir(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("custom/suite"))
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("custom/suite"))
|
||||
|
||||
withWorkingDir(t, root, func() struct{} {
|
||||
testutil.WithWorkingDir(t, root, func() struct{} {
|
||||
if err := Init(); err != nil {
|
||||
t.Fatalf("Init() error = %v", err)
|
||||
}
|
||||
@@ -81,9 +82,9 @@ func TestInitCreatesMissingConfiguredTestDir(t *testing.T) {
|
||||
|
||||
func TestInitFailsWhenExistingConfigInvalid(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), "test_dir = \"e2e\"\n")
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
return Init()
|
||||
})
|
||||
if err == nil {
|
||||
@@ -97,10 +98,10 @@ func TestInitFailsWhenExistingConfigInvalid(t *testing.T) {
|
||||
func TestResolveTestDirFromConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
configured := filepath.Join(root, "custom", "suite")
|
||||
mustMkdirAll(t, configured)
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("custom/suite"))
|
||||
testutil.MustMkdirAll(t, configured)
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("custom/suite"))
|
||||
|
||||
got := withWorkingDir(t, root, func() string {
|
||||
got := testutil.WithWorkingDir(t, root, func() string {
|
||||
path, err := ResolveTestDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
||||
@@ -116,7 +117,7 @@ func TestResolveTestDirFromConfig(t *testing.T) {
|
||||
func TestResolveTestDirMissingConfigFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
_, err := ResolveTestDir()
|
||||
return err
|
||||
})
|
||||
@@ -131,9 +132,9 @@ func TestResolveTestDirMissingConfigFails(t *testing.T) {
|
||||
|
||||
func TestResolveTestDirMissingTestDirFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), "[miro]\n")
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
_, err := ResolveTestDir()
|
||||
return err
|
||||
})
|
||||
@@ -148,9 +149,9 @@ func TestResolveTestDirMissingTestDirFails(t *testing.T) {
|
||||
|
||||
func TestResolveTestDirEmptyTestDirFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"\"\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = \"\"\n")
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
_, err := ResolveTestDir()
|
||||
return err
|
||||
})
|
||||
@@ -165,9 +166,9 @@ func TestResolveTestDirEmptyTestDirFails(t *testing.T) {
|
||||
|
||||
func TestResolveTestDirMalformedConfigFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = [\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), "[miro]\ntest_dir = [\n")
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
_, err := ResolveTestDir()
|
||||
return err
|
||||
})
|
||||
@@ -183,9 +184,9 @@ func TestResolveTestDirMalformedConfigFails(t *testing.T) {
|
||||
func TestResolveTestDirConfiguredMissingDirectoryReturnsConfiguredPath(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
want := filepath.Join(root, "missing")
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("missing"))
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("missing"))
|
||||
|
||||
got := withWorkingDir(t, root, func() string {
|
||||
got := testutil.WithWorkingDir(t, root, func() string {
|
||||
path, err := ResolveTestDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
||||
@@ -200,10 +201,10 @@ func TestResolveTestDirConfiguredMissingDirectoryReturnsConfiguredPath(t *testin
|
||||
|
||||
func TestResolveTestDirConfiguredFileFails(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "case.txt"), "hello\n")
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("case.txt"))
|
||||
testutil.WriteFile(t, filepath.Join(root, "case.txt"), "hello\n")
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("case.txt"))
|
||||
|
||||
err := withWorkingDir(t, root, func() error {
|
||||
err := testutil.WithWorkingDir(t, root, func() error {
|
||||
_, err := ResolveTestDir()
|
||||
return err
|
||||
})
|
||||
@@ -218,13 +219,13 @@ func TestResolveTestDirConfiguredFileFails(t *testing.T) {
|
||||
|
||||
func TestResolveTestDirUsesGitRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
mustGitInit(t, root)
|
||||
testutil.MustGitInit(t, root)
|
||||
want := filepath.Join(root, "e2e")
|
||||
writeFile(t, filepath.Join(root, "miro.toml"), validConfigContent("e2e"))
|
||||
testutil.WriteFile(t, filepath.Join(root, "miro.toml"), testutil.ValidConfigContent("e2e"))
|
||||
subdir := filepath.Join(root, "nested", "dir")
|
||||
mustMkdirAll(t, subdir)
|
||||
testutil.MustMkdirAll(t, subdir)
|
||||
|
||||
got := withWorkingDir(t, subdir, func() string {
|
||||
got := testutil.WithWorkingDir(t, subdir, func() string {
|
||||
path, err := ResolveTestDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveTestDir() error = %v", err)
|
||||
@@ -236,74 +237,3 @@ func TestResolveTestDirUsesGitRoot(t *testing.T) {
|
||||
t.Fatalf("ResolveTestDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func withWorkingDir[T any](t *testing.T, dir string, fn func() T) T {
|
||||
t.Helper()
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Getwd() error = %v", err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("Chdir(%q) error = %v", dir, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := os.Chdir(wd); err != nil {
|
||||
t.Fatalf("restore working directory: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
return fn()
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
|
||||
mustMkdirAll(t, filepath.Dir(path))
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustMkdirAll(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustGitInit(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
|
||||
cmd := exec.Command("git", "init")
|
||||
cmd.Dir = dir
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("git init: %v\n%s", err, out)
|
||||
}
|
||||
}
|
||||
|
||||
func assertRecordShell(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Stat(%q) error = %v", path, err)
|
||||
}
|
||||
if info.Mode().Perm()&0o111 == 0 {
|
||||
t.Fatalf("%q mode = %o, want executable", path, info.Mode().Perm())
|
||||
}
|
||||
|
||||
if got := readFile(t, path); got != buildRecordShellScript() {
|
||||
t.Fatalf("shell = %q, want generated recorder shell", got)
|
||||
}
|
||||
}
|
||||
|
||||
func defaultWrittenConfig(testDir string) string {
|
||||
return "[miro]\n test_dir = \"" + testDir + "\"\n\n[sandbox]\n visible_home = \"/home/test\"\n"
|
||||
}
|
||||
|
||||
func validConfigContent(testDir string) string {
|
||||
return "[miro]\ntest_dir = \"" + testDir + "\"\n\n[sandbox]\nvisible_home = \"/home/test\"\n"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user