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

Js build #247

Merged
merged 9 commits into from
Aug 18, 2021
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: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ lazy val cli = crossProject(JSPlatform, JVMPlatform)

lazy val cliJS = cli.js
.settings(
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)),
scalaJSUseMainModuleInitializer := true
)

Expand Down
48 changes: 0 additions & 48 deletions cli/.js/src/main/scala/aqua/JsApp.scala

This file was deleted.

185 changes: 92 additions & 93 deletions cli/.jvm/src/test/scala/SourcesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,56 @@ import cats.data.Chain
import cats.effect.IO
import cats.effect.unsafe.implicits.global
import fs2.text
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import fs2.io.file.{Files, Path}
import org.scalatest.flatspec.AsyncFlatSpec

class SourcesSpec extends AnyFlatSpec with Matchers {
class SourcesSpec extends AsyncFlatSpec with Matchers {
implicit val aquaIO: AquaIO[IO] = AquaFilesIO.summon[IO]

"AquaFileSources" should "generate correct fileId with imports" in {
val path = Path("cli/.jvm/src/test/test-dir/path-test")
val importPath = path.resolve("imports")

val sourceGen = new AquaFileSources[IO](path, importPath :: Nil)

val result = sourceGen.sources.unsafeRunSync()
result.isValid shouldBe true

val listResult = result
.getOrElse(Chain.empty)
.toList
.map { case (fid, s) =>
(fid.file.toString.split("/").last, s)
}
.sortBy(_._1) // sort cause different systems have different order of file reading

val (id, importFile) = listResult(1)
id shouldBe "index.aqua"
importFile.nonEmpty shouldBe true

val (importNearId, importFileNear) = listResult.head
importNearId shouldBe "importNear.aqua"
importFileNear.nonEmpty shouldBe true
sourceGen.sources.map { result =>
result.isValid shouldBe true

val listResult = result
.getOrElse(Chain.empty)
.toList
.map { case (fid, s) =>
(fid.file.toString.split("/").last, s)
}
.sortBy(_._1) // sort cause different systems have different order of file reading

val (id, importFile) = listResult(1)
id shouldBe "index.aqua"
importFile.nonEmpty shouldBe true

val (importNearId, importFileNear) = listResult.head
importNearId shouldBe "importNear.aqua"
importFileNear.nonEmpty shouldBe true
}.unsafeToFuture()
}

"AquaFileSources" should "throw an error if a source file doesn't exist" in {
val path = Path("some/random/path")

val sourceGen = new AquaFileSources[IO](path, Nil)

val result = sourceGen.sources.unsafeRunSync()
result.isInvalid shouldBe true
sourceGen.sources.map(result => result.isInvalid shouldBe true).unsafeToFuture()
}

"AquaFileSources" should "throw an error if there is no import that is indicated in a source" in {
val path = Path("cli/.jvm/src/test/test-dir")
val importPath = path.resolve("random/import/path")

val sourceGen = new AquaFileSources[IO](path, importPath :: Nil)
val result =
sourceGen.resolveImport(FileModuleId(path.resolve("no-file.aqua")), "no/file").unsafeRunSync()
result.isInvalid shouldBe true
sourceGen
.resolveImport(FileModuleId(path.resolve("no-file.aqua")), "no/file")
.map(result => result.isInvalid shouldBe true)
.unsafeToFuture()
}

"AquaFileSources" should "find correct imports" in {
Expand All @@ -64,36 +64,33 @@ class SourcesSpec extends AnyFlatSpec with Matchers {

val sourceGen = new AquaFileSources[IO](srcPath, importPath :: Nil)

// should be found in importPath
val result =
sourceGen
.resolveImport(FileModuleId(srcPath), "imports/import.aqua")
.unsafeRunSync()

result.isValid shouldBe true
val file = result.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file).unsafeRunSync() shouldBe true

// should be found near src file
val result2 =
sourceGen
.resolveImport(FileModuleId(srcPath), "importNear.aqua")
.unsafeRunSync()

result2.isValid shouldBe true
val file2 = result2.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file2).unsafeRunSync() shouldBe true

// near src file but in another directory
val sourceGen2 = new AquaFileSources[IO](srcPath, Nil)
val result3 =
sourceGen2
.resolveImport(FileModuleId(srcPath), "imports/import.aqua")
.unsafeRunSync()

result3.isValid shouldBe true
val file3 = result3.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file3).unsafeRunSync() shouldBe true
(for {
// should be found in importPath
result <- sourceGen.resolveImport(FileModuleId(srcPath), "imports/import.aqua")
exists <- {
result.isValid shouldBe true
val file = result.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file)
}
_ = exists shouldBe true

// should be found near src file
result2 <- sourceGen.resolveImport(FileModuleId(srcPath), "importNear.aqua")
exists2 <- {
result2.isValid shouldBe true
val file2 = result2.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file2)
}
_ = exists2 shouldBe true
// near src file but in another directory
sourceGen2 = new AquaFileSources[IO](srcPath, Nil)
result3 <- sourceGen2.resolveImport(FileModuleId(srcPath), "imports/import.aqua")
exists3 <- {
result3.isValid shouldBe true
val file3 = result3.getOrElse(FileModuleId(Path("/some/random"))).file
Files[IO].exists(file3)
}
} yield { exists3 shouldBe true }).unsafeToFuture()
}

