github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/internal/context/default_test.go (about) 1 package context 2 3 import ( 4 "os" 5 "reflect" 6 "runtime" 7 "testing" 8 9 "github.com/hashicorp/hcl/v2" 10 "github.com/zclconf/go-cty/cty" 11 "github.com/zclconf/go-cty/cty/function" 12 ) 13 14 func TestGetEnvironmentMap(t *testing.T) { 15 tests := []struct { 16 Name string 17 Before func() 18 Want map[string]cty.Value 19 }{ 20 { 21 Name: "empty environment", 22 Before: func() { 23 os.Clearenv() 24 }, 25 Want: map[string]cty.Value{}, 26 }, 27 { 28 Name: "empty environment", 29 Before: func() { 30 os.Setenv("foo", "bar") 31 }, 32 Want: map[string]cty.Value{ 33 "foo": cty.StringVal("bar"), 34 }, 35 }, 36 } 37 38 for _, test := range tests { 39 t.Run(test.Name, func(t *testing.T) { 40 test.Before() 41 got := GetEnvironmentMap() 42 43 if !reflect.DeepEqual(got, test.Want) { 44 t.Errorf("got: %v, want: %v", got, test.Want) 45 } 46 }) 47 } 48 } 49 50 func TestBuildInitialContext(t *testing.T) { 51 tests := []struct { 52 Name string 53 Want *hcl.EvalContext 54 }{ 55 { 56 Name: "body and url constants", 57 Want: &hcl.EvalContext{ 58 Variables: map[string]cty.Value{ 59 "env": cty.ObjectVal(GetEnvironmentMap()), 60 "os": cty.ObjectVal(map[string]cty.Value{ 61 "name": cty.StringVal(runtime.GOOS), 62 "arch": cty.StringVal(runtime.GOARCH), 63 }), 64 "body": cty.StringVal("body"), 65 "url": cty.StringVal("url"), 66 }, 67 Functions: map[string]function.Function{}, 68 }, 69 }, 70 } 71 72 for _, test := range tests { 73 t.Run(test.Name, func(t *testing.T) { 74 got := BuildInitialContext() 75 76 if !reflect.DeepEqual(got, test.Want) { 77 t.Errorf("got: %v, want: %v", got, test.Want) 78 } 79 }) 80 } 81 }