Skip to content

Commit d07c04c

Browse files
committed
Add test for loadIsRefDeleted
1 parent 18c303e commit d07c04c

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

routers/web/repo/actions/actions.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"bytes"
8+
stdCtx "context"
89
"fmt"
910
"net/http"
1011
"slices"
@@ -245,7 +246,7 @@ func List(ctx *context.Context) {
245246
return
246247
}
247248

248-
if err := loadIsRefDeleted(ctx, runs); err != nil {
249+
if err := loadIsRefDeleted(ctx, ctx.Repo.Repository.ID, runs); err != nil {
249250
log.Error("LoadIsRefDeleted", err)
250251
}
251252

@@ -273,7 +274,7 @@ func List(ctx *context.Context) {
273274

274275
// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.
275276
// TODO: move this function to models/actions/run_list.go but now it will result in a circular import.
276-
func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
277+
func loadIsRefDeleted(ctx stdCtx.Context, repoID int64, runs actions_model.RunList) error {
277278
branches := make(container.Set[string], len(runs))
278279
for _, run := range runs {
279280
refName := git.RefName(run.Ref)
@@ -285,7 +286,7 @@ func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
285286
return nil
286287
}
287288

288-
branchInfos, err := git_model.GetBranches(ctx, ctx.Repo.Repository.ID, branches.Values(), false)
289+
branchInfos, err := git_model.GetBranches(ctx, repoID, branches.Values(), false)
289290
if err != nil {
290291
return err
291292
}

routers/web/repo/actions/actions_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"strings"
88
"testing"
99

10+
actions_model "code.gitea.io/gitea/models/actions"
11+
"code.gitea.io/gitea/models/db"
12+
unittest "code.gitea.io/gitea/models/unittest"
1013
act_model "github.com/nektos/act/pkg/model"
1114
"github.com/stretchr/testify/assert"
1215
)
@@ -154,3 +157,20 @@ func TestReadWorkflow_WorkflowDispatchConfig(t *testing.T) {
154157
Type: "boolean",
155158
}, workflowDispatch.Inputs[2])
156159
}
160+
161+
func Test_loadIsRefDeleted(t *testing.T) {
162+
unittest.PrepareTestEnv(t)
163+
164+
runs, total, err := db.FindAndCount[actions_model.ActionRun](db.DefaultContext, actions_model.FindRunOptions{RepoID: 4})
165+
assert.NoError(t, err)
166+
assert.Len(t, runs, 3)
167+
assert.EqualValues(t, 3, total)
168+
for _, run := range runs {
169+
assert.False(t, run.IsRefDeleted)
170+
}
171+
172+
assert.NoError(t, loadIsRefDeleted(db.DefaultContext, 4, runs))
173+
for _, run := range runs {
174+
assert.True(t, run.IsRefDeleted)
175+
}
176+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
unittest.MainTest(m)
14+
}

0 commit comments

Comments
 (0)