mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-04 14:35:32 +00:00
Removes dead code and redundant logic and consolidates tests, so there is less to read and maintain. No behavior change. 1. **act/runner**: inline single-use wrappers, compile regexes once instead of per call, embed the trampoline as a string, drop the unused legacy result of `parseActionCommand` 2. **act/container**: `sanitizeConfig` and `mergeContainerConfigs` stop returning the configs they already mutate in place, drop never-set option fields and the unused `user` parameter of `ExecWithCmdLine` 3. **act/common and cache server**: drop the unused `Trace`/`Tracef` loggers and the short SHA of `FindGitRevision`, `RegisterJob` releases through `RevokeJob` 4. **daemon, poller and reporter**: one shutdown wait for both poll modes, drop an unreachable fetch timeout branch, guard the reporter's last report time with its existing lock instead of an atomic 5. **build**: drop unused Makefile and release workflow variables, collapse the goreleaser ignore list to the same build targets 6. **tests**: fold near-duplicate tests into tables with shared setup Throughout, hand-rolled logic gives way to `cmp.Or`, `strings.Cut`, `slices` and `min`/`max`. Reviewed-on: https://gitea.com/gitea/runner/pulls/1335 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
205 lines
4.4 KiB
Go
205 lines
4.4 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package ghcontext
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"gitea.dev/actionslib/pkg/model"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSetRef(t *testing.T) {
|
|
oldFindGitRef := findGitRef
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
|
|
findGitRef = func(_ctx context.Context, _file string) (string, error) {
|
|
return "refs/heads/master", nil
|
|
}
|
|
|
|
tables := []struct {
|
|
eventName string
|
|
event map[string]any
|
|
ref string
|
|
refName string
|
|
}{
|
|
{
|
|
eventName: "pull_request_target",
|
|
event: map[string]any{},
|
|
ref: "refs/heads/master",
|
|
refName: "master",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{
|
|
"number": 1234.,
|
|
},
|
|
ref: "refs/pull/1234/merge",
|
|
refName: "1234/merge",
|
|
},
|
|
{
|
|
eventName: "deployment",
|
|
event: map[string]any{
|
|
"deployment": map[string]any{
|
|
"ref": "refs/heads/somebranch",
|
|
},
|
|
},
|
|
ref: "refs/heads/somebranch",
|
|
refName: "somebranch",
|
|
},
|
|
{
|
|
eventName: "release",
|
|
event: map[string]any{
|
|
"release": map[string]any{
|
|
"tag_name": "v1.0.0",
|
|
},
|
|
},
|
|
ref: "refs/tags/v1.0.0",
|
|
refName: "v1.0.0",
|
|
},
|
|
{
|
|
eventName: "push",
|
|
event: map[string]any{
|
|
"ref": "refs/heads/somebranch",
|
|
},
|
|
ref: "refs/heads/somebranch",
|
|
refName: "somebranch",
|
|
},
|
|
{
|
|
eventName: "unknown",
|
|
event: map[string]any{
|
|
"repository": map[string]any{
|
|
"default_branch": "main",
|
|
},
|
|
},
|
|
ref: "refs/heads/main",
|
|
refName: "main",
|
|
},
|
|
{
|
|
eventName: "no-event",
|
|
event: map[string]any{},
|
|
ref: "refs/heads/master",
|
|
refName: "master",
|
|
},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
ghc := &model.GithubContext{
|
|
EventName: table.eventName,
|
|
BaseRef: "master",
|
|
Event: table.event,
|
|
}
|
|
|
|
SetRef(t.Context(), ghc, "/some/dir")
|
|
ghc.SetRefTypeAndName()
|
|
|
|
assert.Equal(t, table.ref, ghc.Ref)
|
|
assert.Equal(t, table.refName, ghc.RefName)
|
|
})
|
|
}
|
|
|
|
t.Run("no-default-branch", func(t *testing.T) {
|
|
findGitRef = func(_ctx context.Context, _file string) (string, error) {
|
|
return "", errors.New("no default branch")
|
|
}
|
|
|
|
ghc := &model.GithubContext{
|
|
EventName: "no-default-branch",
|
|
Event: map[string]any{},
|
|
}
|
|
|
|
SetRef(t.Context(), ghc, "/some/dir")
|
|
|
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
|
})
|
|
}
|
|
|
|
func TestSetSha(t *testing.T) {
|
|
oldFindGitRef := findGitRef
|
|
oldFindGitRevision := findGitRevision
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
|
|
findGitRevision = func(_ctx context.Context, _file string) (string, error) {
|
|
return "1234fakesha", nil
|
|
}
|
|
|
|
tables := []struct {
|
|
eventName string
|
|
event map[string]any
|
|
sha string
|
|
}{
|
|
{
|
|
eventName: "pull_request_target",
|
|
event: map[string]any{
|
|
"pull_request": map[string]any{
|
|
"base": map[string]any{
|
|
"sha": "pr-base-sha",
|
|
},
|
|
},
|
|
},
|
|
sha: "pr-base-sha",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{
|
|
"number": 1234.,
|
|
},
|
|
sha: "1234fakesha",
|
|
},
|
|
{
|
|
eventName: "deployment",
|
|
event: map[string]any{
|
|
"deployment": map[string]any{
|
|
"sha": "deployment-sha",
|
|
},
|
|
},
|
|
sha: "deployment-sha",
|
|
},
|
|
{
|
|
eventName: "push",
|
|
event: map[string]any{
|
|
"after": "push-sha",
|
|
"deleted": false,
|
|
},
|
|
sha: "push-sha",
|
|
},
|
|
{
|
|
eventName: "unknown",
|
|
event: map[string]any{},
|
|
sha: "1234fakesha",
|
|
},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
ghc := &model.GithubContext{
|
|
EventName: table.eventName,
|
|
Event: table.event,
|
|
}
|
|
|
|
SetSha(t.Context(), ghc, "/some/dir")
|
|
|
|
assert.Equal(t, table.sha, ghc.Sha)
|
|
})
|
|
}
|
|
|
|
t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) {
|
|
ctx := WithPinnedCheckout(t.Context())
|
|
for _, head := range []string{"first", "moved"} {
|
|
findGitRevision = func(context.Context, string) (string, error) { return head, nil }
|
|
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }
|
|
ghc := &model.GithubContext{Event: map[string]any{}}
|
|
SetSha(ctx, ghc, "/some/dir")
|
|
SetRef(ctx, ghc, "/some/dir")
|
|
assert.Equal(t, "first", ghc.Sha)
|
|
assert.Equal(t, "refs/heads/first", ghc.Ref)
|
|
}
|
|
})
|
|
}
|