| 1 | package workspace |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestDirectWorkspaceIdentity(t *testing.T) { |
| 10 | f := setup(t) |
| 11 | var err error |
| 12 | f.m, err = New(f.cfg) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | w := f.create(t) |
| 17 | if w.ID != f.a.WorkspaceKey || w.Path == "" { |
| 18 | t.Fatal("missing local identity") |
| 19 | } |
| 20 | if git(t, w.Path, "rev-parse", "HEAD") != f.target.BaseRevision || git(t, w.Path, "branch", "--show-current") != f.target.Ref { |
| 21 | t.Fatal("base or ref changed") |
| 22 | } |
| 23 | dir := filepath.Join(w.Path, ".git") |
| 24 | if info, e := os.Lstat(dir); e != nil || !info.IsDir() { |
| 25 | t.Fatal("metadata escapes checkout") |
| 26 | } |
| 27 | if git(t, w.Path, "rev-parse", "--git-common-dir") != ".git" { |
| 28 | t.Fatal("external common directory") |
| 29 | } |
| 30 | if e := f.m.ValidateTurn(ctx, f.a, w); e != nil { |
| 31 | t.Fatal(e) |
| 32 | } |
| 33 | for _, field := range []string{"id", "path"} { |
| 34 | changed := w |
| 35 | if field == "id" { |
| 36 | changed.ID = "other" |
| 37 | } else { |
| 38 | changed.Path = f.root |
| 39 | } |
| 40 | if err := f.m.ValidateTurn(ctx, f.a, changed); err == nil { |
| 41 | t.Fatalf("accepted changed local workspace %s", field) |
| 42 | } |
| 43 | } |
| 44 | for _, name := range []string{"config", "hooks/pre-commit", "objects/info/alternates", "commondir"} { |
| 45 | t.Run(name, func(t *testing.T) { |
| 46 | path := filepath.Join(dir, name) |
| 47 | old, readErr := os.ReadFile(path) |
| 48 | if e := os.MkdirAll(filepath.Dir(path), 0700); e != nil { |
| 49 | t.Fatal(e) |
| 50 | } |
| 51 | put(t, path, "untrusted\n") |
| 52 | if e := f.m.ValidateTurn(ctx, f.a, w); e == nil { |
| 53 | t.Fatal("accepted untrusted Git metadata") |
| 54 | } |
| 55 | if readErr == nil { |
| 56 | put(t, path, string(old)) |
| 57 | } else if e := os.Remove(path); e != nil { |
| 58 | t.Fatal(e) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | put(t, filepath.Join(w.Path, "file"), "candidate\n") |
| 63 | git(t, w.Path, "commit", "-am", "candidate") |
| 64 | c, err := f.m.CaptureCandidate(ctx, w, f.target) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | put(t, filepath.Join(w.Path, "file"), "later\n") |
| 69 | git(t, w.Path, "commit", "-am", "later") |
| 70 | export := filepath.Join(f.root, "export") |
| 71 | if err = f.m.ExportCandidate(ctx, c, export); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if got := git(t, export, "rev-parse", "HEAD"); got != c.Commit { |
| 75 | t.Fatal("export followed builder changes") |
| 76 | } |
| 77 | again, err := f.m.Create(ctx, f.a, f.brief) |
| 78 | if err != nil || again.ID != w.ID || again.Path != w.Path { |
| 79 | t.Fatalf("retry replaced workspace: %+v %v", again, err) |
| 80 | } |
| 81 | f.a.WorkspaceKey = "attempt-2" |
| 82 | next := f.create(t) |
| 83 | if next.Path == w.Path || git(t, next.Path, "rev-parse", "HEAD") != f.target.BaseRevision { |
| 84 | t.Fatal("attempt reused mutable checkout") |
| 85 | } |
| 86 | if err := os.RemoveAll(filepath.Join(next.Path, ".git")); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if _, err := f.m.Create(ctx, f.a, f.brief); err == nil { |
| 90 | t.Fatal("uncertain workspace replaced") |
| 91 | } |
| 92 | } |
| 93 | |