diff --git a/pkg/when/changein/pattern.go b/pkg/when/changein/pattern.go index ce9ad63..f1d34ad 100644 --- a/pkg/when/changein/pattern.go +++ b/pkg/when/changein/pattern.go @@ -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 += "/" } diff --git a/pkg/when/expression.go b/pkg/when/expression.go index c5c3159..6c28b96 100644 --- a/pkg/when/expression.go +++ b/pkg/when/expression.go @@ -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{}{} @@ -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 } @@ -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 { @@ -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") +} diff --git a/test/e2e/change_in_multiple_functions_in_one_when.rb b/test/e2e/change_in_multiple_functions_in_one_when.rb new file mode 100644 index 0000000..a8170ce --- /dev/null +++ b/test/e2e/change_in_multiple_functions_in_one_when.rb @@ -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" +}))