woodpecker/pipeline/frontend/yaml/types/command_test.go
6543 0bb62be303
Embedding libcompose types for yaml parsing (#495)
since github.com/docker/libcompose is deprecated, unmaintained and archived.

and license is the same as woodpecker's, we can just copy stuff into woodpecker directly.
(we only use types of that project anyway)
2021-10-30 17:52:02 +02:00

61 lines
1.3 KiB
Go

package types
import (
"strings"
"testing"
"gopkg.in/yaml.v3"
"github.com/stretchr/testify/assert"
)
type StructCommand struct {
Entrypoint Command `yaml:"entrypoint,flow,omitempty"`
Command Command `yaml:"command,flow,omitempty"`
}
func TestUnmarshalCommand(t *testing.T) {
s := &StructCommand{}
err := yaml.Unmarshal([]byte(`command: bash`), s)
assert.Nil(t, err)
assert.Equal(t, Command{"bash"}, s.Command)
assert.Nil(t, s.Entrypoint)
bytes, err := yaml.Marshal(s)
assert.Nil(t, err)
s2 := &StructCommand{}
err = yaml.Unmarshal(bytes, s2)
assert.Nil(t, err)
assert.Equal(t, Command{"bash"}, s2.Command)
assert.Nil(t, s2.Entrypoint)
s3 := &StructCommand{}
err = yaml.Unmarshal([]byte(`command:
- echo AAA; echo "wow"
- sleep 3s`), s3)
assert.Nil(t, err)
assert.Equal(t, Command{`echo AAA; echo "wow"`, `sleep 3s`}, s3.Command)
}
var sampleEmptyCommand = `{}`
func TestUnmarshalEmptyCommand(t *testing.T) {
s := &StructCommand{}
err := yaml.Unmarshal([]byte(sampleEmptyCommand), s)
assert.Nil(t, err)
assert.Nil(t, s.Command)
bytes, err := yaml.Marshal(s)
assert.Nil(t, err)
assert.Equal(t, "{}", strings.TrimSpace(string(bytes)))
s2 := &StructCommand{}
err = yaml.Unmarshal(bytes, s2)
assert.Nil(t, err)
assert.Nil(t, s2.Command)
}