-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenvvar_test.go
More file actions
76 lines (60 loc) · 2.05 KB
/
envvar_test.go
File metadata and controls
76 lines (60 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package env
import (
"fmt"
"os"
"testing"
"github.com/Masterminds/cookoo"
)
func TestGet(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
drink := "DEIS_DRINK_OF_CHOICE"
cookies := "DEIS_FAVORITE_COOKIES"
snack := "DEIS_SNACK_TIME"
snackVal := fmt.Sprintf("$%s and $%s cookies", drink, cookies)
// Set drink, but not cookies.
os.Setenv(drink, "coffee")
reg.Route("test", "Test route").
Does(Get, "res").
Using(drink).WithDefault("tea").
Using(cookies).WithDefault("chocolate chip").
Does(Get, "res2").
Using(snack).WithDefault(snackVal)
err := router.HandleRequest("test", cxt, true)
if err != nil {
t.Error(err)
}
// Drink should still be coffee.
if coffee := cxt.Get(drink, "").(string); coffee != "coffee" {
t.Errorf("A great sin has been committed. Expected coffee, but got '%s'", coffee)
}
// Env var should be untouched
if coffee := os.Getenv(drink); coffee != "coffee" {
t.Errorf("Environment was changed from 'coffee' to '%s'", coffee)
}
// Cookies should have been set to the default
if cookies := cxt.Get(cookies, "").(string); cookies != "chocolate chip" {
t.Errorf("Expected chocolate chip cookies, but instead, got '%s' :-(", cookies)
}
// In the environment, cookies should have been set.
if cookies := os.Getenv(cookies); cookies != "chocolate chip" {
t.Errorf("Expected environment to have chocolate chip cookies, but instead, got '%s'", cookies)
}
if both := cxt.Get(snack, "").(string); both != "coffee and chocolate chip cookies" {
t.Errorf("Expected 'coffee and chocolate chip cookies'. Got '%s'", both)
}
}
// TestGetInterpolation is a regression test to make sure that values are
// interpolated correctly.
func TestGetInterpolation(t *testing.T) {
reg, router, cxt := cookoo.Cookoo()
os.Setenv("TEST_ENV", "is")
reg.Route("test", "Test route").
Does(Get, "res").
Using("TEST_ENV2").WithDefault("de$TEST_ENV")
if err := router.HandleRequest("test", cxt, true); err != nil {
t.Error(err)
}
if os.Getenv("TEST_ENV2") != "deis" {
t.Errorf("Expected 'deis', got '%s'", os.Getenv("TEST_ENV2"))
}
}