Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Fix UserAgent test
Browse files Browse the repository at this point in the history
This test created a normal client without having a credential available. By decoupling the tested functionality from NewClient, the credential loading functionality is no longer triggered and this error is avoided.
  • Loading branch information
jpcoenen committed Feb 5, 2021
1 parent 9f74af2 commit 4291882
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
29 changes: 16 additions & 13 deletions pkg/secrethub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,7 @@ func NewClient(with ...ClientOption) (*Client, error) {
}
}

appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
if appName != "" {
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
topLevelAppInfo := &AppInfo{
Name: appName,
Version: appVersion,
}
// Ignore app info from environment variable if name is invalid
if err = topLevelAppInfo.ValidateName(); err == nil {
client.appInfo = append(client.appInfo, topLevelAppInfo)
}
}

client.loadAppInfoFromEnv()
userAgent := client.userAgent()

client.httpClient.Options(http.WithUserAgent(userAgent))
Expand Down Expand Up @@ -286,6 +274,21 @@ func (c *Client) isKeyed() bool {
return c.decrypter != nil
}

func (c *Client) loadAppInfoFromEnv() {
appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
if appName != "" {
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
topLevelAppInfo := &AppInfo{
Name: appName,
Version: appVersion,
}
// Ignore app info from environment variable if name is invalid
if err := topLevelAppInfo.ValidateName(); err == nil {
c.appInfo = append(c.appInfo, topLevelAppInfo)
}
}
}

func (c *Client) userAgent() string {
userAgent := userAgentPrefix
for _, info := range c.appInfo {
Expand Down
8 changes: 4 additions & 4 deletions pkg/secrethub/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func TestClient_userAgent(t *testing.T) {
for _, info := range tc.appInfo {
opts = append(opts, WithAppInfo(info))
}
client, err := NewClient(opts...)
client := &Client{}
err := client.with(opts...)
assert.Equal(t, err, tc.err)
if err != nil {
return
}

client.loadAppInfoFromEnv()

userAgent := client.userAgent()
pattern := tc.expected + " \\(.*\\)"
Expand Down

0 comments on commit 4291882

Please sign in to comment.