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

Move JDK shims to munit.internal #441

Merged
merged 7 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .jvmopts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
-Xmx2G
-XX:ReservedCodeCacheSize=1024m
-XX:+TieredCompilation
-XX:+CMSClassUnloadingEnabled
-Dfile.encoding=UTF-8
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ lazy val tests = crossProject(JSPlatform, JVMPlatform, NativePlatform)
buildInfoPackage := "munit",
buildInfoKeys := Seq[BuildInfoKey](
"sourceDirectory" ->
(ThisBuild / baseDirectory).value / "tests" / "shared" / "src" / "main",
((ThisBuild / baseDirectory).value / "tests" / "shared" / "src" / "main").getAbsolutePath.toString,
scalaVersion
),
publish / skip := true
Expand Down
28 changes: 0 additions & 28 deletions munit/js/src/main/scala/java/nio/file/Path.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package java.lang.reflect
package munit.internal

class InvocationTargetException extends java.lang.RuntimeException
4 changes: 4 additions & 0 deletions munit/js/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ object PlatformCompat {
private var myClassLoader: ClassLoader = _
def setThisClassLoader(loader: ClassLoader): Unit = myClassLoader = loader
def getThisClassLoader: ClassLoader = myClassLoader

type InvocationTargetException = munit.internal.InvocationTargetException
type UndeclaredThrowableException =
munit.internal.UndeclaredThrowableException
}
6 changes: 0 additions & 6 deletions munit/js/src/main/scala/munit/internal/PlatformPathIO.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package java.lang.reflect
package munit.internal

class UndeclaredThrowableException extends java.lang.RuntimeException
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package java.io
package munit.internal.io

import java.net.URI
import java.nio.file.Path
import munit.internal.JSIO
import munit.internal.NodeNIOPath

