Skip to content

Instantly share code, notes, and snippets.

@felixscheinost felixscheinost/SpecBase.groovy Secret

Created Jul 10, 2019
Embed
What would you like to do?
Groovy Test Case Base
package org.jetbrains.plugins.groovy
import com.intellij.jarRepository.JarRepositoryManager
import com.intellij.jarRepository.RemoteRepositoryDescription
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ContentEntry
import com.intellij.openapi.roots.DependencyScope
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.openapi.roots.libraries.Library
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.annotations.NotNull
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties
abstract class SpecBase extends LightCodeInsightFixtureTestCase {
@Override
protected LightProjectDescriptor getProjectDescriptor() {
new DefaultLightProjectDescriptor() {
@Override
void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
super.configureModule(module, model, contentEntry)
// Repositories
List<RemoteRepositoryDescription> repositories = [
RemoteRepositoryDescription.MAVEN_CENTRAL
]
// Libraries
List<String> libraries = [
"org.codehaus.groovy:groovy:2.4.15",
]
libraries.each { String librarySource ->
// Setup in project
Library library = model.moduleLibraryTable.modifiableModel.createLibrary(librarySource)
// Download jar
def libraryProperties = new RepositoryLibraryProperties(librarySource, true)
def roots = JarRepositoryManager.loadDependenciesModal(project, libraryProperties, false, false, null, repositories)
assert !roots.isEmpty()
// Include jar
roots.each {
library.modifiableModel.addRoot(it.file, it.type)
}
library.modifiableModel.commit()
model.moduleLibraryTable.modifiableModel.commit()
model.findLibraryOrderEntry(library).scope = DependencyScope.COMPILE
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.