From c5d74d5c29eeeca516fef4a37cef20f72e05eec9 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sun, 6 Mar 2016 16:31:14 +0300 Subject: [PATCH] Extract VcsPlatformTest --- .idea/modules.xml | 1 + .../com/intellij/vcs/test/VcsPlatformTest.kt | 137 ++++++++++++++++++ platform/vcs-tests/vcs-tests.iml | 17 +++ plugins/git4idea/git4idea.iml | 1 + .../git4idea/push/GitPushOperationBaseTest.kt | 4 +- .../git4idea/rebase/GitRebaseBaseTest.kt | 2 +- .../tests/git4idea/test/GitPlatformTest.kt | 116 +-------------- plugins/github/github.iml | 4 +- 8 files changed, 168 insertions(+), 114 deletions(-) create mode 100644 platform/vcs-tests/testSrc/com/intellij/vcs/test/VcsPlatformTest.kt create mode 100644 platform/vcs-tests/vcs-tests.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 98246cf2ab4c..8fedac9d8313 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -262,6 +262,7 @@ + diff --git a/platform/vcs-tests/testSrc/com/intellij/vcs/test/VcsPlatformTest.kt b/platform/vcs-tests/testSrc/com/intellij/vcs/test/VcsPlatformTest.kt new file mode 100644 index 000000000000..f69cd42cdf5c --- /dev/null +++ b/platform/vcs-tests/testSrc/com/intellij/vcs/test/VcsPlatformTest.kt @@ -0,0 +1,137 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.vcs.test + +import com.intellij.ide.highlighter.ProjectFileType +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.util.text.StringUtil +import com.intellij.openapi.vcs.changes.ChangeListManager +import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager +import com.intellij.openapi.vfs.VfsUtil +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.testFramework.PlatformTestCase +import com.intellij.testFramework.TestLoggerFactory +import com.intellij.testFramework.runInEdtAndWait +import com.intellij.util.ArrayUtil +import java.io.File +import java.util.* + +abstract class VcsPlatformTest : PlatformTestCase() { + + protected lateinit var myTestRoot: File + protected lateinit var myProjectRoot: VirtualFile + protected lateinit var myProjectPath: String + + private lateinit var myTestStartedIndicator: String + + @Throws(Exception::class) + override fun setUp() { + myTestRoot = File(FileUtil.getTempDirectory(), "testRoot") + PlatformTestCase.myFilesToDelete.add(myTestRoot) + checkTestRootIsEmpty(myTestRoot) + + runInEdtAndWait { super@VcsPlatformTest.setUp() } + + myTestStartedIndicator = enableDebugLogging() + + myProjectRoot = myProject.baseDir + myProjectPath = myProjectRoot.path + } + + @Throws(Exception::class) + override fun tearDown() { + try { + runInEdtAndWait { super@VcsPlatformTest.tearDown() } + } + finally { + if (myAssertionsInTestDetected) { + TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator) + } + } + } + + /** + * Returns log categories which will be switched to DEBUG level. + * Implementations must add theirs categories to the ones from super class, + * not to erase log categories from the super class. + * (e.g. by calling `super.getDebugLogCategories().plus(additionalCategories)`. + */ + protected open fun getDebugLogCategories(): Collection = emptyList() + + override fun getIprFile(): File { + val projectRoot = File(myTestRoot, "project") + return FileUtil.createTempFile(projectRoot, name + "_", ProjectFileType.DOT_DEFAULT_EXTENSION) + } + + override fun setUpModule() { + // we don't need a module in Git tests + } + + override fun isRunInEdt(): Boolean { + return false + } + + override fun getTestName(lowercaseFirstLetter: Boolean): String { + var name = super.getTestName(lowercaseFirstLetter) + name = StringUtil.shortenTextWithEllipsis(name.trim { it <= ' ' }.replace(" ", "_"), 12, 6, "_") + if (name.startsWith("_")) { + name = name.substring(1) + } + return name + } + + protected inline fun wasInit(f: () -> Unit): Boolean { + try { + f() + } + catch(e: UninitializedPropertyAccessException) { + return false + } + return true + } + + protected open fun refresh() { + VfsUtil.markDirtyAndRefresh(false, true, false, myProjectRoot) + } + + protected fun updateChangeListManager() { + val changeListManager = ChangeListManager.getInstance(myProject) + VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty() + changeListManager.ensureUpToDate(false) + } + + private fun checkTestRootIsEmpty(testRoot: File) { + val files = testRoot.listFiles() + if (files != null && files.size > 0) { + LOG.warn("Test root was not cleaned up during some previous test run. " + "testRoot: " + testRoot + + ", files: " + Arrays.toString(files)) + for (file in files) { + LOG.assertTrue(FileUtil.delete(file)) + } + } + } + + private fun enableDebugLogging(): String { + TestLoggerFactory.enableDebugLogging(myTestRootDisposable, *ArrayUtil.toStringArray(getDebugLogCategories())) + val testStartedIndicator = createTestStartedIndicator() + LOG.info(testStartedIndicator) + return testStartedIndicator + } + + private fun createTestStartedIndicator(): String { + return "Starting " + javaClass.name + "." + getTestName(false) + Math.random() + } +} \ No newline at end of file diff --git a/platform/vcs-tests/vcs-tests.iml b/platform/vcs-tests/vcs-tests.iml new file mode 100644 index 000000000000..c3a51726ee01 --- /dev/null +++ b/platform/vcs-tests/vcs-tests.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/git4idea/git4idea.iml b/plugins/git4idea/git4idea.iml index d1895970da57..d5a01b819cab 100644 --- a/plugins/git4idea/git4idea.iml +++ b/plugins/git4idea/git4idea.iml @@ -62,5 +62,6 @@ + \ No newline at end of file diff --git a/plugins/git4idea/tests/git4idea/push/GitPushOperationBaseTest.kt b/plugins/git4idea/tests/git4idea/push/GitPushOperationBaseTest.kt index bd0c0d244062..4f12817d5938 100644 --- a/plugins/git4idea/tests/git4idea/push/GitPushOperationBaseTest.kt +++ b/plugins/git4idea/tests/git4idea/push/GitPushOperationBaseTest.kt @@ -56,9 +56,7 @@ abstract class GitPushOperationBaseTest : GitPlatformTest() { myGitRepositoryManager.updateAllRepositories() } - override fun getDebugLogCategories(): Collection { - return listOf("#" + GitPushOperation::class.java.name) - } + override fun getDebugLogCategories() = super.getDebugLogCategories().plus("#" + GitPushOperation::class.java.name) protected fun setupRepositories(repoRoot: String, parentName: String, broName: String): Trinity { val parentRepo = createParentRepo(parentName) diff --git a/plugins/git4idea/tests/git4idea/rebase/GitRebaseBaseTest.kt b/plugins/git4idea/tests/git4idea/rebase/GitRebaseBaseTest.kt index 2108cd3f498e..8e2f02e184b8 100644 --- a/plugins/git4idea/tests/git4idea/rebase/GitRebaseBaseTest.kt +++ b/plugins/git4idea/tests/git4idea/rebase/GitRebaseBaseTest.kt @@ -41,7 +41,7 @@ abstract class GitRebaseBaseTest : GitPlatformTest() { override fun createRepository(rootDir: String) = GitTestUtil.createRepository(myProject, rootDir, false) - override fun getDebugLogCategories() = listOf("#git4idea.rebase") + override fun getDebugLogCategories() = super.getDebugLogCategories().plus("#git4idea.rebase") protected fun GitRepository.`diverge feature and master`() { build(this) { diff --git a/plugins/git4idea/tests/git4idea/test/GitPlatformTest.kt b/plugins/git4idea/tests/git4idea/test/GitPlatformTest.kt index da9e81f50954..5bfe6fa012ac 100644 --- a/plugins/git4idea/tests/git4idea/test/GitPlatformTest.kt +++ b/plugins/git4idea/tests/git4idea/test/GitPlatformTest.kt @@ -15,23 +15,13 @@ */ package git4idea.test -import com.intellij.ide.highlighter.ProjectFileType import com.intellij.notification.Notification import com.intellij.notification.NotificationType import com.intellij.openapi.components.ServiceManager -import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.vcs.* -import com.intellij.openapi.vcs.changes.ChangeListManager -import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager -import com.intellij.openapi.vfs.VfsUtil -import com.intellij.openapi.vfs.VirtualFile -import com.intellij.testFramework.PlatformTestCase -import com.intellij.testFramework.TestLoggerFactory -import com.intellij.testFramework.runInEdtAndWait import com.intellij.testFramework.vcs.AbstractVcsTestCase -import com.intellij.util.ArrayUtil +import com.intellij.vcs.test.VcsPlatformTest import git4idea.DialogManager import git4idea.GitPlatformFacade import git4idea.GitUtil @@ -42,14 +32,9 @@ import git4idea.config.GitVcsSettings import git4idea.repo.GitRepository import git4idea.repo.GitRepositoryManager import java.io.File -import java.util.* -abstract class GitPlatformTest : PlatformTestCase() { +abstract class GitPlatformTest : VcsPlatformTest() { - protected val LOG = Logger.getInstance(GitPlatformTest::class.java) - - protected lateinit var myProjectRoot: VirtualFile - protected lateinit var myProjectPath: String protected lateinit var myGitRepositoryManager: GitRepositoryManager protected lateinit var myGitSettings: GitVcsSettings protected lateinit var myPlatformFacade: GitPlatformFacade @@ -58,23 +43,9 @@ abstract class GitPlatformTest : PlatformTestCase() { protected lateinit var myDialogManager: TestDialogManager protected lateinit var myVcsNotifier: TestVcsNotifier - protected lateinit var myTestRoot: File - - private lateinit var myTestStartedIndicator: String - @Throws(Exception::class) override fun setUp() { - myTestRoot = File(FileUtil.getTempDirectory(), "testRoot") - PlatformTestCase.myFilesToDelete.add(myTestRoot) - - checkTestRootIsEmpty(myTestRoot) - - runInEdtAndWait { super@GitPlatformTest.setUp() } - - myTestStartedIndicator = enableDebugLogging() - - myProjectRoot = myProject.baseDir - myProjectPath = myProjectRoot.path + super.setUp() myGitSettings = GitVcsSettings.getInstance(myProject) myGitSettings.appSettings.pathToGit = GitExecutor.PathHolder.GIT_EXECUTABLE @@ -101,64 +72,14 @@ abstract class GitPlatformTest : PlatformTestCase() { myGit.reset() } finally { - try { - runInEdtAndWait { super@GitPlatformTest.tearDown() } - } - finally { - if (myAssertionsInTestDetected) { - TestLoggerFactory.dumpLogToStdout(myTestStartedIndicator) - } - } + super.tearDown() } } - override fun getIprFile(): File { - val projectRoot = File(myTestRoot, "project") - return FileUtil.createTempFile(projectRoot, name + "_", ProjectFileType.DOT_DEFAULT_EXTENSION) - } - - override fun setUpModule() { - // we don't need a module in Git tests - } - - override fun isRunInEdt(): Boolean { - return false - } - - override fun getTestName(lowercaseFirstLetter: Boolean): String { - var name = super.getTestName(lowercaseFirstLetter) - name = StringUtil.shortenTextWithEllipsis(name.trim { it <= ' ' }.replace(" ", "_"), 12, 6, "_") - if (name.startsWith("_")) { - name = name.substring(1) - } - return name - } - - inline fun wasInit(f: () -> Unit): Boolean { - try { - f() - } - catch(e: UninitializedPropertyAccessException) { - return false - } - return true - } - - private fun enableDebugLogging(): String { - val commonCategories = ArrayList(Arrays.asList("#" + Executor::class.java.name, - "#" + GitHandler::class.java.name, - GitHandler::class.java.name)) - commonCategories.addAll(getDebugLogCategories()) - TestLoggerFactory.enableDebugLogging(myTestRootDisposable, *ArrayUtil.toStringArray(commonCategories)) - val testStartedIndicator = createTestStartedIndicator() - LOG.info(testStartedIndicator) - return testStartedIndicator - } - - protected open fun getDebugLogCategories(): Collection = emptyList() - - private fun createTestStartedIndicator(): String { - return "Starting " + javaClass.name + "." + getTestName(false) + Math.random() + override fun getDebugLogCategories(): Collection { + return super.getDebugLogCategories().plus(listOf("#" + Executor::class.java.name, + "#" + GitHandler::class.java.name, + GitHandler::class.java.name)) } protected open fun createRepository(rootDir: String): GitRepository { @@ -177,20 +98,10 @@ abstract class GitPlatformTest : PlatformTestCase() { GitExecutor.git("remote add %s '%s'", targetName, "$myProjectRoot/$target") } - protected open fun refresh() { - VfsUtil.markDirtyAndRefresh(false, true, false, myProjectRoot) - } - protected fun doActionSilently(op: VcsConfiguration.StandardConfirmation) { AbstractVcsTestCase.setStandardConfirmation(myProject, GitVcs.NAME, op, VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY) } - protected fun updateChangeListManager() { - val changeListManager = ChangeListManager.getInstance(myProject) - VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty() - changeListManager.ensureUpToDate(false) - } - protected fun addSilently() { doActionSilently(VcsConfiguration.StandardConfirmation.ADD) } @@ -199,17 +110,6 @@ abstract class GitPlatformTest : PlatformTestCase() { doActionSilently(VcsConfiguration.StandardConfirmation.REMOVE) } - private fun checkTestRootIsEmpty(testRoot: File) { - val files = testRoot.listFiles() - if (files != null && files.size > 0) { - LOG.warn("Test root was not cleaned up during some previous test run. " + "testRoot: " + testRoot + - ", files: " + Arrays.toString(files)) - for (file in files) { - LOG.assertTrue(FileUtil.delete(file)) - } - } - } - protected fun installHook(gitDir: File, hookName: String, hookContent: String) { val hookFile = File(gitDir, "hooks/$hookName") FileUtil.writeToFile(hookFile, hookContent) diff --git a/plugins/github/github.iml b/plugins/github/github.iml index 1b14d6b1e8a9..48eb9dc897ac 100644 --- a/plugins/github/github.iml +++ b/plugins/github/github.iml @@ -26,6 +26,6 @@ + - - + \ No newline at end of file