github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/builder/dockerfile/parser/line_parsers_test.go (about)

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