shell_parser_test.go 2.9 KB

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