lex_test.go 3.5 KB

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