github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/builder/dockerfile/parser/line_parsers_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestParseNameValOldFormat(t *testing.T) {
    10  	directive := Directive{}
    11  	node, err := parseNameVal("foo bar", "LABEL", &directive)
    12  	assert.NoError(t, err)
    13  
    14  	expected := &Node{
    15  		Value: "foo",
    16  		Next:  &Node{Value: "bar"},
    17  	}
    18  	assert.Equal(t, expected, node)
    19  }
    20  
    21  func TestParseNameValNewFormat(t *testing.T) {
    22  	directive := Directive{}
    23  	node, err := parseNameVal("foo=bar thing=star", "LABEL", &directive)
    24  	assert.NoError(t, err)
    25  
    26  	expected := &Node{
    27  		Value: "foo",
    28  		Next: &Node{
    29  			Value: "bar",
    30  			Next: &Node{
    31  				Value: "thing",
    32  				Next: &Node{
    33  					Value: "star",
    34  				},
    35  			},
    36  		},
    37  	}
    38  	assert.Equal(t, expected, node)
    39  }
    40  
    41  func TestNodeFromLabels(t *testing.T) {
    42  	labels := map[string]string{
    43  		"foo":   "bar",
    44  		"weird": "first' second",
    45  	}
    46  	expected := &Node{
    47  		Value:    "label",
    48  		Original: `LABEL "foo"='bar' "weird"='first' second'`,
    49  		Next: &Node{
    50  			Value: "foo",
    51  			Next: &Node{
    52  				Value: "'bar'",
    53  				Next: &Node{
    54  					Value: "weird",
    55  					Next: &Node{
    56  						Value: "'first' second'",
    57  					},
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	node := NodeFromLabels(labels)
    64  	assert.Equal(t, expected, node)
    65  
    66  }
    67  
    68  func TestParseNameValWithoutVal(t *testing.T) {
    69  	directive := Directive{}
    70  	// In Config.Env, a variable without `=` is removed from the environment. (#31634)
    71  	// However, in Dockerfile, we don't allow "unsetting" an environment variable. (#11922)
    72  	_, err := parseNameVal("foo", "ENV", &directive)
    73  	assert.Error(t, err, "ENV must have two arguments")
    74  }