Skip to content

Commit

Permalink
Add test for multiple change_ins in one when expression
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha committed Mar 17, 2021
1 parent 59c0eb5 commit c7e3751
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/when/changein/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func cleanPattern(workDir, pattern string) string {
cleanPattern = path.Clean(pattern)
}

if cleanPattern[len(pattern)-1] != '/' && pattern[len(pattern)-1] == '/' {
if cleanPattern[len(cleanPattern)-1] != '/' && pattern[len(pattern)-1] == '/' {
cleanPattern += "/"
}

Expand Down
17 changes: 12 additions & 5 deletions pkg/when/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (w *WhenExpression) Eval() error {
}

input := map[string]interface{}{}
input["name"] = requirment.Search("name")
input["params"] = requirment.Search("params")
input["name"] = w.functionName(requirment)
input["params"] = w.functionParams(requirment)
input["result"] = result

w.ReduceInputs.Keywords = map[string]interface{}{}
Expand Down Expand Up @@ -52,8 +52,7 @@ func (w *WhenExpression) IsChangeInFunction(input *gabs.Container) bool {
return false
}

elName := input.Search("name").Data().(string)
if elName != "change_in" {
if w.functionName(input) != "change_in" {
return false
}

Expand All @@ -63,7 +62,7 @@ func (w *WhenExpression) IsChangeInFunction(input *gabs.Container) bool {
func (w *WhenExpression) EvalFunction(input *gabs.Container) (bool, error) {
consolelogger.EmptyLine()

consolelogger.Infof("%s(%+v)\n", input.Search("name").Data(), input.Search("params"))
consolelogger.Infof("%s(%+v)\n", w.functionName(input), w.functionParams(input))

fun, err := changein.Parse(w.Path, input, w.YamlPath)
if err != nil {
Expand All @@ -72,3 +71,11 @@ func (w *WhenExpression) EvalFunction(input *gabs.Container) (bool, error) {

return changein.Eval(fun)
}

func (w *WhenExpression) functionName(input *gabs.Container) string {
return input.Search("name").Data().(string)
}

func (w *WhenExpression) functionParams(input *gabs.Container) *gabs.Container {
return input.Search("params")
}
76 changes: 76 additions & 0 deletions test/e2e/change_in_multiple_functions_in_one_when.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# rubocop:disable all

require_relative "../e2e"
require 'yaml'

pipeline = %{
version: v1.0
name: Test
agent:
machine:
type: e1-standard-2
blocks:
- name: Test
run:
when: "change_in('/lib1') or change_in('/lib2') or change_in('/lib3') or change_in('/lib4')"
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
- name: Test2
run:
when: "change_in('/lib1') and change_in('/lib2') and change_in('/lib3') and change_in('/lib4')"
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
}

origin = TestRepoForChangeIn.setup()

origin.add_file('.semaphore/semaphore.yml', pipeline)
origin.commit!("Bootstrap")

origin.add_file("lib1/A.txt", "hello")
origin.add_file("lib2/A.txt", "hello")
origin.add_file("lib3/A.txt", "hello")
origin.add_file("lib4/A.txt", "hello")
origin.commit!("Changes on master")

origin.create_branch("dev")
origin.add_file("lib2/A.txt", "hello hello")
origin.commit!("Changes in dev")

repo = origin.clone_local_copy(branch: "dev")
repo.run("#{spc} evaluate change-in --input .semaphore/semaphore.yml --output /tmp/output.yml --logs /tmp/logs.yml")

assert_eq(YAML.load_file('/tmp/output.yml'), YAML.load(%{
version: v1.0
name: Test
agent:
machine:
type: e1-standard-2
blocks:
- name: Test
run:
when: "true"
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
- name: Test2
run:
when: "false"
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
}))

0 comments on commit c7e3751

Please sign in to comment.