Repository: apple/pkl
File: pkl-core/src/test/kotlin/org/pkl/core/LanguageSnippetTestsEngine.kt (Kotlin)

Code before:
  override val excludedTests: List<Regex> = listOf(Regex(".*/native/.*"))

  override val inputDir: Path = snippetsDir.resolve("input")

  override val isInputFile: (Path) -> Boolean = { it.isRegularFile() }

  protected val cacheDir: Path by lazy {
    rootProjectDir.resolve("pkl-core/build/packages-cache")
      .also { PackageServer.populateCacheDir(it) }
  }

  protected tailrec fun Path.getProjectDir(): Path? =
    if (Files.exists(this.resolve("PklProject"))) this
    else parent?.getProjectDir()

  override fun expectedOutputFileFor(inputFile: Path): Path {
    val relativePath = inputDir.relativize(inputFile).toString()
    val stdoutPath =
      if (relativePath.matches(hiddenExtensionRegex)) relativePath.dropLast(4)
      else relativePath.dropLast(3) + "pcf"
    return expectedOutputDir.resolve(stdoutPath)
  }

  protected fun String.stripFilePaths() = replace(snippetsDir.toString(), "/\$snippetsDir")

  protected fun String.stripLineNumbers() = replace(lineNumberRegex) { result ->
    // replace line number with equivalent number of 'x' characters to keep formatting intact
    (result.groups[1]!!.value) + "x".repeat(result.groups[3]!!.value.length) + " |"
  }

  protected fun String.stripWebsite() = replace(Release.current().documentation().homepage(), "https://\$pklWebsite/")

  // can't think of a better solution right now
  protected fun String.stripVersionCheckErrorMessage() =
    replace("Pkl version is ${Release.current().version()}", "Pkl version is xxx")
}

class LanguageSnippetTestsEngine : AbstractLanguageSnippetTestsEngine() {
  private fun evaluatorBuilder(): EvaluatorBuilder {
    return EvaluatorBuilder.preconfigured()
      .setLogger(Loggers.stdErr())
      .setStackFrameTransformer(StackFrameTransformers.empty)
      // used by some tests
      .setEnvironmentVariables(
        mapOf(
          "NAME1" to "value1",
          "NAME2" to "value2",
          "/foo/bar" to "foobar",
          "foo bar" to "foo bar",
          "file:///foo/bar" to "file:///foo/bar"
        )

Review comment (performance):
Apparently the "Quality of live improvement" hack in `AbstractPackageResolver.verifyPackageMetadataBytes` wasn't actually needed before this PR replaced the pre-seeded package cache with a real `PackageServer`.
Now the above is needed to successfully run snippet tests in the IDE, which doesn't set the `org.pkl.testMode` system property.

There must be a better solution.
An ordinary class such as `AbstractPackageResolver` shouldn't query a system property.

Code after:

  protected val packageServer: PackageServer = PackageServer()
  
  override val includedTests: List<Regex> = listOf(Regex(".*$selection\\.pkl"))

  override val excludedTests: List<Regex> = listOf(Regex(".*/native/.*"))

  override val inputDir: Path = snippetsDir.resolve("input")

  override val isInputFile: (Path) -> Boolean = { it.isRegularFile() }

  protected tailrec fun Path.getProjectDir(): Path? =
    if (Files.exists(this.resolve("PklProject"))) this
    else parent?.getProjectDir()

  override fun expectedOutputFileFor(inputFile: Path): Path {
    val relativePath = inputDir.relativize(inputFile).toString()
    val stdoutPath =
      if (relativePath.matches(hiddenExtensionRegex)) relativePath.dropLast(4)
      else relativePath.dropLast(3) + "pcf"
    return expectedOutputDir.resolve(stdoutPath)
  }

  override fun beforeAll() {
    // disable SHA verification for packages
    IoUtils.setTestMode()
  }
  
  override fun afterAll() {
    packageServer.close()
  }
  
  protected fun String.stripFilePaths() = replace(snippetsDir.toString(), "/\$snippetsDir")

  protected fun String.stripLineNumbers() = replace(lineNumberRegex) { result ->
    // replace line number with equivalent number of 'x' characters to keep formatting intact
    (result.groups[1]!!.value) + "x".repeat(result.groups[3]!!.value.length) + " |"
  }

  protected fun String.stripWebsite() = replace(Release.current().documentation().homepage(), "https://\$pklWebsite/")

  // can't think of a better solution right now
  protected fun String.stripVersionCheckErrorMessage() =
    replace("Pkl version is ${Release.current().version()}", "Pkl version is xxx")
}

class LanguageSnippetTestsEngine : AbstractLanguageSnippetTestsEngine() {
  private fun evaluatorBuilder(): EvaluatorBuilder {
    return EvaluatorBuilder.preconfigured()
      .setLogger(Loggers.stdErr())
      .setStackFrameTransformer(StackFrameTransformers.empty)