mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-04-25 21:30:23 +08:00
Use `golangci-lint fmt` to format code, upgrading `.golangci.yml` to v2 and mirroring the linter configuration used by https://github.com/go-gitea/gitea. `gci` now handles import ordering into standard, project-local, blank, and default groups. Mirrors https://github.com/go-gitea/gitea/pull/37194. Changes: - Upgrade `.golangci.yml` to v2 format with the same linter set as gitea (minus `prealloc`, `unparam`, `testifylint`, `nilnil` which produced too many pre-existing issues) - Add path-based exclusions (`bodyclose`, `gosec` in tests; `gosec:G115`/`G117` globally) - Run lint via `make lint-go` in CI instead of `golangci/golangci-lint-action`, matching the pattern used by other Gitea repos - Apply safe auto-fixes (`modernize`, `perfsprint`, `usetesting`, etc.) - Add explanations to existing `//nolint` directives - Remove dead code (unused `newRemoteReusableWorkflow` and `networkName`), duplicate imports, and shadowed `max` builtins - Replace deprecated `docker/distribution/reference` with `distribution/reference` - Fix `Deprecated:` comment casing and simplify nil/len checks --- This PR was written with the help of Claude Opus 4.7 Reviewed-on: https://gitea.com/gitea/act/pulls/163 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
153 lines
3.2 KiB
Go
153 lines
3.2 KiB
Go
package common
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewWorkflow(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
// empty
|
|
emptyWorkflow := NewPipelineExecutor()
|
|
assert.Nil(emptyWorkflow(ctx))
|
|
|
|
// error case
|
|
errorWorkflow := NewErrorExecutor(errors.New("test error"))
|
|
assert.NotNil(errorWorkflow(ctx))
|
|
|
|
// multiple success case
|
|
runcount := 0
|
|
successWorkflow := NewPipelineExecutor(
|
|
func(ctx context.Context) error {
|
|
runcount++
|
|
return nil
|
|
},
|
|
func(ctx context.Context) error {
|
|
runcount++
|
|
return nil
|
|
})
|
|
assert.Nil(successWorkflow(ctx))
|
|
assert.Equal(2, runcount)
|
|
}
|
|
|
|
func TestNewConditionalExecutor(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
trueCount := 0
|
|
falseCount := 0
|
|
|
|
err := NewConditionalExecutor(func(ctx context.Context) bool {
|
|
return false
|
|
}, func(ctx context.Context) error {
|
|
trueCount++
|
|
return nil
|
|
}, func(ctx context.Context) error {
|
|
falseCount++
|
|
return nil
|
|
})(ctx)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(0, trueCount)
|
|
assert.Equal(1, falseCount)
|
|
|
|
err = NewConditionalExecutor(func(ctx context.Context) bool {
|
|
return true
|
|
}, func(ctx context.Context) error {
|
|
trueCount++
|
|
return nil
|
|
}, func(ctx context.Context) error {
|
|
falseCount++
|
|
return nil
|
|
})(ctx)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(1, trueCount)
|
|
assert.Equal(1, falseCount)
|
|
}
|
|
|
|
func TestNewParallelExecutor(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
count := 0
|
|
activeCount := 0
|
|
maxCount := 0
|
|
emptyWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
count++
|
|
|
|
activeCount++
|
|
if activeCount > maxCount {
|
|
maxCount = activeCount
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
activeCount--
|
|
|
|
return nil
|
|
})
|
|
|
|
err := NewParallelExecutor(2, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
|
|
|
assert.Equal(3, count, "should run all 3 executors")
|
|
assert.Equal(2, maxCount, "should run at most 2 executors in parallel")
|
|
assert.Nil(err)
|
|
|
|
// Reset to test running the executor with 0 parallelism
|
|
count = 0
|
|
activeCount = 0
|
|
maxCount = 0
|
|
|
|
errSingle := NewParallelExecutor(0, emptyWorkflow, emptyWorkflow, emptyWorkflow)(ctx)
|
|
|
|
assert.Equal(3, count, "should run all 3 executors")
|
|
assert.Equal(1, maxCount, "should run at most 1 executors in parallel")
|
|
assert.Nil(errSingle)
|
|
}
|
|
|
|
func TestNewParallelExecutorFailed(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
count := 0
|
|
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
count++
|
|
return errors.New("fake error")
|
|
})
|
|
err := NewParallelExecutor(1, errorWorkflow)(ctx)
|
|
assert.Equal(1, count)
|
|
assert.ErrorIs(context.Canceled, err)
|
|
}
|
|
|
|
func TestNewParallelExecutorCanceled(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
errExpected := errors.New("fake error")
|
|
|
|
count := 0
|
|
successWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
count++
|
|
return nil
|
|
})
|
|
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
|
|
count++
|
|
return errExpected
|
|
})
|
|
err := NewParallelExecutor(3, errorWorkflow, successWorkflow, successWorkflow)(ctx)
|
|
assert.Equal(3, count)
|
|
assert.Error(errExpected, err)
|
|
}
|