Skip to content

Commit

Permalink
use Resource, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Dec 13, 2024
1 parent 732700d commit efd94b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
20 changes: 7 additions & 13 deletions cli/src/main/scala/org/bykn/bosatsu/PathModule.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.bykn.bosatsu

import cats.effect.IO
import cats.effect.{IO, Resource}
import org.typelevel.paiges.{Doc, Document}

import java.nio.file.{Path => JPath}
Expand All @@ -14,21 +14,15 @@ import cats.syntax.all._
object PathModule extends MainModule[IO, JPath](IOPlatformIO) { self =>
type Path = JPath

def print(str: => String): IO[Unit] =
def print(str: String): IO[Unit] =
IO.println(str)

val parResource: Resource[IO, Par.EC] =
Resource.make(IO(Par.newService()))(es => IO(Par.shutdownService(es)))
.map(Par.ecFromService(_))

def withEC[A](fn: Par.EC => IO[A]): IO[A] =
IO.delay(Par.newService())
.flatMap { es =>
fn(Par.ecFromService(es))
.flatMap { a =>
IO(Par.shutdownService(es)).as(a)
}
.recoverWith { case e =>
IO(Par.shutdownService(es)) *>
IO.raiseError(e)
}
}
parResource.use(fn)

def report(io: IO[Output]): IO[ExitCode] =
io.attempt.flatMap {
Expand Down
11 changes: 6 additions & 5 deletions cli/src/test/scala/org/bykn/bosatsu/PathModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.scalatest.funsuite.AnyFunSuite
import cats.effect.unsafe.implicits.global

class PathModuleTest extends AnyFunSuite {
import PathModule.platformIO.pathPackage

implicit val arbPath: Arbitrary[Path] =
Arbitrary {
Expand All @@ -24,7 +25,7 @@ class PathModuleTest extends AnyFunSuite {

test("test some hand written examples") {
def pn(roots: List[String], file: String): Option[PackageName] =
PathModule.pathPackage(roots.map(Paths.get(_)), Paths.get(file))
pathPackage(roots.map(Paths.get(_)), Paths.get(file))

assert(
pn(List("/root0", "/root1"), "/root0/Bar.bosatsu") == Some(
Expand Down Expand Up @@ -56,13 +57,13 @@ class PathModuleTest extends AnyFunSuite {

test("no roots means no Package") {
forAll { (p: Path) =>
assert(PathModule.pathPackage(Nil, p) == None)
assert(pathPackage(Nil, p) == None)
}
}

test("empty path is not okay for a package") {
forAll { (roots: List[Path]) =>
assert(PathModule.pathPackage(roots, Paths.get("")) == None)
assert(pathPackage(roots, Paths.get("")) == None)
}
}

Expand All @@ -74,7 +75,7 @@ class PathModuleTest extends AnyFunSuite {
PackageName.parse(
rest.asScala.map(_.toString.toLowerCase.capitalize).mkString("/")
)
assert(PathModule.pathPackage(root :: otherRoots, path) == pack)
assert(pathPackage(root :: otherRoots, path) == pack)
}

forAll(law(_, _, _))
Expand All @@ -91,7 +92,7 @@ class PathModuleTest extends AnyFunSuite {
test("if none of the roots are prefixes we have none") {
forAll { (r0: Path, roots0: List[Path], file: Path) =>
val roots = (r0 :: roots0).filterNot(_.toString == "")
val pack = PathModule.pathPackage(roots, file)
val pack = pathPackage(roots, file)

val noPrefix = !roots.exists { r =>
file.asScala.toList.startsWith(r.asScala.toList)
Expand Down

0 comments on commit efd94b7

Please sign in to comment.