v0.9.0 dd8597a

mergerequests_test.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase_test
4
5import (
6	"context"
7	"errors"
8	"testing"
9
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/mock"
12	"github.com/stretchr/testify/require"
13
14	"bnema.dev/ermite/internal/domain"
15	"bnema.dev/ermite/internal/ports"
16	"bnema.dev/ermite/internal/ports/mocks"
17	"bnema.dev/ermite/internal/usecase"
18)
19
20type mrUnit struct {
21	meta  *mocks.RepoMetaStore
22	store *mocks.MergeRequestStore
23	repos *mocks.GitRepos
24	git   *mocks.MergeGit
25	mrs   *usecase.MergeRequests
26	repo  domain.Repo
27}
28
29func newMRUnit(t *testing.T) *mrUnit {
30	t.Helper()
31	u := &mrUnit{
32		meta:  mocks.NewRepoMetaStore(t),
33		store: mocks.NewMergeRequestStore(t),
34		repos: mocks.NewGitRepos(t),
35		git:   mocks.NewMergeGit(t),
36		repo:  domain.Repo{Name: name(t, "proj"), Visibility: domain.VisibilityPrivate, Settings: domain.DefaultRepoSettings()},
37	}
38	clock := mocks.NewClock(t)
39	clock.EXPECT().Now().Return(now).Maybe()
40	sched := mocks.NewMaintenanceScheduler(t)
41	sched.EXPECT().Schedule(mock.Anything, mock.Anything).Return().Maybe()
42	u.mrs = usecase.NewMergeRequests(usecase.MergeRequestsConfig{
43		Meta: u.meta, Store: u.store, Repos: u.repos, Git: u.git,
44		Signer: mocks.NewCommitSigner(t), Bus: mocks.NewEventBus(t), Clock: clock, Maint: sched,
45	})
46	return u
47}
48
49// GO-004: a failure to read Git is an error for the log, not a field violation
50// that would show its text to the caller.
51func TestCreateReturnsGitFailuresAsErrors(t *testing.T) {
52	t.Parallel()
53	boom := errors.New("fatal: /srv/ermite/repos/proj.git: permission denied")
54	params := ports.CreateMergeRequestParams{Title: "t", Source: "feat", Target: "main"}
55
56	t.Run("reading a branch", func(t *testing.T) {
57		t.Parallel()
58		u := newMRUnit(t)
59		u.meta.EXPECT().Get(mock.Anything, u.repo.Name).Return(u.repo, nil)
60		u.git.EXPECT().BranchHead(mock.Anything, u.repo.Name, "feat").Return(domain.ObjectID{}, boom)
61		_, err := u.mrs.Create(context.Background(), domain.Root(), u.repo.Name, params)
62		require.ErrorIs(t, err, boom)
63		var fe *domain.InvalidFieldsError
64		assert.False(t, errors.As(err, &fe), "not a field violation: %v", err)
65		assert.NotErrorIs(t, err, domain.ErrInvalid)
66	})
67
68	t.Run("reading the default branch", func(t *testing.T) {
69		t.Parallel()
70		u := newMRUnit(t)
71		u.meta.EXPECT().Get(mock.Anything, u.repo.Name).Return(u.repo, nil)
72		u.repos.EXPECT().DefaultBranch(mock.Anything, u.repo.Name).Return("", false, boom)
73		_, err := u.mrs.Create(context.Background(), domain.Root(), u.repo.Name, ports.CreateMergeRequestParams{Title: "t", Source: "feat"})
74		require.ErrorIs(t, err, boom)
75		var fe *domain.InvalidFieldsError
76		assert.False(t, errors.As(err, &fe), "not a field violation: %v", err)
77	})
78
79	t.Run("a branch that does not exist stays a violation", func(t *testing.T) {
80		t.Parallel()
81		u := newMRUnit(t)
82		u.meta.EXPECT().Get(mock.Anything, u.repo.Name).Return(u.repo, nil)
83		u.git.EXPECT().BranchHead(mock.Anything, u.repo.Name, "feat").Return(domain.ObjectID{}, domain.ErrNotFound)
84		u.git.EXPECT().BranchHead(mock.Anything, u.repo.Name, "main").Return(someID(t), nil)
85		_, err := u.mrs.Create(context.Background(), domain.Root(), u.repo.Name, params)
86		assert.Contains(t, fieldsOf(t, err), "source_branch")
87	})
88}
89
90func someID(t *testing.T) domain.ObjectID {
91	t.Helper()
92	return oidFrom(t, oidA)
93}
94
95// GO-003: the settings are read again under the merge lock; a failure of that
96// read is returned, and a repository deleted meanwhile is not found.
97func TestMergeReturnsTheFailureToReadTheSettings(t *testing.T) {
98	t.Parallel()
99	boom := errors.New("kv: closed")
100	for label, tc := range map[string]struct{ err, want error }{
101		"read failure":      {boom, boom},
102		"deleted meanwhile": {domain.ErrNotFound, domain.ErrNotFound},
103	} {
104		t.Run(label, func(t *testing.T) {
105			t.Parallel()
106			u := newMRUnit(t)
107			u.meta.EXPECT().Get(mock.Anything, u.repo.Name).Return(u.repo, nil).Once()
108			u.meta.EXPECT().Get(mock.Anything, u.repo.Name).Return(domain.Repo{}, tc.err).Once()
109			u.store.EXPECT().Get(mock.Anything, u.repo.Name, int64(1)).Return(domain.MergeRequest{
110				Repo: u.repo.Name, Number: 1, State: domain.MRStateOpen, Source: "feat", Target: "main",
111			}, nil)
112			_, err := u.mrs.Merge(context.Background(), domain.Root(), u.repo.Name, 1, ports.MergeParams{HeadID: someID(t)})
113			require.ErrorIs(t, err, tc.want)
114			if tc.want == boom {
115				assert.NotErrorIs(t, err, domain.ErrNotFound)
116			}
117		})
118	}
119}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close