github.com/camronlevanger/libcompose@v0.4.1-0.20180423130544-6bb86d53fa21/yaml/build_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  var (
    11  	buildno = "1"
    12  	user    = "vincent"
    13  	empty   = "\x00"
    14  )
    15  
    16  func TestMarshalBuild(t *testing.T) {
    17  	builds := []struct {
    18  		build    Build
    19  		expected string
    20  	}{
    21  		{
    22  			expected: `{}
    23  `,
    24  		},
    25  		{
    26  			build: Build{
    27  				Context: ".",
    28  			},
    29  			expected: `context: .
    30  `,
    31  		},
    32  		{
    33  			build: Build{
    34  				Context:    ".",
    35  				Dockerfile: "alternate",
    36  			},
    37  			expected: `context: .
    38  dockerfile: alternate
    39  `,
    40  		},
    41  		{
    42  			build: Build{
    43  				Context:    ".",
    44  				Dockerfile: "alternate",
    45  				Args: map[string]*string{
    46  					"buildno": &buildno,
    47  					"user":    &user,
    48  				},
    49  			},
    50  			expected: `args:
    51    buildno: "1"
    52    user: vincent
    53  context: .
    54  dockerfile: alternate
    55  `,
    56  		},
    57  	}
    58  	for _, build := range builds {
    59  		bytes, err := yaml.Marshal(build.build)
    60  		assert.Nil(t, err)
    61  		assert.Equal(t, build.expected, string(bytes), "should be equal")
    62  	}
    63  }
    64  
    65  func TestUnmarshalBuild(t *testing.T) {
    66  	builds := []struct {
    67  		yaml     string
    68  		expected *Build
    69  	}{
    70  		{
    71  			yaml: `.`,
    72  			expected: &Build{
    73  				Context: ".",
    74  			},
    75  		},
    76  		{
    77  			yaml: `context: .`,
    78  			expected: &Build{
    79  				Context: ".",
    80  			},
    81  		},
    82  		{
    83  			yaml: `context: .
    84  dockerfile: alternate`,
    85  			expected: &Build{
    86  				Context:    ".",
    87  				Dockerfile: "alternate",
    88  			},
    89  		},
    90  		{
    91  			yaml: `context: .
    92  dockerfile: alternate
    93  args:
    94    buildno: 1
    95    user: vincent`,
    96  			expected: &Build{
    97  				Context:    ".",
    98  				Dockerfile: "alternate",
    99  				Args: map[string]*string{
   100  					"buildno": &buildno,
   101  					"user":    &user,
   102  				},
   103  			},
   104  		},
   105  		{
   106  			yaml: `context: .
   107  args:
   108    - buildno
   109    - user`,
   110  			expected: &Build{
   111  				Context: ".",
   112  				Args: map[string]*string{
   113  					"buildno": &empty,
   114  					"user":    &empty,
   115  				},
   116  			},
   117  		},
   118  		{
   119  			yaml: `context: .
   120  args:
   121    - buildno=1
   122    - user=vincent`,
   123  			expected: &Build{
   124  				Context: ".",
   125  				Args: map[string]*string{
   126  					"buildno": &buildno,
   127  					"user":    &user,
   128  				},
   129  			},
   130  		},
   131  	}
   132  	for _, build := range builds {
   133  		actual := &Build{}
   134  		err := yaml.Unmarshal([]byte(build.yaml), actual)
   135  		assert.Nil(t, err)
   136  		assert.Equal(t, build.expected, actual, "should be equal")
   137  	}
   138  }