line_parsers_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package parser // import "github.com/docker/docker/builder/dockerfile/parser"
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. "github.com/gotestyourself/gotestyourself/assert"
  6. is "github.com/gotestyourself/gotestyourself/assert/cmp"
  7. )
  8. func TestParseNameValOldFormat(t *testing.T) {
  9. directive := Directive{}
  10. node, err := parseNameVal("foo bar", "LABEL", &directive)
  11. assert.Check(t, err)
  12. expected := &Node{
  13. Value: "foo",
  14. Next: &Node{Value: "bar"},
  15. }
  16. assert.DeepEqual(t, expected, node, cmpNodeOpt)
  17. }
  18. var cmpNodeOpt = cmp.AllowUnexported(Node{})
  19. func TestParseNameValNewFormat(t *testing.T) {
  20. directive := Directive{}
  21. node, err := parseNameVal("foo=bar thing=star", "LABEL", &directive)
  22. assert.Check(t, err)
  23. expected := &Node{
  24. Value: "foo",
  25. Next: &Node{
  26. Value: "bar",
  27. Next: &Node{
  28. Value: "thing",
  29. Next: &Node{
  30. Value: "star",
  31. },
  32. },
  33. },
  34. }
  35. assert.DeepEqual(t, expected, node, cmpNodeOpt)
  36. }
  37. func TestParseNameValWithoutVal(t *testing.T) {
  38. directive := Directive{}
  39. // In Config.Env, a variable without `=` is removed from the environment. (#31634)
  40. // However, in Dockerfile, we don't allow "unsetting" an environment variable. (#11922)
  41. _, err := parseNameVal("foo", "ENV", &directive)
  42. assert.Check(t, is.ErrorContains(err, ""), "ENV must have two arguments")
  43. }