diff --git a/artifactory_test.go b/artifactory_test.go index c4b52b484..f4ae45b01 100644 --- a/artifactory_test.go +++ b/artifactory_test.go @@ -5543,27 +5543,47 @@ func TestArtifactoryCurl(t *testing.T) { _, err := createServerConfigAndReturnPassphrase(t) defer deleteServerConfig(t) assert.NoError(t, err) - // Check curl command with config default server - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version") - assert.NoError(t, err) - // Check curl command with '--server-id' flag - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId) - assert.NoError(t, err) - // Check curl command with invalid server id - should get an error. - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_"+tests.ServerId) - assert.Error(t, err) - // Set JFROG_CLI_SERVER_ID and check curl command - _ = os.Setenv(coreutils.ServerID, tests.ServerId) + // Store the original environment variable value (if exists) to restore later. + originalServerID, hasOriginal := os.LookupEnv(coreutils.ServerID) defer func() { - _ = os.Unsetenv(coreutils.ServerID) - }() // Cleanup after test - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version") - assert.NoError(t, err) + if hasOriginal { + _ = os.Setenv(coreutils.ServerID, originalServerID) + } else { + _ = os.Unsetenv(coreutils.ServerID) + } + }() - // Set JFROG_CLI_SERVER_ID with --server-id flag (should use the flag) - err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId) - assert.NoError(t, err) + testRuns := []struct { + name string + envVar string + envValue string + args []string + expectError bool + }{ + {"defaultConfig", "", "", []string{"curl", "-XGET", "/api/system/version"}, false}, + {"serverIdFlag", "", "", []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false}, + {"invalidServerId", "", "", []string{"curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_" + tests.ServerId}, true}, + {"envVarSet", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version"}, false}, + {"envVarWithFlag", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false}, + {"priorityFlagOverEnv", coreutils.ServerID, "wrong_server_id", []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false}, + {"priorityEnvOverDefault", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version"}, false}, + } + + for _, test := range testRuns { + t.Run(test.name, func(t *testing.T) { + if test.envVar != "" { + _ = os.Setenv(test.envVar, test.envValue) + } + + err := artifactoryCli.WithoutCredentials().Exec(test.args...) + if test.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } cleanArtifactoryTest() }