Skip to content

Commit

Permalink
Add test for using default semantics depending on optimalization mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Dec 7, 2023
1 parent 07495aa commit 50a6049
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.bsp/
out/
.idea/
14 changes: 11 additions & 3 deletions cli/src/org/scalajs/cli/Scalajsld.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ object Scalajsld {
.action { (x, c) => c.copy(outputDir = Some(x)) }
.text("Output directory of linker (required)")
opt[Unit]('f', "fastOpt")
.action { (_, c) => c.copy(noOpt = false, fullOpt = false) }
.action { (_, c) =>
c.copy(noOpt = false, fullOpt = false)
}
.text("Optimize code (this is the default)")
opt[Unit]('n', "noOpt")
.action { (_, c) => c.copy(noOpt = true, fullOpt = false) }
Expand All @@ -135,9 +137,15 @@ object Scalajsld {
.action { (x, c) => c.copy(outputPatterns = OutputPatterns.fromJSFile(x)) }
.text("Pattern for JS file names (default: `%s.js`). " +
"Expects a printf-style pattern with a single placeholder for the module ID. " +
"A typical use case is changing the file extension, e.g. `%.mjs` for Node.js modules.")
"A typical use case is changing the file extension, e.g. `%.mjs` for Node.js modules."
)
opt[Unit]('u', "fullOpt")
.action { (_, c) => c.copy(noOpt = false, fullOpt = true) }
.action { (_, c) =>
c.copy(
noOpt = false,
fullOpt = true
)
}
.text("Fully optimize code (uses Google Closure Compiler)")
opt[Unit]('p', "prettyPrint")
.action { (_, c) => c.copy(prettyPrint = true) }
Expand Down
133 changes: 133 additions & 0 deletions tests/test/src/org/scalajs/cli/tests/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,137 @@ class Tests extends munit.FunSuite {
val splitRunOutput =splitRunRes.out.trim()
assert(splitRunOutput == "asdf 2")
}

test("fullLinkJs mode does not throw") {
val dir = os.temp.dir()
os.write(
dir / "foo.scala",
"""object Foo {
| def main(args: Array[String]): Unit = {
| val s = "Hello"
| println("Hello" + s.charAt(5))
| }
|
| class A
|}
|""".stripMargin
)

val scalaJsLibraryCp = os
.proc(
"cs",
"fetch",
"--classpath",
"-E",
"org.scala-lang:scala-library",
s"org.scala-js::scalajs-library:$scalaJsVersion"
)
.call(cwd = dir)
.out
.trim()

os.makeDir.all(dir / "bin")
os.proc(
"cs",
"launch",
"scalac:2.13.6",
"--",
"-classpath",
scalaJsLibraryCp,
s"-Xplugin:${os.proc("cs", "fetch", "--intransitive", s"org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion").call(cwd = dir).out.trim()}",
"-d",
"bin",
"foo.scala"
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)

val res = os
.proc(
launcher,
"--stdlib",
scalaJsLibraryCp,
"-s",
"--fullOpt",
"-o",
"test.js",
"-mm",
"Foo.main",
"bin"
)
.call(cwd = dir, mergeErrIntoOut = true)

val testJsSize = os.size(dir / "test.js")
val testJsMapSize = os.size(dir / "test.js.map")
assert(testJsSize > 0)
assert(testJsMapSize > 0)

val runRes = os.proc("node", "test.js").call(cwd = dir, check = true)
}

test("fastLinkJs mode throws") {
val dir = os.temp.dir()
os.write(
dir / "foo.scala",
"""object Foo {
| def main(args: Array[String]): Unit = {
| val s = "Hello"
| println("Hello" + s.charAt(5))
| }
|
| class A
|}
|""".stripMargin
)

val scalaJsLibraryCp = os
.proc(
"cs",
"fetch",
"--classpath",
"-E",
"org.scala-lang:scala-library",
s"org.scala-js::scalajs-library:$scalaJsVersion"
)
.call(cwd = dir)
.out
.trim()

os.makeDir.all(dir / "bin")
os.proc(
"cs",
"launch",
"scalac:2.13.6",
"--",
"-classpath",
scalaJsLibraryCp,
s"-Xplugin:${os.proc("cs", "fetch", "--intransitive", s"org.scala-js:scalajs-compiler_2.13.6:$scalaJsVersion").call(cwd = dir).out.trim()}",
"-d",
"bin",
"foo.scala"
).call(cwd = dir, stdin = os.Inherit, stdout = os.Inherit)

os
.proc(
launcher,
"--stdlib",
scalaJsLibraryCp,
"--fastOpt",
"-s",
"-o",
"test.js",
"-mm",
"Foo.main",
"bin"
)
.call(cwd = dir, mergeErrIntoOut = true)

val testJsSize = os.size(dir / "test.js")
val testJsMapSize = os.size(dir / "test.js.map")
assert(testJsSize > 0)
assert(testJsMapSize > 0)

val runRes = os.proc("node", "test.js").call(cwd = dir, check = false, stderr = os.Pipe)
assert(runRes.exitCode == 1)

assert(runRes.err.trim().contains("UndefinedBehaviorError"))
}
}

0 comments on commit 50a6049

Please sign in to comment.