116 lines
2.3 KiB
Go
116 lines
2.3 KiB
Go
package workflow
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestFromYamlBytesCreatesWorkflow(t *testing.T) {
|
|
sample := `
|
|
jobs:
|
|
jobA:
|
|
runs-on: default
|
|
steps:
|
|
- run: echo "test"
|
|
`
|
|
|
|
workflow, err := FromYamlBytes([]byte(sample))
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected no errors, got %#v", err)
|
|
}
|
|
|
|
if workflow == nil {
|
|
t.Errorf("Expected workflow struct, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestFromYamlBytesDefaultsJobNameToObjectKey(t *testing.T) {
|
|
sample := `
|
|
jobs:
|
|
jobA:
|
|
runs-on: default
|
|
steps:
|
|
- run: echo "test"
|
|
`
|
|
|
|
workflow, _ := FromYamlBytes([]byte(sample))
|
|
|
|
if workflow.Jobs["jobA"].Name != "jobA" {
|
|
t.Errorf("Expected job name to be \"jobA\", got %s instead.", workflow.Jobs["jobA"].Name)
|
|
}
|
|
}
|
|
|
|
func TestFromYamlBytesReturnsErrorIfErrorParsing(t *testing.T) {
|
|
sample := `not-yaml!`
|
|
|
|
workflow, err := FromYamlBytes([]byte(sample))
|
|
|
|
if err == nil {
|
|
t.Error("Expected error errors, got nil")
|
|
}
|
|
|
|
if workflow != nil {
|
|
t.Errorf("Expected nil workflow, got %#v.", workflow)
|
|
}
|
|
}
|
|
|
|
func TestFromYamlFileCreatesWorkflow(t *testing.T) {
|
|
sample := `
|
|
jobs:
|
|
jobA:
|
|
runs-on: default
|
|
steps:
|
|
- run: echo "test"
|
|
`
|
|
|
|
workflowPath := filepath.Join(t.TempDir(), "workflow.yml")
|
|
os.WriteFile(workflowPath, []byte(sample), 0755)
|
|
|
|
workflow, err := FromYamlFile(workflowPath)
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected no errors, got %#v", err)
|
|
}
|
|
|
|
if workflow == nil {
|
|
t.Errorf("Expected workflow struct, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestFromYamlFileReturnsErrorIfErrorParsing(t *testing.T) {
|
|
sample := `not-yaml!`
|
|
|
|
workflowPath := filepath.Join(t.TempDir(), "workflow.yml")
|
|
os.WriteFile(workflowPath, []byte(sample), 0755)
|
|
|
|
workflow, err := FromYamlFile(workflowPath)
|
|
|
|
if err == nil {
|
|
t.Error("Expected errors, got nil.")
|
|
}
|
|
|
|
if workflow != nil {
|
|
t.Errorf("Expected nil workflow, got %#v.", workflow)
|
|
}
|
|
}
|
|
|
|
func TestFromYamlFileSetsSourcePathOnWorkflow(t *testing.T) {
|
|
sample := `
|
|
jobs:
|
|
jobA:
|
|
runs-on: default
|
|
steps:
|
|
- run: echo "test"
|
|
`
|
|
|
|
workflowPath := filepath.Join(t.TempDir(), "workflow.yml")
|
|
os.WriteFile(workflowPath, []byte(sample), 0755)
|
|
|
|
workflow, _ := FromYamlFile(workflowPath)
|
|
|
|
if workflow.SourcePath != workflowPath {
|
|
t.Errorf("Expected workflow file path to be %s, got %s instead.", workflowPath, workflow.SourcePath)
|
|
}
|
|
}
|