Skip to content

Add ability to pass rootclasspaths to KtTestCompiler #3066

@tyvsmith

Description

@tyvsmith

Expected Behavior

KtTestCompiler can take additional optional paths for loading on the root classpath for testing scenarios.

Current Behavior

Currently, KtTestCompiler allows for a test environment without specifying additional existing classes that may be needed on the root classpath.

Context

We have this change internally used for our testing environment when dealing with existing files both on the main classpath and in testing (we share EP and detekt stubs in some cases). I can upstream this if it'll be widely valuable.

Proposed Change:

internal fun createEnvironment(additionalPaths: List<File> = listOf()): KotlinCoreEnvironmentWrapper {
  val configuration = CompilerConfiguration()
  configuration.put(CommonConfigurationKeys.MODULE_NAME, "test_module")
  configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)

  // Get the runtime location of stdlib jar and pass to the compiler so it's available to generate
  // the
  // BindingContext for rules under test.
  val path = File(CharRange::class.java.protectionDomain.codeSource.location.path)
  configuration.addJvmClasspathRoot(path)
  
  configuration.addJvmClasspathRoots(additionalPaths)

  val parentDisposable = Disposer.newDisposable()
  val kotlinCoreEnvironment =
      KotlinCoreEnvironment.createForTests(
          parentDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
  return KotlinCoreEnvironmentWrapper(kotlinCoreEnvironment, parentDisposable)
}

Example usage:

    val EXTRA_CLASSPATH_FILES =
        listOf(File(TestBannedMethods::class.java.protectionDomain.codeSource.location.path))

    val ENVIRONMENT = createEnvironment(EXTRA_CLASSPATH_FILES)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions