WithRepository runs a test function with an isolated repository and UserActions
(t *testing.T, name string, setup RepositorySetup, fn func(t *testing.T, repo *repository.Repository, user *UserActions))
| 36 | |
| 37 | // WithRepository runs a test function with an isolated repository and UserActions |
| 38 | func WithRepository(t *testing.T, name string, setup RepositorySetup, fn func(t *testing.T, repo *repository.Repository, user *UserActions)) { |
| 39 | // Initialize Dagger (needed for environment operations) |
| 40 | initializeDaggerOnce(t) |
| 41 | |
| 42 | ctx := context.Background() |
| 43 | |
| 44 | // Create isolated temp directories |
| 45 | repoDir, err := os.MkdirTemp("", "cu-test-"+name+"-*") |
| 46 | require.NoError(t, err, "Failed to create repo dir") |
| 47 | |
| 48 | configDir, err := os.MkdirTemp("", "cu-test-config-"+name+"-*") |
| 49 | require.NoError(t, err, "Failed to create config dir") |
| 50 | |
| 51 | // Initialize git repo |
| 52 | cmds := [][]string{ |
| 53 | {"init"}, |
| 54 | {"config", "user.email", "test@example.com"}, |
| 55 | {"config", "user.name", "Test User"}, |
| 56 | {"config", "commit.gpgsign", "false"}, |
| 57 | } |
| 58 | |
| 59 | for _, cmd := range cmds { |
| 60 | _, err := repository.RunGitCommand(ctx, repoDir, cmd...) |
| 61 | require.NoError(t, err, "Failed to run git %v", cmd) |
| 62 | } |
| 63 | |
| 64 | // Run setup to populate repo |
| 65 | if setup != nil { |
| 66 | setup(t, repoDir) |
| 67 | } |
| 68 | |
| 69 | // Open repository with isolated base path |
| 70 | repo, err := repository.OpenWithBasePath(ctx, repoDir, configDir) |
| 71 | require.NoError(t, err, "Failed to open repository") |
| 72 | |
| 73 | // Create UserActions with extended capabilities |
| 74 | user := NewUserActions(t, repo, testDaggerClient).WithDirectAccess(repoDir, configDir) |
| 75 | |
| 76 | // Cleanup |
| 77 | t.Cleanup(func() { |
| 78 | // Clean up any environments created during the test |
| 79 | envs, _ := repo.List(context.Background()) |
| 80 | for _, env := range envs { |
| 81 | repo.Delete(context.Background(), env.ID) |
| 82 | } |
| 83 | |
| 84 | // Remove directories |
| 85 | os.RemoveAll(repoDir) |
| 86 | os.RemoveAll(configDir) |
| 87 | }) |
| 88 | |
| 89 | // Run the test function |
| 90 | fn(t, repo, user) |
| 91 | } |
| 92 | |
| 93 | // RepositorySetup is a function that prepares a test repository |
| 94 | type RepositorySetup func(t *testing.T, repoDir string) |