2024-08-12 04:34:17 +00:00
|
|
|
package workflow
|
|
|
|
|
|
|
|
import (
|
2024-08-21 03:35:49 +00:00
|
|
|
"reflect"
|
2024-08-12 04:34:17 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWorkingDirectoryDefaultsToCurrentDirectory(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
workDir := workflow.GetWorkingDirectory("jobA", 0)
|
|
|
|
if workDir != "." {
|
|
|
|
t.Errorf("Expected current directory as working directory, got %s", workDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorkingDirectoryUsesStepDefault(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
working-directory: ./test
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
workDir := workflow.GetWorkingDirectory("jobA", 0)
|
|
|
|
if workDir != "./test" {
|
|
|
|
t.Errorf("Expected current directory as ./test working directory, got %s", workDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorkingDirectoryUsesJobDefaultsIfNoStepDefault(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
defaults:
|
|
|
|
run:
|
|
|
|
working-directory: ./test-job
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
workDir := workflow.GetWorkingDirectory("jobA", 0)
|
|
|
|
if workDir != "./test-job" {
|
|
|
|
t.Errorf("Expected current directory as ./test working directory, got %s", workDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorkingDirectoryFromStepOverridesJobDefault(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
defaults:
|
|
|
|
run:
|
|
|
|
working-directory: ./test-job
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
working-directory: ./test-step
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
workDir := workflow.GetWorkingDirectory("jobA", 0)
|
|
|
|
if workDir != "./test-step" {
|
|
|
|
t.Errorf("Expected current directory as ./test working directory, got %s", workDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-08-21 03:35:49 +00:00
|
|
|
|
|
|
|
func TestWorkflowEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "1"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "1"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "1"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobEnvOverwritesWorkflowEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
env:
|
|
|
|
TEST: 2
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "2"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepEnvOverwritesWorkflowEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
env:
|
|
|
|
TEST: 2
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "2"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepEnvOverwritesJobEnv(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
env:
|
|
|
|
TEST: 1
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
env:
|
|
|
|
TEST: 2
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
env := workflow.GetEnv("jobA", 0)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(env, map[string]string{"TEST": "2"}) {
|
|
|
|
t.Errorf("Unexpected env: %#v", env)
|
|
|
|
}
|
|
|
|
}
|
2024-08-23 03:38:32 +00:00
|
|
|
|
|
|
|
func TestStepShellOverridesAllOtherShellValues(t *testing.T) {
|
|
|
|
type TestCase struct {
|
|
|
|
sample string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
|
|
{sample: `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
defaults:
|
|
|
|
run:
|
|
|
|
shell: "job"
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
shell: "step"
|
|
|
|
`, name: "With job default"}, {
|
|
|
|
sample: `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
shell: "step"
|
|
|
|
`, name: "Without job default"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
workflow, _ := FromYamlBytes([]byte(testCase.sample))
|
|
|
|
|
|
|
|
shell := workflow.GetShell("jobA", 0)
|
|
|
|
|
|
|
|
if shell != "step" {
|
|
|
|
t.Errorf("Unexpected shell: %#v", shell)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepShellDefaultsToBash(t *testing.T) {
|
|
|
|
sample := `
|
|
|
|
jobs:
|
|
|
|
jobA:
|
|
|
|
runs-on: default
|
|
|
|
steps:
|
|
|
|
- run: echo "test"
|
|
|
|
`
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
|
|
|
|
shell := workflow.GetShell("jobA", 0)
|
|
|
|
|
|
|
|
if shell != "bash" {
|
|
|
|
t.Errorf("Unexpected shell: %#v", shell)
|
|
|
|
}
|
|
|
|
}
|