parser_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package parser
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "testing"
  10. )
  11. const testDir = "testfiles"
  12. const negativeTestDir = "testfiles-negative"
  13. const testFileLineInfo = "testfile-line/Dockerfile"
  14. func getDirs(t *testing.T, dir string) []string {
  15. f, err := os.Open(dir)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. defer f.Close()
  20. dirs, err := f.Readdirnames(0)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. return dirs
  25. }
  26. func TestTestNegative(t *testing.T) {
  27. for _, dir := range getDirs(t, negativeTestDir) {
  28. dockerfile := filepath.Join(negativeTestDir, dir, "Dockerfile")
  29. df, err := os.Open(dockerfile)
  30. if err != nil {
  31. t.Fatalf("Dockerfile missing for %s: %v", dir, err)
  32. }
  33. defer df.Close()
  34. d := Directive{LookingForDirectives: true}
  35. SetEscapeToken(DefaultEscapeToken, &d)
  36. _, err = Parse(df, &d)
  37. if err == nil {
  38. t.Fatalf("No error parsing broken dockerfile for %s", dir)
  39. }
  40. }
  41. }
  42. func TestTestData(t *testing.T) {
  43. for _, dir := range getDirs(t, testDir) {
  44. dockerfile := filepath.Join(testDir, dir, "Dockerfile")
  45. resultfile := filepath.Join(testDir, dir, "result")
  46. df, err := os.Open(dockerfile)
  47. if err != nil {
  48. t.Fatalf("Dockerfile missing for %s: %v", dir, err)
  49. }
  50. defer df.Close()
  51. d := Directive{LookingForDirectives: true}
  52. SetEscapeToken(DefaultEscapeToken, &d)
  53. ast, err := Parse(df, &d)
  54. if err != nil {
  55. t.Fatalf("Error parsing %s's dockerfile: %v", dir, err)
  56. }
  57. content, err := ioutil.ReadFile(resultfile)
  58. if err != nil {
  59. t.Fatalf("Error reading %s's result file: %v", dir, err)
  60. }
  61. if runtime.GOOS == "windows" {
  62. // CRLF --> CR to match Unix behavior
  63. content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
  64. }
  65. if ast.Dump()+"\n" != string(content) {
  66. fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump())
  67. fmt.Fprintln(os.Stderr, "Expected:\n"+string(content))
  68. t.Fatalf("%s: AST dump of dockerfile does not match result", dir)
  69. }
  70. }
  71. }
  72. func TestParseWords(t *testing.T) {
  73. tests := []map[string][]string{
  74. {
  75. "input": {"foo"},
  76. "expect": {"foo"},
  77. },
  78. {
  79. "input": {"foo bar"},
  80. "expect": {"foo", "bar"},
  81. },
  82. {
  83. "input": {"foo\\ bar"},
  84. "expect": {"foo\\ bar"},
  85. },
  86. {
  87. "input": {"foo=bar"},
  88. "expect": {"foo=bar"},
  89. },
  90. {
  91. "input": {"foo bar 'abc xyz'"},
  92. "expect": {"foo", "bar", "'abc xyz'"},
  93. },
  94. {
  95. "input": {`foo bar "abc xyz"`},
  96. "expect": {"foo", "bar", `"abc xyz"`},
  97. },
  98. {
  99. "input": {"àöû"},
  100. "expect": {"àöû"},
  101. },
  102. {
  103. "input": {`föo bàr "âbc xÿz"`},
  104. "expect": {"föo", "bàr", `"âbc xÿz"`},
  105. },
  106. }
  107. for _, test := range tests {
  108. d := Directive{LookingForDirectives: true}
  109. SetEscapeToken(DefaultEscapeToken, &d)
  110. words := parseWords(test["input"][0], &d)
  111. if len(words) != len(test["expect"]) {
  112. t.Fatalf("length check failed. input: %v, expect: %q, output: %q", test["input"][0], test["expect"], words)
  113. }
  114. for i, word := range words {
  115. if word != test["expect"][i] {
  116. t.Fatalf("word check failed for word: %q. input: %q, expect: %q, output: %q", word, test["input"][0], test["expect"], words)
  117. }
  118. }
  119. }
  120. }
  121. func TestLineInformation(t *testing.T) {
  122. df, err := os.Open(testFileLineInfo)
  123. if err != nil {
  124. t.Fatalf("Dockerfile missing for %s: %v", testFileLineInfo, err)
  125. }
  126. defer df.Close()
  127. d := Directive{LookingForDirectives: true}
  128. SetEscapeToken(DefaultEscapeToken, &d)
  129. ast, err := Parse(df, &d)
  130. if err != nil {
  131. t.Fatalf("Error parsing dockerfile %s: %v", testFileLineInfo, err)
  132. }
  133. if ast.StartLine != 5 || ast.EndLine != 31 {
  134. fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 5, 31, ast.StartLine, ast.EndLine)
  135. t.Fatalf("Root line information doesn't match result.")
  136. }
  137. if len(ast.Children) != 3 {
  138. fmt.Fprintf(os.Stderr, "Wrong number of child: expected(%d), actual(%d)\n", 3, len(ast.Children))
  139. t.Fatalf("Root line information doesn't match result for %s", testFileLineInfo)
  140. }
  141. expected := [][]int{
  142. {5, 5},
  143. {11, 12},
  144. {17, 31},
  145. }
  146. for i, child := range ast.Children {
  147. if child.StartLine != expected[i][0] || child.EndLine != expected[i][1] {
  148. t.Logf("Wrong line information for child %d: expected(%d-%d), actual(%d-%d)\n",
  149. i, expected[i][0], expected[i][1], child.StartLine, child.EndLine)
  150. t.Fatalf("Root line information doesn't match result.")
  151. }
  152. }
  153. }