Skip to content

Commit

Permalink
add specific unit tests for findInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Apr 9, 2021
1 parent 6c92f2f commit d899937
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/workflow/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func findInputs(yamlContent []byte) (map[string]WorkflowInput, error) {
}

if len(rootNode.Content) != 1 {
return nil, errors.New("invalid yaml file")
return nil, errors.New("invalid YAML file")
}

var onKeyNode *yaml.Node
Expand Down
104 changes: 101 additions & 3 deletions pkg/cmd/workflow/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,106 @@ func Test_magicFieldValue(t *testing.T) {
}
}

func Test_findInputs(t *testing.T) {
tests := []struct {
name string
YAML []byte
wantErr bool
errMsg string
wantOut map[string]WorkflowInput
}{
{
name: "blank",
YAML: []byte{},
wantErr: true,
errMsg: "invalid YAML file",
},
{
name: "no event specified",
YAML: []byte("name: workflow"),
wantErr: true,
errMsg: "invalid workflow: no 'on' key",
},
{
name: "not workflow_dispatch",
YAML: []byte("name: workflow\non: pull_request"),
wantErr: true,
errMsg: "unable to manually run a workflow without a workflow_dispatch event",
},
{
name: "bad inputs",
YAML: []byte("name: workflow\non:\n workflow_dispatch:\n inputs: lol "),
wantErr: true,
errMsg: "could not decode workflow inputs: yaml: unmarshal errors:\n line 4: cannot unmarshal !!str `lol` into map[string]run.WorkflowInput",
},
{
name: "short syntax",
YAML: []byte("name: workflow\non: workflow_dispatch"),
wantOut: map[string]WorkflowInput{},
},
{
name: "inputs",
YAML: []byte(`name: workflow
on:
workflow_dispatch:
inputs:
foo:
required: true
description: good foo
bar:
default: boo
baz:
description: it's baz
quux:
required: true
default: "cool"
jobs:
yell:
runs-on: ubuntu-latest
steps:
- name: echo
run: |
echo "echo"`),
wantOut: map[string]WorkflowInput{
"foo": {
Required: true,
Description: "good foo",
},
"bar": {
Default: "boo",
},
"baz": {
Description: "it's baz",
},
"quux": {
Required: true,
Default: "cool",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := findInputs(tt.YAML)
if tt.wantErr {
assert.Error(t, err)
if err != nil {
assert.Equal(t, tt.errMsg, err.Error())
}
return
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.wantOut, result)
})
}

}

func TestRun(t *testing.T) {
minimalYAMLContent := []byte(`
noInputsYAMLContent := []byte(`
name: minimal workflow
on: workflow_dispatch
jobs:
Expand All @@ -221,7 +319,7 @@ jobs:
run: |
echo "AUUUGH!"
`)
encodedMinimalYAMLContent := base64.StdEncoding.EncodeToString(minimalYAMLContent)
encodedNoInputsYAMLContent := base64.StdEncoding.EncodeToString(noInputsYAMLContent)
yamlContent := []byte(`
name: a workflow
on:
Expand Down Expand Up @@ -448,7 +546,7 @@ jobs:
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/contents/.github/workflows/minimal.yml"),
httpmock.JSONResponse(struct{ Content string }{
Content: encodedMinimalYAMLContent,
Content: encodedNoInputsYAMLContent,
}))
reg.Register(
httpmock.REST("POST", "repos/OWNER/REPO/actions/workflows/1/dispatches"),
Expand Down

0 comments on commit d899937

Please sign in to comment.