2020-02-10 18:35:00 -06:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-12 01:55:20 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-10 18:35:00 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadWorkflow_StringEvent(t *testing.T) {
|
|
|
|
yaml := `
|
|
|
|
name: local-action-docker-url
|
|
|
|
on: push
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: ./actions/docker-url
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, err := ReadWorkflow(strings.NewReader(yaml))
|
2020-02-12 01:55:20 -06:00
|
|
|
assert.NoError(t, err, "read workflow should succeed")
|
2020-02-10 18:35:00 -06:00
|
|
|
|
2020-02-12 01:55:20 -06:00
|
|
|
assert.Len(t, workflow.On(), 1)
|
|
|
|
assert.Contains(t, workflow.On(), "push")
|
2020-02-10 18:35:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWorkflow_ListEvent(t *testing.T) {
|
|
|
|
yaml := `
|
|
|
|
name: local-action-docker-url
|
|
|
|
on: [push, pull_request]
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: ./actions/docker-url
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, err := ReadWorkflow(strings.NewReader(yaml))
|
2020-02-12 01:55:20 -06:00
|
|
|
assert.NoError(t, err, "read workflow should succeed")
|
2020-02-10 18:35:00 -06:00
|
|
|
|
2020-02-12 01:55:20 -06:00
|
|
|
assert.Len(t, workflow.On(), 2)
|
|
|
|
assert.Contains(t, workflow.On(), "push")
|
|
|
|
assert.Contains(t, workflow.On(), "pull_request")
|
2020-02-10 18:35:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWorkflow_MapEvent(t *testing.T) {
|
|
|
|
yaml := `
|
|
|
|
name: local-action-docker-url
|
2021-04-01 13:36:41 -05:00
|
|
|
on:
|
2020-02-10 18:35:00 -06:00
|
|
|
push:
|
|
|
|
branches:
|
|
|
|
- master
|
|
|
|
pull_request:
|
|
|
|
branches:
|
|
|
|
- master
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: ./actions/docker-url
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, err := ReadWorkflow(strings.NewReader(yaml))
|
2020-02-12 01:55:20 -06:00
|
|
|
assert.NoError(t, err, "read workflow should succeed")
|
|
|
|
assert.Len(t, workflow.On(), 2)
|
|
|
|
assert.Contains(t, workflow.On(), "push")
|
|
|
|
assert.Contains(t, workflow.On(), "pull_request")
|
2020-02-10 18:35:00 -06:00
|
|
|
}
|
2020-02-25 00:35:08 -06:00
|
|
|
|
|
|
|
func TestReadWorkflow_StringContainer(t *testing.T) {
|
|
|
|
yaml := `
|
|
|
|
name: local-action-docker-url
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test:
|
|
|
|
container: nginx:latest
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: ./actions/docker-url
|
|
|
|
test2:
|
2021-04-01 13:36:41 -05:00
|
|
|
container:
|
2020-02-25 00:35:08 -06:00
|
|
|
image: nginx:latest
|
|
|
|
env:
|
|
|
|
foo: bar
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: ./actions/docker-url
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, err := ReadWorkflow(strings.NewReader(yaml))
|
|
|
|
assert.NoError(t, err, "read workflow should succeed")
|
|
|
|
assert.Len(t, workflow.Jobs, 2)
|
|
|
|
assert.Contains(t, workflow.Jobs["test"].Container().Image, "nginx:latest")
|
|
|
|
assert.Contains(t, workflow.Jobs["test2"].Container().Image, "nginx:latest")
|
|
|
|
assert.Contains(t, workflow.Jobs["test2"].Container().Env["foo"], "bar")
|
|
|
|
}
|
2021-04-01 13:36:41 -05:00
|
|
|
|
|
|
|
func TestReadWorkflow_StepsTypes(t *testing.T) {
|
|
|
|
yaml := `
|
|
|
|
name: invalid step definition
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- name: test1
|
|
|
|
uses: actions/checkout@v2
|
|
|
|
run: echo
|
|
|
|
- name: test2
|
|
|
|
run: echo
|
|
|
|
- name: test3
|
|
|
|
uses: actions/checkout@v2
|
|
|
|
- name: test4
|
|
|
|
uses: docker://nginx:latest
|
|
|
|
- name: test5
|
|
|
|
uses: ./local-action
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, err := ReadWorkflow(strings.NewReader(yaml))
|
|
|
|
assert.NoError(t, err, "read workflow should succeed")
|
|
|
|
assert.Len(t, workflow.Jobs, 1)
|
|
|
|
assert.Len(t, workflow.Jobs["test"].Steps, 5)
|
|
|
|
assert.Equal(t, workflow.Jobs["test"].Steps[0].Type(), StepTypeInvalid)
|
|
|
|
assert.Equal(t, workflow.Jobs["test"].Steps[1].Type(), StepTypeRun)
|
|
|
|
assert.Equal(t, workflow.Jobs["test"].Steps[2].Type(), StepTypeUsesActionRemote)
|
|
|
|
assert.Equal(t, workflow.Jobs["test"].Steps[3].Type(), StepTypeUsesDockerURL)
|
|
|
|
assert.Equal(t, workflow.Jobs["test"].Steps[4].Type(), StepTypeUsesActionLocal)
|
|
|
|
}
|
2021-05-05 00:57:33 -05:00
|
|
|
|
|
|
|
func TestStep_ShellCommand(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
shell string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"pwsh", "pwsh -command . '{0}'"},
|
|
|
|
{"powershell", "powershell -command . '{0}'"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.shell, func(t *testing.T) {
|
|
|
|
got := (&Step{Shell: tt.shell}).ShellCommand()
|
|
|
|
assert.Equal(t, got, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|