parser_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package parser
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "testing"
  10. "github.com/docker/docker/pkg/testutil/assert"
  11. )
  12. const testDir = "testfiles"
  13. const negativeTestDir = "testfiles-negative"
  14. const testFileLineInfo = "testfile-line/Dockerfile"
  15. func getDirs(t *testing.T, dir string) []string {
  16. f, err := os.Open(dir)
  17. assert.NilError(t, err)
  18. defer f.Close()
  19. dirs, err := f.Readdirnames(0)
  20. assert.NilError(t, err)
  21. return dirs
  22. }
  23. func TestTestNegative(t *testing.T) {
  24. for _, dir := range getDirs(t, negativeTestDir) {
  25. dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
  26. df, err := os.Open(dockerfile)
  27. assert.NilError(t, err)
  28. defer df.Close()
  29. _, err = Parse(df)
  30. assert.Error(t, err, "")
  31. }
  32. }
  33. func TestTestData(t *testing.T) {
  34. for _, dir := range getDirs(t, testDir) {
  35. dockerfile := filepath.Join(testDir, dir, "Dockerfile")
  36. resultfile := filepath.Join(testDir, dir, "result")
  37. df, err := os.Open(dockerfile)
  38. assert.NilError(t, err)
  39. defer df.Close()
  40. result, err := Parse(df)
  41. assert.NilError(t, err)
  42. content, err := ioutil.ReadFile(resultfile)
  43. assert.NilError(t, err)
  44. if runtime.GOOS == "windows" {
  45. // CRLF --> CR to match Unix behavior
  46. content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
  47. }
  48. assert.Equal(t, result.AST.Dump()+"\n", string(content), "In "+dockerfile)
  49. }
  50. }
  51. func TestParseWords(t *testing.T) {
  52. tests := []map[string][]string{
  53. {
  54. "input": {"foo"},
  55. "expect": {"foo"},
  56. },
  57. {
  58. "input": {"foo bar"},
  59. "expect": {"foo", "bar"},
  60. },
  61. {
  62. "input": {"foo\\ bar"},
  63. "expect": {"foo\\ bar"},
  64. },
  65. {
  66. "input": {"foo=bar"},
  67. "expect": {"foo=bar"},
  68. },
  69. {
  70. "input": {"foo bar 'abc xyz'"},
  71. "expect": {"foo", "bar", "'abc xyz'"},
  72. },
  73. {
  74. "input": {`foo bar "abc xyz"`},
  75. "expect": {"foo", "bar", `"abc xyz"`},
  76. },
  77. {
  78. "input": {"àöû"},
  79. "expect": {"àöû"},
  80. },
  81. {
  82. "input": {`föo bàr "âbc xÿz"`},
  83. "expect": {"föo", "bàr", `"âbc xÿz"`},
  84. },
  85. }
  86. for _, test := range tests {
  87. words := parseWords(test["input"][0], NewDefaultDirective())
  88. assert.DeepEqual(t, words, test["expect"])
  89. }
  90. }
  91. func TestLineInformation(t *testing.T) {
  92. df, err := os.Open(testFileLineInfo)
  93. assert.NilError(t, err)
  94. defer df.Close()
  95. result, err := Parse(df)
  96. assert.NilError(t, err)
  97. ast := result.AST
  98. if ast.StartLine != 5 || ast.endLine != 31 {
  99. fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.endLine)
  100. t.Fatal("Root line information doesn't match result.")
  101. }
  102. assert.Equal(t, len(ast.Children), 3)
  103. expected := [][]int{
  104. {5, 5},
  105. {11, 12},
  106. {17, 31},
  107. }
  108. for i, child := range ast.Children {
  109. if child.StartLine != expected[i][0] || child.endLine != expected[i][1] {
  110. t.Logf("Wrong line information for child %d: expected(%d-%d), actual(%d-%d)\n",
  111. i, expected[i][0], expected[i][1], child.StartLine, child.endLine)
  112. t.Fatal("Root line information doesn't match result.")
  113. }
  114. }
  115. }