Skip to content

Commit

Permalink
Make function more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Nov 10, 2024
1 parent 13444e6 commit ec983f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions common/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func (cc *ConfigCommand) CommandName() string {
return "config"
}

// Exec runs the ConfigCommand and then triggers a usage report,
// ExecAndReportUsage runs the ConfigCommand and then triggers a usage report,
// which requires the Artifactory URL initialized by cc.Run().
// A channel ensures usage reporting completes before returning.
func (cc *ConfigCommand) Exec() error {
func (cc *ConfigCommand) ExecAndReportUsage() error {
err := cc.Run()
channel := make(chan bool)
// Triggers the report usage.
Expand Down
26 changes: 16 additions & 10 deletions common/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestDefaultServerId(t *testing.T) {
outputConfig, err := GetConfig(config.DefaultServerId, false)
assert.NoError(t, err)
assert.Equal(t, config.DefaultServerId, outputConfig.ServerId)
assert.NoError(t, NewConfigCommand(Delete, config.DefaultServerId).Exec())
assert.NoError(t, NewConfigCommand(Delete, config.DefaultServerId).Run())
}

func TestArtifactorySshKey(t *testing.T) {
Expand Down Expand Up @@ -94,12 +94,12 @@ func TestApiKeyInAccessToken(t *testing.T) {
// Should throw error if access token is API key and no username
configCmd := NewConfigCommand(AddOrEdit, testServerId).SetDetails(inputDetails).SetUseBasicAuthOnly(true).SetInteractive(false)
configCmd.disablePrompts = true
assert.ErrorContains(t, configCmd.Exec(), "the provided Access Token is an API key")
assert.ErrorContains(t, configCmd.Run(), "the provided Access Token is an API key")

// Should work without error if access token is API key but username exists
inputDetails.User = "ADMIN"
configCmd.SetDetails(inputDetails)
assert.NoError(t, configCmd.Exec())
assert.NoError(t, configCmd.Run())
}

func TestMTLS(t *testing.T) {
Expand Down Expand Up @@ -181,13 +181,13 @@ func TestBasicAuthOnlyOption(t *testing.T) {
outputConfig, err := configAndGetTestServer(t, inputDetails, true, false)
assert.NoError(t, err)
assert.Equal(t, coreutils.TokenRefreshDisabled, outputConfig.ArtifactoryTokenRefreshInterval, "expected refreshable token to be disabled")
assert.NoError(t, NewConfigCommand(Delete, testServerId).Exec())
assert.NoError(t, NewConfigCommand(Delete, testServerId).Run())

// Verify setting the option enables refreshable tokens.
outputConfig, err = configAndGetTestServer(t, inputDetails, false, false)
assert.NoError(t, err)
assert.Equal(t, coreutils.TokenRefreshDefaultInterval, outputConfig.ArtifactoryTokenRefreshInterval, "expected refreshable token to be enabled")
assert.NoError(t, NewConfigCommand(Delete, testServerId).Exec())
assert.NoError(t, NewConfigCommand(Delete, testServerId).Run())
}

func TestMakeDefaultOption(t *testing.T) {
Expand Down Expand Up @@ -219,7 +219,7 @@ func configAndAssertDefault(t *testing.T, inputDetails *config.ServerDetails, ma
}

func deleteServer(t *testing.T, serverId string) {
assert.NoError(t, NewConfigCommand(Delete, serverId).Exec())
assert.NoError(t, NewConfigCommand(Delete, serverId).Run())
}

type unsafeUrlTest struct {
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestAssertUrlsSafe(t *testing.T) {
// Test interactive - should fail with an error
configCmd := NewConfigCommand(AddOrEdit, testServerId).SetDetails(inputDetails).SetInteractive(true)
configCmd.disablePrompts = true
err := configCmd.Exec()
err := configCmd.Run()
if testCase.isSafe {
assert.NoError(t, err)
} else {
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestKeyDecryptionError(t *testing.T) {
// Configure server with JFROG_CLI_ENCRYPTION_KEY set
configCmd := NewConfigCommand(AddOrEdit, testServerId).SetDetails(inputDetails).SetUseBasicAuthOnly(true).SetInteractive(false)
configCmd.disablePrompts = true
assert.NoError(t, configCmd.Exec())
assert.NoError(t, configCmd.Run())

// Get the server details when JFROG_CLI_ENCRYPTION_KEY is not set and expect an error
assert.NoError(t, os.Unsetenv(coreutils.EncryptionKey))
Expand Down Expand Up @@ -363,6 +363,12 @@ func TestCommandName(t *testing.T) {
assert.NoError(t, err)
}

func TestConfigCommand_ExecAndReportUsage(t *testing.T) {
cc := NewConfigCommand(AddOrEdit, testServerId)
err := cc.ExecAndReportUsage()
assert.NoError(t, err)
}

func testExportImport(t *testing.T, inputDetails *config.ServerDetails) {
configToken, err := config.Export(inputDetails)
assert.NoError(t, err)
Expand All @@ -375,7 +381,7 @@ func configAndTest(t *testing.T, inputDetails *config.ServerDetails, interactive
outputConfig, err := configAndGetTestServer(t, inputDetails, true, interactive)
assert.NoError(t, err)
assert.Equal(t, configStructToString(t, inputDetails), configStructToString(t, outputConfig), "unexpected configuration was saved to file")
assert.NoError(t, NewConfigCommand(Delete, testServerId).Exec())
assert.NoError(t, NewConfigCommand(Delete, testServerId).Run())
testExportImport(t, inputDetails)
}

Expand All @@ -392,7 +398,7 @@ func doConfig(t *testing.T, serverId string, inputDetails *config.ServerDetails,
configCmd := NewConfigCommand(AddOrEdit, serverId).SetDetails(inputDetails).SetUseBasicAuthOnly(basicAuthOnly).
SetInteractive(interactive).SetMakeDefault(makeDefault)
configCmd.disablePrompts = true
assert.NoError(t, configCmd.Exec())
assert.NoError(t, configCmd.Run())
}

func configStructToString(t *testing.T, artConfig *config.ServerDetails) string {
Expand Down

0 comments on commit ec983f1

Please sign in to comment.