summaryrefslogtreecommitdiff
path: root/scalajslib/src/test/scala/mill/scalajslib/HelloJSWorldTests.scala
blob: 2094687b70a986769f215474f929697b72a4be9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mill.scalajslib

import java.io.{FileReader, StringWriter}
import java.util.jar.JarFile
import javax.script.{ScriptContext, ScriptEngineManager}

import ammonite.ops._
import mill._
import mill.scalalib.PublishModule
import mill.scalalib.publish.{Developer, License, PomSettings, SCM}
import mill.util.{TestEvaluator, TestUtil}
import utest._

import scala.collection.JavaConverters._


object HelloJSWorldTests extends TestSuite {


  trait HelloJSWorldModule extends ScalaJSModule with PublishModule {
    override def basePath = HelloJSWorldTests.workspacePath
    override def mainClass = Some("Main")
  }

  object HelloJSWorld extends TestUtil.BaseModule {
    val matrix = for {
      scala <- Seq("2.11.8", "2.12.3", "2.12.4")
      scalaJS <- Seq("0.6.20", "0.6.21", "1.0.0-M2")
    } yield (scala, scalaJS)

    object build extends Cross[BuildModule](matrix:_*)

    class BuildModule(scalaVersion0: String, sjsVersion0: String) extends HelloJSWorldModule {
      def scalaVersion = scalaVersion0
      def scalaJSVersion = sjsVersion0
      def pomSettings = PomSettings(
        organization = "com.lihaoyi",
        description = "hello js world ready for real world publishing",
        url = "https://github.com/lihaoyi/hello-world-publish",
        licenses = Seq(
          License("Apache License, Version 2.0",
            "http://www.apache.org/licenses/LICENSE-2.0")),
        scm = SCM(
          "https://github.com/lihaoyi/hello-world-publish",
          "scm:git:https://github.com/lihaoyi/hello-world-publish"
        ),
        developers =
          Seq(Developer("lihaoyi", "Li Haoyi", "https://github.com/lihaoyi"))
      )
    }
  }

  val srcPath = pwd / 'scalajslib / 'src / 'test / 'resources / "hello-js-world"
  val workspacePath = pwd / 'target / 'workspace / "hello-js-world"
  val outputPath = workspacePath / 'out
  val mainObject = workspacePath / 'src / 'main / 'scala / "Main.scala"

  val helloWorldEvaluator = new TestEvaluator(
    HelloJSWorld,
    workspacePath,
    srcPath
  )

  class Console {
    val out = new StringWriter()
    def log(s: String): Unit = out.append(s)
  }

  def runJS(path: Path): String = {
    val engineManager = new ScriptEngineManager
    val engine = engineManager.getEngineByName("nashorn")
    val console = new Console
    engine.getBindings(ScriptContext.ENGINE_SCOPE).put("console", console)
    engine.eval(new FileReader(path.toIO))
    console.out.toString
  }

  def tests: Tests = Tests {
    prepareWorkspace()
    'compile - {
      def testCompileFromScratch(scalaVersion: String,
                          scalaJSVersion: String): Unit = {
        val Right((result, evalCount)) =
          helloWorldEvaluator(HelloJSWorld.build(scalaVersion, scalaJSVersion).compile)

        val outPath = result.classes.path
        val outputFiles = ls.rec(outPath)
        val expectedClassfiles = compileClassfiles(outPath)
        assert(
          outputFiles.toSet == expectedClassfiles,
          evalCount > 0
        )

        // don't recompile if nothing changed
        val Right((_, unchangedEvalCount)) =
          helloWorldEvaluator(HelloJSWorld.build(scalaVersion, scalaJSVersion).compile)
        assert(unchangedEvalCount == 0)
      }

      'fromScratch_2124_0621 - testCompileFromScratch("2.12.4", "0.6.21")
      'fromScratch_2123_0621 - testCompileFromScratch("2.12.3", "0.6.21")
      'fromScratch_2118_0621 - testCompileFromScratch("2.11.8", "0.6.21")
      'fromScratch_2124_100M2 - testCompileFromScratch("2.11.8", "1.0.0-M2")
    }

    def testRun(scalaVersion: String,
                scalaJSVersion: String,
                mode: OptimizeMode): Unit = {
      val task = mode match {
        case FullOpt => HelloJSWorld.build(scalaVersion, scalaJSVersion).fullOpt
        case FastOpt => HelloJSWorld.build(scalaVersion, scalaJSVersion).fastOpt
      }
      val Right((result, evalCount)) = helloWorldEvaluator(task)
      val output = runJS(result.path)
      assert(output == "Hello Scala.js")
    }

    'fullOpt - {
      'run_2124_0621 - testRun("2.12.4", "0.6.21", FullOpt)
      'run_2123_0621 - testRun("2.12.3", "0.6.21", FullOpt)
      'run_2118_0621 - testRun("2.11.8", "0.6.21", FullOpt)
      'run_2124_100M2 - testRun("2.11.8", "1.0.0-M2", FullOpt)
    }
    'fastOpt - {
      'run_2124_0621 - testRun("2.12.4", "0.6.21", FastOpt)
      'run_2123_0621 - testRun("2.12.3", "0.6.21", FastOpt)
      'run_2118_0621 - testRun("2.11.8", "0.6.21", FastOpt)
      'run_2124_100M2 - testRun("2.11.8", "1.0.0-M2", FastOpt)
    }
    'jar - {
      'containsSJSIRs - {
        val Right((result, evalCount)) = helloWorldEvaluator(HelloJSWorld.build("2.12.4", "0.6.21").jar)
        val jar = result.path
        val entries = new JarFile(jar.toIO).entries().asScala.map(_.getName)
        assert(entries.contains("Main$.sjsir"))
      }
    }
    'publish - {
      def testArtifactId(scalaVersion: String,
                         scalaJSVersion: String,
                         artifactId: String): Unit = {
        val Right((result, evalCount)) = helloWorldEvaluator(HelloJSWorld.build(scalaVersion, scalaJSVersion).artifact)
        assert(result.id == artifactId)
      }
      'artifactId_0621 - testArtifactId("2.12.4", "0.6.21", "hello-js-world_sjs0.6_2.12")
      'artifactId_0621 - testArtifactId("2.12.4", "1.0.0-M2", "hello-js-world_sjs1.0.0-M2_2.12")
    }
  }

  def compileClassfiles(parentDir: Path) = Set(
    parentDir / "Main.class",
    parentDir / "Main$.class",
    parentDir / "Main$delayedInit$body.class",
    parentDir / "Main$.sjsir",
    parentDir / "Main$delayedInit$body.sjsir"
  )

  def prepareWorkspace(): Unit = {
    rm(workspacePath)
    mkdir(workspacePath / up)
    cp(srcPath, workspacePath)
  }

}