blob: 81674939459b447d1bb2aec816bcf65e952151b6 [file] [log] [blame]
Jiri Simsa1f97e1f2014-05-28 17:32:47 -07001package exec
2
3import (
4 "testing"
5)
6
7func TestEnv(t *testing.T) {
8 env := make([]string, 0)
Jiri Simsa70c32052014-06-18 11:38:21 -07009 env = Setenv(env, "NAME", "VALUE1")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070010 if expected, got := 1, len(env); expected != got {
11 t.Fatalf("Unexpected length of environment variable slice: expected %d, got %d", expected, got)
12 }
13 if expected, got := "NAME=VALUE1", env[0]; expected != got {
14 t.Fatalf("Unexpected element in the environment variable slice: expected %d, got %d", expected, got)
15 }
Jiri Simsa70c32052014-06-18 11:38:21 -070016 env = Setenv(env, "NAME", "VALUE2")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070017 if expected, got := 1, len(env); expected != got {
18 t.Fatalf("Unexpected length of environment variable slice: expected %d, got %d", expected, got)
19 }
20 if expected, got := "NAME=VALUE2", env[0]; expected != got {
21 t.Fatalf("Unexpected element in the environment variable slice: expected %d, got %d", expected, got)
22 }
Jiri Simsa70c32052014-06-18 11:38:21 -070023 value, err := Getenv(env, "NAME")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070024 if err != nil {
25 t.Fatalf("Unexpected error when looking up environment variable value: %v", err)
26 }
27 if expected, got := "VALUE2", value; expected != got {
28 t.Fatalf("Unexpected value of an environment variable: expected %d, got %d", expected, got)
29 }
Jiri Simsa70c32052014-06-18 11:38:21 -070030 value, err = Getenv(env, "NONAME")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070031 if err == nil {
32 t.Fatalf("Expected error when looking up environment variable value, got none", value)
33 }
34}
Bogdan Caprita5420f172014-10-10 15:58:14 -070035
36func TestMerge(t *testing.T) {
37 env := []string{"ANIMAL=GOPHER", "METAL=CHROMIUM"}
38 env = Mergeenv(env, []string{"CAR=VEYRON", "METAL=VANADIUM"})
39 if want, got := 3, len(env); want != got {
40 t.Fatalf("Expected %d env vars, got %d instead", want, got)
41 }
42 for n, want := range map[string]string{
43 "ANIMAL": "GOPHER",
44 "CAR": "VEYRON",
45 "METAL": "VANADIUM",
46 } {
47 if got, err := Getenv(env, n); err != nil {
48 t.Fatalf("Expected a value when looking up %q, got none", n)
49 } else if got != want {
50 t.Fatalf("Unexpected value of environment variable %q: expected %q, got %q", n, want, got)
51 }
52 }
53}