Skip to content

Commit

Permalink
fix(go1.15): require non-nil parent in context.WithValue
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Nov 13, 2020
1 parent a7983af commit 7e0cba1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions kv/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ func (s *organizationsStore) Delete(ctx context.Context, o *chronograf.Organizat

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

sourcesStore := organizations.NewSourcesStore(s.client.SourcesStore(), o.ID)
Expand Down
3 changes: 3 additions & 0 deletions organizations/dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func TestDashboards_All(t *testing.T) {
}
for _, tt := range tests {
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
if tt.args.ctx == nil {
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
}
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
gots, err := s.All(tt.args.ctx)
if (err != nil) != tt.wantErr {
Expand Down
3 changes: 3 additions & 0 deletions organizations/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func TestOrganizations_All(t *testing.T) {
}
for _, tt := range tests {
s := organizations.NewOrganizationsStore(tt.fields.OrganizationsStore, tt.args.organization)
if tt.args.ctx == nil {
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
}
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
gots, err := s.All(tt.args.ctx)
if (err != nil) != tt.wantErr {
Expand Down
3 changes: 3 additions & 0 deletions organizations/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func TestServers_All(t *testing.T) {
}
for _, tt := range tests {
s := organizations.NewServersStore(tt.fields.ServersStore, tt.args.organization)
if tt.args.ctx == nil {
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
}
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
gots, err := s.All(tt.args.ctx)
if (err != nil) != tt.wantErr {
Expand Down
3 changes: 3 additions & 0 deletions organizations/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func TestSources_All(t *testing.T) {
}
for _, tt := range tests {
s := organizations.NewSourcesStore(tt.fields.SourcesStore, tt.args.organization)
if tt.args.ctx == nil {
tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737
}
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
gots, err := s.All(tt.args.ctx)
if (err != nil) != tt.wantErr {
Expand Down
3 changes: 3 additions & 0 deletions server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ func hasServerContext(ctx context.Context) bool {
}

func serverContext(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background() // context could be possible nil before go 1.15, see https://github.com/golang/go/issues/40737
}
return context.WithValue(ctx, ServerContextKey, true)
}
9 changes: 8 additions & 1 deletion server/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"github.com/bouk/httprouter"
Expand Down Expand Up @@ -135,7 +136,7 @@ func Test_ValidSourceRequest(t *testing.T) {
},
},
wants: wants{
err: fmt.Errorf("invalid source URI: parse im a bad url: invalid URI for request"),
err: fmt.Errorf("invalid source URI: parse"), // im a bad url: invalid URI for request
},
},
}
Expand All @@ -150,6 +151,12 @@ func Test_ValidSourceRequest(t *testing.T) {
return
}
if err.Error() != tt.wants.err.Error() {
if err != nil && tt.wants.err != nil {
if strings.HasPrefix(err.Error(), tt.wants.err.Error()) {
// error messages vary between go versions, but they have the same prefixes
return
}
}
t.Errorf("%q. ValidSourceRequest() = %q, want %q", tt.name, err, tt.wants.err)
}
})
Expand Down

0 comments on commit 7e0cba1

Please sign in to comment.