error-action: add marshal yaml

This commit is contained in:
Tigor Hutasuhut 2024-06-25 17:12:30 +07:00
parent 0ad2e1a1e1
commit 44c1f1a629
2 changed files with 46 additions and 0 deletions

View file

@ -16,6 +16,10 @@ type Job struct {
type ErrorAction string type ErrorAction string
func (er ErrorAction) MarshalYAML() (interface{}, error) {
return er.String(), nil
}
func (ea *ErrorAction) UnmarshalYAML(value *yaml.Node) error { func (ea *ErrorAction) UnmarshalYAML(value *yaml.Node) error {
if value.IsZero() { if value.IsZero() {
*ea = ErrorActionStopJob *ea = ErrorActionStopJob

View file

@ -88,3 +88,45 @@ func TestErrorAction_UnmarshalYAML(t *testing.T) {
}) })
} }
} }
func TestErrorAction_MarshalYAML(t *testing.T) {
tests := []struct {
name string
er ErrorAction
want []byte
}{
{
name: "continue",
er: ErrorActionContinue,
want: []byte("E: continue"),
},
{
name: "stop-job",
er: ErrorActionStopJob,
want: []byte("E: stop-job"),
},
{
name: "stop-action",
er: ErrorActionStopAction,
want: []byte("E: stop-action"),
},
{
name: "stop-run",
er: ErrorActionStopRun,
want: []byte("E: stop-run"),
},
}
type placeholder struct {
E ErrorAction `yaml:"E"`
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := placeholder{E: tt.er}
got, err := yaml.Marshal(e)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, tt.want, got)
})
}
}