// obtained implementation by experimentation on the JDK.
class File(path: String) {
Expand All @@ -19,8 +16,8 @@ class File(path: String) {
uri.getPath
}
)
def toPath: Path =
NodeNIOPath(path)
def toPath: MunitPath =
MunitPath(path)
def toURI: URI = {
val file = getAbsoluteFile.toString
val uripath =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package java.nio.file
package munit.internal.io

import scala.scalajs.js
import java.{util => ju}
import java.nio.charset.StandardCharsets

import munit.internal.JSIO

import scala.collection.JavaConverters._

object Files {
def readAllLines(path: Path): ju.List[String] = {
def readAllLines(path: MunitPath): ju.List[String] = {
val bytes = readAllBytes(path)
val text = new String(bytes, StandardCharsets.UTF_8)
text.linesIterator.toSeq.asJava
}
def readAllBytes(path: Path): Array[Byte] = {
def readAllBytes(path: MunitPath): Array[Byte] = {
val jsArray = JSIO.fs match {
case Some(fs) =>
fs.readFileSync(path.toString).asInstanceOf[js.Array[Int]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package munit.internal
package munit.internal.io

import scala.scalajs.js
import scala.util.Try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,63 @@
package munit.internal
package munit.internal.io

import java.io.File
import java.net.URI
import java.nio.file.WatchEvent.{Kind, Modifier}
import java.nio.file._
import java.util

import scala.collection.JavaConverters._

// Rough implementation of java.nio.Path, should work similarly for the happy
// path but has undefined behavior for error handling.
case class NodeNIOPath(filename: String) extends Path {
case class MunitPath(filename: String) {
private[this] val escapedSeparator =
java.util.regex.Pattern.quote(File.separator)
def getFileSystem(): FileSystem = ???
def toRealPath(x: LinkOption*): Path = ???
def register(x: WatchService, y: Array[Kind[_]], z: Modifier*): WatchKey = ???
def register(x: WatchService, y: Kind[_]*): WatchKey = ???
def compareTo(x: Path): Int = ???

private def adjustIndex(idx: Int): Int =
if (isAbsolute) idx + 1 else idx
override def subpath(beginIndex: Int, endIndex: Int): Path =
NodeNIOPath(
def subpath(beginIndex: Int, endIndex: Int): MunitPath =
MunitPath(
filename
.split(escapedSeparator)
.slice(adjustIndex(beginIndex), adjustIndex(endIndex))
.mkString
)
override def toFile: File =
def toFile: File =
new File(filename)
override def isAbsolute: Boolean = JSIO.path match {
def isAbsolute: Boolean = JSIO.path match {
case Some(path) => path.isAbsolute(filename).asInstanceOf[Boolean]
case None => filename.startsWith(File.separator)
}
override def getName(index: Int): Path =
NodeNIOPath(
def getName(index: Int): MunitPath =
MunitPath(
filename
.split(escapedSeparator)
.lift(adjustIndex(index))
.getOrElse(throw new IllegalArgumentException)
)
override def getParent: Path =
def getParent: MunitPath =
JSIO.path match {
case Some(path) =>
NodeNIOPath(path.dirname(filename).asInstanceOf[String])
MunitPath(path.dirname(filename).asInstanceOf[String])
case None =>
throw new UnsupportedOperationException(
"Path.getParent() is only supported in Node.js"
)
}

override def toAbsolutePath: Path =
def toAbsolutePath: MunitPath =
if (isAbsolute) this
else NodeNIOPath.workingDirectory.resolve(this)
override def relativize(other: Path): Path =
else MunitPath.workingDirectory.resolve(this)
def relativize(other: MunitPath): MunitPath =
JSIO.path match {
case Some(path) =>
NodeNIOPath(
MunitPath(
path.relative(filename, other.toString()).asInstanceOf[String]
)
case None =>
throw new UnsupportedOperationException(
"Path.relativize() is only supported in Node.js"
)
}
override def getNameCount: Int = {
def getNameCount: Int = {
val strippeddrive =
if ((filename.length > 1) && (filename(1) == ':')) filename.substring(2)
else filename
Expand All @@ -73,44 +66,44 @@ case class NodeNIOPath(filename: String) extends Path {
if (remaining.isEmpty) first.length
else remaining.length
}
override def toUri: URI = toFile.toURI
override def getFileName: Path =
def toUri: URI = toFile.toURI
def getFileName(): MunitPath =
JSIO.path match {
case Some(path) =>
NodeNIOPath(path.basename(filename).asInstanceOf[String])
MunitPath(path.basename(filename).asInstanceOf[String])
case None =>
throw new UnsupportedOperationException(
"Path.getFileName() is only supported in Node.js"
)
}
override def getRoot: Path =
def getRoot: MunitPath =
if (!isAbsolute) null
else NodeNIOPath(File.separator)
override def normalize(): Path =
else MunitPath(File.separator)
def normalize(): MunitPath =
JSIO.path match {
case Some(path) =>
NodeNIOPath(path.normalize(filename).asInstanceOf[String])
MunitPath(path.normalize(filename).asInstanceOf[String])
case None =>
throw new UnsupportedOperationException(
"Path.normalize() is only supported in Node.js"
)
}
override def endsWith(other: Path): Boolean =
def endsWith(other: MunitPath): Boolean =
endsWith(other.toString)
override def endsWith(other: String): Boolean =
def endsWith(other: String): Boolean =
paths(filename).endsWith(paths(other))
// JSPath.resolve(relpath, relpath) produces an absolute path from cwd.
// This method turns the generated absolute path back into a relative path.
private def adjustResolvedPath(resolved: Path): Path =
private def adjustResolvedPath(resolved: MunitPath): MunitPath =
if (isAbsolute) resolved
else NodeNIOPath.workingDirectory.relativize(resolved)
override def resolveSibling(other: Path): Path =
else MunitPath.workingDirectory.relativize(resolved)
def resolveSibling(other: MunitPath): MunitPath =
resolveSibling(other.toString)
override def resolveSibling(other: String): Path =
def resolveSibling(other: String): MunitPath =
JSIO.path match {
case Some(path) =>
adjustResolvedPath(
NodeNIOPath(
MunitPath(
path
.resolve(path.dirname(filename).asInstanceOf[String], other)
.asInstanceOf[String]
Expand All @@ -121,36 +114,36 @@ case class NodeNIOPath(filename: String) extends Path {
"Path.normalize() is only supported in Node.js"
)
}
override def resolve(other: Path): Path =
def resolve(other: MunitPath): MunitPath =
resolve(other.toString)
override def resolve(other: String): Path =
def resolve(other: String): MunitPath =
JSIO.path match {
case Some(path) =>
adjustResolvedPath(
NodeNIOPath(path.resolve(filename, other).asInstanceOf[String])
MunitPath(path.resolve(filename, other).asInstanceOf[String])
)
case None =>
throw new UnsupportedOperationException(
"Path.normalize() is only supported in Node.js"
)
}
override def startsWith(other: Path): Boolean =
def startsWith(other: MunitPath): Boolean =
startsWith(other.toString)
override def startsWith(other: String): Boolean =
def startsWith(other: String): Boolean =
paths(filename).startsWith(paths(other))
private def paths(name: String) =
name.split(escapedSeparator)
override def toString: String =
filename
override def iterator(): util.Iterator[Path] =
def iterator(): util.Iterator[MunitPath] =
filename
.split(File.separator)
.iterator
.map(name => NodeNIOPath(name): Path)
.map(name => MunitPath(name): MunitPath)
.asJava
}

object NodeNIOPath {
def workingDirectory: NodeNIOPath =
NodeNIOPath(PlatformPathIO.workingDirectoryString)
object MunitPath {
def workingDirectory: MunitPath =
MunitPath(JSIO.cwd())
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package java.nio.file
package munit.internal.io

import java.io.File
import java.net.URI

import munit.internal.NodeNIOPath

object Paths {
// NOTE: We can't use Scala-style varargs since those have a different jvm
// signature than Java-style varargs. The boot classpath contains nio.file.Path
// so call-sites to `get` will resolve to the original java.nio.file.Paths.get,
// which results in a Scala.js linking error when using Scala varargs.
def get(first: String, more: Array[String] = Array.empty): Path = {
def get(first: String, more: Array[String] = Array.empty): MunitPath = {
val path =
if (more.isEmpty) first
else first + File.separator + more.mkString(File.separator)
NodeNIOPath(path)
MunitPath(path)
}

def get(uri: URI): Path = {
def get(uri: URI): MunitPath = {
if (uri.getScheme != "file")
throw new IllegalArgumentException("only file: URIs are supported")
val uripath = uri.getPath
val parts = uripath.split('/').toList
val (leading, trailing) = parts.span(_ == "")
trailing match {
case drive :: path if (drive.length == 2 && drive(1) == ':') =>
NodeNIOPath(trailing.mkString("\\"))
case _ => NodeNIOPath(uripath)
MunitPath(trailing.mkString("\\"))
case _ => MunitPath(uripath)
}
}
}
16 changes: 16 additions & 0 deletions munit/js/src/main/scala/munit/internal/io/PlatformIO.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package munit.internal.io

object PlatformIO {
type File = munit.internal.io.File
object File {
def separatorChar = munit.internal.io.File.separatorChar
}

object Files {
def readAllLines(path: Path): java.util.List[String] =
munit.internal.io.Files.readAllLines(path)
}

type Path = MunitPath
val Paths = munit.internal.io.Paths
}
4 changes: 4 additions & 0 deletions munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ object PlatformCompat {
def isJS: Boolean = false
def isNative: Boolean = false
def getThisClassLoader: ClassLoader = this.getClass().getClassLoader()

type InvocationTargetException = java.lang.reflect.InvocationTargetException
type UndeclaredThrowableException =
java.lang.reflect.UndeclaredThrowableException
}
18 changes: 18 additions & 0 deletions munit/jvm/src/main/scala/munit/internal/io/PlatformIO.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package munit.internal.io

object PlatformIO {
type File = java.io.File
object File {
def separatorChar = java.io.File.separatorChar
}

object Files {
def readAllLines(path: Path): java.util.List[String] =
java.nio.file.Files.readAllLines(path)
}

type Path = java.nio.file.Path
object Paths {
def get(path: String): Path = java.nio.file.Paths.get(path)
}
}
Loading