shell_parser_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 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.Len(t, 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.NoError(t, err)
  46. assert.Equal(t, newWord, 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. lineNum := 0
  61. for scanner.Scan() {
  62. line := scanner.Text()
  63. lineNum = lineNum + 1
  64. if strings.HasPrefix(line, "#") {
  65. continue
  66. }
  67. if strings.HasPrefix(line, "ENV ") {
  68. line = strings.TrimLeft(line[3:], " ")
  69. envs = append(envs, line)
  70. continue
  71. }
  72. words := strings.Split(line, "|")
  73. if len(words) != 2 {
  74. t.Fatalf("Error in '%s'(line %d) - should be exactly one | in: %q", fn, lineNum, line)
  75. }
  76. test := strings.TrimSpace(words[0])
  77. expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
  78. result, err := ProcessWords(test, envs, '\\')
  79. if err != nil {
  80. result = []string{"error"}
  81. }
  82. if len(result) != len(expected) {
  83. t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
  84. }
  85. for i, w := range expected {
  86. if w != result[i] {
  87. t.Fatalf("Error on line %d. %q was suppose to result in %q, but got %q instead", lineNum, test, expected, result)
  88. }
  89. }
  90. }
  91. }
  92. func TestGetEnv(t *testing.T) {
  93. sw := &shellWord{
  94. word: "",
  95. envs: nil,
  96. pos: 0,
  97. }
  98. sw.envs = []string{}
  99. if sw.getEnv("foo") != "" {
  100. t.Fatal("2 - 'foo' should map to ''")
  101. }
  102. sw.envs = []string{"foo"}
  103. if sw.getEnv("foo") != "" {
  104. t.Fatal("3 - 'foo' should map to ''")
  105. }
  106. sw.envs = []string{"foo="}
  107. if sw.getEnv("foo") != "" {
  108. t.Fatal("4 - 'foo' should map to ''")
  109. }
  110. sw.envs = []string{"foo=bar"}
  111. if sw.getEnv("foo") != "bar" {
  112. t.Fatal("5 - 'foo' should map to 'bar'")
  113. }
  114. sw.envs = []string{"foo=bar", "car=hat"}
  115. if sw.getEnv("foo") != "bar" {
  116. t.Fatal("6 - 'foo' should map to 'bar'")
  117. }
  118. if sw.getEnv("car") != "hat" {
  119. t.Fatal("7 - 'car' should map to 'hat'")
  120. }
  121. // Make sure we grab the first 'car' in the list
  122. sw.envs = []string{"foo=bar", "car=hat", "car=bike"}
  123. if sw.getEnv("car") != "hat" {
  124. t.Fatal("8 - 'car' should map to 'hat'")
  125. }
  126. }