mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-04-24 12:50:31 +08:00
Merges the `gitea.com/gitea/act` fork into this repository as the `act/` directory and consumes it as a local package. The `replace github.com/nektos/act => gitea.com/gitea/act` directive is removed; act's dependencies are merged into the root `go.mod`. - Imports rewritten: `github.com/nektos/act/pkg/...` → `gitea.com/gitea/act_runner/act/...` (flattened — `pkg/` boundary dropped to match the layout forgejo-runner adopted). - Dropped act's CLI (`cmd/`, `main.go`) and all upstream project files; kept the library tree + `LICENSE`. - Added `// Copyright <year> The Gitea Authors ...` / `// Copyright <year> nektos` headers to 104 `.go` files. - Pre-existing act lint violations annotated inline with `//nolint:<linter> // pre-existing issue from nektos/act`. `.golangci.yml` is unchanged vs `main`. - Makefile test target: `-race -short` (matches forgejo-runner). - Pre-existing integration test failures fixed: race in parallel executor (atomic counters); TestSetupEnv / command_test / expression_test / run_context_test updated to match gitea fork runtime; TestJobExecutor and TestActionCache gated on `testing.Short()`. Full `gitea/act` commit history is reachable via the second parent. Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
217 lines
4.4 KiB
Go
217 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 model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSetRef(t *testing.T) {
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
oldFindGitRef := findGitRef
|
|
oldFindGitRevision := findGitRevision
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
|
|
findGitRef = func(ctx context.Context, file string) (string, error) {
|
|
return "refs/heads/master", nil
|
|
}
|
|
|
|
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
|
|
return "", "1234fakesha", 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 := &GithubContext{
|
|
EventName: table.eventName,
|
|
BaseRef: "master",
|
|
Event: table.event,
|
|
}
|
|
|
|
ghc.SetRef(context.Background(), "main", "/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 := &GithubContext{
|
|
EventName: "no-default-branch",
|
|
Event: map[string]any{},
|
|
}
|
|
|
|
ghc.SetRef(context.Background(), "", "/some/dir")
|
|
|
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
|
})
|
|
}
|
|
|
|
func TestSetSha(t *testing.T) {
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
oldFindGitRef := findGitRef
|
|
oldFindGitRevision := findGitRevision
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
|
|
findGitRef = func(ctx context.Context, file string) (string, error) {
|
|
return "refs/heads/master", nil
|
|
}
|
|
|
|
findGitRevision = func(ctx context.Context, file string) (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: "release",
|
|
event: map[string]any{},
|
|
sha: "1234fakesha",
|
|
},
|
|
{
|
|
eventName: "push",
|
|
event: map[string]any{
|
|
"after": "push-sha",
|
|
"deleted": false,
|
|
},
|
|
sha: "push-sha",
|
|
},
|
|
{
|
|
eventName: "unknown",
|
|
event: map[string]any{},
|
|
sha: "1234fakesha",
|
|
},
|
|
{
|
|
eventName: "no-event",
|
|
event: map[string]any{},
|
|
sha: "1234fakesha",
|
|
},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
ghc := &GithubContext{
|
|
EventName: table.eventName,
|
|
BaseRef: "master",
|
|
Event: table.event,
|
|
}
|
|
|
|
ghc.SetSha(context.Background(), "/some/dir")
|
|
|
|
assert.Equal(t, table.sha, ghc.Sha)
|
|
})
|
|
}
|
|
}
|