shell_parser_test.go 3.3 KB

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