github.com/flatt-online-training/libcompose@v0.4.0/yaml/build_test.go (about)

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