Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass rootclasspaths to KtTestCompiler #3066

Closed
tyvsmith opened this issue Sep 11, 2020 · 4 comments
Closed

Add ability to pass rootclasspaths to KtTestCompiler #3066

tyvsmith opened this issue Sep 11, 2020 · 4 comments
Milestone

Comments

@tyvsmith
Copy link
Contributor

tyvsmith commented Sep 11, 2020

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)
@schalkms
Copy link
Member

Thanks for providing a good suggestion!
I think it’s fine to merge this function upwards, since only test environment code gets added.
Let’s wait for @arturbosch feedback.

@schalkms
Copy link
Member

Btw sorry for the late reply. This good idea got missed somehow.

@arturbosch
Copy link
Member

arturbosch commented Sep 21, 2020

@tyvsmith yes please propose a PR. Thanks @schalkms .

@schalkms
Copy link
Member

Closed by e52f6f2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants