diff --git a/internal/bootstrap_test.go b/internal/bootstrap_test.go index c7b5fe4..ee0400f 100644 --- a/internal/bootstrap_test.go +++ b/internal/bootstrap_test.go @@ -310,9 +310,10 @@ func TestOnlyOneOfTheEnvironmentPairFailsWithoutCallingTheBroker(t *testing.T) { name string envURL bool envKey bool + err string }{ - {name: "only BIDIREKT_BROKER_URL", envURL: true}, - {name: "only BIDIREKT_API_KEY", envKey: true}, + {name: "only BIDIREKT_BROKER_URL", envURL: true, err: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile"}, + {name: "only BIDIREKT_API_KEY", envKey: true, err: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY to use the saved profile"}, } for _, test := range tests { @@ -329,7 +330,7 @@ func TestOnlyOneOfTheEnvironmentPairFailsWithoutCallingTheBroker(t *testing.T) { execution := executeRoot(t, components.New(), "create-participant", "pets") - require.EqualError(t, execution.err, "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither") + require.EqualError(t, execution.err, test.err) assert.Empty(t, execution.stderr) assert.Zero(t, broker.requests.Load()) }) diff --git a/internal/components/profile_resolution.go b/internal/components/profile_resolution.go index 4ab754a..c13fe2e 100644 --- a/internal/components/profile_resolution.go +++ b/internal/components/profile_resolution.go @@ -1,7 +1,6 @@ package components import ( - "errors" "fmt" "sort" "strings" @@ -12,8 +11,6 @@ const ( apiKeyPrefix = "bdk_" ) -var errOnlyOneEnvironmentVariable = errors.New("set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither") - type ProfileSources struct { EnvBrokerURL string EnvAPIKey string @@ -51,8 +48,10 @@ func ResolveProfile(sources ProfileSources) (ResolvedProfile, error) { case sources.EnvBrokerURL != "" && sources.EnvAPIKey != "": resolved = ResolvedProfile{BrokerURL: sources.EnvBrokerURL, APIKey: sources.EnvAPIKey, Origin: brokerURLEnvOrigin, Profile: profileName} apiKeyOrigin = "from BIDIREKT_API_KEY" - case sources.EnvBrokerURL != "" || sources.EnvAPIKey != "": - return ResolvedProfile{}, errOnlyOneEnvironmentVariable + case sources.EnvBrokerURL != "": + return ResolvedProfile{}, errOnlyOneEnvironmentVariable("BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY") + case sources.EnvAPIKey != "": + return ResolvedProfile{}, errOnlyOneEnvironmentVariable("BIDIREKT_API_KEY", "BIDIREKT_BROKER_URL") default: profiles, err := sources.ReadProfiles() if err != nil { @@ -83,6 +82,10 @@ func ResolveProfile(sources ProfileSources) (ResolvedProfile, error) { return resolved, nil } +func errOnlyOneEnvironmentVariable(set, missing string) error { + return fmt.Errorf("%s is set but %s is not — set %s too, or unset %s to use the saved profile", set, missing, missing, set) +} + func otherProfileNames(profiles map[string]Profile, activeProfileName string) []string { var names []string for name := range profiles { diff --git a/internal/features/login/login_command.go b/internal/features/login/login_command.go index 970ef43..0537f00 100644 --- a/internal/features/login/login_command.go +++ b/internal/features/login/login_command.go @@ -123,8 +123,10 @@ func warnAfterSignIn(stderr io.Writer, config *components.Config, previous compo switch { case config.BrokerURL != "" && config.APIKey != "": warnings = append(warnings, "BIDIREKT_BROKER_URL and BIDIREKT_API_KEY are set and win over this profile") - case config.BrokerURL != "" || config.APIKey != "": - warnings = append(warnings, "only one of BIDIREKT_BROKER_URL and BIDIREKT_API_KEY is set — commands will fail until both or neither are") + case config.BrokerURL != "": + warnings = append(warnings, onlyOneEnvironmentVariableWarning("BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY")) + case config.APIKey != "": + warnings = append(warnings, onlyOneEnvironmentVariableWarning("BIDIREKT_API_KEY", "BIDIREKT_BROKER_URL")) } for _, warning := range warnings { @@ -135,6 +137,10 @@ func warnAfterSignIn(stderr io.Writer, config *components.Config, previous compo return nil } +func onlyOneEnvironmentVariableWarning(set, missing string) string { + return fmt.Sprintf("%s is set but %s is not — commands will fail until you set %s too, or unset %s", set, missing, missing, set) +} + func link(brokerURL, path string) string { return components.RedactedURL(strings.TrimSuffix(brokerURL, "/")) + path } diff --git a/tests/components/profile_resolution_test.go b/tests/components/profile_resolution_test.go index bce8c62..8efa8e4 100644 --- a/tests/components/profile_resolution_test.go +++ b/tests/components/profile_resolution_test.go @@ -110,18 +110,32 @@ func TestResolveProfileReadsTheConfigFileOnlyWithoutTheEnvironmentPair(t *testin } func TestResolveProfileRejectsOnlyOneOfTheEnvironmentPair(t *testing.T) { - for _, sources := range []components.ProfileSources{ - {EnvBrokerURL: "http://env.internal"}, - {EnvAPIKey: "bdk_env"}, - } { - t.Run(sources.EnvBrokerURL+sources.EnvAPIKey, func(t *testing.T) { - sources.ReadProfiles = func() (map[string]components.Profile, error) { + tests := []struct { + name string + sources components.ProfileSources + err string + }{ + { + name: "only BIDIREKT_BROKER_URL", + sources: components.ProfileSources{EnvBrokerURL: "http://env.internal"}, + err: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile", + }, + { + name: "only BIDIREKT_API_KEY", + sources: components.ProfileSources{EnvAPIKey: "bdk_env"}, + err: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY to use the saved profile", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + test.sources.ReadProfiles = func() (map[string]components.Profile, error) { return map[string]components.Profile{"default": {BrokerURL: "http://default.internal", APIKey: "bdk_default"}}, nil } - _, err := components.ResolveProfile(sources) + _, err := components.ResolveProfile(test.sources) - assert.EqualError(t, err, "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither") + assert.EqualError(t, err, test.err) }) } } diff --git a/tests/e2e/commands.bats b/tests/e2e/commands.bats index 8f1804e..c892151 100644 --- a/tests/e2e/commands.bats +++ b/tests/e2e/commands.bats @@ -32,7 +32,7 @@ the config file has the profile e2e_$RUN — pass --profile e2e_$RUN or set BIDI @test "fails with BIDIREKT_BROKER_URL set and BIDIREKT_API_KEY unset" { run env -u BIDIREKT_API_KEY bidirekt list-participants assert_failure 1 - assert_output "set both BIDIREKT_BROKER_URL and BIDIREKT_API_KEY, or neither" + assert_output "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL to use the saved profile" } @test "shows only not signed in for a profile without an API key" { diff --git a/tests/integration/login_command_test.go b/tests/integration/login_command_test.go index 602833f..7bf9408 100644 --- a/tests/integration/login_command_test.go +++ b/tests/integration/login_command_test.go @@ -186,19 +186,31 @@ func TestLoginCommand(t *testing.T) { assert.JSONEq(t, `{"profiles":{"acme":{"brokerUrl":"`+acmeURL+`","apiKey":"`+acmeKey+`"}}}`, readConfigFile(t, configFilePath)) }) - for _, variable := range []string{"BIDIREKT_BROKER_URL", "BIDIREKT_API_KEY"} { - t.Run("with only "+variable+" set it warns that commands will fail", func(t *testing.T) { + onlyOneVariableTests := []struct { + variable string + value string + warning string + }{ + { + variable: "BIDIREKT_BROKER_URL", + value: globexURL, + warning: "BIDIREKT_BROKER_URL is set but BIDIREKT_API_KEY is not — commands will fail until you set BIDIREKT_API_KEY too, or unset BIDIREKT_BROKER_URL\n", + }, + { + variable: "BIDIREKT_API_KEY", + value: "bdk_globex", + warning: "BIDIREKT_API_KEY is set but BIDIREKT_BROKER_URL is not — commands will fail until you set BIDIREKT_BROKER_URL too, or unset BIDIREKT_API_KEY\n", + }, + } + for _, test := range onlyOneVariableTests { + t.Run("with only "+test.variable+" set it warns that commands will fail", func(t *testing.T) { isolateConfigFile(t, "") - value := globexURL - if variable == "BIDIREKT_API_KEY" { - value = "bdk_globex" - } - t.Setenv(variable, value) + t.Setenv(test.variable, test.value) execution := executeLogin(t, signInAcme(acmeURL+"\n", "--profile", "acme")) require.NoError(t, execution.err) - assert.True(t, strings.HasSuffix(execution.stderr, "only one of BIDIREKT_BROKER_URL and BIDIREKT_API_KEY is set — commands will fail until both or neither are\n")) + assert.True(t, strings.HasSuffix(execution.stderr, test.warning)) }) }