diff --git a/plugins/git4idea/tests/git4idea/remote/GitRemoteTest.kt b/plugins/git4idea/tests/git4idea/remote/GitRemoteTest.kt index f8b3811183cc..1906cf76a362 100644 --- a/plugins/git4idea/tests/git4idea/remote/GitRemoteTest.kt +++ b/plugins/git4idea/tests/git4idea/remote/GitRemoteTest.kt @@ -16,11 +16,16 @@ package git4idea.remote import com.intellij.openapi.components.service +import com.intellij.testFramework.RunAll +import com.intellij.util.ThrowableRunnable import git4idea.checkout.GitCheckoutProvider import git4idea.commands.GitHttpAuthService import git4idea.commands.GitHttpAuthenticator +import git4idea.remote.GitRemoteTest.ConfigScope.GLOBAL +import git4idea.remote.GitRemoteTest.ConfigScope.SYSTEM import git4idea.test.GitHttpAuthTestService import git4idea.test.GitPlatformTest +import git4idea.test.git import java.io.File import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit @@ -29,6 +34,7 @@ class GitRemoteTest : GitPlatformTest() { private lateinit var authenticator : TestAuthenticator private lateinit var authTestService : GitHttpAuthTestService + private lateinit var credentialHelpers: Map private val projectName = "projectA" @@ -38,15 +44,16 @@ class GitRemoteTest : GitPlatformTest() { authenticator = TestAuthenticator() authTestService = service() as GitHttpAuthTestService authTestService.register(authenticator) + + credentialHelpers = readAndResetCredentialHelpers() } override fun tearDown() { - try{ - authTestService.cleanup() - } - finally { - super.tearDown() - } + RunAll() + .append(ThrowableRunnable { authTestService.cleanup() }) + .append(ThrowableRunnable { restoreCredentialHelpers() }) + .append(ThrowableRunnable { super.tearDown() }) + .run() } fun `test clone from http with username`() { @@ -97,6 +104,24 @@ class GitRemoteTest : GitPlatformTest() { return cloneWaiter } + private fun readAndResetCredentialHelpers(): Map { + val system = readAndResetCredentialHelper(SYSTEM) + val global = readAndResetCredentialHelper(GLOBAL) + return mapOf(SYSTEM to system, GLOBAL to global) + } + + private fun readAndResetCredentialHelper(scope: ConfigScope): String { + val value = git("config ${scope.param()} --get-all credential.helper", true) + git("config ${scope.param()} --unset-all credential.helper", true) + return value + } + + private fun restoreCredentialHelpers() { + credentialHelpers.forEach { scope, value -> + if (value.isNotBlank()) git("config ${scope.param()} credential.helper ${value}", true) + } + } + private fun assertCloneSuccessful(cloneCompleted: CountDownLatch) { assertTrue("Clone didn't complete during the reasonable period of time", cloneCompleted.await(30, TimeUnit.SECONDS)) assertTrue("Repository directory was not found", File(testRoot, projectName).exists()) @@ -179,4 +204,11 @@ class GitRemoteTest : GitPlatformTest() { return usernameAsked } } + + private enum class ConfigScope { + SYSTEM, + GLOBAL; + + fun param() = "--${name.toLowerCase()}" + } } \ No newline at end of file