|
| 1 | +package goenv |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParse(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + goos string |
| 11 | + lines []string |
| 12 | + goroot string |
| 13 | + gopath string |
| 14 | + }{ |
| 15 | + { |
| 16 | + goos: "windows", |
| 17 | + lines: []string{ |
| 18 | + "set GOROOT=C:\\Program Files\\Go\r\n", |
| 19 | + "set GOPATH=C:\\Users\\me\\go\r\n", |
| 20 | + }, |
| 21 | + goroot: "C:\\Program Files\\Go", |
| 22 | + gopath: "C:\\Users\\me\\go", |
| 23 | + }, |
| 24 | + |
| 25 | + // Don't do trim on Windows. |
| 26 | + { |
| 27 | + goos: "windows", |
| 28 | + lines: []string{ |
| 29 | + "set GOROOT=C:\\Program Files\\Go \r\n", |
| 30 | + "set GOPATH=C:\\Users\\me\\go \r\n", |
| 31 | + }, |
| 32 | + goroot: "C:\\Program Files\\Go ", |
| 33 | + gopath: "C:\\Users\\me\\go ", |
| 34 | + }, |
| 35 | + |
| 36 | + { |
| 37 | + goos: "linux", |
| 38 | + lines: []string{ |
| 39 | + "GOROOT=\"/usr/local/go\"\n", |
| 40 | + "GOPATH=\"/home/me/go\"\n", |
| 41 | + }, |
| 42 | + goroot: "/usr/local/go", |
| 43 | + gopath: "/home/me/go", |
| 44 | + }, |
| 45 | + |
| 46 | + // Trim lines on Linux. |
| 47 | + { |
| 48 | + goos: "linux", |
| 49 | + lines: []string{ |
| 50 | + " GOROOT=\"/usr/local/go\" \n", |
| 51 | + "GOPATH=\"/home/me/go\" \n", |
| 52 | + }, |
| 53 | + goroot: "/usr/local/go", |
| 54 | + gopath: "/home/me/go", |
| 55 | + }, |
| 56 | + |
| 57 | + // Quotes preserve the whitespace. |
| 58 | + { |
| 59 | + goos: "linux", |
| 60 | + lines: []string{ |
| 61 | + " GOROOT=\"/usr/local/go \" \n", |
| 62 | + "GOPATH=\"/home/me/go \" \n", |
| 63 | + }, |
| 64 | + goroot: "/usr/local/go ", |
| 65 | + gopath: "/home/me/go ", |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for i, test := range tests { |
| 70 | + data := []byte(strings.Join(test.lines, "")) |
| 71 | + vars, err := parseGoEnv(data, test.goos) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("test %d failed: %v", i, err) |
| 74 | + } |
| 75 | + if vars["GOROOT"] != test.goroot { |
| 76 | + t.Errorf("test %d GOROOT mismatch: have %q, want %q", i, vars["GOROOT"], test.goroot) |
| 77 | + continue |
| 78 | + } |
| 79 | + if vars["GOPATH"] != test.gopath { |
| 80 | + t.Errorf("test %d GOPATH mismatch: have %q, want %q", i, vars["GOPATH"], test.gopath) |
| 81 | + continue |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments