git tests: reset credential.helper for all tests which need it

This commit is contained in:
Kirill Likhodedov
2017-11-18 15:07:11 +03:00
parent 8309d42bb5
commit 71dc6ad33c
3 changed files with 55 additions and 46 deletions
@@ -16,17 +16,12 @@
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.config.GitVersion
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
@@ -35,7 +30,6 @@ class GitRemoteTest : GitPlatformTest() {
private lateinit var authenticator : TestAuthenticator
private lateinit var authTestService : GitHttpAuthTestService
private lateinit var credentialHelpers: Map<ConfigScope, String>
private val projectName = "projectA"
@@ -45,18 +39,19 @@ class GitRemoteTest : GitPlatformTest() {
authenticator = TestAuthenticator()
authTestService = service<GitHttpAuthService>() as GitHttpAuthTestService
authTestService.register(authenticator)
credentialHelpers = readAndResetCredentialHelpers()
}
override fun tearDown() {
RunAll()
.append(ThrowableRunnable { authTestService.cleanup() })
.append(ThrowableRunnable { restoreCredentialHelpers() })
.append(ThrowableRunnable { super.tearDown() })
.run()
try {
authTestService.cleanup()
}
finally {
super.tearDown()
}
}
override fun hasRemoteGitOperation() = true
fun `test clone from http with username`() {
val cloneWaiter = cloneOnPooledThread(makeUrl("gituser"))
@@ -112,24 +107,6 @@ class GitRemoteTest : GitPlatformTest() {
return cloneWaiter
}
private fun readAndResetCredentialHelpers(): Map<ConfigScope, String> {
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())
@@ -212,11 +189,4 @@ class GitRemoteTest : GitPlatformTest() {
return usernameAsked
}
}
private enum class ConfigScope {
SYSTEM,
GLOBAL;
fun param() = "--${name.toLowerCase()}"
}
}
@@ -22,7 +22,9 @@ import com.intellij.openapi.vcs.Executor
import com.intellij.openapi.vcs.Executor.cd
import com.intellij.openapi.vcs.VcsConfiguration
import com.intellij.openapi.vcs.VcsShowConfirmationOption
import com.intellij.testFramework.RunAll
import com.intellij.testFramework.vcs.AbstractVcsTestCase
import com.intellij.util.ThrowableRunnable
import com.intellij.vcs.log.VcsFullCommitDetails
import com.intellij.vcs.log.impl.VcsLogUtil
import com.intellij.vcs.test.VcsPlatformTest
@@ -36,6 +38,8 @@ import git4idea.config.GitVcsSettings
import git4idea.log.GitLogProvider
import git4idea.repo.GitRepository
import git4idea.repo.GitRepositoryManager
import git4idea.test.GitPlatformTest.ConfigScope.GLOBAL
import git4idea.test.GitPlatformTest.ConfigScope.SYSTEM
import java.io.File
abstract class GitPlatformTest : VcsPlatformTest() {
@@ -48,6 +52,8 @@ abstract class GitPlatformTest : VcsPlatformTest() {
protected lateinit var vcsHelper: MockVcsHelper
protected lateinit var logProvider: GitLogProvider
private lateinit var credentialHelpers: Map<ConfigScope, String>
@Throws(Exception::class)
override fun setUp() {
super.setUp()
@@ -69,18 +75,19 @@ abstract class GitPlatformTest : VcsPlatformTest() {
assumeSupportedGitVersion(vcs)
addSilently()
removeSilently()
credentialHelpers = if (hasRemoteGitOperation()) readAndResetCredentialHelpers() else emptyMap()
}
@Throws(Exception::class)
override fun tearDown() {
try {
if (wasInit { dialogManager }) dialogManager.cleanup()
if (wasInit { git }) git.reset()
if (wasInit { settings }) settings.appSettings.setPathToGit(null)
}
finally {
super.tearDown()
}
RunAll()
.append(ThrowableRunnable { restoreCredentialHelpers() })
.append(ThrowableRunnable { if (wasInit { dialogManager }) dialogManager.cleanup() })
.append(ThrowableRunnable { if (wasInit { git }) git.reset() })
.append(ThrowableRunnable { if (wasInit { settings }) settings.appSettings.setPathToGit(null) })
.append(ThrowableRunnable { super.tearDown() })
.run()
}
override fun getDebugLogCategories(): Collection<String> {
@@ -89,6 +96,8 @@ abstract class GitPlatformTest : VcsPlatformTest() {
"#output." + GitHandler::class.java.name))
}
protected open fun hasRemoteGitOperation() = false
protected open fun createRepository(rootDir: String): GitRepository {
return createRepository(project, rootDir)
}
@@ -160,6 +169,24 @@ abstract class GitPlatformTest : VcsPlatformTest() {
hookFile.setExecutable(true, false)
}
private fun readAndResetCredentialHelpers(): Map<ConfigScope, String> {
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)
}
}
protected fun readDetails(hashes: List<String>): List<VcsFullCommitDetails> = VcsLogUtil.getDetails(logProvider, projectRoot, hashes)
protected fun readDetails(hash: String) = readDetails(listOf(hash)).first()
@@ -182,4 +209,11 @@ abstract class GitPlatformTest : VcsPlatformTest() {
protected data class ReposTrinity(val projectRepo: GitRepository, val parent: File, val bro: File)
private enum class ConfigScope {
SYSTEM,
GLOBAL;
fun param() = "--${name.toLowerCase()}"
}
}
@@ -75,6 +75,11 @@ public abstract class GithubTest extends GitPlatformTest {
VfsTestUtil.createDir (projectRoot, "folder/empty_folder");
}
@Override
protected boolean hasRemoteGitOperation() {
return true;
}
protected void checkNotification(@NotNull NotificationType type, @Nullable String title, @Nullable String content) {
Notification actualNotification = vcsNotifier.getLastNotification();
assertNotNull("No notification was shown", actualNotification);