summaryrefslogtreecommitdiff
path: root/scalajslib/test/src/mill/scalajslib/HelloJSWorldTests.scala
blob: d616d1b007095a3dac6e38cc91194ac3547b768b (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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.define.Discover
import mill.eval.{Evaluator, Result}
import mill.scalalib.{CrossScalaModule, DepSyntax, Lib, PublishModule, TestRunner}
import mill.scalalib.publish.{Developer, License, PomSettings, SCM}
import mill.util.{TestEvaluator, TestUtil}
import utest._


import scala.collection.JavaConverters._


object HelloJSWorldTests extends TestSuite {
  val workspacePath =  TestUtil.getOutPathStatic() / "hello-js-world"

  trait HelloJSWorldModule extends CrossScalaModule with ScalaJSModule with PublishModule {
    override def millSourcePath = workspacePath
    def publishVersion = "0.0.1-SNAPSHOT"
    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.22", "1.0.0-M2")
    } yield (scala, scalaJS)

    object helloJsWorld extends Cross[BuildModule](matrix:_*)
    class BuildModule(val crossScalaVersion: String, sjsVersion0: String) extends HelloJSWorldModule {
      override def artifactName = "hello-js-world"
      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"))
      )
    }

    object buildUTest extends Cross[BuildModuleUtest](matrix:_*)
    class BuildModuleUtest(crossScalaVersion: String, sjsVersion0: String)
      extends BuildModule(crossScalaVersion, sjsVersion0) {
      object test extends super.Tests {
        override def sources = T.sources{ millSourcePath / 'src / 'utest }
        def testFrameworks = Seq("utest.runner.Framework")
        override def ivyDeps = Agg(
          ivy"com.lihaoyi:utest_sjs${scalaJSBinaryVersion()}_${Lib.scalaBinaryVersion(scalaVersion())}:0.6.3"
        )
      }
    }

    object buildScalaTest extends Cross[BuildModuleScalaTest](matrix:_*)
    class BuildModuleScalaTest(crossScalaVersion: String, sjsVersion0: String)
      extends BuildModule(crossScalaVersion, sjsVersion0) {
      object test extends super.Tests {
        override def sources = T.sources{ millSourcePath / 'src / 'scalatest }
        def testFrameworks = Seq("org.scalatest.tools.Framework")
        override def ivyDeps = Agg(
          ivy"org.scalatest:scalatest_sjs${scalaJSBinaryVersion()}_${Lib.scalaBinaryVersion(scalaVersion())}:3.0.4"
        )
      }
    }
    override def millDiscover = Discover[this.type]
  }

  val millSourcePath = pwd / 'scalajslib / 'test / 'resources / "hello-js-world"

  val helloWorldEvaluator = TestEvaluator.static(HelloJSWorld)


  val mainObject = helloWorldEvaluator.outPath / 'src / "Main.scala"

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

  def runJS(path: Path): String = {
    val engineManager = new ScriptEngineManager(null)
    val engine = engineManager.getEngineByName("nashorn")
    val console = new Console
    val bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE)
    bindings.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.helloJsWorld(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.helloJsWorld(scalaVersion, scalaJSVersion).compile)
        assert(unchangedEvalCount == 0)
      }

      'fromScratch_2124_0622 - testCompileFromScratch("2.12.4", "0.6.22")
      'fromScratch_2123_0622 - testCompileFromScratch("2.12.3", "0.6.22")
      'fromScratch_2118_0622 - testCompileFromScratch("2.11.8", "0.6.22")
      'fromScratch_2124_100M2 - testCompileFromScratch("2.12.4", "1.0.0-M2")
    }

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

    'fullOpt - {
      'run_2124_0622 - testRun("2.12.4", "0.6.22", FullOpt)
      'run_2123_0622 - testRun("2.12.3", "0.6.22", FullOpt)
      'run_2118_0622 - testRun("2.11.8", "0.6.22", FullOpt)
      'run_2124_100M2 - testRun("2.12.4", "1.0.0-M2", FullOpt)
    }
    'fastOpt - {
      'run_2124_0622 - testRun("2.12.4", "0.6.22", FastOpt)
      'run_2123_0622 - testRun("2.12.3", "0.6.22", FastOpt)
      'run_2118_0622 - testRun("2.11.8", "0.6.22", FastOpt)
      'run_2124_100M2 - testRun("2.12.4", "1.0.0-M2", FastOpt)
    }
    'jar - {
      'containsSJSIRs - {
        val Right((result, evalCount)) = helloWorldEvaluator(HelloJSWorld.helloJsWorld("2.12.4", "0.6.22").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.helloJsWorld(scalaVersion, scalaJSVersion).artifactMetadata)
        assert(result.id == artifactId)
      }
      'artifactId_0622 - testArtifactId("2.12.4", "0.6.22", "hello-js-world_sjs0.6_2.12")
      'artifactId_100M2 - testArtifactId("2.12.4", "1.0.0-M2", "hello-js-world_sjs1.0.0-M2_2.12")
    }
    'test - {
      def runTests(testTask: define.Command[(String, Seq[TestRunner.Result])]): Map[String, Map[String, TestRunner.Result]] = {
        val Left(Result.Failure(_, Some(res))) = helloWorldEvaluator(testTask)

        val (doneMsg, testResults) = res
        testResults
          .groupBy(_.fullyQualifiedName)
          .mapValues(_.map(e => e.selector -> e).toMap)
      }

      def checkUtest(scalaVersion: String, scalaJSVersion: String) = {
        val resultMap = runTests(HelloJSWorld.buildUTest(scalaVersion, scalaJSVersion).test.test())

        val mainTests = resultMap("MainTests")
        val argParserTests = resultMap("ArgsParserTests")

        assert(
          mainTests.size == 2,
          mainTests("MainTests.vmName.containJs").status == "Success",
          mainTests("MainTests.vmName.containScala").status == "Success",

          argParserTests.size == 2,
          argParserTests("ArgsParserTests.one").status == "Success",
          argParserTests("ArgsParserTests.two").status == "Failure"
        )
      }

      def checkScalaTest(scalaVersion: String, scalaJSVersion: String) = {
        val resultMap = runTests(HelloJSWorld.buildScalaTest(scalaVersion, scalaJSVersion).test.test())

        val mainSpec = resultMap("MainSpec")
        val argParserSpec = resultMap("ArgsParserSpec")

        assert(
          mainSpec.size == 2,
          mainSpec("vmName should contain js").status == "Success",
          mainSpec("vmName should contain Scala").status == "Success",

          argParserSpec.size == 2,
          argParserSpec("parse should one").status == "Success",
          argParserSpec("parse should two").status == "Failure"
        )
      }

      'utest_2118_0622 - checkUtest("2.11.8", "0.6.22")
      'utest_2124_0622 - checkUtest("2.12.4", "0.6.22")
      'utest_2118_100M2 - checkUtest("2.11.8", "1.0.0-M2")
      'utest_2124_100M2 - checkUtest("2.12.4", "1.0.0-M2")

      'scalaTest_2118_0622 - checkScalaTest("2.11.8", "0.6.22")
      'scalaTest_2124_0622 - checkScalaTest("2.12.4", "0.6.22")
