Skip to content

Commit 7e0cba1

Browse files
committed
fix(go1.15): require non-nil parent in context.WithValue
1 parent a7983af commit 7e0cba1

File tree

7 files changed

+26
-1
lines changed

7 files changed

+26
-1
lines changed

Diff for: kv/organizations.go

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func (s *organizationsStore) Delete(ctx context.Context, o *chronograf.Organizat
154154

155155
// Each of the associated organization stores expects organization to be
156156
// set on the context.
157+
if ctx == nil {
158+
ctx = context.Background() // context could be possible nil before go 1.15, see https://github.com/golang/go/issues/40737
159+
}
157160
ctx = context.WithValue(ctx, organizations.ContextKey, o.ID)
158161

159162
sourcesStore := organizations.NewSourcesStore(s.client.SourcesStore(), o.ID)

Diff for: organizations/dashboards_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func TestDashboards_All(t *testing.T) {
7878
}
7979
for _, tt := range tests {
8080
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
81+
if tt.args.ctx == nil {
82+
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
83+
}
8184
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
8285
gots, err := s.All(tt.args.ctx)
8386
if (err != nil) != tt.wantErr {

Diff for: organizations/organizations_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func TestOrganizations_All(t *testing.T) {
9494
}
9595
for _, tt := range tests {
9696
s := organizations.NewOrganizationsStore(tt.fields.OrganizationsStore, tt.args.organization)
97+
if tt.args.ctx == nil {
98+
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
99+
}
97100
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
98101
gots, err := s.All(tt.args.ctx)
99102
if (err != nil) != tt.wantErr {

Diff for: organizations/servers_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func TestServers_All(t *testing.T) {
7979
}
8080
for _, tt := range tests {
8181
s := organizations.NewServersStore(tt.fields.ServersStore, tt.args.organization)
82+
if tt.args.ctx == nil {
83+
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
84+
}
8285
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
8386
gots, err := s.All(tt.args.ctx)
8487
if (err != nil) != tt.wantErr {

Diff for: organizations/sources_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func TestSources_All(t *testing.T) {
7979
}
8080
for _, tt := range tests {
8181
s := organizations.NewSourcesStore(tt.fields.SourcesStore, tt.args.organization)
82+
if tt.args.ctx == nil {
83+
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
84+
}
8285
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
8386
gots, err := s.All(tt.args.ctx)
8487
if (err != nil) != tt.wantErr {

Diff for: server/context.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ func hasServerContext(ctx context.Context) bool {
2626
}
2727

2828
func serverContext(ctx context.Context) context.Context {
29+
if ctx == nil {
30+
ctx = context.Background() // context could be possible nil before go 1.15, see https://github.com/golang/go/issues/40737
31+
}
2932
return context.WithValue(ctx, ServerContextKey, true)
3033
}

Diff for: server/sources_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/http/httptest"
1010
"reflect"
11+
"strings"
1112
"testing"
1213

1314
"github.com/bouk/httprouter"
@@ -135,7 +136,7 @@ func Test_ValidSourceRequest(t *testing.T) {
135136
},
136137
},
137138
wants: wants{
138-
err: fmt.Errorf("invalid source URI: parse im a bad url: invalid URI for request"),
139+
err: fmt.Errorf("invalid source URI: parse"), // im a bad url: invalid URI for request
139140
},
140141
},
141142
}
@@ -150,6 +151,12 @@ func Test_ValidSourceRequest(t *testing.T) {
150151
return
151152
}
152153
if err.Error() != tt.wants.err.Error() {
154+
if err != nil && tt.wants.err != nil {
155+
if strings.HasPrefix(err.Error(), tt.wants.err.Error()) {
156+
// error messages vary between go versions, but they have the same prefixes
157+
return
158+
}
159+
}
153160
t.Errorf("%q. ValidSourceRequest() = %q, want %q", tt.name, err, tt.wants.err)
154161
}
155162
})

0 commit comments

Comments
 (0)