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