shell_parser_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package dockerfile
  2. import (
  3. "bufio"
  4. "os"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. )
  9. func TestShellParser4EnvVars(t *testing.T) {
  10. fn := "envVarTest"
  11. lineCount := 0
  12. file, err := os.Open(fn)
  13. if err != nil {
  14. t.Fatalf("Can't open '%s': %s", err, fn)
  15. }
  16. defer file.Close()
  17. scanner := bufio.NewScanner(file)
  18. envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
  19. for scanner.Scan() {
  20. line := scanner.Text()
  21. lineCount++
  22. // Trim comments and blank lines
  23. i := strings.Index(line, "#")
  24. if i >= 0 {
  25. line = line[:i]
  26. }
  27. line = strings.TrimSpace(line)
  28. if line == "" {
  29. continue
  30. }
  31. words := strings.Split(line, "|")
  32. if len(words) != 3 {
  33. t.Fatalf("Error in '%s' - should be exactly one | in:%q", fn, line)
  34. }
  35. words[0] = strings.TrimSpace(words[0])
  36. words[1] = strings.TrimSpace(words[1])
  37. words[2] = strings.TrimSpace(words[2])
  38. // Key W=Windows; A=All; U=Unix
  39. if (words[0] != "W") && (words[0] != "A") && (words[0] != "U") {
  40. t.Fatalf("Invalid tag %s at line %d of %s. Must be W, A or U", words[0], lineCount, fn)
  41. }
  42. if ((words[0] == "W" || words[0] == "A") && runtime.GOOS == "windows") ||
  43. ((words[0] == "U" || words[0] == "A") && runtime.GOOS != "windows") {
  44. newWord, err := ProcessWord(words[1], envs, '\\')
  45. if err != nil {
  46. newWord = "error"
  47. }
  48. if newWord != words[2] {
  49. t.Fatalf("Error. Src: %s Calc: %s Expected: %s at line %d", words[1], newWord, words[2], lineCount)
  50. }
  51. }
  52. }
  53. }
  54. func TestShellParser4Words(t *testing.T) {
  55. fn := "wordsTest"
  56. file, err := os.Open(fn)
  57. if err != nil {
  58. t.Fatalf("Can't open '%s': %s", err, fn)
  59. }
  60. defer file.Close()
  61. envs := []string{}
  62. scanner := bufio.NewScanner(file)
  63. for scanner.Scan() {
  64. line := scanner.Text()
  65. if strings.HasPrefix(line, "#") {
  66. continue
  67. }
  68. if strings.HasPrefix(line, "ENV ") {
  69. line = strings.TrimLeft(line[3:], " ")
  70. envs = append(envs, line)
  71. continue
  72. }
  73. words := strings.Split(line, "|")
  74. if len(words) != 2 {
  75. t.Fatalf("Error in '%s' - should be exactly one | in: %q", fn, line)
  76. }
  77. test := strings.TrimSpace(words[0])
  78. expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
  79. result, err := ProcessWords(test, envs, '\\')
  80. if err != nil {
  81. result = []string{"error"}
  82. }
  83. if len(result) != len(expected) {
  84. t.Fatalf("Error. %q was suppose to result in %q, but got %q instead", test, expected, result)
  85. }
  86. for i, w := range expected {
  87. if w != result[i] {
  88. t.Fatalf("Error. %q was suppose to result in %q, but got %q instead", test, expected, result)
  89. }
  90. }
  91. }
  92. }
  93. func TestGetEnv(t *testing.T) {
  94. sw := &shellWord{
  95. word: "",
  96. envs: nil,
  97. pos: 0,
  98. }
  99. sw.envs = []string{}
  100. if sw.getEnv("foo") != "" {
  101. t.Fatal("2 - 'foo' should map to ''")
  102. }
  103. sw.envs = []string{"foo"}
  104. if sw.getEnv("foo") != "" {
  105. t.Fatal("3 - 'foo' should map to ''")
  106. }
  107. sw.envs = []string{"foo="}
  108. if sw.getEnv("foo") != "" {
  109. t.Fatal("4 - 'foo' should map to ''")
  110. }
  111. sw.envs = []string{"foo=bar"}
  112. if sw.getEnv("foo") != "bar" {
  113. t.Fatal("5 - 'foo' should map to 'bar'")
  114. }
  115. sw.envs = []string{"foo=bar", "car=hat"}
  116. if sw.getEnv("foo") != "bar" {
  117. t.Fatal("6 - 'foo' should map to 'bar'")
  118. }
  119. if sw.getEnv("car") != "hat" {
  120. t.Fatal("7 - 'car' should map to 'hat'")
  121. }
  122. // Make sure we grab the first 'car' in the list
  123. sw.envs = []string{"foo=bar", "car=hat", "car=bike"}
  124. if sw.getEnv("car") != "hat" {
  125. t.Fatal("8 - 'car' should map to 'hat'")
  126. }
  127. }