From 2eb4de02ee7ddec7de56ce74f5d9fdd3d2fd0524 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 29 Mar 2023 13:57:29 +0800 Subject: [PATCH] Expose SetJob to make EraseNeeds work (#35) Related to #33 Reviewed-on: https://gitea.com/gitea/act/pulls/35 Reviewed-by: Lunny Xiao --- pkg/jobparser/jobparser.go | 4 ++-- pkg/jobparser/jobparser_test.go | 11 ++------- pkg/jobparser/model.go | 5 +++-- pkg/jobparser/model_test.go | 25 +++++++++++++++++++++ pkg/jobparser/testdata/erase_needs.in.yaml | 16 +++++++++++++ pkg/jobparser/testdata/erase_needs.out.yaml | 23 +++++++++++++++++++ pkg/jobparser/testdata_test.go | 18 +++++++++++++++ 7 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 pkg/jobparser/testdata/erase_needs.in.yaml create mode 100644 pkg/jobparser/testdata/erase_needs.out.yaml create mode 100644 pkg/jobparser/testdata_test.go diff --git a/pkg/jobparser/jobparser.go b/pkg/jobparser/jobparser.go index 08557d3..f062bce 100644 --- a/pkg/jobparser/jobparser.go +++ b/pkg/jobparser/jobparser.go @@ -61,8 +61,8 @@ func Parse(content []byte, options ...ParseOption) ([]*SingleWorkflow, error) { Env: workflow.Env, Defaults: workflow.Defaults, } - if err := swf.setJob(id, job); err != nil { - return nil, fmt.Errorf("setJob: %w", err) + if err := swf.SetJob(id, job); err != nil { + return nil, fmt.Errorf("SetJob: %w", err) } ret = append(ret, swf) } diff --git a/pkg/jobparser/jobparser_test.go b/pkg/jobparser/jobparser_test.go index ec8a980..fedf996 100644 --- a/pkg/jobparser/jobparser_test.go +++ b/pkg/jobparser/jobparser_test.go @@ -1,8 +1,6 @@ package jobparser import ( - "embed" - "path/filepath" "strings" "testing" @@ -13,9 +11,6 @@ import ( "gopkg.in/yaml.v3" ) -//go:embed testdata -var f embed.FS - func TestParse(t *testing.T) { tests := []struct { name string @@ -40,10 +35,8 @@ func TestParse(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - content, err := f.ReadFile(filepath.Join("testdata", tt.name+".in.yaml")) - require.NoError(t, err) - want, err := f.ReadFile(filepath.Join("testdata", tt.name+".out.yaml")) - require.NoError(t, err) + content := ReadTestdata(t, tt.name+".in.yaml") + want := ReadTestdata(t, tt.name+".out.yaml") got, err := Parse(content, tt.options...) if tt.wantErr { require.Error(t, err) diff --git a/pkg/jobparser/model.go b/pkg/jobparser/model.go index e63048a..b3e2ae4 100644 --- a/pkg/jobparser/model.go +++ b/pkg/jobparser/model.go @@ -50,7 +50,7 @@ func (w *SingleWorkflow) jobs() ([]string, []*Job, error) { 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{ id: job, } @@ -114,8 +114,9 @@ func (j *Job) Needs() []string { return (&model.Job{RawNeeds: j.RawNeeds}).Needs() } -func (j *Job) EraseNeeds() { +func (j *Job) EraseNeeds() *Job { j.RawNeeds = yaml.Node{} + return j } func (j *Job) RunsOn() []string { diff --git a/pkg/jobparser/model_test.go b/pkg/jobparser/model_test.go index 3ee13e7..3effe92 100644 --- a/pkg/jobparser/model_test.go +++ b/pkg/jobparser/model_test.go @@ -6,7 +6,10 @@ import ( "testing" "github.com/nektos/act/pkg/model" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) 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()) + }) +} diff --git a/pkg/jobparser/testdata/erase_needs.in.yaml b/pkg/jobparser/testdata/erase_needs.in.yaml new file mode 100644 index 0000000..a7d1f9b --- /dev/null +++ b/pkg/jobparser/testdata/erase_needs.in.yaml @@ -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] diff --git a/pkg/jobparser/testdata/erase_needs.out.yaml b/pkg/jobparser/testdata/erase_needs.out.yaml new file mode 100644 index 0000000..959960d --- /dev/null +++ b/pkg/jobparser/testdata/erase_needs.out.yaml @@ -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 diff --git a/pkg/jobparser/testdata_test.go b/pkg/jobparser/testdata_test.go new file mode 100644 index 0000000..fb75a50 --- /dev/null +++ b/pkg/jobparser/testdata_test.go @@ -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 +}