Skip to content

Commit

Permalink
Parse when errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyasha committed Jan 28, 2021
1 parent 10cf6e3 commit 953f68d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 32 deletions.
9 changes: 9 additions & 0 deletions pkg/logs/error_invalid_change_in.go → pkg/logs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ type ErrorChangeInMissingBranch struct {
func (e *ErrorChangeInMissingBranch) Error() string {
return e.Message
}

type ErrorInvalidWhenExpression struct {
Message string `json:"message"`
Location Location `json:"location"`
}

func (e *ErrorInvalidWhenExpression) Error() string {
return e.Message
}
2 changes: 1 addition & 1 deletion pkg/pipelines/extract_when_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (e *whenExtractor) Parse() ([]when.WhenExpression, error) {
}

for index := range e.list {
e.list[index].Requirments = requirments[index]
e.list[index].Requirments = requirments[index].Inputs
}

return e.list, nil
Expand Down
21 changes: 0 additions & 21 deletions pkg/pipelines/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pipelines

import (
"encoding/json"
"fmt"
"time"

gabs "github.com/Jeffail/gabs/v2"
Expand All @@ -25,30 +24,18 @@ var TotalEval int64
var TotalReduce int64

func (p *Pipeline) EvaluateChangeIns() error {
fmt.Println("Evaluating start.")

start1 := n()

list, err := p.ExtractWhenConditions()
if err != nil {
return err
}

TotalList = n() - start1

start2 := n()

for index := range list {
err := list[index].Eval()
if err != nil {
return err
}
}

TotalEval = n() - start2

start3 := n()

expressions := []string{}
inputs := []whencli.ReduceInputs{}

Expand All @@ -66,14 +53,6 @@ func (p *Pipeline) EvaluateChangeIns() error {
p.raw.Set(expressions[index], list[index].Path...)
}

TotalReduce = n() - start3

fmt.Println("Evaluating end.")

fmt.Printf("Parse When Expressions: %dms\n", TotalList)
fmt.Printf("Evaluated change_in: %dms\n", TotalEval)
fmt.Printf("Reduce When Expressions: %dms\n", TotalReduce)

return nil
}

Expand Down
44 changes: 34 additions & 10 deletions pkg/when/whencli/list_inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,55 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"

gabs "github.com/Jeffail/gabs/v2"
)

func ListInputs(expressions []string) ([]*gabs.Container, error) {
type ListInputsResult struct {
Expression string
Inputs *gabs.Container
Error string
}

func ListInputs(expressions []string) ([]ListInputsResult, error) {
var err error
res := []ListInputsResult{}

inputPath := "/tmp/when-expressions"
outputPath := "/tmp/parsed-when-expressions"

err = ListInputsPrepareInputFile(inputPath, expressions)
if err != nil {
return []*gabs.Container{}, nil
return res, nil
}

output, err := exec.Command("when", "list-inputs", "--input", inputPath, "--output", outputPath).CombinedOutput()
if err != nil {
return []*gabs.Container{}, fmt.Errorf("unprecessable when expressions %s", string(output))
return res, fmt.Errorf("unprecessable when expressions %s", string(output))
}

result, err := ListInputsLoadResults(outputPath)
results, err := ListInputsLoadResults(outputPath)
if err != nil {
return result, fmt.Errorf("unprocessable when expressions %s, when CLI output: %s", err.Error(), output)
return res, fmt.Errorf("unprocessable when expressions %s, when CLI output: %s", err.Error(), output)
}

return prepareResults(expressions, results)
}

func prepareResults(expressions []string, results *gabs.Container) ([]ListInputsResult, error) {
result := []ListInputsResult{}

for index, el := range results.Children() {
result = append(result, ListInputsResult{
Expression: expressions[index],
Inputs: el.Search("inputs"),
Error: el.Search("error").Data().(string),
})
log.Println(index)
log.Println(el)
}

return result, nil
Expand All @@ -48,22 +72,22 @@ func ListInputsPrepareInputFile(path string, expressions []string) error {
return nil
}

func ListInputsLoadResults(path string) ([]*gabs.Container, error) {
func ListInputsLoadResults(path string) (*gabs.Container, error) {
file, err := os.Open(path)
if err != nil {
return []*gabs.Container{}, err
return nil, err
}
defer file.Close()

content, err := ioutil.ReadAll(file)
if err != nil {
return []*gabs.Container{}, err
return nil, err
}

inputs, err := gabs.ParseJSON(content)
if err != nil {
return []*gabs.Container{}, err
return nil, err
}

return inputs.Children(), nil
return inputs, nil
}
47 changes: 47 additions & 0 deletions pkg/when/whencli/list_inputs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package whencli

import (
"testing"

gabs "github.com/Jeffail/gabs/v2"
assert "github.com/stretchr/testify/assert"
)

func Test__ListInputs(t *testing.T) {
expressions := []string{
"branch = 'master'",
"change_in('lib')",
"branch = ",
}

results, err := ListInputs(expressions)

assert.Nil(t, err)
assert.Equal(t, 3, len(results))
assert.Equal(t, []ListInputsResult{
ListInputsResult{
Expression: expressions[0],
Error: "",
Inputs: fromJSON(`[{"name": "branch", "type": "keyword"}]`),
},
ListInputsResult{
Expression: expressions[1],
Error: "",
Inputs: fromJSON(`[{"name": "change_in", "params": ["lib"], "type": "fun"}]`),
},
ListInputsResult{
Expression: expressions[2],
Error: "Invalid or incomplete expression at the end of the line.",
Inputs: fromJSON(`[]`),
},
}, results)
}

func fromJSON(s string) *gabs.Container {
res, err := gabs.ParseJSON([]byte(s))
if err != nil {
panic(err)
}

return res
}
71 changes: 71 additions & 0 deletions test/e2e/change_in_invalid_when.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# rubocop:disable all

require_relative "../e2e"

system "rm -f /tmp/output.yml"
system "rm -f /tmp/logs.jsonl"

pipeline = %{
version: v1.0
name: Test
agent:
machine:
type: e1-standard-2
blocks:
- name: Test
skip:
when: "branch = 'master' and ahahahaha and change_in('/lib')"
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
- name: Test
skip:
when: "branch ="
task:
jobs:
- name: Hello
commands:
- echo "Hello World"
}

origin = TestRepoForChangeIn.setup()

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

origin.add_file("lib/A.txt", "hello")
origin.commit!("Changes on master")

origin.create_branch("dev")
origin.add_file("lib/B.txt", "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.jsonl", fail: false)

assert_eq($?.exitstatus, 1)

errors = File.read('/tmp/logs.jsonl').lines.map { |l| JSON.parse(l) }
assert_eq(errors.size, 2)

assert_eq(errors[0], {
"type" => "ErrorInvalidWhenExpression",
"message" => "Invalid when expression: branch = 'master' and ahahahaha and change_in('/lib')",
"location" => {
"file" => ".semaphore/semaphore.yml",
"path" => ["blocks", "0", "skip", "when"]
}
})

assert_eq(errors[1], {
"type" => "ErrorInvalidWhenExpression",
"message" => "Invalid when expression: branch =",
"location" => {
"file" => ".semaphore/semaphore.yml",
"path" => ["blocks", "1", "skip", "when"]
}
})

0 comments on commit 953f68d

Please sign in to comment.