mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-15 20:33:42 +00:00
refactor: remove dead code and consolidate tests (#1335)
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]>
This commit is contained in:
1 parent
923a0bfb59
commit
d882311ec6
107 files changed
+2408
-6500
No files matched your search
@@ -13,21 +13,17 @@ import (
|
||||
)
|
||||
|
||||
func Test_generateWorkflow(t *testing.T) {
|
||||
type args struct {
|
||||
task *runnerv1.Task
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
payload string
|
||||
needs map[string]*runnerv1.TaskNeed
|
||||
assert func(t *testing.T, wf *model.Workflow)
|
||||
want1 string
|
||||
jobID string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "has needs",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Build and deploy
|
||||
on: push
|
||||
|
||||
@@ -39,34 +35,29 @@ jobs:
|
||||
- uses: actions/checkout@v3
|
||||
- run: ./deploy --build ${{ needs.job1.outputs.output1 }}
|
||||
- run: ./deploy --build ${{ needs.job2.outputs.output2 }}
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{
|
||||
"job1": {
|
||||
Outputs: map[string]string{
|
||||
"output1": "output1 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"job2": {
|
||||
Outputs: map[string]string{
|
||||
"output2": "output2 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{
|
||||
"job1": {
|
||||
Outputs: map[string]string{
|
||||
"output1": "output1 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"job2": {
|
||||
Outputs: map[string]string{
|
||||
"output2": "output2 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, []string{"job1", "job2"}, wf.GetJob("job9").Needs())
|
||||
},
|
||||
want1: "job9",
|
||||
wantErr: false,
|
||||
jobID: "job9",
|
||||
},
|
||||
{
|
||||
name: "no needs",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Simple workflow
|
||||
on: push
|
||||
|
||||
@@ -76,23 +67,18 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: echo "hello"
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("test")
|
||||
require.Equal(t, []string{}, job.Needs())
|
||||
require.Len(t, job.Steps, 2)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "needs list",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Workflow with list needs
|
||||
on: push
|
||||
|
||||
@@ -102,40 +88,34 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo "deploying"
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{
|
||||
"build": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"test": {
|
||||
Outputs: map[string]string{
|
||||
"coverage": "80%",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"lint": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_FAILURE,
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{
|
||||
"build": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"test": {
|
||||
Outputs: map[string]string{
|
||||
"coverage": "80%",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"lint": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_FAILURE,
|
||||
},
|
||||
},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("deploy")
|
||||
needs := job.Needs()
|
||||
require.Equal(t, []string{"build", "lint", "test"}, needs)
|
||||
require.Equal(t, []string{"build", "lint", "test"}, job.Needs())
|
||||
require.Equal(t, "80%", wf.Jobs["test"].Outputs["coverage"])
|
||||
require.Equal(t, "failure", wf.Jobs["lint"].Result)
|
||||
},
|
||||
want1: "deploy",
|
||||
wantErr: false,
|
||||
jobID: "deploy",
|
||||
},
|
||||
{
|
||||
name: "workflow env and defaults",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Complex workflow
|
||||
on:
|
||||
push:
|
||||
@@ -159,25 +139,19 @@ jobs:
|
||||
node-version: "18"
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, "Complex workflow", wf.Name)
|
||||
require.Equal(t, "production", wf.Env["NODE_ENV"])
|
||||
require.Equal(t, "true", wf.Env["CI"])
|
||||
job := wf.GetJob("build")
|
||||
require.Len(t, job.Steps, 4)
|
||||
require.Len(t, wf.GetJob("build").Steps, 4)
|
||||
},
|
||||
want1: "build",
|
||||
wantErr: false,
|
||||
jobID: "build",
|
||||
},
|
||||
{
|
||||
name: "job with container and services",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Integration tests
|
||||
on: push
|
||||
|
||||
@@ -192,24 +166,18 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm test
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("integration")
|
||||
container := job.Container()
|
||||
require.Equal(t, "node:18", container.Image)
|
||||
require.Equal(t, "node:18", job.Container().Image)
|
||||
require.Equal(t, "postgres:15", job.Services["postgres"].Image)
|
||||
},
|
||||
want1: "integration",
|
||||
wantErr: false,
|
||||
jobID: "integration",
|
||||
},
|
||||
{
|
||||
name: "job with matrix strategy",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Matrix build
|
||||
on: push
|
||||
|
||||
@@ -222,40 +190,28 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: go test ./...
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("test")
|
||||
matrixes, err := job.GetMatrixes()
|
||||
matrixes, err := wf.GetJob("test").GetMatrixes()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, matrixes, 2)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "special yaml characters in values",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte("name: \"Special: characters & test\"\non: push\n\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: 'echo \"hello: world\"'\n - run: 'echo \"quotes & ampersands\"'\n - run: |\n echo \"multiline\"\n echo \"script\"\n"),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
name: "special yaml characters in values",
|
||||
payload: "name: \"Special: characters & test\"\non: push\n\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: 'echo \"hello: world\"'\n - run: 'echo \"quotes & ampersands\"'\n - run: |\n echo \"multiline\"\n echo \"script\"\n",
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, "Special: characters & test", wf.Name)
|
||||
job := wf.GetJob("test")
|
||||
require.Len(t, job.Steps, 3)
|
||||
require.Len(t, wf.GetJob("test").Steps, 3)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "invalid yaml",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Bad workflow
|
||||
on: push
|
||||
jobs:
|
||||
@@ -264,25 +220,21 @@ jobs:
|
||||
steps:
|
||||
- run: echo "ok"
|
||||
bad-indent: true
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
assert: nil,
|
||||
want1: "",
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, got1, err := generateWorkflow(tt.args.task)
|
||||
got, got1, err := generateWorkflow(&runnerv1.Task{WorkflowPayload: []byte(tt.payload), Needs: tt.needs})
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
tt.assert(t, got)
|
||||
require.Equal(t, tt.want1, got1)
|
||||
require.Equal(t, tt.jobID, got1)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -321,7 +273,6 @@ func Test_yamlV4NodeRoundTrip(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("node kind constants", func(t *testing.T) {
|
||||
// Verify yaml/v4 node kind constants are usable (same API as v3)
|
||||
require.NotEqual(t, yaml.ScalarNode, yaml.SequenceNode)
|
||||
require.NotEqual(t, yaml.SequenceNode, yaml.MappingNode)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user