shell_parser_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dockerfile
  2. import (
  3. "bufio"
  4. "os"
  5. "runtime"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestShellParser4EnvVars(t *testing.T) {
  11. fn := "envVarTest"
  12. lineCount := 0
  13. file, err := os.Open(fn)
  14. assert.NoError(t, err)
  15. defer file.Close()
  16. shlex := NewShellLex('\\')
  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. assert.Len(t, words, 3)
  33. platform := strings.TrimSpace(words[0])
  34. source := strings.TrimSpace(words[1])
  35. expected := strings.TrimSpace(words[2])
  36. // Key W=Windows; A=All; U=Unix
  37. if platform != "W" && platform != "A" && platform != "U" {
  38. t.Fatalf("Invalid tag %s at line %d of %s. Must be W, A or U", platform, lineCount, fn)
  39. }
  40. if ((platform == "W" || platform == "A") && runtime.GOOS == "windows") ||
  41. ((platform == "U" || platform == "A") && runtime.GOOS != "windows") {
  42. newWord, err := shlex.ProcessWord(source, envs)
  43. if expected == "error" {
  44. assert.Error(t, err)
  45. } else {
  46. assert.NoError(t, err)
  47. assert.Equal(t, newWord, expected)
  48. }
  49. }
  50. }
  51. }
  52. func TestShellParser4Words(t *testing.T) {
  53. fn := "wordsTest"
  54. file, err := os.Open(fn)
  55. if err != nil {
  56. t.Fatalf("Can't open '%s': %s", err, fn)
  57. }
  58. defer file.Close()
  59. shlex := NewShellLex('\\')
  60. envs := []string{}
  61. scanner := bufio.NewScanner(file)
  62. lineNum := 0
  63. for scanner.Scan() {
  64. line := scanner.Text()
  65. lineNum = lineNum + 1
  66. if strings.HasPrefix(line, "#") {
  67. continue
  68. }
  69. if strings.HasPrefix(line, "ENV ") {
  70. line = strings.TrimLeft(line[3:], " ")
  71. envs = append(envs, line)
  72. continue
  73. }
  74. words := strings.Split(line, "|")
  75. if len(words) != 2 {
  76. t.Fatalf("Error in '%s'(line %d) - should be exactly one | in: %q", fn, lineNum, line)
  77. }
  78. test := strings.TrimSpace(words[0])
  79. expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
  80. result, err := shlex.ProcessWords(test, envs)
  81. if err != nil {
  82. result = []string{"error"}
  83. }
  84. if len(result) != len(expected) {
  85. t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
  86. }
  87. for i, w := range expected {
  88. if w != result[i] {
  89. t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
  90. }
  91. }
  92. }
  93. }
  94. func TestGetEnv(t *testing.T) {
  95. sw := &shellWord{envs: nil}
  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. }