Skip to content

Commit

Permalink
Merge pull request #2 from xmidt-org/feature/external-options
Browse files Browse the repository at this point in the history
Provide accepts external options that override injected options
  • Loading branch information
johnabass authored Oct 25, 2023
2 parents f3315c5 + e41cabd commit db2eaa4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
9 changes: 9 additions & 0 deletions constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package praetor

const (
testAddress = "localhost:1234"
testScheme = "https"
)
2 changes: 0 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/stretchr/testify/suite"
)

const testAddress = "localhost:1234"

type OptionSuite struct {
suite.Suite
}
Expand Down
103 changes: 78 additions & 25 deletions provide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/consul/api"
"github.com/stretchr/testify/suite"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)

type ProvideSuite struct {
Expand All @@ -19,24 +20,24 @@ type ProvideSuite struct {

func (suite *ProvideSuite) testDecorateSuccess() {
original := api.Config{
Address: "localhost:1234",
Address: testAddress,
}

decorated, err := Decorate(
original,
func(cfg *api.Config) error {
cfg.Scheme = "https"
cfg.Scheme = testScheme
return nil
},
)

suite.NoError(err)
suite.Equal(api.Config{Address: "localhost:1234", Scheme: "https"}, decorated)
suite.Equal(api.Config{Address: testAddress, Scheme: testScheme}, decorated)
}

func (suite *ProvideSuite) testDecorateOptionError() {
original := api.Config{
Address: "localhost:1234",
Address: testAddress,
}

expectedErr := errors.New("expected")
Expand All @@ -59,10 +60,10 @@ func (suite *ProvideSuite) TestDecorate() {
func (suite *ProvideSuite) testNewSuccess() {
c, err := New(
api.Config{
Address: "localhost:1234",
Address: testAddress,
},
func(cfg *api.Config) error {
cfg.Scheme = "https"
cfg.Scheme = testScheme
return nil
},
func(cfg *api.Config) error {
Expand All @@ -78,7 +79,7 @@ func (suite *ProvideSuite) testNewSuccess() {
func (suite *ProvideSuite) testNewNoOptions() {
c, err := New(
api.Config{
Address: "localhost:1234",
Address: testAddress,
},
)

Expand All @@ -91,7 +92,7 @@ func (suite *ProvideSuite) testNewOptionError() {

c, actualErr := New(
api.Config{
Address: "localhost:1234",
Address: testAddress,
},
func(*api.Config) error {
return expectedErr
Expand All @@ -108,39 +109,91 @@ func (suite *ProvideSuite) TestNew() {
suite.Run("OptionError", suite.testNewOptionError)
}

func (suite *ProvideSuite) TestExperiment() {
func (suite *ProvideSuite) testProvideDefault() {
var c *api.Client
app := fx.New(
app := fxtest.New(
suite.T(),
Provide(),
fx.Provide(
fx.Populate(&c),
)

suite.NoError(app.Err())
suite.NotNil(c)
}

func (suite *ProvideSuite) testProvideWithOptions() {
var (
c *api.Client
hc = new(http.Client)

external1 = func(cfg *api.Config) {
// injected options should execute first
suite.Equal("different:9999", cfg.Address)
suite.Equal(testScheme, cfg.Scheme)
cfg.Address = testAddress
}

external2 = WithHTTPClient(hc)
)

app := fxtest.New(
suite.T(),
fx.Supply(
fx.Annotate(
Option(func(cfg *api.Config) error {
cfg.Address = "different:9999"
return nil
}),
fx.ResultTags(`group:"consul.options"`),
),
fx.Annotate(
func() Option {
return func(cfg *api.Config) error {
suite.T().Log("option 1")
return nil
}
},
Option(func(cfg *api.Config) error {
cfg.Scheme = testScheme
return nil
}),
fx.ResultTags(`group:"consul.options"`),
),
),
Provide(AsOption(external1), external2),
fx.Populate(&c),
)

suite.NoError(app.Err())
suite.NotNil(c)
}

func (suite *ProvideSuite) testProvideWithConfig() {
var c *api.Client
app := fxtest.New(
suite.T(),
fx.Supply(
api.Config{
Address: "original:8888",
},
fx.Annotate(
func() []Option {
return []Option{
func(cfg *api.Config) error {
suite.T().Log("option 2")
return nil
},
}
},
Option(func(cfg *api.Config) error {
// the original configuration should be visible here
suite.Equal("original:8888", cfg.Address)
cfg.Address = "different:9999"
return nil
}),
fx.ResultTags(`group:"consul.options"`),
),
),
Provide(),
fx.Populate(&c),
)

suite.NoError(app.Err())
suite.NotNil(c)
}

func (suite *ProvideSuite) TestProvide() {
suite.Run("Default", suite.testProvideDefault)
suite.Run("WithOptions", suite.testProvideWithOptions)
suite.Run("WithConfig", suite.testProvideWithConfig)
}

func TestProvide(t *testing.T) {
suite.Run(t, new(ProvideSuite))
}

0 comments on commit db2eaa4

Please sign in to comment.