|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/morluto/gitcontribute/internal/cli" |
| 13 | + "github.com/morluto/gitcontribute/internal/config" |
| 14 | + "github.com/morluto/gitcontribute/internal/workspace" |
| 15 | +) |
| 16 | + |
| 17 | +func TestWorkspaceCreateAndShow(t *testing.T) { |
| 18 | + if _, err := exec.LookPath("git"); err != nil { |
| 19 | + t.Skip("git not available") |
| 20 | + } |
| 21 | + ctx := context.Background() |
| 22 | + remote, baseSHA, candidateSHA := setupAppGitRemote(t) |
| 23 | + |
| 24 | + paths := config.NewPaths(&config.Env{Home: t.TempDir()}) |
| 25 | + svc, err := New(paths, "test", nil) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("new service: %v", err) |
| 28 | + } |
| 29 | + defer func() { _ = svc.Close() }() |
| 30 | + if _, err := svc.Init(ctx); err != nil { |
| 31 | + t.Fatalf("init: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + inv, err := svc.StartInvestigation(ctx, cli.RepoRef{Owner: "owner", Repo: "repo"}, candidateSHA, "") |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("start investigation: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + t.Run("rejects credential remote before persistence", func(t *testing.T) { |
| 40 | + fixtureUser := strings.Join([]string{"fixture", "user"}, "-") |
| 41 | + fixturePassword := strings.Join([]string{"fixture", "password"}, "-") |
| 42 | + credentialRemote := "https://" + fixtureUser + ":" + fixturePassword + "@github.com/owner/repo.git" |
| 43 | + _, err := svc.CreateWorkspace(ctx, inv.ID, cli.WorkspaceCreateOptions{ |
| 44 | + Remote: credentialRemote, |
| 45 | + Name: "credential-test", |
| 46 | + }) |
| 47 | + if !errors.Is(err, workspace.ErrInvalidRemote) { |
| 48 | + t.Fatalf("CreateWorkspace credential remote error = %v, want ErrInvalidRemote", err) |
| 49 | + } |
| 50 | + if strings.Contains(err.Error(), fixturePassword) { |
| 51 | + t.Fatalf("CreateWorkspace error exposed credential: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + dataDir, err := paths.DataDir() |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + if _, err := os.Stat(filepath.Join(dataDir, "workspaces", "mirrors")); !errors.Is(err, os.ErrNotExist) { |
| 59 | + t.Fatalf("mirror directory was written before remote validation: %v", err) |
| 60 | + } |
| 61 | + c, err := svc.openCorpus(ctx) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if _, err := c.GetWorkspace(ctx, "credential-test"); !errors.Is(err, workspace.ErrNotFound) { |
| 66 | + t.Fatalf("credential remote workspace was persisted: %v", err) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + ws, err := svc.CreateWorkspace(ctx, inv.ID, cli.WorkspaceCreateOptions{ |
| 71 | + Remote: remote, |
| 72 | + BaseRef: "master", |
| 73 | + CandidateRef: "feature", |
| 74 | + Name: "ws-test", |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("create workspace: %v", err) |
| 78 | + } |
| 79 | + if ws.ID != "ws-test" || ws.InvestigationID != inv.ID || ws.BaseSHA != baseSHA || ws.CandidateSHA != candidateSHA { |
| 80 | + t.Fatalf("unexpected workspace: %+v", ws) |
| 81 | + } |
| 82 | + if _, err := os.Stat(ws.Path); err != nil { |
| 83 | + t.Fatalf("workspace path missing: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + shown, err := svc.ShowWorkspace(ctx, ws.ID) |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("show workspace: %v", err) |
| 89 | + } |
| 90 | + if shown.ID != ws.ID || shown.BaseSHA != baseSHA { |
| 91 | + t.Fatalf("workspace roundtrip failed: %+v", shown) |
| 92 | + } |
| 93 | +} |
0 commit comments