101 lines
2 KiB
Go
101 lines
2 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 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)
|
||
|
}
|
||
|
}
|