<commit_before>package scala.tools.nsc.classpath

import java.io.ByteArrayOutputStream
import java.nio.file.{FileSystems, Files, Path}
import java.util.jar.Attributes
import java.util.jar.Attributes.Name
import org.junit.{Assert, Test}

import scala.tools.nsc.{CloseableRegistry, Global, Settings}
import scala.tools.testkit.BytecodeTesting
import scala.util.Properties

class MultiReleaseJarTest extends BytecodeTesting {
  import compiler._
  @Test
  def mrJar(): Unit = {
    if (!Properties.isJavaAtLeast("9")) { println("skipping mrJar() on old JDK"); return} // TODO test that the compiler warns that --release is unsupported.

    val temp1 = Files.createTempFile("mr-jar-test-", ".jar")

    // TODO test fails if both Global runs look at the same JAR on disk. Caching problem in our classpath implementation?
    // val temp2 = temp1
    val temp2 = Files.createTempFile("mr-jar-test-", ".jar")
    val cleanup = new CloseableRegistry

    try {
      def code(newApi: String) = s"package p1; abstract class Versioned { def oldApi: Int; $newApi }"

      val oldC = compileToBytes(code("")).head._2
      val newC = compileToBytes(code("def newApi: Int")).head._2
      List(temp1, temp2).foreach(temp => createZip(temp, List(
        "/p1/Versioned.class" -> oldC,
        "/META-INF/versions/9/p1/Versioned.class" -> newC,
        "/META-INF/MANIFEST.MF" -> createManifest)
      ))

      def declsOfC(jarPath: Path, release: String) = {
        val settings = new Settings()
        settings.usejavacp.value = true
        settings.classpath.value = jarPath.toAbsolutePath.toString
        val g = new Global(settings)
        cleanup.registerCloseable(g)
        settings.release.value = release
        new g.Run
        val decls = g.rootMirror.staticClass("p1.Versioned").info.decls.filterNot(_.isConstructor).map(_.name.toString).toList.sorted
        decls
      }

      Assert.assertEquals(List("newApi", "oldApi"), declsOfC(temp1, "9"))
      Assert.assertEquals(List("oldApi"), declsOfC(temp2, "8"))
    } finally {
      cleanup.close()
      List(temp1, temp2).foreach(Files.deleteIfExists)
    }
  }

  @Test
  def ctSymTest(): Unit = {
    if (!Properties.isJavaAtLeast("9")) { println("skipping mrJar() on old JDK"); return} // TODO test that the compiler warns that --release is unsupported.
    val cleanup = new CloseableRegistry

    def lookup(className: String, release: String): Boolean = {
      val settings = new Settings()
      settings.usejavacp.value = true
      val g = new Global(settings)
      cleanup.registerCloseable(g)
      import g._
      settings.release.value = release
      new Run
      rootMirror.getClassIfDefined(className) != NoSymbol
    }<commit_msg>make MultiReleaseJarTest pass on Windows<commit_after>package scala.tools.nsc.classpath

import java.io.ByteArrayOutputStream
import java.nio.file.{FileSystems, Files, Path}
import java.util.jar.Attributes
import java.util.jar.Attributes.Name
import org.junit.{Assert, Test}

import scala.tools.nsc.{CloseableRegistry, Global, Settings}
import scala.tools.testkit.{BytecodeTesting, ForDeletion}
import scala.util.{Properties, Using}

class MultiReleaseJarTest extends BytecodeTesting {
  import compiler._
  @Test
  def mrJar(): Unit = {
    if (!Properties.isJavaAtLeast("9")) return // TODO test that the compiler warns that --release is unsupported.

    // TODO test fails if both Global runs look at the same JAR on disk. Caching problem in our classpath implementation? So use two JARs.
    def makeTemp() = Files.createTempFile("mr-jar-test-", ".jar")
    Using.resources(ForDeletion(makeTemp()), ForDeletion(makeTemp())) { (temp1, temp2) =>

      def code(newApi: String) = s"package p1; abstract class Versioned { def oldApi: Int; $newApi }"

      val oldC = compileToBytes(code("")).head._2
      val newC = compileToBytes(code("def newApi: Int")).head._2
      List(temp1.path, temp2.path).foreach(temp => createZip(temp, List(
        "/p1/Versioned.class" -> oldC,
        "/META-INF/versions/9/p1/Versioned.class" -> newC,
        "/META-INF/MANIFEST.MF" -> createManifest)
      ))

      def declsOfC(jarPath: Path, release: String) = {
        val settings = new Settings()
        settings.usejavacp.value = true
        settings.classpath.value = jarPath.toAbsolutePath.toString
        val g = new Global(settings)
        settings.release.value = release
        new g.Run
        val decls = g.rootMirror.staticClass("p1.Versioned").info.decls.filterNot(_.isConstructor).map(_.name.toString).toList.sorted
        g.close()
        decls
      }

      Assert.assertEquals(List("newApi", "oldApi"), declsOfC(temp1.path, "9"))
      Assert.assertEquals(List("oldApi"), declsOfC(temp2.path, "8"))
    }
  }

  @Test
  def ctSymTest(): Unit = {
    if (!Properties.isJavaAtLeast("9")) return // TODO test that the compiler warns that --release is unsupported.
    val cleanup = new CloseableRegistry

    def lookup(className: String, release: String): Boolean = {
      val settings = new Settings()
      settings.usejavacp.value = true
      val g = new Global(settings)
      cleanup.registerCloseable(g)
      import g._
      settings.release.value = release
      new Run
      rootMirror.getClassIfDefined(className) != NoSymbol
    }
{'fraction_non_alphanumeric': 0.12106858658582796, 'fraction_numerical': 0.0071996968548692685, 'mean_word_length': 3.2538275584206287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078446', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1611, 'n_words': 398}