Change key to struct type

This commit is contained in:
joshcarp 2022-03-16 17:26:58 +11:00
parent 9439d8696c
commit d969ce0e38

View file

@ -2060,11 +2060,12 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
} }
func TestSetContext(t *testing.T) { func TestSetContext(t *testing.T) {
key, val := "foo", "bar" type key struct{}
val := "val"
root := &Command{ root := &Command{
Use: "root", Use: "root",
Run: func(cmd *Command, args []string) { Run: func(cmd *Command, args []string) {
key := cmd.Context().Value(key) key := cmd.Context().Value(key{})
got, ok := key.(string) got, ok := key.(string)
if !ok { if !ok {
t.Error("key not found in context") t.Error("key not found in context")
@ -2074,7 +2075,8 @@ func TestSetContext(t *testing.T) {
} }
}, },
} }
ctx := context.WithValue(context.Background(), key, val)
ctx := context.WithValue(context.Background(), key{}, val)
root.SetContext(ctx) root.SetContext(ctx)
err := root.Execute() err := root.Execute()
if err != nil { if err != nil {
@ -2083,16 +2085,17 @@ func TestSetContext(t *testing.T) {
} }
func TestSetContextPreRun(t *testing.T) { func TestSetContextPreRun(t *testing.T) {
key, val := "foo", "bar" type key struct{}
val := "bar"
root := &Command{ root := &Command{
Use: "root", Use: "root",
PreRun: func(cmd *Command, args []string) { PreRun: func(cmd *Command, args []string) {
ctx := context.WithValue(cmd.Context(), key, val) ctx := context.WithValue(cmd.Context(), key{}, val)
cmd.SetContext(ctx) cmd.SetContext(ctx)
}, },
Run: func(cmd *Command, args []string) { Run: func(cmd *Command, args []string) {
key := cmd.Context().Value(key) val := cmd.Context().Value(key{})
got, ok := key.(string) got, ok := val.(string)
if !ok { if !ok {
t.Error("key not found in context") t.Error("key not found in context")
} }
@ -2108,18 +2111,19 @@ func TestSetContextPreRun(t *testing.T) {
} }
func TestSetContextPreRunOverwrite(t *testing.T) { func TestSetContextPreRunOverwrite(t *testing.T) {
key, val := "foo", "bar" type key struct{}
val := "bar"
root := &Command{ root := &Command{
Use: "root", Use: "root",
Run: func(cmd *Command, args []string) { Run: func(cmd *Command, args []string) {
key := cmd.Context().Value(key) key := cmd.Context().Value(key{})
_, ok := key.(string) _, ok := key.(string)
if ok { if ok {
t.Error("key found in context when not expected") t.Error("key found in context when not expected")
} }
}, },
} }
ctx := context.WithValue(context.Background(), key, val) ctx := context.WithValue(context.Background(), key{}, val)
root.SetContext(ctx) root.SetContext(ctx)
err := root.ExecuteContext(context.Background()) err := root.ExecuteContext(context.Background())
if err != nil { if err != nil {
@ -2128,18 +2132,19 @@ func TestSetContextPreRunOverwrite(t *testing.T) {
} }
func TestSetContextPersistentPreRun(t *testing.T) { func TestSetContextPersistentPreRun(t *testing.T) {
key, val := "foo", "bar" type key struct{}
val := "bar"
root := &Command{ root := &Command{
Use: "root", Use: "root",
PersistentPreRun: func(cmd *Command, args []string) { PersistentPreRun: func(cmd *Command, args []string) {
ctx := context.WithValue(cmd.Context(), key, val) ctx := context.WithValue(cmd.Context(), key{}, val)
cmd.SetContext(ctx) cmd.SetContext(ctx)
}, },
} }
child := &Command{ child := &Command{
Use: "child", Use: "child",
Run: func(cmd *Command, args []string) { Run: func(cmd *Command, args []string) {
key := cmd.Context().Value(key) key := cmd.Context().Value(key{})
got, ok := key.(string) got, ok := key.(string)
if !ok { if !ok {
t.Error("key not found in context") t.Error("key not found in context")