line_parsers_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 TestNodeFromLabels(t *testing.T) {
  38. labels := map[string]string{
  39. "foo": "bar",
  40. "weird": "first' second",
  41. }
  42. expected := &Node{
  43. Value: "label",
  44. Original: `LABEL "foo"='bar' "weird"='first' second'`,
  45. Next: &Node{
  46. Value: "foo",
  47. Next: &Node{
  48. Value: "'bar'",
  49. Next: &Node{
  50. Value: "weird",
  51. Next: &Node{
  52. Value: "'first' second'",
  53. },
  54. },
  55. },
  56. },
  57. }
  58. node := NodeFromLabels(labels)
  59. assert.DeepEqual(t, expected, node, cmpNodeOpt)
  60. }
  61. func TestParseNameValWithoutVal(t *testing.T) {
  62. directive := Directive{}
  63. // In Config.Env, a variable without `=` is removed from the environment. (#31634)
  64. // However, in Dockerfile, we don't allow "unsetting" an environment variable. (#11922)
  65. _, err := parseNameVal("foo", "ENV", &directive)
  66. assert.Check(t, is.ErrorContains(err, ""), "ENV must have two arguments")
  67. }