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