parser_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. _, err = Parse(df)
  34. if err == nil {
  35. t.Fatalf("No error parsing broken dockerfile for %s", dir)
  36. }
  37. df.Close()
  38. }
  39. }
  40. func TestTestData(t *testing.T) {
  41. for _, dir := range getDirs(t, testDir) {
  42. dockerfile := filepath.Join(testDir, dir, "Dockerfile")
  43. resultfile := filepath.Join(testDir, dir, "result")
  44. df, err := os.Open(dockerfile)
  45. if err != nil {
  46. t.Fatalf("Dockerfile missing for %s: %v", dir, err)
  47. }
  48. defer df.Close()
  49. ast, err := Parse(df)
  50. if err != nil {
  51. t.Fatalf("Error parsing %s's dockerfile: %v", dir, err)
  52. }
  53. content, err := ioutil.ReadFile(resultfile)
  54. if err != nil {
  55. t.Fatalf("Error reading %s's result file: %v", dir, err)
  56. }
  57. if runtime.GOOS == "windows" {
  58. // CRLF --> CR to match Unix behavior
  59. content = bytes.Replace(content, []byte{'\x0d', '\x0a'}, []byte{'\x0a'}, -1)
  60. }
  61. if ast.Dump()+"\n" != string(content) {
  62. fmt.Fprintln(os.Stderr, "Result:\n"+ast.Dump())
  63. fmt.Fprintln(os.Stderr, "Expected:\n"+string(content))
  64. t.Fatalf("%s: AST dump of dockerfile does not match result", dir)
  65. }
  66. }
  67. }
  68. func TestParseWords(t *testing.T) {
  69. tests := []map[string][]string{
  70. {
  71. "input": {"foo"},
  72. "expect": {"foo"},
  73. },
  74. {
  75. "input": {"foo bar"},
  76. "expect": {"foo", "bar"},
  77. },
  78. {
  79. "input": {"foo=bar"},
  80. "expect": {"foo=bar"},
  81. },
  82. {
  83. "input": {"foo bar 'abc xyz'"},
  84. "expect": {"foo", "bar", "'abc xyz'"},
  85. },
  86. {
  87. "input": {`foo bar "abc xyz"`},
  88. "expect": {"foo", "bar", `"abc xyz"`},
  89. },
  90. }
  91. for _, test := range tests {
  92. words := parseWords(test["input"][0])
  93. if len(words) != len(test["expect"]) {
  94. t.Fatalf("length check failed. input: %v, expect: %v, output: %v", test["input"][0], test["expect"], words)
  95. }
  96. for i, word := range words {
  97. if word != test["expect"][i] {
  98. t.Fatalf("word check failed for word: %q. input: %v, expect: %v, output: %v", word, test["input"][0], test["expect"], words)
  99. }
  100. }
  101. }
  102. }
  103. func TestLineInformation(t *testing.T) {
  104. df, err := os.Open(testFileLineInfo)
  105. if err != nil {
  106. t.Fatalf("Dockerfile missing for %s: %v", testFileLineInfo, err)
  107. }
  108. defer df.Close()
  109. ast, err := Parse(df)
  110. if err != nil {
  111. t.Fatalf("Error parsing dockerfile %s: %v", testFileLineInfo, err)
  112. }
  113. if ast.StartLine != 4 || ast.EndLine != 30 {
  114. fmt.Fprintf(os.Stderr, "Wrong root line information: expected(%d-%d), actual(%d-%d)\n", 4, 30, ast.StartLine, ast.EndLine)
  115. t.Fatalf("Root line information doesn't match result.")
  116. }
  117. if len(ast.Children) != 3 {
  118. fmt.Fprintf(os.Stderr, "Wrong number of child: expected(%d), actual(%d)\n", 3, len(ast.Children))
  119. t.Fatalf("Root line information doesn't match result.")
  120. }
  121. expected := [][]int{
  122. {4, 4},
  123. {10, 11},
  124. {16, 30},
  125. }
  126. for i, child := range ast.Children {
  127. if child.StartLine != expected[i][0] || child.EndLine != expected[i][1] {
  128. fmt.Fprintf(os.Stderr, "Wrong line information for child %d: expected(%d-%d), actual(%d-%d)\n",
  129. i, expected[i][0], expected[i][1], child.StartLine, child.EndLine)
  130. t.Fatalf("Root line information doesn't match result.")
  131. }
  132. }
  133. }