Skip to content

Commit

Permalink
When extractor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha committed Mar 29, 2021
1 parent 4d6a1c5 commit 6ae569b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var evaluateChangeInCmd = &cobra.Command{
logs.Open(logsPath)
logs.SetCurrentPipelineFilePath(input)

ppl, err := pipelines.LoadFromYaml(input)
ppl, err := pipelines.LoadFromFile(input)
check(err)

err = ppl.EvaluateChangeIns()
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipelines/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/ghodss/yaml"
)

func LoadFromYaml(path string) (*Pipeline, error) {
func LoadFromFile(path string) (*Pipeline, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/pipelines/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Pipeline) Blocks() []*gabs.Container {
}

func (p *Pipeline) Promotions() []*gabs.Container {
return p.raw.Search("blocks").Children()
return p.raw.Search("promotions").Children()
}

func (p *Pipeline) PathExists(path []string) bool {
Expand All @@ -43,8 +43,8 @@ func (p *Pipeline) GetStringFromPath(path []string) string {
return p.raw.Search(path...).Data().(string)
}

func (p *Pipeline) PriorityRules() []*gabs.Container {
return p.raw.Search("priority").Children()
func (p *Pipeline) GlobalPriorityRules() []*gabs.Container {
return p.raw.Search("global_job_config", "priority").Children()
}

func (p *Pipeline) QueueRules() []*gabs.Container {
Expand Down
33 changes: 33 additions & 0 deletions pkg/pipelines/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pipelines

import (
"testing"

assert "github.com/stretchr/testify/assert"
)

func Test__PipelineBlocks(t *testing.T) {
pipeline, err := LoadFromFile("../../test/fixtures/all_when_locations.yml")

assert.Nil(t, err)
assert.Equal(t, 2, len(pipeline.Blocks()))
}

func Test__PipelinePromotions(t *testing.T) {
pipeline, err := LoadFromFile("../../test/fixtures/all_when_locations.yml")

assert.Nil(t, err)
assert.Equal(t, 1, len(pipeline.Promotions()))
}

func Test__PipelineGlobalPriorityRules(t *testing.T) {
pipeline, err := LoadFromFile("../../test/fixtures/all_when_locations.yml")

assert.Nil(t, err)
assert.Equal(t, 2, len(pipeline.GlobalPriorityRules()))

pipeline2, err := LoadFromFile("../../test/fixtures/hello.yml")

assert.Nil(t, err)
assert.Equal(t, 0, len(pipeline2.GlobalPriorityRules()))
}
4 changes: 2 additions & 2 deletions pkg/pipelines/when_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func (e *whenEvaluator) ExtractFromPromotions() {
}

func (e *whenEvaluator) ExtractFromPriority() {
for index := range e.pipeline.PriorityRules() {
e.tryExtractingFromPath([]string{"priority", strconv.Itoa(index), "when"})
for index := range e.pipeline.GlobalPriorityRules() {
e.tryExtractingFromPath([]string{"global_job_config", "priority", strconv.Itoa(index), "when"})
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/pipelines/when_evaluator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pipelines

import (
"testing"

when "github.com/semaphoreci/spc/pkg/when"
assert "github.com/stretchr/testify/assert"
)

func Test__WhenEvaluatorExtractAll(t *testing.T) {
pipeline, err := LoadFromFile("../../test/fixtures/all_when_locations.yml")
assert.Nil(t, err)

e := newWhenEvaluator(pipeline)

assert.Equal(t, e, []when.WhenExpression{
{},
})
}
37 changes: 37 additions & 0 deletions test/fixtures/all_when_locations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: v1.0

global_job_config:
priority:
- value: 70
when: "branch = 'master'"
- value: 45
when: true

blocks:
- name: Unit
run:
when: "change_in('lib')"
task:
jobs:
- name: Run
commands:
- echo "hello"

- name: E2E
run:
when: "change_in('lib')"
task:
jobs:
- name: Run
priority:
- value: 70
when: "branch = 'master'"
- value: 45
when: true
commands:
- echo "hello"

promotions:
- name: Production
auto_promote:
when: "change_in('lib')"

0 comments on commit 6ae569b

Please sign in to comment.