|
| 1 | +package checkpoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/entireio/cli/cmd/entire/cli/paths" |
| 13 | + |
| 14 | + "github.com/go-git/go-git/v6/plumbing" |
| 15 | + "github.com/go-git/go-git/v6/plumbing/object" |
| 16 | + "github.com/go-git/go-git/v6/x/plugin" |
| 17 | +) |
| 18 | + |
| 19 | +type stubSigner struct { |
| 20 | + sig []byte |
| 21 | + err error |
| 22 | +} |
| 23 | + |
| 24 | +func (s *stubSigner) Sign(_ io.Reader) ([]byte, error) { |
| 25 | + return s.sig, s.err |
| 26 | +} |
| 27 | + |
| 28 | +func setupSigningEnv(t *testing.T, disableSigning bool) { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + dir := t.TempDir() |
| 32 | + |
| 33 | + // Minimal git repo so paths.WorktreeRoot resolves. |
| 34 | + if err := os.MkdirAll(filepath.Join(dir, ".git"), 0o755); err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + entireDir := filepath.Join(dir, ".entire") |
| 39 | + if err := os.MkdirAll(entireDir, 0o755); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + if disableSigning { |
| 44 | + content := `{"sign_checkpoint_commits": false}` |
| 45 | + if err := os.WriteFile(filepath.Join(entireDir, "settings.json"), []byte(content), 0o644); err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + paths.ClearWorktreeRootCache() |
| 51 | + t.Chdir(dir) |
| 52 | + t.Cleanup(func() { |
| 53 | + resetPluginEntry("object-signer") |
| 54 | + paths.ClearWorktreeRootCache() |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func newTestCommit() *object.Commit { |
| 59 | + sig := object.Signature{ |
| 60 | + Name: "Test", |
| 61 | + Email: "test@test.com", |
| 62 | + When: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), |
| 63 | + } |
| 64 | + return &object.Commit{ |
| 65 | + TreeHash: plumbing.ZeroHash, |
| 66 | + Author: sig, |
| 67 | + Committer: sig, |
| 68 | + Message: "test commit", |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestSignCommitBestEffort_Signs(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel |
| 73 | + setupSigningEnv(t, false) |
| 74 | + |
| 75 | + err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer { |
| 76 | + return &stubSigner{sig: []byte("FAKESIG")} |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + |
| 82 | + commit := newTestCommit() |
| 83 | + SignCommitBestEffort(context.Background(), commit) |
| 84 | + |
| 85 | + if commit.Signature != "FAKESIG" { |
| 86 | + t.Errorf("expected signature %q, got %q", "FAKESIG", commit.Signature) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestSignCommitBestEffort_SkipsWhenDisabled(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel |
| 91 | + setupSigningEnv(t, true) |
| 92 | + |
| 93 | + err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer { |
| 94 | + t.Fatal("signer should not be called when signing is disabled") |
| 95 | + return nil |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + commit := newTestCommit() |
| 102 | + SignCommitBestEffort(context.Background(), commit) |
| 103 | + |
| 104 | + if commit.Signature != "" { |
| 105 | + t.Errorf("expected empty signature, got %q", commit.Signature) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestSignCommitBestEffort_ErrorIsBestEffort(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel |
| 110 | + setupSigningEnv(t, false) |
| 111 | + |
| 112 | + err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer { |
| 113 | + return &stubSigner{err: errors.New("signing failed")} |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + |
| 119 | + commit := newTestCommit() |
| 120 | + SignCommitBestEffort(context.Background(), commit) |
| 121 | + |
| 122 | + if commit.Signature != "" { |
| 123 | + t.Errorf("expected empty signature after error, got %q", commit.Signature) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestSignCommitBestEffort_NoSignerRegistered(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel |
| 128 | + setupSigningEnv(t, false) |
| 129 | + |
| 130 | + commit := newTestCommit() |
| 131 | + SignCommitBestEffort(context.Background(), commit) |
| 132 | + |
| 133 | + if commit.Signature != "" { |
| 134 | + t.Errorf("expected empty signature without signer, got %q", commit.Signature) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestSignCommitBestEffort_NilSigner(t *testing.T) { //nolint:paralleltest // t.Chdir requires non-parallel |
| 139 | + setupSigningEnv(t, false) |
| 140 | + |
| 141 | + err := plugin.Register(plugin.ObjectSigner(), func() plugin.Signer { |
| 142 | + return nil |
| 143 | + }) |
| 144 | + if err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + |
| 148 | + commit := newTestCommit() |
| 149 | + SignCommitBestEffort(context.Background(), commit) |
| 150 | + |
| 151 | + if commit.Signature != "" { |
| 152 | + t.Errorf("expected empty signature with nil signer, got %q", commit.Signature) |
| 153 | + } |
| 154 | +} |
0 commit comments