summaryrefslogtreecommitdiff
path: root/contrib/twirllib/test/src/HelloWorldTests.scala
blob: 67344ea663a626c83484c54713a485f7589f4a5b (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
package mill.twirllib

import mill.util.{TestEvaluator, TestUtil}
import utest.framework.TestPath
import utest.{TestSuite, Tests, assert, _}

import scala.io.Codec

object HelloWorldTests extends TestSuite {

  trait HelloBase extends TestUtil.BaseModule {
    override def millSourcePath: os.Path = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
  }

  trait HelloWorldModule extends mill.twirllib.TwirlModule {

    def twirlVersion = "1.3.15"

  }

  object HelloWorld extends HelloBase {

    object core extends HelloWorldModule {
      override def twirlAdditionalImports: Seq[String] = testAdditionalImports
      override def twirlConstructorAnnotations: Seq[String] = testConstructorAnnotations
    }

  }

  object HelloWorldWithInclusiveDot extends HelloBase {

    object core extends HelloWorldModule {
      override def twirlInclusiveDot: Boolean = true
    }

  }

  def workspaceTest[T](
      m: TestUtil.BaseModule,
      resourcePathSuffix: String
  )(t: TestEvaluator => T)(implicit tp: TestPath): T = {
    val eval = new TestEvaluator(m)
    os.remove.all(m.millSourcePath)
    os.remove.all(eval.outPath)
    os.makeDir.all(m.millSourcePath / os.up)
    os.copy(
      os.pwd / 'contrib / 'twirllib / 'test / 'resources / resourcePathSuffix,
      m.millSourcePath
    )
    t(eval)
  }

  def compileClassfiles: Seq[os.RelPath] = Seq[os.RelPath](
    "hello.template.scala",
    "wrapper.template.scala"
  )

  def expectedDefaultImports: Seq[String] = Seq(
    "import _root_.play.twirl.api.TwirlFeatureImports._",
    "import _root_.play.twirl.api.TwirlHelperImports._",
    "import _root_.play.twirl.api.Html",
    "import _root_.play.twirl.api.JavaScript",
    "import _root_.play.twirl.api.Txt",
    "import _root_.play.twirl.api.Xml"
  )

  def testAdditionalImports: Seq[String] = Seq(
    "mill.twirl.test.AdditionalImport1._",
    "mill.twirl.test.AdditionalImport2._"
  )

  def testConstructorAnnotations = Seq(
  "@org.springframework.stereotype.Component()",
  "@something.else.Thing()"
  )

  def tests: Tests = Tests {
    'twirlVersion - {

      'fromBuild - workspaceTest(HelloWorld, "hello-world") { eval =>
        val Right((result, evalCount)) =
          eval.apply(HelloWorld.core.twirlVersion)

        assert(
          result == "1.3.15",
          evalCount > 0
        )
      }
    }
    'compileTwirl - workspaceTest(HelloWorld, "hello-world") { eval =>
      val Right((result, evalCount)) = eval.apply(HelloWorld.core.compileTwirl)

      val outputFiles = os.walk(result.classes.path).filter(_.last.endsWith(".scala"))
      val expectedClassfiles = compileClassfiles.map(
        eval.outPath / 'core / 'compileTwirl / 'dest / 'html / _
      )

      assert(
        result.classes.path == eval.outPath / 'core / 'compileTwirl / 'dest,
        outputFiles.nonEmpty,
        outputFiles.forall(expectedClassfiles.contains),
        outputFiles.size == 2,
        evalCount > 0,
        outputFiles.forall { p =>
          val lines = os.read.lines(p).map(_.trim)
          (expectedDefaultImports ++ testAdditionalImports.map(s => s"import $s")).forall(lines.contains)
        },
        outputFiles.filter(_.toString().contains("hello.template.scala")).forall { p =>
          val lines = os.read.lines(p).map(_.trim)
          val expectedClassDeclaration = s"class hello ${testConstructorAnnotations.mkString}"
          lines.exists(_.startsWith(expectedClassDeclaration))
        },

      )

      // don't recompile if nothing changed
      val Right((_, unchangedEvalCount)) =
        eval.apply(HelloWorld.core.compileTwirl)

      assert(unchangedEvalCount == 0)
    }
    'compileTwirlInclusiveDot - workspaceTest(HelloWorldWithInclusiveDot, "hello-world-inclusive-dot") { eval =>
      val Right((result, evalCount)) = eval.apply(HelloWorldWithInclusiveDot.core.compileTwirl)

      val outputFiles = os.walk(result.classes.path).filter(_.last.endsWith(".scala"))
      val expectedClassfiles = compileClassfiles.map( name =>
        eval.outPath / 'core / 'compileTwirl / 'dest / 'html / name.toString().replace(".template.scala", "$$TwirlInclusiveDot.template.scala")
      )

      println(s"outputFiles: $outputFiles")

      assert(
        result.classes.path == eval.outPath / 'core / 'compileTwirl / 'dest,
        outputFiles.nonEmpty,
        outputFiles.forall(expectedClassfiles.contains),
        outputFiles.size == 2,
        evalCount > 0,
        outputFiles.filter(_.toString().contains("hello.template.scala")).forall { p =>
          val lines = os.read.lines(p).map(_.trim)
          lines.exists(_.contains("$$TwirlInclusiveDot"))
        },

      )

      // don't recompile if nothing changed
      val Right((_, unchangedEvalCount)) =
        eval.apply(HelloWorld.core.compileTwirl)

      assert(unchangedEvalCount == 0)
    }
  }
}