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