Expose SetJob to make EraseNeeds work (#35)

Related to #33

Reviewed-on: https://gitea.com/gitea/act/pulls/35
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Jason Song 2023-03-29 13:57:29 +08:00
parent 342ad6a51a
commit 2eb4de02ee
7 changed files with 89 additions and 13 deletions

View file

@ -61,8 +61,8 @@ func Parse(content []byte, options ...ParseOption) ([]*SingleWorkflow, error) {
Env: workflow.Env, Env: workflow.Env,
Defaults: workflow.Defaults, Defaults: workflow.Defaults,
} }
if err := swf.setJob(id, job); err != nil { if err := swf.SetJob(id, job); err != nil {
return nil, fmt.Errorf("setJob: %w", err) return nil, fmt.Errorf("SetJob: %w", err)
} }
ret = append(ret, swf) ret = append(ret, swf)
} }

View file

@ -1,8 +1,6 @@
package jobparser package jobparser
import ( import (
"embed"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -13,9 +11,6 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
//go:embed testdata
var f embed.FS
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -40,10 +35,8 @@ func TestParse(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
content, err := f.ReadFile(filepath.Join("testdata", tt.name+".in.yaml")) content := ReadTestdata(t, tt.name+".in.yaml")
require.NoError(t, err) want := ReadTestdata(t, tt.name+".out.yaml")
want, err := f.ReadFile(filepath.Join("testdata", tt.name+".out.yaml"))
require.NoError(t, err)
got, err := Parse(content, tt.options...) got, err := Parse(content, tt.options...)
if tt.wantErr { if tt.wantErr {
require.Error(t, err) require.Error(t, err)

View file

@ -50,7 +50,7 @@ func (w *SingleWorkflow) jobs() ([]string, []*Job, error) {
return ids, jobs, nil return ids, jobs, nil
} }
func (w *SingleWorkflow) setJob(id string, job *Job) error { func (w *SingleWorkflow) SetJob(id string, job *Job) error {
m := map[string]*Job{ m := map[string]*Job{
id: job, id: job,
} }
@ -114,8 +114,9 @@ func (j *Job) Needs() []string {
return (&model.Job{RawNeeds: j.RawNeeds}).Needs() return (&model.Job{RawNeeds: j.RawNeeds}).Needs()
} }
func (j *Job) EraseNeeds() { func (j *Job) EraseNeeds() *Job {
j.RawNeeds = yaml.Node{} j.RawNeeds = yaml.Node{}
return j
} }
func (j *Job) RunsOn() []string { func (j *Job) RunsOn() []string {

View file

@ -6,7 +6,10 @@ import (
"testing" "testing"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
) )
func TestParseRawOn(t *testing.T) { func TestParseRawOn(t *testing.T) {
@ -195,3 +198,25 @@ func TestParseRawOn(t *testing.T) {
}) })
} }
} }
func TestSingleWorkflow_SetJob(t *testing.T) {
t.Run("erase needs", func(t *testing.T) {
content := ReadTestdata(t, "erase_needs.in.yaml")
want := ReadTestdata(t, "erase_needs.out.yaml")
swf, err := Parse(content)
require.NoError(t, err)
builder := &strings.Builder{}
for _, v := range swf {
id, job := v.Job()
require.NoError(t, v.SetJob(id, job.EraseNeeds()))
if builder.Len() > 0 {
builder.WriteString("---\n")
}
encoder := yaml.NewEncoder(builder)
encoder.SetIndent(2)
require.NoError(t, encoder.Encode(v))
}
assert.Equal(t, string(want), builder.String())
})
}

View file

@ -0,0 +1,16 @@
name: test
jobs:
job1:
runs-on: linux
steps:
- run: uname -a
job2:
runs-on: linux
steps:
- run: uname -a
needs: job1
job3:
runs-on: linux
steps:
- run: uname -a
needs: [job1, job2]

View file

@ -0,0 +1,23 @@
name: test
jobs:
job1:
name: job1
runs-on: linux
steps:
- run: uname -a
---
name: test
jobs:
job2:
name: job2
runs-on: linux
steps:
- run: uname -a
---
name: test
jobs:
job3:
name: job3
runs-on: linux
steps:
- run: uname -a

View file

@ -0,0 +1,18 @@
package jobparser
import (
"embed"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
//go:embed testdata
var testdata embed.FS
func ReadTestdata(t *testing.T, name string) []byte {
content, err := testdata.ReadFile(filepath.Join("testdata", name))
require.NoError(t, err)
return content
}