//      No scalatest artifact for scala.js 1.0.0-M2 published yet
//      'scalaTest_2118_100M2 - checkScalaTest("2.11.8", "1.0.0-M2")
//      'scalaTest_2124_100M2 - checkScalaTest("2.12.4", "1.0.0-M2")
    }

    def checkRun(scalaVersion: String, scalaJSVersion: String, mainClass: Option[String] = None): Unit = {
      val task = mainClass match {
        case Some(main) => HelloJSWorld.helloJsWorld(scalaVersion, scalaJSVersion).runMain(main)
        case None => HelloJSWorld.helloJsWorld(scalaVersion, scalaJSVersion).run()
      }

      val Right((_, evalCount)) = helloWorldEvaluator(task)

      val paths = Evaluator.resolveDestPaths(
        helloWorldEvaluator.outPath,
        task.ctx.segments
      )
      val log = read(paths.log)
      assert(
        evalCount > 0,
        log.contains("node"),
        log.contains("Scala.js")
      )
    }

    'runMain - {
      val mainClass = Some("Main")
      'run_2118_0622  - checkRun("2.11.8", "0.6.22", mainClass)
      'run_2124_0622  - checkRun("2.12.4", "0.6.22", mainClass)
      'run_2118_100M2 - checkRun("2.11.8", "1.0.0-M2", mainClass)
      'run_2124_100M2 - checkRun("2.12.4", "1.0.0-M2", mainClass)

      'wrongMain - {
        val wrongMainClass = "Foo"
        val task = HelloJSWorld.helloJsWorld("2.12.4", "0.6.22").runMain(wrongMainClass)

        val Left(Result.Exception(ex, _)) = helloWorldEvaluator(task)

        assert(
          ex.isInstanceOf[Exception]
        )
      }
    }
    'run - {
      'run_2118_0622  - checkRun("2.11.8", "0.6.22")
      'run_2124_0622  - checkRun("2.12.4", "0.6.22")
      'run_2118_100M2 - checkRun("2.11.8", "1.0.0-M2")
      'run_2124_100M2 - checkRun("2.12.4", "1.0.0-M2")
    }
  }

  def compileClassfiles(parentDir: Path) = Set(
    parentDir / "ArgsParser$.class",
    parentDir / "ArgsParser$.sjsir",
    parentDir / "ArgsParser.class",
    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(millSourcePath, workspacePath)
  }

}