diff --git a/cloud/common.go b/cloud/common.go index 261b549..ec35369 100644 --- a/cloud/common.go +++ b/cloud/common.go @@ -41,6 +41,14 @@ func NewClient(ctx context.Context, socket HTTPClient, socketPath string) Client } } +// NewTestClient returns the initialized client with test value set +// from the mock client passed into it. +func NewTestClient(ctx context.Context, mock *MockClient, socketPath string) Client { + client := NewClient(ctx, mock, socketPath) + client.test = mock.test + return client +} + // InitClientUnixSocket configures the client to use a unix domain socket // This configures the cloud.Client with the socket path // It must be called before using any of the cloud.Client functions. @@ -64,7 +72,7 @@ type Client struct { host string // defaults to localhost socketPath string rawFunc func(string, string, int, []byte) // Pass the raw json data to a user function - Test bool // Used to fake the presense of the socket for testing + test bool // Used to fake the presense of the socket for testing } // SetRawCallback sets a function that will be called with the server response @@ -233,7 +241,7 @@ func AppendQuery(url, query string) string { } func (c Client) Exists() bool { - if c.Test { + if c.test { return true } return checkSocketError(c.socketPath, nil) == nil diff --git a/cloud/utils.go b/cloud/utils.go index c485ade..a2816d9 100644 --- a/cloud/utils.go +++ b/cloud/utils.go @@ -58,7 +58,7 @@ func TearDownTemporaryRepository(dir string) error { type MockClient struct { DoFunc func(req *http.Request) (*http.Response, error) Req http.Request - Test bool + test bool } // Do saves the request in m.Req and runs the function set in m.DoFunc @@ -67,3 +67,13 @@ func (m *MockClient) Do(req *http.Request) (*http.Response, error) { m.Req = *req return m.DoFunc(req) } + +// TestOff turns off the test flag used to fake the presense of the socket file +func (m *MockClient) TestOff() { + m.test = false +} + +// TestOn turns on the test flag used to fake the presense of the socket file +func (m *MockClient) TestOn() { + m.test = true +} diff --git a/cmd/composer-cli/root/test_utilities.go b/cmd/composer-cli/root/test_utilities.go index ee67685..2dbd48c 100644 --- a/cmd/composer-cli/root/test_utilities.go +++ b/cmd/composer-cli/root/test_utilities.go @@ -138,8 +138,7 @@ func cobraInit() { cobra.OnInitialize(func() { // This function is called at the start of each command execution Client = weldr.NewClient(context.Background(), &mockWeldrClient, 1, "") - Cloud = cloud.NewClient(context.Background(), &mockCloudClient, "") - Cloud.Test = mockCloudClient.Test + Cloud = cloud.NewTestClient(context.Background(), &mockCloudClient, "") setupJSONOutput() }) cobraInitialized = true @@ -149,7 +148,7 @@ func cobraInit() { // SetupCmdTest initializes the weldr client with a Mock Client used to capture test details // Pass in a function to be run when the client queries the server. See weldr test functions. func SetupCmdTest(f func(request *http.Request) (*http.Response, error)) *weldr.MockClient { - mockCloudClient.Test = false + mockCloudClient.TestOff() mockWeldrClient = weldr.MockClient{ DoFunc: f, } @@ -157,12 +156,12 @@ func SetupCmdTest(f func(request *http.Request) (*http.Response, error)) *weldr. } // SetupCloudCmdTest initializes the cloud client with a Mock Client used to capture test details -// Pass in a function to be run when the client queries the server. See cloud test functions. +// Pass in a function to be run when the client queries the server. Set cloud test functions. func SetupCloudCmdTest(f func(request *http.Request) (*http.Response, error)) *cloud.MockClient { mockCloudClient = cloud.MockClient{ DoFunc: f, - Test: true, } + mockCloudClient.TestOn() return &mockCloudClient }