blob: 3fb073a183cbcfe81ecd2b4d3a68f898904cc136 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Jiri Simsa1f97e1f2014-05-28 17:32:47 -07005package exec
6
7import (
8 "testing"
9)
10
11func TestEnv(t *testing.T) {
12 env := make([]string, 0)
Jiri Simsa70c32052014-06-18 11:38:21 -070013 env = Setenv(env, "NAME", "VALUE1")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070014 if expected, got := 1, len(env); expected != got {
15 t.Fatalf("Unexpected length of environment variable slice: expected %d, got %d", expected, got)
16 }
17 if expected, got := "NAME=VALUE1", env[0]; expected != got {
18 t.Fatalf("Unexpected element in the environment variable slice: expected %d, got %d", expected, got)
19 }
Jiri Simsa70c32052014-06-18 11:38:21 -070020 env = Setenv(env, "NAME", "VALUE2")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070021 if expected, got := 1, len(env); expected != got {
22 t.Fatalf("Unexpected length of environment variable slice: expected %d, got %d", expected, got)
23 }
24 if expected, got := "NAME=VALUE2", env[0]; expected != got {
25 t.Fatalf("Unexpected element in the environment variable slice: expected %d, got %d", expected, got)
26 }
Jiri Simsa70c32052014-06-18 11:38:21 -070027 value, err := Getenv(env, "NAME")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070028 if err != nil {
29 t.Fatalf("Unexpected error when looking up environment variable value: %v", err)
30 }
31 if expected, got := "VALUE2", value; expected != got {
32 t.Fatalf("Unexpected value of an environment variable: expected %d, got %d", expected, got)
33 }
Jiri Simsa70c32052014-06-18 11:38:21 -070034 value, err = Getenv(env, "NONAME")
Jiri Simsa1f97e1f2014-05-28 17:32:47 -070035 if err == nil {
36 t.Fatalf("Expected error when looking up environment variable value, got none", value)
37 }
38}
Bogdan Caprita5420f172014-10-10 15:58:14 -070039
40func TestMerge(t *testing.T) {
41 env := []string{"ANIMAL=GOPHER", "METAL=CHROMIUM"}
42 env = Mergeenv(env, []string{"CAR=VEYRON", "METAL=VANADIUM"})
43 if want, got := 3, len(env); want != got {
44 t.Fatalf("Expected %d env vars, got %d instead", want, got)
45 }
46 for n, want := range map[string]string{
47 "ANIMAL": "GOPHER",
48 "CAR": "VEYRON",
49 "METAL": "VANADIUM",
50 } {
51 if got, err := Getenv(env, n); err != nil {
52 t.Fatalf("Expected a value when looking up %q, got none", n)
53 } else if got != want {
54 t.Fatalf("Unexpected value of environment variable %q: expected %q, got %q", n, want, got)
55 }
56 }
57}