"AquaFileSources" should "resolve correct path for target" in {
Expand All @@ -106,11 +103,15 @@ class SourcesSpec extends AnyFlatSpec with Matchers {

val suffix = "_custom.super"

val resolved = sourceGen.resolveTargetPath(filePath, targetPath, suffix).unsafeRunSync()
resolved.isValid shouldBe true
sourceGen
.resolveTargetPath(filePath, targetPath, suffix)
.map { resolved =>
resolved.isValid shouldBe true

val targetFilePath = resolved.toOption.get
targetFilePath.toString shouldBe "/target/dir/some-dir/file_custom.super"
val targetFilePath = resolved.toOption.get
targetFilePath.toString shouldBe "/target/dir/some-dir/file_custom.super"
}
.unsafeToFuture()
}

"AquaFileSources" should "write correct file with correct path" in {
Expand All @@ -121,38 +122,36 @@ class SourcesSpec extends AnyFlatSpec with Matchers {

// clean up
val resultPath = Path("cli/.jvm/src/test/test-dir/target/imports/import_hey.custom")
Files[IO].deleteIfExists(resultPath).unsafeRunSync()

val sourceGen = new AquaFileSources[IO](path, Nil)
val content = "some random content"
val compiled = AquaCompiled[FileModuleId](
FileModuleId(filePath),
Seq(Generated("_hey.custom", content))
)

val resolved = sourceGen.write(targetPath)(compiled).unsafeRunSync()
resolved.size shouldBe 1
resolved.head.isValid shouldBe true

Files[IO].exists(resultPath).unsafeRunSync() shouldBe true

val resultText = Files[IO]
.readAll(resultPath)
.fold(
Vector
.empty[Byte]
)((acc, b) => acc :+ b)
.flatMap(fs2.Stream.emits)
.through(text.utf8.decode)
.attempt
.compile
.last
.unsafeRunSync()
.get
.right
.get
resultText shouldBe content

Files[IO].deleteIfExists(resultPath).unsafeRunSync()
(for {
_ <- Files[IO].deleteIfExists(resultPath)
sourceGen = new AquaFileSources[IO](path, Nil)
content = "some random content"
compiled = AquaCompiled[FileModuleId](
FileModuleId(filePath),
Seq(Generated("_hey.custom", content))
)
resolved <- sourceGen.write(targetPath)(compiled)
_ = {
resolved.size shouldBe 1
resolved.head.isValid shouldBe true
}
exists <- Files[IO].exists(resultPath)
_ = exists shouldBe true
result <- Files[IO]
.readAll(resultPath)
.fold(
Vector
.empty[Byte]
)((acc, b) => acc :+ b)
.flatMap(fs2.Stream.emits)
.through(text.utf8.decode)
.attempt
.compile
.last
resultText = result.get.right.get
_ <- Files[IO].deleteIfExists(resultPath)
} yield {
resultText shouldBe content
}).unsafeToFuture()
}
}
Loading